diff --git a/club/migrations/0010_auto_20170817_1537.py b/club/migrations/0010_auto_20170817_1537.py new file mode 100644 index 00000000..24949949 --- /dev/null +++ b/club/migrations/0010_auto_20170817_1537.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('club', '0009_mailing_mailingsubscription'), + ] + + operations = [ + migrations.AlterField( + model_name='mailingsubscription', + name='email', + field=models.EmailField(verbose_name='Email address', max_length=254), + ), + migrations.AlterUniqueTogether( + name='mailingsubscription', + unique_together=set([('user', 'email', 'mailing')]), + ), + ] diff --git a/club/models.py b/club/models.py index 5a652e20..9484e4e2 100644 --- a/club/models.py +++ b/club/models.py @@ -226,16 +226,21 @@ class Membership(models.Model): class Mailing(models.Model): """ This class correspond to a mailing list - + Remember that mailing lists should be validated by UTBM """ club = models.ForeignKey(Club, verbose_name=_('Club'), related_name="mailings", null=False, blank=False) email = models.EmailField(_('Email address'), unique=True) def is_owned_by(self, user): - return self.club.has_rights_in_club(user) or user.is_root + return user.is_in_group(self) or user.is_root or user.is_board_member - def can_be_edited_by(self, user): - return self.is_owned_by(user) + def can_view(self, user): + return self.club.has_rights_in_club(user) + + def delete(self): + for sub in self.subscriptions.all(): + sub.delete() + super(Mailing, self).delete() def __str__(self): return "%s - %s" % (self.club, self.email) @@ -243,14 +248,31 @@ class Mailing(models.Model): class MailingSubscription(models.Model): """ - This class make the link between user and mailing list + This class makes the link between user and mailing list """ mailing = models.ForeignKey(Mailing, verbose_name=_('Mailing'), related_name="subscriptions", null=False, blank=False) user = models.ForeignKey(User, verbose_name=_('User'), related_name="mailing_subscriptions", null=True, blank=True) - email = models.EmailField(_('Email address'), unique=True) + email = models.EmailField(_('Email address'), blank=False, null=False) + + class Meta: + unique_together = (('user', 'email', 'mailing'),) + + def clean(self): + if not self.user and not self.email: + raise ValidationError(_("At least user or email is required")) + if self.user and not self.email: + self.email = self.user.email + super(MailingSubscription, self).clean() def is_owned_by(self, user): return self.mailing.club.has_rights_in_club(user) or user.is_root def can_be_edited_by(self, user): return self.is_owned_by(user) or (user is not None and user.id == self.user.id) + + def __str__(self): + if self.user: + user = str(self.user) + else: + user = _("Unregistered user") + return "(%s) - %s : %s" % (self.mailing, user, self.email) diff --git a/club/templates/club/mailing.jinja b/club/templates/club/mailing.jinja index 26721097..47f11884 100644 --- a/club/templates/club/mailing.jinja +++ b/club/templates/club/mailing.jinja @@ -8,11 +8,16 @@ {% if has_objects %} {% for mailing in object_list %} -

{% trans %}Mailing{% endtrans %} {{ mailing.email }}

+

{% trans %}Mailing{% endtrans %} {{ mailing.email }} + {%- if user.is_owner(mailing) -%} + {% trans %}Delete{% endtrans %} + {%- endif -%} +

+
- + {% for subscriber in mailing.subscriptions.all() %} @@ -22,6 +27,7 @@ {% endif %} + {% endfor %}
{% trans %}User{% endtrans %}{% trans %}Mail{%endtrans%}{% trans %}Email{%endtrans%}
{% trans %}Unregistered user{% endtrans %}{{ subscriber.email }}{% trans %}Delete{% endtrans %}
@@ -31,8 +37,6 @@

{% trans %}No mailing list existing for this club{% endtrans %}

{% endif %} - {% if club.has_rights_in_club(user) %} - {% if has_objects %}

{% trans %}New member{% endtrans %}

@@ -48,9 +52,5 @@ {{ new_mailing.as_p() }}

- {% endif %} {% endblock %} - - - diff --git a/club/urls.py b/club/urls.py index 6b233a6e..42074a5c 100644 --- a/club/urls.py +++ b/club/urls.py @@ -42,5 +42,7 @@ urlpatterns = [ url(r'^(?P[0-9]+)/mailing$', ClubMailingView.as_view(action=MailingFormType.DISPLAY), name='mailing'), url(r'^(?P[0-9]+)/mailing/new/mailing$', ClubMailingView.as_view(action=MailingFormType.MAILING), name='mailing_create'), url(r'^(?P[0-9]+)/mailing/new/subscription$', ClubMailingView.as_view(action=MailingFormType.MEMBER), name='mailing_subscription_create'), + url(r'^(?P[0-9]+)/mailing/delete$', MailingDeleteView.as_view(), name='mailing_delete'), + url(r'^(?P[0-9]+)/mailing/delete/subscription$', MailingSubscriptionDeleteView.as_view(), name='mailing_subscription_delete'), url(r'^membership/(?P[0-9]+)/set_old$', MembershipSetOldView.as_view(), name='membership_set_old'), ] diff --git a/club/views.py b/club/views.py index 9eeb9abd..6a3b35a0 100644 --- a/club/views.py +++ b/club/views.py @@ -35,6 +35,7 @@ from django.utils import timezone from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext as _t from ajax_select.fields import AutoCompleteSelectField +from django.core.exceptions import PermissionDenied from django.shortcuts import get_object_or_404 from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, TabedViewMixin, CanCreateMixin @@ -56,6 +57,8 @@ class MailingForm(forms.ModelForm): super(MailingForm, self).__init__(*args, **kwargs) if club_id: self.fields['club'].queryset = Club.objects.filter(id=club_id) + self.fields['club'].initial = club_id + self.fields['club'].widget = forms.HiddenInput() class MailingSubscriptionForm(forms.ModelForm): @@ -66,9 +69,22 @@ class MailingSubscriptionForm(forms.ModelForm): def __init__(self, *args, **kwargs): club_id = kwargs.pop('club_id', None) super(MailingSubscriptionForm, self).__init__(*args, **kwargs) + self.fields['email'].required = False if club_id: self.fields['mailing'].queryset = Mailing.objects.filter(club__id=club_id) + def clean(self): + cleaned_data = super(MailingSubscriptionForm, self).clean() + user = cleaned_data.get('user', None) + email = cleaned_data.get('email', None) + mailing = cleaned_data.get('mailing') + if not user and not email: + raise forms.ValidationError(_("At least user or email should be filled")) + if user and not email: + email = user.email + if user and MailingSubscription.objects.filter(mailing=mailing, email=email).exists(): + raise forms.ValidationError(_("This email is already suscribed in this mailing")) + user = AutoCompleteSelectField('users', label=_('User'), help_text=None, required=False) @@ -94,11 +110,6 @@ class ClubTabsMixin(TabedViewMixin): 'slug': 'elderlies', 'name': _("Old members"), }) - tab_list.append({ - 'url': reverse('club:mailing', kwargs={'club_id': self.object.id}), - 'slug': 'mailing', - 'name': _("Mailing list"), - }) if self.request.user.can_edit(self.object): tab_list.append({ 'url': reverse('club:tools', kwargs={'club_id': self.object.id}), @@ -115,6 +126,11 @@ class ClubTabsMixin(TabedViewMixin): 'slug': 'sellings', 'name': _("Sellings"), }) + tab_list.append({ + 'url': reverse('club:mailing', kwargs={'club_id': self.object.id}), + 'slug': 'mailing', + 'name': _("Mailing list"), + }) if self.request.user.is_owner(self.object): tab_list.append({ 'url': reverse('club:club_prop', kwargs={'club_id': self.object.id}), @@ -384,17 +400,24 @@ class MailingFormType(Enum): MAILING = 2 -class ClubMailingView(CanViewMixin, ListView): +class ClubMailingView(ClubTabsMixin, ListView): """ A list of mailing for a given club """ action = None model = Mailing template_name = "club/mailing.jinja" + current_tab = 'mailing' + + def authorized(self): + return self.club.has_rights_in_club(self.user) or self.user.is_root or self.user.is_board_member def dispatch(self, request, *args, **kwargs): self.club = get_object_or_404(Club, pk=kwargs['club_id']) self.user = request.user + self.object = self.club + if not self.authorized(): + raise PermissionDenied self.member_form = MailingSubscriptionForm(club_id=self.club.id) self.mailing_form = MailingForm(club_id=self.club.id) return super(ClubMailingView, self).dispatch(request, *args, **kwargs) @@ -405,10 +428,12 @@ class ClubMailingView(CanViewMixin, ListView): if self.action == MailingFormType.MAILING: form = MailingForm string = 'new_mailing' + model = Mailing elif self.action == MailingFormType.MEMBER: form = MailingSubscriptionForm string = 'new_member' - return MailingGenericCreateView.as_view(list_view=self, form_class=form, form_kwarg_string=string)(request, *args, **kwargs) + model = MailingSubscription + return MailingGenericCreateView.as_view(model=model, list_view=self, form_class=form, form_kwarg_string=string)(request, *args, **kwargs) return res def get_queryset(self): @@ -424,11 +449,10 @@ class ClubMailingView(CanViewMixin, ListView): return kwargs -class MailingGenericCreateView(CanCreateMixin, CreateView, SingleObjectMixin): +class MailingGenericCreateView(CreateView, SingleObjectMixin): """ Create a new mailing list """ - model = Mailing list_view = None form_class = None form_kwarg_string = None @@ -446,8 +470,38 @@ class MailingGenericCreateView(CanCreateMixin, CreateView, SingleObjectMixin): return kwargs def dispatch(self, request, *args, **kwargs): + if not self.list_view.authorized(): + raise PermissionDenied self.template_name = self.list_view.template_name return super(MailingGenericCreateView, self).dispatch(request, *args, **kwargs) def get_success_url(self, **kwargs): return reverse_lazy('club:mailing', kwargs={'club_id': self.list_view.club.id}) + + +class MailingDeleteView(CanEditMixin, DeleteView): + + model = Mailing + template_name = 'core/delete_confirm.jinja' + pk_url_kwarg = "mailing_id" + + def dispatch(self, request, *args, **kwargs): + self.club_id = self.get_object().club.id + return super(MailingDeleteView, self).dispatch(request, *args, **kwargs) + + def get_success_url(self, **kwargs): + return reverse_lazy('club:mailing', kwargs={'club_id': self.club_id}) + + +class MailingSubscriptionDeleteView(CanEditMixin, DeleteView): + + model = MailingSubscription + template_name = 'core/delete_confirm.jinja' + pk_url_kwarg = "mailing_subscription_id" + + def dispatch(self, request, *args, **kwargs): + self.club_id = self.get_object().mailing.club.id + return super(MailingSubscriptionDeleteView, self).dispatch(request, *args, **kwargs) + + def get_success_url(self, **kwargs): + return reverse_lazy('club:mailing', kwargs={'club_id': self.club_id}) diff --git a/core/templates/core/user_clubs.jinja b/core/templates/core/user_clubs.jinja index 0739c5e6..9d138695 100644 --- a/core/templates/core/user_clubs.jinja +++ b/core/templates/core/user_clubs.jinja @@ -58,5 +58,12 @@ {% endfor %} + +{% if user.mailing_subscriptions.exists() %} +

{% trans %}Subscribed mailing lists{% endtrans %}

+ {% for sub in user.mailing_subscriptions.all() %} +

{{ sub }} {% trans %}Unsuscribe{% endtrans %}

+ {% endfor %} +{% endif %} {% endblock %} diff --git a/core/templates/core/user_tools.jinja b/core/templates/core/user_tools.jinja index 4033f702..3f7b0804 100644 --- a/core/templates/core/user_tools.jinja +++ b/core/templates/core/user_tools.jinja @@ -13,6 +13,7 @@ {% if user.is_root %}
  • {% trans %}Groups{% endtrans %}
  • {% trans %}Merge users{% endtrans %}
  • +
  • {% trans %}Mailing lists administration{% endtrans %}
  • {% endif %} {% if user.can_create_subscription or user.is_root %}
  • {% trans %}Subscriptions{% endtrans %}
  • diff --git a/counter/migrations/0014_auto_20170817_1537.py b/counter/migrations/0014_auto_20170817_1537.py new file mode 100644 index 00000000..8e4e3df5 --- /dev/null +++ b/counter/migrations/0014_auto_20170817_1537.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('counter', '0013_customer_recorded_products'), + ] + + operations = [ + migrations.AlterField( + model_name='customer', + name='recorded_products', + field=models.IntegerField(verbose_name='recorded product', default=0), + ), + ] diff --git a/rootplace/templates/rootplace/mailing_lists.jinja b/rootplace/templates/rootplace/mailing_lists.jinja new file mode 100644 index 00000000..16fabf24 --- /dev/null +++ b/rootplace/templates/rootplace/mailing_lists.jinja @@ -0,0 +1,30 @@ +{% extends "core/base.jinja" %} + +{% block title %} +{% trans %}Mailing lists administration{% endtrans %} +{% endblock %} + +{% block content %} +

    {% trans %}This page list all existing mailing lists{% endtrans %}

    + {% if has_objects %} + + + + + + {% for mailing in object_list %} + + + + + {% endfor %} +
    {% trans %}Email{% endtrans %}{% trans %}Club{%endtrans%}
    {{ mailing.email }}{{ mailing.club }} {% trans %}Delete{% endtrans %}
    + + {% else %} +

    {% trans %}No mailing existing{% endtrans %}

    + {% endif %} + +{% endblock %} + + + diff --git a/rootplace/urls.py b/rootplace/urls.py index 6ab433e3..732efe1c 100644 --- a/rootplace/urls.py +++ b/rootplace/urls.py @@ -28,4 +28,5 @@ from rootplace.views import * urlpatterns = [ url(r'^merge$', MergeUsersView.as_view(), name='merge'), + url(r'^mailings$', MailingListAdminView.as_view(), name='mailings') ] diff --git a/rootplace/views.py b/rootplace/views.py index 7a9cb64e..7aef0366 100644 --- a/rootplace/views.py +++ b/rootplace/views.py @@ -2,6 +2,7 @@ # # Copyright 2016,2017 # - Skia +# - Sli # # Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM, # http://ae.utbm.fr. @@ -27,6 +28,7 @@ from django.utils.translation import ugettext as _ from django.views.generic.edit import FormView from django.core.urlresolvers import reverse from django import forms +from django.views.generic import ListView from django.core.exceptions import PermissionDenied from ajax_select.fields import AutoCompleteSelectField @@ -34,6 +36,8 @@ from ajax_select.fields import AutoCompleteSelectField from core.views import CanViewMixin from core.models import User from counter.models import Customer +from club.models import Mailing + def merge_users(u1, u2): u1.nick_name = u1.nick_name or u2.nick_name @@ -86,10 +90,12 @@ def merge_users(u1, u2): u2.delete() return u1 + class MergeForm(forms.Form): user1 = AutoCompleteSelectField('users', label=_("User that will be kept"), help_text=None, required=True) user2 = AutoCompleteSelectField('users', label=_("User that will be deleted"), help_text=None, required=True) + class MergeUsersView(FormView): template_name = "rootplace/merge.jinja" form_class = MergeForm @@ -107,3 +113,17 @@ class MergeUsersView(FormView): def get_success_url(self): return reverse('core:user_profile', kwargs={'user_id': self.final_user.id}) + +class MailingListAdminView(ListView): + template_name = "rootplace/mailing_lists.jinja" + model = Mailing + + def dispatch(self, request, *args, **kwargs): + if not request.user.is_root: + raise PermissionDenied + return super(MailingListAdminView, self).dispatch(request, *args, **kwargs) + + def get_context_data(self, **kwargs): + kwargs = super(MailingListAdminView, self).get_context_data(**kwargs) + kwargs['has_objects'] = len(kwargs['object_list']) > 0 + return kwargs