views.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from django.contrib import messages
  4. from django.contrib.auth.decorators import login_required
  5. from django import forms
  6. from django.forms import formset_factory
  7. from django.shortcuts import render, redirect
  8. from .models import MaillingList, MaillingListSubscription, SyncCommandError
  9. from coin.members.models import Member
  10. class SubscriptionForm(forms.Form):
  11. subscribed = forms.BooleanField(widget=forms.Select(choices=(
  12. (True, 'Abonné·e'),
  13. (False, 'Non abonné·e'),
  14. )), required=False)
  15. maillinglist = forms.ModelChoiceField(
  16. queryset=MaillingList.objects.all(),
  17. widget=forms.HiddenInput,
  18. )
  19. class Meta:
  20. widgets = {
  21. 'subscribed': forms.Select
  22. }
  23. SubscriptionFormSet = formset_factory(SubscriptionForm, extra=0)
  24. class MemberSubscriptionsForm(forms.ModelForm):
  25. class Meta:
  26. model = Member
  27. fields = []
  28. @login_required
  29. def lists_list(request):
  30. if request.method == 'POST':
  31. formset = SubscriptionFormSet(request.POST, request.FILES)
  32. if formset.is_valid():
  33. # we do proper add/remove instead of clearing and setting to take
  34. # care of acurate signal sending (cf signal receivers in models.py).
  35. new_subscriptions = set(
  36. i['maillinglist']
  37. for i in formset.cleaned_data
  38. if i['subscribed']
  39. )
  40. old_subscriptions = set(request.user.subscribed_maillinglists.all())
  41. try:
  42. # add
  43. for mail_list in new_subscriptions - old_subscriptions:
  44. MaillingListSubscription.objects.create(member=request.user, maillinglist=mail_list)
  45. # remove
  46. to_remove = old_subscriptions - new_subscriptions
  47. MaillingListSubscription.objects.filter(member=request.user, maillinglist__in=to_remove).delete()
  48. except SyncCommandError as e:
  49. messages.error(
  50. request,
  51. "Impossible de sauvegarder tes abonnements. "
  52. "Contacte les administrateur·ice·s système pour "
  53. "qu'iels voient ce qui se passe… \n"
  54. "Tes abonnements n'ont **pas** été mis à jour.")
  55. else:
  56. if old_subscriptions != new_subscriptions:
  57. messages.success(
  58. request,
  59. 'Tes (dés)abonnements aux listes mail ont été '
  60. 'pris en compte.')
  61. else:
  62. messages.warning(
  63. request,
  64. "Vous n'avez modifié aucun abonnement.")
  65. return redirect('maillists:lists-list')
  66. else:
  67. user_subscriptions = request.user.subscribed_maillinglists.all()
  68. formset = SubscriptionFormSet(initial=[
  69. {'subscribed': l in user_subscriptions, 'maillinglist': l}
  70. for l in MaillingList.objects.all()
  71. ])
  72. return render(request, 'maillists/maillinglist_list.html', {
  73. 'formset': formset,
  74. 'all_lists': MaillingList.objects.all(),
  75. 'my_lists': request.user.subscribed_maillinglists.all(),
  76. })