From 38edf3b0ebc262c66c313e949179a0365e11ef4b Mon Sep 17 00:00:00 2001 From: grisel-davy Date: Sat, 30 Jun 2018 19:19:40 +0200 Subject: [PATCH] =?UTF-8?q?Cr=C3=A9ation,=20modification,=20suppression=20?= =?UTF-8?q?d'adresses=20mail=20de=20contact?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- preferences/admin.py | 7 ++ preferences/forms.py | 30 ++++++- preferences/models.py | 23 +++++ .../preferences/aff_mailcontact.html | 45 ++++++++++ .../preferences/display_preferences.html | 9 ++ preferences/urls.py | 15 +++- preferences/views.py | 83 ++++++++++++++++++- re2o/templatetags/acl.py | 1 - 8 files changed, 206 insertions(+), 7 deletions(-) create mode 100644 preferences/templates/preferences/aff_mailcontact.html diff --git a/preferences/admin.py b/preferences/admin.py index 296dc57c..043370db 100644 --- a/preferences/admin.py +++ b/preferences/admin.py @@ -34,6 +34,7 @@ from .models import ( OptionalTopologie, GeneralOption, Service, + MailContact, AssoOption, MailMessageOption, HomeOption @@ -65,6 +66,11 @@ class ServiceAdmin(VersionAdmin): pass +class MailContactAdmin(VersionAdmin): + """Class admin gestion des adresses mail de contact""" + pass + + class AssoOptionAdmin(VersionAdmin): """Class admin options de l'asso""" pass @@ -86,5 +92,6 @@ admin.site.register(OptionalTopologie, OptionalTopologieAdmin) admin.site.register(GeneralOption, GeneralOptionAdmin) admin.site.register(HomeOption, HomeOptionAdmin) admin.site.register(Service, ServiceAdmin) +admin.site.register(MailContact, MailContactAdmin) admin.site.register(AssoOption, AssoOptionAdmin) admin.site.register(MailMessageOption, MailMessageOptionAdmin) diff --git a/preferences/forms.py b/preferences/forms.py index 477d4d57..193beca8 100644 --- a/preferences/forms.py +++ b/preferences/forms.py @@ -35,7 +35,8 @@ from .models import ( AssoOption, MailMessageOption, HomeOption, - Service + Service, + MailContact ) @@ -227,3 +228,30 @@ class DelServiceForm(Form): self.fields['services'].queryset = instances else: self.fields['services'].queryset = Service.objects.all() + +class MailContactForm(ModelForm): + """Edition, ajout d'adresse de contact""" + class Meta: + model = MailContact + fields = '__all__' + + def __init__(self, *args, **kwargs): + prefix = kwargs.pop('prefix', self.Meta.model.__name__) + super(MailContactForm, self).__init__(*args, prefix=prefix, **kwargs) + + +class DelMailContactForm(Form): + """Suppression d'adresse de contact""" + mailcontacts = forms.ModelMultipleChoiceField( + queryset=MailContact.objects.none(), + label="Enregistrements adresses actuels", + widget=forms.CheckboxSelectMultiple + ) + + def __init__(self, *args, **kwargs): + instances = kwargs.pop('instances', None) + super(DelMailContactForm, self).__init__(*args, **kwargs) + if instances: + self.fields['mailcontacts'].queryset = instances + else: + self.fields['mailcontacts'].queryset = MailContact.objects.all() diff --git a/preferences/models.py b/preferences/models.py index c2d6aa74..d6a1c6b6 100644 --- a/preferences/models.py +++ b/preferences/models.py @@ -267,6 +267,29 @@ class Service(AclMixin, models.Model): def __str__(self): return str(self.name) +class MailContact(AclMixin, models.Model): + """Addresse mail de contact associée à un commentaire descriptif""" + + address = models.EmailField( + default = "contact@example.org", + help_text = "Adresse mail de contact" + ) + + commentary = models.CharField( + blank = True, + null = True, + help_text = "Description de l'utilisation de l'adresse mail associée", + max_length = 256 + ) + + class Meta: + permissions = ( + ("view_mailcontact", "Peut voir les mails de contact"), + ) + + def __str__(self): + return(self.address) + class AssoOption(AclMixin, PreferencesModel): """Options générales de l'asso : siret, addresse, nom, etc""" diff --git a/preferences/templates/preferences/aff_mailcontact.html b/preferences/templates/preferences/aff_mailcontact.html new file mode 100644 index 00000000..55d268f0 --- /dev/null +++ b/preferences/templates/preferences/aff_mailcontact.html @@ -0,0 +1,45 @@ +{% comment %} +Re2o est un logiciel d'administration développé initiallement au rezometz. Il +se veut agnostique au réseau considéré, de manière à être installable en +quelques clics. + +Copyright © 2017 Gabriel Détraz +Copyright © 2017 Goulven Kermarec +Copyright © 2017 Augustin Lemesle + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +{% endcomment %} +{% load acl %} + + + + + + + + + {% for mailcontact in mailcontact_list %} + + + + + + {% endfor %} +
AdresseCommentaire
{{ mailcontact.address }}{{ mailcontact.commentary }} + {% can_edit mailcontact %} + {% include 'buttons/edit.html' with href='preferences:edit-mailcontact' id=mailcontact.id %} + {% acl_end %} + {% include 'buttons/history.html' with href='preferences:history' name='mailcontact' id=mailcontact.id %} +
diff --git a/preferences/templates/preferences/display_preferences.html b/preferences/templates/preferences/display_preferences.html index a3f2dbc3..eb2fcea8 100644 --- a/preferences/templates/preferences/display_preferences.html +++ b/preferences/templates/preferences/display_preferences.html @@ -220,6 +220,15 @@ with this program; if not, write to the Free Software Foundation, Inc., Editer + +

Liste des adresses mail de contact

+ {% can_create preferences.MailContact%} + Ajouter une adresse + {% acl_end %} + Supprimer une ou plusieurs adresses + {% include "preferences/aff_mailcontact.html" with mailcontact_list=mailcontact_list %} +

+ diff --git a/preferences/urls.py b/preferences/urls.py index ec35cc41..896dc45f 100644 --- a/preferences/urls.py +++ b/preferences/urls.py @@ -72,6 +72,19 @@ urlpatterns = [ views.edit_service, name='edit-service' ), - url(r'^del_services/$', views.del_services, name='del-services'), + url(r'^del_service/$', views.del_service, name='del-service'), + url(r'^add_mailcontact/$', views.add_mailcontact, name='add-mailcontact'), + url( + r'^edit_mailcontact/(?P[0-9]+)$', + views.edit_mailcontact, + name='edit-mailcontact' + ), + url(r'^del_mailcontact/$', views.del_mailcontact, name='del-mailcontact'), + url( + r'^history/(?P\w+)/(?P[0-9]+)$', + re2o.views.history, + name='history', + kwargs={'application': 'preferences'}, + ), url(r'^$', views.display_options, name='display-options'), ] diff --git a/preferences/views.py b/preferences/views.py index b8ca39d2..1ad0b42d 100644 --- a/preferences/views.py +++ b/preferences/views.py @@ -42,9 +42,10 @@ from reversion import revisions as reversion from re2o.views import form from re2o.acl import can_create, can_edit, can_delete_set, can_view_all -from .forms import ServiceForm, DelServiceForm +from .forms import ServiceForm, DelServiceForm, MailContactForm, DelMailContactForm from .models import ( Service, + MailContact, OptionalUser, OptionalMachine, AssoOption, @@ -71,6 +72,7 @@ def display_options(request): homeoptions, _created = HomeOption.objects.get_or_create() mailmessageoptions, _created = MailMessageOption.objects.get_or_create() service_list = Service.objects.all() + mailcontact_list = MailContact.objects.all() return form({ 'useroptions': useroptions, 'machineoptions': machineoptions, @@ -79,7 +81,8 @@ def display_options(request): 'assooptions': assooptions, 'homeoptions': homeoptions, 'mailmessageoptions': mailmessageoptions, - 'service_list': service_list + 'service_list': service_list, + 'mailcontact_list': mailcontact_list }, 'preferences/display_preferences.html', request) @@ -169,7 +172,7 @@ def edit_service(request, service_instance, **_kwargs): @login_required @can_delete_set(Service) -def del_services(request, instances): +def del_service(request, instances): """Suppression d'un service de la page d'accueil""" services = DelServiceForm(request.POST or None, instances=instances) if services.is_valid(): @@ -179,7 +182,7 @@ def del_services(request, instances): with transaction.atomic(), reversion.create_revision(): services_del.delete() reversion.set_user(request.user) - messages.success(request, "Le service a été supprimée") + messages.success(request, "Le service a été supprimé") except ProtectedError: messages.error(request, "Erreur le service\ suivant %s ne peut être supprimé" % services_del) @@ -189,3 +192,75 @@ def del_services(request, instances): 'preferences/preferences.html', request ) + + +@login_required +@can_create(MailContact) +def add_mailcontact(request): + """Ajout d'une adresse de contact""" + mailcontact = MailContactForm( + request.POST or None, + request.FILES or None + ) + if mailcontact.is_valid(): + with transaction.atomic(), reversion.create_revision(): + mailcontact.save() + reversion.set_user(request.user) + reversion.set_comment("Création") + messages.success(request, "Cette adresse a été ajoutée") + return redirect(reverse('preferences:display-options')) + return form( + {'preferenceform': mailcontact, 'action_name': 'Ajouter'}, + 'preferences/preferences.html', + request + ) + + +@login_required +@can_edit(MailContact) +def edit_mailcontact(request, mailcontact_instance, **_kwargs): + """Edition des adresses de contacte affichées""" + mailcontact = MailContactForm( + request.POST or None, + request.FILES or None, + instance=mailcontact_instance + ) + if mailcontact.is_valid(): + with transaction.atomic(), reversion.create_revision(): + mailcontact.save() + reversion.set_user(request.user) + reversion.set_comment("Modification") + messages.success(request, "Adresse modifiée") + return redirect(reverse('preferences:display-options')) + return form( + {'preferenceform': mailcontact, 'action_name': 'Editer'}, + 'preferences/preferences.html', + request + ) + + +@login_required +@can_delete_set(MailContact) +def del_mailcontact(request, instances): + """Suppression d'une adresse de contact""" + mailcontacts = DelMailContactForm( + request.POST or None, + instances=instances + ) + if mailcontacts.is_valid(): + mailcontacts_dels = mailcontacts.cleaned_data['mailcontacts'] + for mailcontacts_del in mailcontacts_dels: + try: + with transaction.atomic(), reversion.create_revision(): + mailcontacts_del.delete() + reversion.set_user(request.user) + messages.success(request, "L'adresse a été supprimée") + except ProtectedError: + messages.error(request, "Erreur le service\ + suivant %s ne peut être supprimé" % mailcontacts_del) + return redirect(reverse('preferences:display-options')) + return form( + {'preferenceform': mailcontacts, 'action_name': 'Supprimer'}, + 'preferences/preferences.html', + request + ) diff --git a/re2o/templatetags/acl.py b/re2o/templatetags/acl.py index fe13c5ac..9a439f88 100644 --- a/re2o/templatetags/acl.py +++ b/re2o/templatetags/acl.py @@ -79,7 +79,6 @@ from django.contrib.contenttypes.models import ContentType register = template.Library() - def get_model(model_name): """Retrieve the model object from its name""" splitted = model_name.split('.')
Url du compte twitter