Commit 93539ef1 authored by Robert's avatar Robert

Add group subscriptions

parent bc205f1c
[flake8]
ignore = E121,E123,E126,E226,E24,E704,W503
......@@ -7,8 +7,6 @@ def is_gestalt(user, gestalt):
@rules.predicate
def is_group_member(user, group):
if type(group) == models.GroupAttention:
group = group.group
return group and group.members.filter(user=user).exists()
@rules.predicate
......
{% load entities rules thumbnail utils %}
{% load rules thumbnail utils %}
{% has_perm 'content.view_content' user group.get_head_gallery as can_view_head_gallery %}
{% has_perm 'entities.change_group' user group as can_edit %}
{% has_perm 'entities.create_group_attention' user group as can_create_attention %}
{% has_perm 'entities.create_group_content' user group as can_create_content %}
{% has_perm 'entities.create_group_message' user group as can_create_message %}
{% has_perm 'entities.create_membership' user group as can_create_membership %}
{% has_perm 'entities.delete_group_attention' user attention as can_delete_attention %}
{% has_perm 'entities.delete_membership' user membership as can_delete_membership %}
{% has_perm 'entities.list_members' user group as can_list_members %}
......@@ -130,6 +128,8 @@
{% endif %}
<div class="btn-group group-actions-user">
{% include_features '_group_actions.html' %}
<a href="{% url 'message-create' group.pk %}" class="btn btn-default btn-sm">
<i class="sg sg-message"></i> Nachricht schreiben
</a>
......@@ -138,20 +138,6 @@
<input class="dropdown-state" id="group-more" type="checkbox" />
<label class="btn btn-default btn-sm btn-more" for="group-more"></label>
<ul class="dropdown dropdown-small">
{% if can_delete_attention %}
<li>
<a href="{% url 'group-attention-delete' attention.pk %}" title="Du erhältst Benachrichtungen für diese Gruppe">
<i class="sg sg-attention"></i> Deabbonnieren
</a>
</li>
{% elif not can_delete_membership %}
<li>
<a href="{% url 'group-attention-create' group.pk %}" title="Bei neuen Beiträgen benachrichtigt werden">
<i class="sg sg-attention"></i> Abbonieren
</a>
</li>
{% endif %}
{% if can_delete_membership %}
<li>
<a href="{% url 'membership-delete' membership.pk %}">
......
......@@ -78,7 +78,6 @@ class Group(utils_views.List):
template_name = 'entities/group_detail.html'
def get_context_data(self, **kwargs):
kwargs['attention'] = self.get_attention()
kwargs['calendar_events'] = self.get_events().around()
kwargs['intro_content'] = self.get_intro_content()
kwargs['membership'] = self.get_membership()
......@@ -86,12 +85,6 @@ class Group(utils_views.List):
kwargs['upcoming_events'] = self.get_events().upcoming(3)
return super().get_context_data(**kwargs)
def get_attention(self):
try:
return models.GroupAttention.objects.get(attendee=self.request.user.gestalt, group=self.get_group())
except (AttributeError, models.GroupAttention.DoesNotExist):
return None
def get_events(self):
return content_models.Event.objects.permitted(self.request.user).filter(groups=self.get_group())
......
class Attention(utils_forms.FormMixin, forms.ModelForm):
attendee_email = forms.EmailField(disabled=True, widget=forms.HiddenInput)
attended_object = forms.ModelChoiceField(disabled=True, queryset=content_models.Content.objects.all(), widget=forms.HiddenInput)
layout = ('attendee_email',
layout.HTML('<p>Möchtest Du per E-Mail benachrichtigt werden, '
'wenn dem Beitrag neue Kommentare hinzugefügt werden?</p>'),
utils_forms.Submit('Benachrichtigungen erhalten'))
class Meta:
fields = ('attended_object',)
model = subscriptions_models.Subscription
def save(self):
attention = super().save(commit=False)
attention.attendee = models.Gestalt.objects.get(user__email=self.cleaned_data['attendee_email'])
attention.attended_object = self.cleaned_data['attended_object']
attention.save()
return attention
class GroupAttention(utils_forms.FormMixin, forms.ModelForm):
attendee_email = forms.EmailField(disabled=True, widget=forms.HiddenInput)
group = forms.ModelChoiceField(disabled=True, queryset=models.Group.objects.all(), widget=forms.HiddenInput)
layout = ('attendee_email',
layout.HTML('<p>Möchtest Du per E-Mail benachrichtigt werden, '
'wenn Mitglieder der Gruppe <em>{{ group }}</em> neue Beiträge '
'veröffentlichen?</p>'),
utils_forms.Submit('Benachrichtigungen erhalten'))
class Meta:
fields = ('group',)
model = subscriptions_models.Subscription
def save(self):
attention = super().save(commit=False)
attention.attendee = models.Gestalt.objects.get(user__email=self.cleaned_data['attendee_email'])
attention.save()
return attention
......@@ -21,19 +21,20 @@ def is_subscriber(user, subscription):
rules.add_perm(
'subscriptions.create_content_subscription',
rules.is_authenticated &
content_rules.is_permitted &
~content_rules.is_author &
~content_rules.is_group_member &
~content_rules.is_recipient &
~is_subscribed_to)
rules.is_authenticated
& content_rules.is_permitted
& ~content_rules.is_author
& ~content_rules.is_group_member
& ~content_rules.is_recipient
& ~is_subscribed_to)
rules.add_perm(
'subscriptions.create_group_subscription',
rules.is_authenticated &
~entities_rules.is_group_member &
~is_subscribed_to)
rules.is_authenticated
& ~entities_rules.is_group_member
& ~is_subscribed_to)
rules.add_perm(
'subscriptions.delete_subscription',
rules.is_authenticated & is_subscriber)
rules.is_authenticated
& is_subscriber)
{% load rules subscriptions %}
{% has_perm 'subscriptions.create_group_subscription' user group as can_create_subscription %}
{% has_perm 'subscriptions.delete_subscription' user group|subscription:user as can_delete_subscription %}
{% if can_create_subscription %}
<a href="{% url 'group-subscribe' group.pk %}" class="btn btn-default btn-sm" title="Bei neuen Beiträgen benachrichtigt werden">
<i class="sg sg-subscription"></i> Abonnieren
</a>
{% endif %}
{% if can_delete_subscription %}
<a href="{% url 'group-unsubscribe' group.pk %}" class="btn btn-default btn-sm" title="Du erhältst Benachrichtungen für diese Gruppe">
<i class="sg sg-subscription"></i> Abonniert
</a>
{% endif %}
......@@ -12,6 +12,13 @@ urlpatterns = [
views.ContentUnsubscribe.as_view(),
name='content-unsubscribe'),
#urls.url(r'^group/attention/(?P<pk>[0-9]+)/delete/$', views.GroupAttentionDelete.as_view(), name='group-attention-delete'),
#urls.url(r'^group/(?P<group_pk>[0-9]+)/pay_attention/$', views.GroupAttentionCreate.as_view(), name='group-attention-create'),
urls.url(
r'^group/(?P<group_pk>[0-9]+)/subscribe/$',
views.GroupSubscribe.as_view(),
name='group-subscribe'),
urls.url(
r'^group/(?P<group_pk>[0-9]+)/unsubscribe/$',
views.GroupUnsubscribe.as_view(),
name='group-unsubscribe'),
]
from . import models
from crispy_forms import layout
from django import forms as django_forms
from django.contrib.contenttypes import models as contenttypes_models
from utils import forms as utils_forms, views
......@@ -13,16 +11,21 @@ class SubscriptionMixin:
class Subscribe(SubscriptionMixin, views.Create):
action = 'Abonnieren'
fields = (
utils_forms.Field('content_type', type='constant'),
utils_forms.Field('content_type', type='constant'),
utils_forms.Field('object_id', type='constant'),
utils_forms.Field('subscriber', type='constant'))
def get_initial(self):
return {
'content_type': contenttypes_models.ContentType.objects \
.get_for_model(self.related_object).pk,
'object_id': self.related_object.pk,
'subscriber': self.request.user.gestalt.pk,
'content_type':
contenttypes_models.ContentType.objects
.get_for_model(self.related_object).pk,
'object_id':
self.related_object.pk,
'subscriber':
self.request.user.gestalt.pk,
}
......@@ -36,10 +39,26 @@ class ContentSubscribe(Subscribe):
return self.get_content()
class GroupSubscribe(Subscribe):
description = (
'Benachrichtigt werden, wenn in der Gruppe <em>{{ group }}</em> '
'neue Beiträge veröffentlicht werden')
permission = 'subscriptions.create_group_subscription'
def get_related_object(self):
return self.get_group()
class Unsubscribe(SubscriptionMixin, views.Delete):
action = 'Abonnement aufheben'
permission = 'subscriptions.delete_subscription'
def get_object(self):
return models.Subscription.objects.filter(
subscribed_to=self.related_object,
subscriber=self.request.user.gestalt
).first()
def get_parent(self):
return self.object.subscribed_to
......@@ -49,37 +68,14 @@ class ContentUnsubscribe(Unsubscribe):
'Keine Benachrichtigungen mehr für den Beitrag '
'<em>{{ content }}</em> erhalten')
def get_object(self):
return models.Subscription.objects.filter(
subscribed_to=self.get_content(),
subscriber=self.request.user.gestalt
).first()
'''
class GroupAttentionCreate(utils_views.ActionMixin, generic.CreateView):
action = 'Benachrichtigungen erhalten'
form_class = forms.GroupAttention
menu = 'group'
permission = 'entities.create_group_attention'
def get_initial(self):
return {'attendee_email': self.request.user.email, 'group': self.get_group().pk}
def get_parent(self):
return self.get_group()
def get_related_object(self):
return self.get_content()
def get_permission_object(self):
return self.get_group()
class GroupAttentionDelete(utils_views.ActionMixin, utils_views.DeleteView):
action = 'Keine Benachrichtigungen mehr erhalten'
layout = layout.HTML('<p>Möchtest Du wirklich keine Benachrichtigungen '
'für die Gruppe <em>{{ group }}</em> mehr erhalten?</p>')
menu = 'group'
model = subscriptions_models.Subscription
permission = 'entities.delete_group_attention'
class GroupUnsubscribe(Unsubscribe):
description = (
'Keine Benachrichtigungen mehr für die Gruppe '
'<em>{{ group }}</em> erhalten')
def get_parent(self):
def get_related_object(self):
return self.get_group()
'''
......@@ -267,18 +267,20 @@ class PageMixin(
sidebar = ('calendar', 'groups')
class Create(ActionMixin, generic.CreateView):
def __init__(self, *args, **kwargs):
self._fields = self.fields
self.fields = self.get_fields()
super().__init__(*args, **kwargs)
class RelatedObjectMixin:
def dispatch(self, request, *args, **kwargs):
self.related_object = self.get_related_object()
if not self.related_object:
raise http.Http404('Zugehöriges Objekt nicht gefunden')
return super().dispatch(request, *args, **kwargs)
class Create(RelatedObjectMixin, ActionMixin, generic.CreateView):
def __init__(self, *args, **kwargs):
self._fields = self.fields
self.fields = self.get_fields()
super().__init__(*args, **kwargs)
@staticmethod
def get_field_name(field):
if isinstance(field, layout.Field):
......@@ -303,7 +305,7 @@ class Create(ActionMixin, generic.CreateView):
return self.related_object
class Delete(ActionMixin, edit_views.FormMixin, generic.DeleteView):
class Delete(RelatedObjectMixin, ActionMixin, edit_views.FormMixin, generic.DeleteView):
pass
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment