clubs: remove moderator and club from mailing form + display not moderated mailings

This commit is contained in:
Antoine Bartuccio 2019-05-09 17:43:47 +02:00
parent 1d07195881
commit d1fb9cc4c3
Signed by: klmp200
GPG Key ID: E7245548C53F904B
5 changed files with 70 additions and 44 deletions

View File

@ -88,7 +88,7 @@ class MailingForm(forms.Form):
) )
# Include fields for handling mailing creation # Include fields for handling mailing creation
mailing_fields = ("email", "club", "moderator") mailing_fields = ("email",)
self.fields.update(forms.fields_for_model(Mailing, fields=mailing_fields)) self.fields.update(forms.fields_for_model(Mailing, fields=mailing_fields))
for field in mailing_fields: for field in mailing_fields:
self.fields["mailing_" + field] = self.fields.pop(field) self.fields["mailing_" + field] = self.fields.pop(field)
@ -103,17 +103,10 @@ class MailingForm(forms.Form):
self.fields["subscription_" + field] = self.fields.pop(field) self.fields["subscription_" + field] = self.fields.pop(field)
self.fields["subscription_" + field].required = False self.fields["subscription_" + field].required = False
self.fields["mailing_club"].queryset = Club.objects.filter(id=club_id)
self.fields["mailing_club"].initial = club_id
self.fields["mailing_club"].widget = forms.HiddenInput()
self.fields["subscription_mailing"].queryset = Mailing.objects.filter( self.fields["subscription_mailing"].queryset = Mailing.objects.filter(
club__id=club_id, is_moderated=True club__id=club_id, is_moderated=True
) )
self.fields["mailing_moderator"].queryset = User.objects.filter(id=user_id)
self.fields["mailing_moderator"].initial = user_id
self.fields["mailing_moderator"].widget = forms.HiddenInput()
def check_required(self, cleaned_data, field): def check_required(self, cleaned_data, field):
""" """
If the given field doesn't exist or has no value, add a required error on it If the given field doesn't exist or has no value, add a required error on it
@ -150,8 +143,8 @@ class MailingForm(forms.Form):
if cleaned_data["action"] == self.ACTION_NEW_MAILING: if cleaned_data["action"] == self.ACTION_NEW_MAILING:
self.check_required(cleaned_data, "mailing_email") self.check_required(cleaned_data, "mailing_email")
self.check_required(cleaned_data, "mailing_club") # self.check_required(cleaned_data, "mailing_club")
self.check_required(cleaned_data, "mailing_moderator") # self.check_required(cleaned_data, "mailing_moderator")
if cleaned_data["action"] == self.ACTION_NEW_SUBSCRIPTION: if cleaned_data["action"] == self.ACTION_NEW_SUBSCRIPTION:
self.check_required(cleaned_data, "subscription_mailing") self.check_required(cleaned_data, "subscription_mailing")

View File

@ -338,6 +338,8 @@ class Mailing(models.Model):
) )
def clean(self): def clean(self):
if Mailing.objects.filter(email=self.email).exists():
raise ValidationError(_("This mailing list already exists."))
if self.can_moderate(self.moderator): if self.can_moderate(self.moderator):
self.is_moderated = True self.is_moderated = True
else: else:

View File

@ -6,11 +6,20 @@
{% endblock %} {% endblock %}
{% block content %} {% block content %}
{% if mailings %}
<b>{% trans %}Remember : mailing lists need to be moderated, if your new created list is not shown wait until moderation takes action{% endtrans %}</b> <b>{% trans %}Remember : mailing lists need to be moderated, if your new created list is not shown wait until moderation takes action{% endtrans %}</b>
{% for mailing in mailings %} {% if mailings_not_moderated %}
<p>{% trans %}Mailing lists waiting for moderation{% endtrans %}</p>
<ul>
{% for mailing in mailings_not_moderated %}
<li>{{ mailing.email_full }}<a href="{{ url('club:mailing_delete', mailing_id=mailing.id) }}"> - {% trans %}Delete{% endtrans %}</a></li>
{% endfor %}
</ul>
{% endif %}
{% if mailings_moderated %}
{% for mailing in mailings_moderated %}
<h2>{% trans %}Mailing{% endtrans %} {{ mailing.email_full }} <h2>{% trans %}Mailing{% endtrans %} {{ mailing.email_full }}
{%- if user.is_owner(mailing) -%} {%- if user.is_owner(mailing) -%}
<a href="{{ url('club:mailing_delete', mailing_id=mailing.id) }}"> - {% trans %}Delete{% endtrans %}</a> <a href="{{ url('club:mailing_delete', mailing_id=mailing.id) }}"> - {% trans %}Delete{% endtrans %}</a>
@ -87,15 +96,11 @@
<h2>{% trans %}New mailing{% endtrans %}</h2> <h2>{% trans %}New mailing{% endtrans %}</h2>
<form action="{{ url('club:mailing', club_id=club.id) }}" method="post" enctype="multipart/form-data"> <form action="{{ url('club:mailing', club_id=club.id) }}" method="post" enctype="multipart/form-data">
{% csrf_token %} {% csrf_token %}
{{ form.mailing_club.errors }}
{{ form.mailing_moderator.errors }}
<p> <p>
{{ form.mailing_email.errors }} {{ form.mailing_email.errors }}
<label for="{{ form.mailing_email.id_for_label }}">{{ form.mailing_email.label }}</label> <label for="{{ form.mailing_email.id_for_label }}">{{ form.mailing_email.label }}</label>
{{ form.mailing_email }} {{ form.mailing_email }}
</p> </p>
{{ form.mailing_club }}
{{ form.mailing_moderator }}
<input hidden type="number" name="{{ form.action.name }}" value="{{ form_actions.NEW_MALING }}" /> <input hidden type="number" name="{{ form.action.name }}" value="{{ form_actions.NEW_MALING }}" />
<p><input type="submit" value="{% trans %}Create mailing list{% endtrans %}" /></p> <p><input type="submit" value="{% trans %}Create mailing list{% endtrans %}" /></p>
</form> </form>

View File

@ -35,7 +35,7 @@ from django.core.urlresolvers import reverse, reverse_lazy
from django.utils import timezone from django.utils import timezone
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.utils.translation import ugettext as _t from django.utils.translation import ugettext as _t
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied, ValidationError, NON_FIELD_ERRORS
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
from core.views import ( from core.views import (
@ -532,6 +532,9 @@ class ClubMailingView(ClubTabsMixin, CanEditMixin, DetailFormView):
kwargs["mailings_moderated"] = ( kwargs["mailings_moderated"] = (
kwargs["mailings"].exclude(is_moderated=False).all() kwargs["mailings"].exclude(is_moderated=False).all()
) )
kwargs["mailings_not_moderated"] = (
kwargs["mailings"].exclude(is_moderated=True).all()
)
kwargs["form_actions"] = { kwargs["form_actions"] = {
"NEW_MALING": self.form_class.ACTION_NEW_MAILING, "NEW_MALING": self.form_class.ACTION_NEW_MAILING,
"NEW_SUBSCRIPTION": self.form_class.ACTION_NEW_SUBSCRIPTION, "NEW_SUBSCRIPTION": self.form_class.ACTION_NEW_SUBSCRIPTION,
@ -539,20 +542,24 @@ class ClubMailingView(ClubTabsMixin, CanEditMixin, DetailFormView):
} }
return kwargs return kwargs
def add_new_mailing(self, cleaned_data): def add_new_mailing(self, cleaned_data) -> ValidationError:
""" """
Create a new mailing list from the form Create a new mailing list from the form
""" """
mailing = Mailing( mailing = Mailing(
club=cleaned_data["mailing_club"], club=self.get_object(),
email=cleaned_data["mailing_email"], email=cleaned_data["mailing_email"],
moderator=cleaned_data["mailing_moderator"], moderator=self.request.user,
is_moderated=False, is_moderated=False,
) )
try:
mailing.clean() mailing.clean()
except ValidationError as validation_error:
return validation_error
mailing.save() mailing.save()
return None
def add_new_subscription(self, cleaned_data): def add_new_subscription(self, cleaned_data) -> ValidationError:
""" """
Add mailing subscriptions for each user given and/or for the specified email in form Add mailing subscriptions for each user given and/or for the specified email in form
""" """
@ -568,9 +575,15 @@ class ClubMailingView(ClubTabsMixin, CanEditMixin, DetailFormView):
mailing=cleaned_data["subscription_mailing"], mailing=cleaned_data["subscription_mailing"],
email=cleaned_data["subscription_email"], email=cleaned_data["subscription_email"],
) )
try:
sub.clean() sub.clean()
except ValidationError as validation_error:
return validation_error
sub.save() sub.save()
return None
def remove_subscription(self, cleaned_data): def remove_subscription(self, cleaned_data):
""" """
Remove specified users from a mailing list Remove specified users from a mailing list
@ -588,16 +601,21 @@ class ClubMailingView(ClubTabsMixin, CanEditMixin, DetailFormView):
resp = super(ClubMailingView, self).form_valid(form) resp = super(ClubMailingView, self).form_valid(form)
cleaned_data = form.clean() cleaned_data = form.clean()
error = None
if cleaned_data["action"] == self.form_class.ACTION_NEW_MAILING: if cleaned_data["action"] == self.form_class.ACTION_NEW_MAILING:
self.add_new_mailing(cleaned_data) error = self.add_new_mailing(cleaned_data)
if cleaned_data["action"] == self.form_class.ACTION_NEW_SUBSCRIPTION: if cleaned_data["action"] == self.form_class.ACTION_NEW_SUBSCRIPTION:
self.add_new_subscription(cleaned_data) error = self.add_new_subscription(cleaned_data)
if cleaned_data["action"] == self.form_class.ACTION_REMOVE_SUBSCRIPTION: if cleaned_data["action"] == self.form_class.ACTION_REMOVE_SUBSCRIPTION:
self.remove_subscription(cleaned_data) self.remove_subscription(cleaned_data)
if error:
form.add_error(NON_FIELD_ERRORS, error)
return self.form_invalid(form)
return resp return resp
def get_success_url(self, **kwargs): def get_success_url(self, **kwargs):

View File

@ -6,7 +6,7 @@
msgid "" msgid ""
msgstr "" msgstr ""
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2019-05-01 22:45+0200\n" "POT-Creation-Date: 2019-05-09 11:33+0200\n"
"PO-Revision-Date: 2016-07-18\n" "PO-Revision-Date: 2016-07-18\n"
"Last-Translator: Skia <skia@libskia.so>\n" "Last-Translator: Skia <skia@libskia.so>\n"
"Language-Team: AE info <ae.info@utbm.fr>\n" "Language-Team: AE info <ae.info@utbm.fr>\n"
@ -174,10 +174,10 @@ msgstr "étiquette"
msgid "target type" msgid "target type"
msgstr "type de cible" msgstr "type de cible"
#: accounting/models.py:313 club/models.py:413 #: accounting/models.py:313 club/models.py:415
#: club/templates/club/club_members.jinja:16 #: club/templates/club/club_members.jinja:16
#: club/templates/club/club_old_members.jinja:8 #: club/templates/club/club_old_members.jinja:8
#: club/templates/club/mailing.jinja:32 #: club/templates/club/mailing.jinja:41
#: counter/templates/counter/cash_summary_list.jinja:32 #: counter/templates/counter/cash_summary_list.jinja:32
#: counter/templates/counter/stats.jinja:15 #: counter/templates/counter/stats.jinja:15
#: counter/templates/counter/stats.jinja:52 #: counter/templates/counter/stats.jinja:52
@ -345,7 +345,7 @@ msgstr "Compte en banque : "
#: accounting/templates/accounting/club_account_details.jinja:60 #: accounting/templates/accounting/club_account_details.jinja:60
#: accounting/templates/accounting/label_list.jinja:26 #: accounting/templates/accounting/label_list.jinja:26
#: club/templates/club/club_sellings.jinja:50 #: club/templates/club/club_sellings.jinja:50
#: club/templates/club/mailing.jinja:16 club/templates/club/mailing.jinja:34 #: club/templates/club/mailing.jinja:25 club/templates/club/mailing.jinja:43
#: com/templates/com/mailing_admin.jinja:19 #: com/templates/com/mailing_admin.jinja:19
#: com/templates/com/news_admin_list.jinja:41 #: com/templates/com/news_admin_list.jinja:41
#: com/templates/com/news_admin_list.jinja:70 #: com/templates/com/news_admin_list.jinja:70
@ -1054,7 +1054,7 @@ msgstr "description"
msgid "past member" msgid "past member"
msgstr "Anciens membres" msgstr "Anciens membres"
#: club/models.py:323 club/models.py:418 #: club/models.py:323 club/models.py:420
msgid "Email address" msgid "Email address"
msgstr "Adresse email" msgstr "Adresse email"
@ -1071,19 +1071,23 @@ msgstr "est modéré"
msgid "moderator" msgid "moderator"
msgstr "modérateur" msgstr "modérateur"
#: club/models.py:406 club/templates/club/mailing.jinja:14 #: club/models.py:342
msgid "This mailing list already exists."
msgstr "Cette liste de diffusion existe déjà."
#: club/models.py:408 club/templates/club/mailing.jinja:23
msgid "Mailing" msgid "Mailing"
msgstr "Liste de diffusion" msgstr "Liste de diffusion"
#: club/models.py:425 #: club/models.py:427
msgid "At least user or email is required" msgid "At least user or email is required"
msgstr "Au moins un utilisateur ou un email est nécessaire" msgstr "Au moins un utilisateur ou un email est nécessaire"
#: club/models.py:433 #: club/models.py:435
msgid "This email is already suscribed in this mailing" msgid "This email is already suscribed in this mailing"
msgstr "Cet email est déjà abonné à cette mailing" msgstr "Cet email est déjà abonné à cette mailing"
#: club/models.py:459 #: club/models.py:461
msgid "Unregistered user" msgid "Unregistered user"
msgstr "Utilisateur non enregistré" msgstr "Utilisateur non enregistré"
@ -1275,7 +1279,7 @@ msgstr "Gestion des laveries"
msgid "Mailing lists" msgid "Mailing lists"
msgstr "Mailing listes" msgstr "Mailing listes"
#: club/templates/club/mailing.jinja:11 #: club/templates/club/mailing.jinja:10
msgid "" msgid ""
"Remember : mailing lists need to be moderated, if your new created list is " "Remember : mailing lists need to be moderated, if your new created list is "
"not shown wait until moderation takes action" "not shown wait until moderation takes action"
@ -1284,40 +1288,44 @@ msgstr ""
"nouvellement créee n'est pas affichée, attendez jusqu'à qu'un modérateur " "nouvellement créee n'est pas affichée, attendez jusqu'à qu'un modérateur "
"entre en action" "entre en action"
#: club/templates/club/mailing.jinja:20 #: club/templates/club/mailing.jinja:13
msgid "Mailing lists waiting for moderation"
msgstr "Listes de diffusions en attente de modération"
#: club/templates/club/mailing.jinja:29
msgid "Generate mailing list" msgid "Generate mailing list"
msgstr "Générer la liste de diffusion" msgstr "Générer la liste de diffusion"
#: club/templates/club/mailing.jinja:33 #: club/templates/club/mailing.jinja:42
#: com/templates/com/mailing_admin.jinja:10 #: com/templates/com/mailing_admin.jinja:10
msgid "Email" msgid "Email"
msgstr "Email" msgstr "Email"
#: club/templates/club/mailing.jinja:49 #: club/templates/club/mailing.jinja:58
msgid "Remove from mailing list" msgid "Remove from mailing list"
msgstr "Supprimer de la liste de diffusion" msgstr "Supprimer de la liste de diffusion"
#: club/templates/club/mailing.jinja:53 #: club/templates/club/mailing.jinja:62
msgid "There is no subscriber for this mailing list" msgid "There is no subscriber for this mailing list"
msgstr "Il n'y a pas d'abonnés dans cette liste de diffusion" msgstr "Il n'y a pas d'abonnés dans cette liste de diffusion"
#: club/templates/club/mailing.jinja:58 #: club/templates/club/mailing.jinja:67
msgid "No mailing list existing for this club" msgid "No mailing list existing for this club"
msgstr "Aucune mailing liste n'existe pour ce club" msgstr "Aucune mailing liste n'existe pour ce club"
#: club/templates/club/mailing.jinja:63 #: club/templates/club/mailing.jinja:72
msgid "New member" msgid "New member"
msgstr "Nouveau membre" msgstr "Nouveau membre"
#: club/templates/club/mailing.jinja:83 #: club/templates/club/mailing.jinja:92
msgid "Add to mailing list" msgid "Add to mailing list"
msgstr "Ajouter à la mailing liste" msgstr "Ajouter à la mailing liste"
#: club/templates/club/mailing.jinja:87 #: club/templates/club/mailing.jinja:96
msgid "New mailing" msgid "New mailing"
msgstr "Nouvelle liste de diffusion" msgstr "Nouvelle liste de diffusion"
#: club/templates/club/mailing.jinja:100 #: club/templates/club/mailing.jinja:109
msgid "Create mailing list" msgid "Create mailing list"
msgstr "Créer une liste de diffusion" msgstr "Créer une liste de diffusion"