Skip to content

Commit

Permalink
Merge pull request zestedesavoir#2398 from GerardPaligot/refacto_mp
Browse files Browse the repository at this point in the history
Refactorisation des vues du module des MPs
  • Loading branch information
SpaceFox committed Mar 12, 2015
2 parents e7d8369 + 46eecbe commit 7527fe6
Show file tree
Hide file tree
Showing 20 changed files with 657 additions and 1,002 deletions.
2 changes: 1 addition & 1 deletion templates/article/includes/sidebar_actions.part.html
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ <h3>{% trans "Validation" %}</h3>
</a>
</li>
<li>
<a href="{% url "zds.mp.views.new" %}?{% for username in authors.all %}&amp;username={{ username }}{% endfor %}"
<a href="{% url 'mp-new' %}?{% for username in authors.all %}&amp;username={{ username }}{% endfor %}"
class="ico-after cite blue"
>
{% trans "Envoyer un MP" %}
Expand Down
4 changes: 2 additions & 2 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@
{% with topics=user|interventions_privatetopics %}
{% with unread_topics=topics.unread %}
{% with total_topics=topics.total %}
<a href="{% url "zds.mp.views.index" %}" class="ico-link">
<a href="{% url "mp-list" %}" class="ico-link">
{% if total_topics > 0 %}
<span class="notif-count">{{ total_topics }}</span>
{% endif %}
Expand Down Expand Up @@ -308,7 +308,7 @@
</li>
{% endif %}
</ul>
<a href="{% url "zds.mp.views.index" %}" class="dropdown-link-all">
<a href="{% url "mp-list" %}" class="dropdown-link-all">
{% trans "Tous les messages" %}
</a>
</div>
Expand Down
2 changes: 1 addition & 1 deletion templates/member/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ <h3>{% trans "Actions" %}</h3>
<ul>
{% if usr != user and not profile.is_private %}
<li>
<a href="{% url "zds.mp.views.new" %}?username={{ usr.username }}" class="ico-after cite blue">
<a href="{% url 'mp-new' %}?username={{ usr.username }}" class="ico-after cite blue">
{% trans "Envoyer un message privé" %}
</a>
</li>
Expand Down
4 changes: 2 additions & 2 deletions templates/mp/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


{% block breadcrumb_base %}
<li><a href="{% url "zds.mp.views.index" %}">{% trans "Messagerie Privée" %}</a></li>
<li><a href="{% url "mp-list" %}">{% trans "Messagerie Privée" %}</a></li>
{% endblock %}


Expand All @@ -29,7 +29,7 @@

{% block sidebar %}
<aside class="sidebar mobile-menu-hide">
<a href="{% url "zds.mp.views.new" %}" class="new-btn ico-after more blue">
<a href="{% url 'mp-new' %}" class="new-btn ico-after more blue">
{% trans "Nouvelle conversation" %}
</a>
{% block sidebar_actions %}{% endblock %}
Expand Down
2 changes: 1 addition & 1 deletion templates/mp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ <h3>{% trans "Actions" %}</h3>
{% trans "Supprimer les conversations sélectionnées" %}
</a>

<form action="{% url "zds.mp.views.index" %}" method="post" id="delete-conversations" class="modal modal-small">
<form action="{% url "mp-list-delete" %}" method="post" id="delete-conversations" class="modal modal-small">
<p>
{% trans "Attention, vous vous appretez à supprimer toutes les conversations sélectionnées" %}.
</p>
Expand Down
13 changes: 2 additions & 11 deletions templates/mp/post/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@


{% block content %}
{% comment %}
{% captureas form_action %}
{% url "zds.mp.views.answer" %}?sujet={{ topic.pk }}
{% endcaptureas %}

{% include "misc/message_form.html" with member=user %}
{% endcomment %}

{% crispy form %}

{% if form.text.value %}
Expand All @@ -54,12 +46,11 @@ <h3>{% trans "Actions" %}</h3>
<ul>
<li>
<a href="#add-participant" class="open-modal">{% trans "Ajouter un membre" %}</a>
<form action="{% url "zds.mp.views.add_participant" %}" method="post" id="add-participant" class="modal modal-medium">
<form action="{% url "mp-edit-participant" topic.pk topic.slug %}" method="post" id="add-participant" class="modal modal-medium">
<p>
{% trans "Vous allez rajouter un nouveau membre dans la conversation privé" %}.
</p>
<input type="hidden" name="topic_pk" value="{{ topic.pk }}">
<input type="text" class="input" name="user_pk" placeholder="Pseudo du membre">
<input type="text" class="input" name="username" placeholder="Pseudo du membre">

{% csrf_token %}
<button type="submit">{% trans "Ajouter" %}</button>
Expand Down
6 changes: 3 additions & 3 deletions templates/mp/post/new.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
{% endif %}

{% captureas form_action %}
{% url "zds.mp.views.answer" %}?sujet={{ topic.pk }}
{% url "private-posts-new" topic.pk topic.slug %}
{% endcaptureas %}

{% include "misc/message_form.html" with member=user text=form.text.value %}
Expand All @@ -55,11 +55,11 @@
<div class="content-wrapper">
{% for message in posts %}
{% captureas edit_link %}
{% url "zds.mp.views.edit_post" %}?message={{ message.pk }}
{% url "private-posts-edit" topic.pk topic.slug message.pk %}
{% endcaptureas %}

{% captureas cite_link %}
{% url "zds.mp.views.answer" %}?sujet={{ topic.pk }}&amp;cite={{ message.pk }}
{% url "private-posts-new" topic.pk topic.slug %}?cite={{ message.pk }}
{% endcaptureas %}

{% include "misc/message.part.html" %}
Expand Down
18 changes: 8 additions & 10 deletions templates/mp/topic/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@
</ul>
</div>

{% include "misc/pagination.part.html" with position="top" %}
{% include "misc/paginator.html" with position="top" %}


<div>
{% for message in posts %}
{% captureas edit_link %}
{% url "zds.mp.views.edit_post" %}?message={{ message.pk }}
{% url "private-posts-edit" topic.pk topic.slug message.pk %}
{% endcaptureas %}

{% captureas cite_link %}
{% url "zds.mp.views.answer" %}?sujet={{ topic.pk }}&amp;cite={{ message.pk }}
{% url "private-posts-new" topic.pk topic.slug %}?cite={{ message.pk }}
{% endcaptureas %}

{% if topic.last_message.pk = message.pk %}
Expand All @@ -77,12 +77,12 @@
</div>


{% include "misc/pagination.part.html" with position="bottom" %}
{% include "misc/paginator.html" with position="bottom" %}



{% captureas form_action %}
{% url "zds.mp.views.answer" %}?sujet={{ topic.pk }}
{% url "private-posts-new" topic.pk topic.slug %}
{% endcaptureas %}

{% include "misc/message_form.html" with member=user %}
Expand All @@ -99,13 +99,12 @@ <h3>{% trans "Actions" %}</h3>
<a href="#add-participant" class="open-modal ico-after more blue">
{% trans "Ajouter un membre" %}
</a>
<form action="{% url "zds.mp.views.add_participant" %}" method="post" id="add-participant" class="modal modal-medium">
<form action="{% url "mp-edit-participant" topic.pk topic.slug %}" method="post" id="add-participant" class="modal modal-medium">
<p>
{% trans "Vous allez rajouter un nouveau membre dans la conversation privée" %}.
</p>
<input type="hidden" name="topic_pk" value="{{ topic.pk }}">
<input
type="text" class="input" name="user_pk"
type="text" class="input" name="username"
placeholder="Pseudo du membre"
data-autocomplete="{'type':'single'}"
>
Expand All @@ -120,11 +119,10 @@ <h3>{% trans "Actions" %}</h3>
<a href="#leave-conversation" class="open-modal ico-after cross blue">
{% trans "Quitter la conversation" %}
</a>
<form action="{% url "zds.mp.views.leave" %}" method="post" id="leave-conversation" class="modal modal-small">
<form action="{% url "mp-delete" topic.pk topic.slug %}" method="post" id="leave-conversation" class="modal modal-small">
<p>
{% trans "Attention, vous vous apprêtez à quitter définitivement cette conversation" %}.
</p>
<input type="hidden" name="topic_pk" value="{{ topic.pk }}">

{% csrf_token %}
<button type="submit" name="leave">{% trans "Confirmer" %}</button>
Expand Down
2 changes: 1 addition & 1 deletion templates/tutorial/includes/warn_typo.part.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</p>
<p>
{% trans "Pas assez de place ?" %}
<a href="{% url "zds.mp.views.new" %}?title={% trans "Je voudrais signaler une faute dans le tutoriel" %} &quot;{{ tutorial.title }}&quot;{% for username in tutorial.authors.all %}&amp;username={{ username }}{% endfor %}" >
<a href="{% url 'mp-new' %}?title={% trans "Je voudrais signaler une faute dans le tutoriel" %} &quot;{{ tutorial.title }}&quot;{% for username in tutorial.authors.all %}&amp;username={{ username }}{% endfor %}" >
{% trans "Envoyez un MP" %}
{% if tutorial.authors.all|length > 1 %}
{% trans "aux auteurs" %}
Expand Down
2 changes: 1 addition & 1 deletion templates/tutorial/tutorial/view.html
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ <h3>{% trans "Validation" %}</h3>
</a>
</li>
<li>
<a href="{% url "zds.mp.views.new" %}?{% for username in tutorial.authors.all %}&amp;username={{ username }}{% endfor %}"
<a href="{% url 'mp-new' %}?{% for username in tutorial.authors.all %}&amp;username={{ username }}{% endfor %}"
class="ico-after cite blue"
>
{% trans "Envoyer un MP" %}
Expand Down
87 changes: 87 additions & 0 deletions zds/mp/commons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# -*- coding: utf-8 -*-

from django.http import Http404
from django.shortcuts import get_object_or_404
from django.utils.translation import ugettext_lazy as _

from zds.member.commons import Validator
from zds.member.models import Profile


class ParticipantsValidator(Validator):
"""
Validates participants field of a MP.
"""

def validate_participants(self, value, username):
"""
Checks about participants.
:param value: participants value
:return: participants value
"""
msg = None
if value:
participants = value.strip()
if participants != '':
if len(participants) == 1 and participants[0].strip() == ',':
msg = _(u'Vous devez spécfier des participants valides')
for participant in participants.split(','):
participant = participant.strip()
if participant == '':
continue
if participant.strip().lower() == username.lower():
msg = _(u'Vous ne pouvez pas vous écrire à vous-même !')
try:
current = get_object_or_404(Profile, user__username=participant)
if current.is_private():
msg = _(u'Vous avez tenté d\'ajouter un utilisateur injoignable.')
except Http404:
msg = _(u'Un des participants saisi est introuvable')
else:
msg = _(u'Le champ participants ne peut être vide')
if msg is not None:
self.throw_error('participants', msg)
return value


class TitleValidator(Validator):
"""
Validates title field of a MP.
"""

def validate_title(self, value):
"""
Checks about title.
:param value: title value
:return: title value
"""
msg = None
if value:
if value.strip() == '':
msg = _(u'Le champ titre ne peut être vide')
if msg is not None:
self.throw_error('title', msg)
return value


class TextValidator(Validator):
"""
Validates text field of a MP.
"""

def validate_text(self, value):
"""
Checks about text.
:param value: text value
:return: text value
"""
msg = None
if value:
if value.strip() == '':
msg = _(u'Le champ text ne peut être vide')
if msg is not None:
self.throw_error('text', msg)
return value
42 changes: 10 additions & 32 deletions zds/mp/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Field, Hidden
from django import forms
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from zds.mp.commons import ParticipantsValidator, TitleValidator, TextValidator

from zds.mp.models import PrivateTopic
from zds.utils.forms import CommonLayoutEditor
from django.utils.translation import ugettext_lazy as _


class PrivateTopicForm(forms.Form):
class PrivateTopicForm(forms.Form, ParticipantsValidator, TitleValidator, TextValidator):
participants = forms.CharField(
label=_('Participants'),
widget=forms.TextInput(
Expand Down Expand Up @@ -39,7 +39,6 @@ class PrivateTopicForm(forms.Form):

text = forms.CharField(
label='Texte',
required=False,
widget=forms.Textarea(
attrs={
'placeholder': _('Votre message au format Markdown.'),
Expand All @@ -65,35 +64,15 @@ def __init__(self, username, *args, **kwargs):
def clean(self):
cleaned_data = super(PrivateTopicForm, self).clean()

participants = cleaned_data.get('participants')
title = cleaned_data.get('title')
text = cleaned_data.get('text')

if participants is not None and participants.strip() == '':
self._errors['participants'] = self.error_class(
[_(u'Le champ participants ne peut être vide')])

if participants is not None and participants.strip() != '':
receivers = participants.strip().split(',')
for receiver in receivers:
if User.objects.filter(username__exact=receiver.strip()).count() == 0 and receiver.strip() != '' \
or receiver.strip() == '':
self._errors['participants'] = self.error_class(
[_(u'Un des participants saisi est introuvable')])
elif receiver.strip().lower() == self.username.lower():
self._errors['participants'] = self.error_class(
[_(u'Vous ne pouvez pas vous écrire à vous-même !')])

if title is not None and title.strip() == '':
self._errors['title'] = self.error_class(
[_(u'Le champ titre ne peut être vide')])

if text is not None and text.strip() == '':
self._errors['text'] = self.error_class(
[_(u'Le champ text ne peut être vide')])
self.validate_participants(cleaned_data.get('participants'), self.username)
self.validate_title(cleaned_data.get('title'))
self.validate_text(cleaned_data.get('text'))

return cleaned_data

def throw_error(self, key=None, message=None):
self._errors[key] = self.error_class([message])


class PrivatePostForm(forms.Form):
text = forms.CharField(
Expand All @@ -106,11 +85,10 @@ class PrivatePostForm(forms.Form):
)
)

def __init__(self, topic, user, *args, **kwargs):
def __init__(self, topic, *args, **kwargs):
super(PrivatePostForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_action = reverse(
'zds.mp.views.answer') + '?sujet=' + str(topic.pk)
self.helper.form_action = reverse('private-posts-new', args=[topic.pk, topic.slug])
self.helper.form_method = 'post'

self.helper.layout = Layout(
Expand Down

0 comments on commit 7527fe6

Please sign in to comment.