Commit 69e997d5 authored by Sli's avatar Sli

Refactoring mailings

parent 3a6f7009
......@@ -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')
]
......@@ -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)
......@@ -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):
......
......@@ -41,7 +41,7 @@
<h2>{% trans %}New member{% endtrans %}</h2>
<form action="{{ url('club:mailing_subscription_create', club_id=club.id) }}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ new_member.as_p() }}
{{ add_member.as_p() }}
<p><input type="submit" value="{% trans %}Add to mailing list{% endtrans %}" /></p>
</form>
{% endif %}
......@@ -49,7 +49,7 @@
<h2>{% trans %}New mailing{% endtrans %}</h2>
<form action="{{ url('club:mailing_create', club_id=club.id) }}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ new_mailing.as_p() }}
{{ add_mailing.as_p() }}
<p><input type="submit" value="{% trans %}Create mailing list{% endtrans %}" /></p>
</form>
......
{%- for mailing in object_list -%}
{{ mailing.fetch_format() }}
{%- endfor -%}
\ No newline at end of file
......@@ -39,11 +39,10 @@ urlpatterns = [
url(r'^(?P<club_id>[0-9]+)/sellings/csv$', ClubSellingCSVView.as_view(), name='sellings_csv'),
url(r'^(?P<club_id>[0-9]+)/prop$', ClubEditPropView.as_view(), name='club_prop'),
url(r'^(?P<club_id>[0-9]+)/tools$', ClubToolsView.as_view(), name='tools'),
url(r'^(?P<club_id>[0-9]+)/mailing$', ClubMailingView.as_view(action=MailingFormType.DISPLAY), name='mailing'),
url(r'^(?P<club_id>[0-9]+)/mailing/new/mailing$', ClubMailingView.as_view(action=MailingFormType.MAILING), name='mailing_create'),
url(r'^(?P<club_id>[0-9]+)/mailing/new/subscription$', ClubMailingView.as_view(action=MailingFormType.MEMBER), name='mailing_subscription_create'),
url(r'^(?P<club_id>[0-9]+)/mailing$', ClubMailingView.as_view(action="display"), name='mailing'),
url(r'^(?P<club_id>[0-9]+)/mailing/new/mailing$', ClubMailingView.as_view(action="add_mailing"), name='mailing_create'),
url(r'^(?P<club_id>[0-9]+)/mailing/new/subscription$', ClubMailingView.as_view(action="add_member"), name='mailing_subscription_create'),
url(r'^(?P<mailing_id>[0-9]+)/mailing/delete$', MailingDeleteView.as_view(), name='mailing_delete'),
url(r'^(?P<mailing_subscription_id>[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<membership_id>[0-9]+)/set_old$', MembershipSetOldView.as_view(), name='membership_set_old'),
]
......@@ -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)
......@@ -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:
......
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