From 69e997d587b017c7584959030ba300c2704467ad Mon Sep 17 00:00:00 2001 From: klmp200 Date: Sat, 19 Aug 2017 01:19:31 +0200 Subject: [PATCH] Refactoring mailings --- api/urls.py | 1 + api/views/club.py | 20 +++++++++- club/models.py | 2 + club/templates/club/mailing.jinja | 4 +- club/templates/club/mailing_output.jinja | 3 -- club/urls.py | 7 ++-- club/views.py | 47 ++++-------------------- sith/urls.py | 3 -- 8 files changed, 34 insertions(+), 53 deletions(-) delete mode 100644 club/templates/club/mailing_output.jinja diff --git a/api/urls.py b/api/urls.py index 299d04a6..1289668f 100644 --- a/api/urls.py +++ b/api/urls.py @@ -48,5 +48,6 @@ urlpatterns = [ url(r'^', include(router.urls)), url(r'^login/', include('rest_framework.urls', namespace='rest_framework')), url(r'^markdown$', RenderMarkdown, name='api_markdown'), + url(r'^mailings$', FetchMailingLists, name='mailings_fetch') ] diff --git a/api/views/club.py b/api/views/club.py index fae04fc3..ece79602 100644 --- a/api/views/club.py +++ b/api/views/club.py @@ -22,9 +22,15 @@ # # +from rest_framework.response import Response from rest_framework import serializers +from rest_framework.decorators import api_view, renderer_classes +from rest_framework.renderers import StaticHTMLRenderer -from club.models import Club +from django.conf import settings +from django.core.exceptions import PermissionDenied + +from club.models import Club, Mailing from api.views import RightModelViewSet @@ -43,3 +49,15 @@ class ClubViewSet(RightModelViewSet): serializer_class = ClubSerializer queryset = Club.objects.all() + + +@api_view(['GET']) +@renderer_classes((StaticHTMLRenderer,)) +def FetchMailingLists(request): + key = request.GET.get('key', '') + if key != settings.SITH_MAILING_FETCH_KEY: + raise PermissionDenied + data = '' + for mailing in Mailing.objects.all(): + data += mailing.fetch_format() + return Response(data) diff --git a/club/models.py b/club/models.py index 90df31a3..7cac6154 100644 --- a/club/models.py +++ b/club/models.py @@ -276,6 +276,8 @@ class MailingSubscription(models.Model): raise ValidationError(_("At least user or email is required")) if self.user and not self.email: self.email = self.user.email + if MailingSubscription.objects.filter(mailing=self.mailing, email=self.email).exists(): + raise ValidationError(_("This email is already suscribed in this mailing")) super(MailingSubscription, self).clean() def is_owned_by(self, user): diff --git a/club/templates/club/mailing.jinja b/club/templates/club/mailing.jinja index 66e57864..101e630e 100644 --- a/club/templates/club/mailing.jinja +++ b/club/templates/club/mailing.jinja @@ -41,7 +41,7 @@

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

{% csrf_token %} - {{ new_member.as_p() }} + {{ add_member.as_p() }}

{% endif %} @@ -49,7 +49,7 @@

{% trans %}New mailing{% endtrans %}

{% csrf_token %} - {{ new_mailing.as_p() }} + {{ add_mailing.as_p() }}

diff --git a/club/templates/club/mailing_output.jinja b/club/templates/club/mailing_output.jinja deleted file mode 100644 index 8a7133a7..00000000 --- a/club/templates/club/mailing_output.jinja +++ /dev/null @@ -1,3 +0,0 @@ -{%- for mailing in object_list -%} -{{ mailing.fetch_format() }} -{%- endfor -%} \ No newline at end of file diff --git a/club/urls.py b/club/urls.py index bfd178a0..96195b11 100644 --- a/club/urls.py +++ b/club/urls.py @@ -39,11 +39,10 @@ urlpatterns = [ url(r'^(?P[0-9]+)/sellings/csv$', ClubSellingCSVView.as_view(), name='sellings_csv'), url(r'^(?P[0-9]+)/prop$', ClubEditPropView.as_view(), name='club_prop'), url(r'^(?P[0-9]+)/tools$', ClubToolsView.as_view(), name='tools'), - 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$', ClubMailingView.as_view(action="display"), name='mailing'), + url(r'^(?P[0-9]+)/mailing/new/mailing$', ClubMailingView.as_view(action="add_mailing"), name='mailing_create'), + url(r'^(?P[0-9]+)/mailing/new/subscription$', ClubMailingView.as_view(action="add_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'^mailing/fetch$', MailingFetchView.as_view(), name='mailing_fetch'), 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 a283279c..f372679e 100644 --- a/club/views.py +++ b/club/views.py @@ -75,18 +75,6 @@ class MailingSubscriptionForm(forms.ModelForm): 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) @@ -396,12 +384,6 @@ class ClubStatView(TemplateView): return kwargs -class MailingFormType(Enum): - DISPLAY = 0 - MEMBER = 1 - MAILING = 2 - - class ClubMailingView(ClubTabsMixin, ListView): """ A list of mailing for a given club @@ -426,16 +408,14 @@ class ClubMailingView(ClubTabsMixin, ListView): def post(self, request, *args, **kwargs): res = super(ClubMailingView, self).get(request, *args, **kwargs) - if self.action != MailingFormType.DISPLAY: - if self.action == MailingFormType.MAILING: + if self.action != "display": + if self.action == "add_mailing": form = MailingForm - string = 'new_mailing' model = Mailing - elif self.action == MailingFormType.MEMBER: + elif self.action == "add_member": form = MailingSubscriptionForm - string = 'new_member' model = MailingSubscription - return MailingGenericCreateView.as_view(model=model, list_view=self, form_class=form, form_kwarg_string=string)(request, *args, **kwargs) + return MailingGenericCreateView.as_view(model=model, list_view=self, form_class=form)(request, *args, **kwargs) return res def get_queryset(self): @@ -443,8 +423,8 @@ class ClubMailingView(ClubTabsMixin, ListView): def get_context_data(self, **kwargs): kwargs = super(ClubMailingView, self).get_context_data(**kwargs) - kwargs['new_member'] = self.member_form - kwargs['new_mailing'] = self.mailing_form + kwargs['add_member'] = self.member_form + kwargs['add_mailing'] = self.mailing_form kwargs['club'] = self.club kwargs['user'] = self.user kwargs['has_objects'] = len(kwargs['object_list']) > 0 @@ -457,13 +437,12 @@ class MailingGenericCreateView(CreateView, SingleObjectMixin): """ list_view = None form_class = None - form_kwarg_string = None def get_context_data(self, **kwargs): view_kwargs = self.list_view.get_context_data(**kwargs) for key, data in super(MailingGenericCreateView, self).get_context_data(**kwargs).items(): view_kwargs[key] = data - view_kwargs[self.form_kwarg_string] = view_kwargs['form'] + view_kwargs[self.list_view.action] = view_kwargs['form'] return view_kwargs def get_form_kwargs(self): @@ -507,15 +486,3 @@ class MailingSubscriptionDeleteView(CanEditMixin, DeleteView): def get_success_url(self, **kwargs): return reverse_lazy('club:mailing', kwargs={'club_id': self.club_id}) - - -class MailingFetchView(ListView): - - model = Mailing - template_name = 'club/mailing_output.jinja' - - def dispatch(self, request, *args, **kwargs): - key = request.GET.get('key', '') - if key != settings.SITH_MAILING_FETCH_KEY: - raise PermissionDenied - return super(MailingFetchView, self).dispatch(request, *args, **kwargs) diff --git a/sith/urls.py b/sith/urls.py index e21a53cb..2b744c24 100644 --- a/sith/urls.py +++ b/sith/urls.py @@ -43,7 +43,6 @@ from django.conf.urls.static import static from django.conf import settings from django.views.i18n import javascript_catalog from ajax_select import urls as ajax_select_urls -from club.views import MailingFetchView js_info_dict = { 'packages': ('sith',), @@ -73,8 +72,6 @@ urlpatterns = [ url(r'^ajax_select/', include(ajax_select_urls)), url(r'^i18n/', include('django.conf.urls.i18n')), url(r'^jsi18n/$', javascript_catalog, js_info_dict, name='javascript-catalog'), - # This url is for legacy use - url(r'^mailing.php$', MailingFetchView.as_view(), name='mailing_fetch_legacy'), ] if settings.DEBUG: