From 2650065787ea4631a3a16c1462e60d1fb2e64ff9 Mon Sep 17 00:00:00 2001 From: Grizzly Date: Fri, 12 Jul 2019 11:41:17 +0000 Subject: [PATCH] Ouverture de ticket pour les utilisaterus non authentifies --- tickets/forms.py | 5 ++++- tickets/models.py | 8 +++++++- tickets/templates/tickets/form_ticket.html | 21 +++++++++++++++++++-- tickets/views.py | 21 +++++++++++++++------ 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/tickets/forms.py b/tickets/forms.py index f2a6891f..d914d022 100644 --- a/tickets/forms.py +++ b/tickets/forms.py @@ -25,9 +25,12 @@ class EditTicketForm(FormRevMixin, ModelForm): class NewTicketForm(ModelForm): """ Creation d'une machine""" + + email = forms.EmailField(required=False) + class Meta: model = Ticket - fields = ['title', 'description'] + fields = ['title', 'description', 'email'] #def __init(self,*args, **kwargs): #prefix = kwargs.pop('prefix', self.Meta.model.__name__) diff --git a/tickets/models.py b/tickets/models.py index ba0b183e..01a4d77b 100644 --- a/tickets/models.py +++ b/tickets/models.py @@ -9,7 +9,9 @@ class Ticket(models.Model): user = models.ForeignKey( 'users.User', on_delete=models.CASCADE, - related_name="tickets") + related_name="tickets", + blank=True, + null=True) title = models.CharField( max_length=255, help_text=_("Nom du ticket"), @@ -21,6 +23,10 @@ class Ticket(models.Model): blank=False, null=False) date = models.DateTimeField(auto_now_add=True) + email = models.EmailField( + help_text = _("Une adresse mail pour vous recontacter"), + max_length=100, + null=True) assigned_staff = models.ForeignKey( 'users.User', on_delete=models.PROTECT, diff --git a/tickets/templates/tickets/form_ticket.html b/tickets/templates/tickets/form_ticket.html index 79e9c68f..d80a993e 100644 --- a/tickets/templates/tickets/form_ticket.html +++ b/tickets/templates/tickets/form_ticket.html @@ -40,9 +40,26 @@ with this program; if not, write to the Free Software Foundation, Inc., {% endfor %} -
+ {% csrf_token %} - {% bootstrap_form ticketform %} + {% if not user.is_authenticated %} +

{% trans "Vous n'êtes pas authentifié. Veuillez fournir une adresse mail afin que nous puissions vous recontacter." %}

+ {% bootstrap_field ticketform.email %} + {% endif %} + {% bootstrap_field ticketform.title %} +
+

{% trans "Description de votre problème. Veuillez fournir le plus d'informations possible afin de faciliter la recherche de solution. Voici quelques informations dont nous pourions avoir besoin:" %}

+ + {% bootstrap_field ticketform.description %} {% bootstrap_button "Ouvrir le Ticket" button_type="submit" icon='ok' button_class='btn-success' %}
{% endblock %} diff --git a/tickets/views.py b/tickets/views.py index 270bf0df..cb138590 100644 --- a/tickets/views.py +++ b/tickets/views.py @@ -20,13 +20,22 @@ def new_ticket(request): ticketform = NewTicketForm(request.POST) if ticketform.is_valid(): + email = ticketform.cleaned_data.get('email') ticket = ticketform.save(commit=False) - ticket.user = request.user - ticket.save() - messages.success(request,'Votre ticket à été ouvert. Nous vous répondront le plus rapidement possible.') - return redirect(reverse('users:profil',kwargs={'userid':str(request.user.id)})) - else: - messages.error(request, 'Formulaire invalide') + #raise ValueError("email: {} type: {}".format(email,type(email))) + if request.user.is_authenticated: + ticket.user = request.user + ticket.save() + messages.success(request,'Votre ticket à été ouvert. Nous vous répondront le plus rapidement possible.') + return redirect(reverse('users:profil',kwargs={'userid':str(request.user.id)})) + if not request.user.is_authenticated and email != "": + ticket.save() + messages.success(request,'Votre ticket à été ouvert. Nous vous répondront le plus rapidement possible.') + return redirect(reverse('index')) + else: + messages.error(request,"Vous n'êtes pas authentifié, veuillez vous authentifier ou fournir une adresse mail pour que nous puissions vous recontacter") + return form({'ticketform':ticketform,},'tickets/form_ticket.html',request) + else: ticketform = NewTicketForm return form({'ticketform':ticketform,},'tickets/form_ticket.html',request)