views.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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, 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. for mail_list in new_subscriptions - old_subscriptions:
  43. request.user.subscribed_maillinglists.add(mail_list)
  44. for mail_list in old_subscriptions - new_subscriptions:
  45. request.user.subscribed_maillinglists.remove(mail_list)
  46. except SyncCommandError as e:
  47. messages.error(
  48. request,
  49. "Impossible de sauvegarder tes abonnements. "
  50. "Contacte les administrateur·ice·s système pour "
  51. "qu'iels voient ce qui se passe… \n"
  52. "Tes abonnements n'ont **pas** été mis à jour.")
  53. else:
  54. if old_subscriptions != new_subscriptions:
  55. messages.success(
  56. request,
  57. 'Tes (dés)abonnements aux listes mail ont été '
  58. 'pris en compte.')
  59. else:
  60. messages.warning(
  61. request,
  62. "Vous n'avez modifié aucun abonnement.")
  63. return redirect('maillists:lists-list')
  64. else:
  65. user_subscriptions = request.user.subscribed_maillinglists.all()
  66. formset = SubscriptionFormSet(initial=[
  67. {'subscribed': l in user_subscriptions, 'maillinglist': l}
  68. for l in MaillingList.objects.all()
  69. ])
  70. return render(request, 'maillists/maillinglist_list.html', {
  71. 'formset': formset,
  72. 'all_lists': MaillingList.objects.all(),
  73. 'my_lists': request.user.subscribed_maillinglists.all(),
  74. })