views.py 3.1 KB

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