Browse Source

Implement a view for a member to manage its own subscriptions

Jocelyn Delalande 6 years ago
parent
commit
a75f43e32f

+ 12 - 0
coin/static/css/local.css

@@ -181,6 +181,18 @@ table.no-background tr {
     word-break: break-all;
 }
 
+/* Specific table: mailling-list subscriptions */
+#mail-list-subscriptions .select-col {
+    min-width: 8rem;
+}
+#mail-list-subscriptions tr td select:last-child {
+    margin: 0;  /* So that they line-up vertically */
+    padding: 0;
+}
+#mail-list-subscriptions input[type=submit] {
+     min-width: 100%;
+}
+
 /* login page */
 #login-form {}
 #login-form table td {

+ 5 - 0
coin/templates/menu_items.html

@@ -7,6 +7,11 @@
 <li class="{% ifactive 'hardware_provisioning:loan-list' %}active{% endifactive %}"><a href="{% url 'hardware_provisioning:loan-list' %}"><i
             class="fa fa-exchange fa-fw"></i> Mon matériel</a></li>
 {% endif %}
+{% if 'maillists' in INSTALLED_APPS %}
+<li class="{% ifactive 'maillists:lists-list' %}active{% endifactive %}"><a href="{% url 'maillists:lists-list' %}"><i
+            class="fa fa-envelope fa-fw"></i> Listes mail</a></li>
+
+{% endif %}
 <li class="{% ifactive 'members:contact' %}active{% endifactive %}"><a href="{% url 'members:contact' %}"><i class="fa fa-life-ring fa-fw"></i> Contact / Support</a></li>
 <li class="divider"></li>
 {% if user.is_staff %}<li><a href="{% url 'admin:index' %}"><i class="fa fa-cogs fa-fw"></i> Administration</a></li>{% endif %}

+ 3 - 1
maillists/app.py

@@ -2,8 +2,10 @@
 from __future__ import unicode_literals
 
 from django.apps import AppConfig
+import coin.apps
 
 
-class MailListsConfig(AppConfig):
+class MailListsConfig(AppConfig, coin.apps.AppURLs):
     name = 'maillists'
     verbose_name = "Listes mail"
+    exported_urlpatterns = [('maillists', 'maillists.urls')]

+ 47 - 0
maillists/templates/maillists/maillinglist_list.html

@@ -0,0 +1,47 @@
+{% extends "base.html" %}
+
+{% block title %}Listes mail - {{ block.super }}{% endblock %}
+
+{% block content %}
+<h2>Listes mail : mes abonnements</h2>
+
+<style>
+ table  {
+ }
+
+</style>
+
+{% for message in messages %}
+<div class="message {{ message.tags }}">{{ message }}</div>
+{% endfor %}
+
+
+<p>
+    Dans ce tableau, tu peux gérer toi-même tes abonnements/désabonnements aux
+    listes qui sont en libre accès pour les membres.
+</p>
+
+<form action="" method="post" id="mail-list-subscriptions">{% csrf_token %}
+    {{ formset.management_form }}
+    <table class="full-width">
+        <thead>
+            <tr>
+                <th>Liste</th>
+                <th class="select-col">Abonnement</th>
+                <th>Description</th>
+            </tr>
+        </thead>
+        <tbody>
+            {% for form in formset %}
+            <tr>
+                <td>{{ form.initial.maillinglist }}</td>
+                <td>{{ form.maillinglist }}{{ form.subscribed }}</td>
+                <td>{{ form.initial.maillinglist.description|linebreaks }}</td>
+            </tr>
+            {% endfor %}
+        </tbody>
+    </table>
+    <input type="submit" class="button" value="Enregistrer mes paramètres d'abonnement" />
+</form>
+
+{% endblock %}

+ 10 - 0
maillists/urls.py

@@ -0,0 +1,10 @@
+# -*- coding: utf-8 -*-
+
+from __future__ import unicode_literals
+from django.conf.urls import url
+from . import views
+
+
+urlpatterns = [
+    url(r'^$', views.lists_list, name='lists-list'),
+]

+ 93 - 0
maillists/views.py

@@ -0,0 +1,93 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.contrib import messages
+from django.contrib.auth.decorators import login_required
+from django import forms
+from django.forms import formset_factory
+from django.shortcuts import render, redirect
+
+from .models import MaillingList
+from coin.members.models import Member
+
+#MaillingListFormSet = modelformset_factory(MaillingList, fields=)
+
+
+class SubscriptionForm(forms.Form):
+    subscribed = forms.BooleanField(widget=forms.Select(choices=(
+        (True, 'Abonné·e'),
+        (False, 'Non abonné·e'),
+    )), required=False)
+    maillinglist = forms.ModelChoiceField(
+        queryset=MaillingList.objects.all(),
+        widget=forms.HiddenInput,
+    )
+
+    class Meta:
+        widgets = {
+            'subscribed': forms.Select
+        }
+
+
+SubscriptionFormSet = formset_factory(SubscriptionForm, extra=0)
+
+
+class MemberSubscriptionsForm(forms.ModelForm):
+    class Meta:
+        model = Member
+        fields = []
+
+
+@login_required
+def lists_list(request):
+    if request.method == 'POST':
+        formset = SubscriptionFormSet(request.POST, request.FILES)
+
+        if formset.is_valid():
+            # we do proper add/remove instead of clearing and setting to take
+            # care of acurate signal sending (cf signal receivers in models.py).
+            new_subscriptions = set(
+                i['maillinglist']
+                for i in formset.cleaned_data
+                if i['subscribed']
+            )
+            old_subscriptions = set(request.user.subscribed_maillinglists.all())
+            try:
+                for mail_list in new_subscriptions - old_subscriptions:
+                    request.user.subscribed_maillinglists.add(mail_list)
+
+                for mail_list in old_subscriptions - new_subscriptions:
+                    request.user.subscribed_maillinglists.remove(mail_list)
+            except SystemError as e:
+                messages.error(
+                    request,
+                    "Impossible de sauvegarder tes abonnements. "
+                    "Contacte les administrateur·ice·s système pour "
+                    "qu'iels voient ce qui se passe… \n"
+                    "Tes abonnements n'ont **pas** été mis à jour.")
+            else:
+
+                if old_subscriptions != new_subscriptions:
+                    messages.success(
+                        request,
+                        'Tes (dés)abonnements aux listes mail ont été '
+                        'pris en compte.')
+                else:
+                    messages.warning(
+                        request,
+                        "Vous n'avez modifié aucun abonnement.")
+
+            return redirect('maillists:lists-list')
+
+    else:
+        user_subscriptions = request.user.subscribed_maillinglists.all()
+        formset = SubscriptionFormSet(initial=[
+            {'subscribed': l in user_subscriptions, 'maillinglist': l}
+            for l in MaillingList.objects.all()
+        ])
+
+    return render(request, 'maillists/maillinglist_list.html', {
+        'formset': formset,
+        'all_lists': MaillingList.objects.all(),
+        'my_lists': request.user.subscribed_maillinglists.all(),
+    })