[damned-lies] Limit commit authors to users having full name in profile



commit f1ba2e28fd3fe9074112de02a3addaad97d440b8
Author: Claude Paroz <claude 2xlibre net>
Date:   Thu Aug 21 22:19:58 2014 +0200

    Limit commit authors to users having full name in profile
    
    Feature asked by GNOME sysadmins.

 vertimus/forms.py       |   26 ++++++++++++++++++++++++--
 vertimus/tests/tests.py |   10 +++++++++-
 2 files changed, 33 insertions(+), 3 deletions(-)
---
diff --git a/vertimus/forms.py b/vertimus/forms.py
index 6cadbab..8ccb155 100644
--- a/vertimus/forms.py
+++ b/vertimus/forms.py
@@ -22,11 +22,14 @@ import os
 
 from django import forms
 from django.core.urlresolvers import reverse
-from django.utils.translation import ugettext_lazy as _
+from django.utils.html import format_html
+from django.utils.safestring import mark_safe
+from django.utils.translation import ugettext, ugettext_lazy as _
 from vertimus.models import Action, ActionCI
 from stats.models import Person
 from stats.utils import po_file_stats
 
+
 class ActionWidget(forms.Select):
     """ Custom widget to disable the separator option (containing "--------") """
     def render_options(self, choices, selected_choices):
@@ -35,6 +38,25 @@ class ActionWidget(forms.Select):
                                   "<option disabled='disabled'>--------</option>")
         return options
 
+
+class AuthorWidget(forms.Select):
+    """ Custom widget to disable people without full name """
+    def render_options(self, choices, selected_choices):
+        output = ["<option value=\"None\">--------</option>"]
+        field = self.choices.field
+        for obj in field.queryset.all():
+            val, label = field.prepare_value(obj), field.label_from_instance(obj)
+            selected_html = ''
+            if label == obj.username:
+                selected_html = mark_safe(' disabled="disabled"')
+                label += ugettext(" (full name missing)")
+            elif val in selected_choices:
+                selected_html = mark_safe(' selected="selected"')
+            output.append(format_html('<option value="{0}"{1}>{2}</option>',
+                val, selected_html, label))
+        return '\n'.join(output)
+
+
 class ActionForm(forms.Form):
     action = forms.ChoiceField(label=_("Action"),
 #        help_text="Choose an action you want to apply",
@@ -46,7 +68,7 @@ class ActionForm(forms.Form):
         required=False,
         widget=forms.Textarea(attrs={'rows':8, 'cols':70}))
     author = forms.ModelChoiceField(label=_("Commit author"), empty_label=None,
-        queryset=Person.objects.none(), required=False)
+        queryset=Person.objects.none(), widget=AuthorWidget, required=False)
     file = forms.FileField(label=_("File"), required=False,
                            help_text=_("Upload a .po, .gz, .bz2 or .png file"))
     send_to_ml = forms.BooleanField(label=_("Send message to the team mailing list"), required=False)
diff --git a/vertimus/tests/tests.py b/vertimus/tests/tests.py
index e864d15..f8d5376 100644
--- a/vertimus/tests/tests.py
+++ b/vertimus/tests/tests.py
@@ -316,18 +316,26 @@ class VertimusTest(TeamsAndRolesTests):
 
         action = Action.new_by_name('WC', person=self.pt, comment="Hi!")
         action.apply_on(state, {'send_to_ml': action.send_mail_to_ml})
+        p3 = Person.objects.create(username='username')
+        action = Action.new_by_name('WC', person=p3, comment="Hello!")
+        action.apply_on(state, {'send_to_ml': action.send_mail_to_ml})
 
         self.upload_file(state, 'UP')
 
         self.assertIn(ActionCI, map(type, state.get_available_actions(self.pcoo)))
 
         form = ActionForm(state, state.get_available_actions(self.pcoo), True)
-        self.assertEqual(len(form.fields['author'].choices), 2)
+        self.assertEqual(len(form.fields['author'].choices), 3)
         choices = list(form.fields['author'].choices)
         self.assertListEqual(
             [choices[0][1], choices[1][1]],
             ['John Reviewer', 'John Translator'])
         self.assertEqual(form.fields['author'].initial, self.pr)
+        rendered_form = form.as_p()
+        self.assertIn('<option value="%d" disabled="disabled">username (full name missing)</option>' % p3.pk,
+                      rendered_form)
+        self.assertIn('<option value="%d">John Translator</option>' % self.pt.pk,
+                      rendered_form)
 
     def test_action_ic(self):
         state = StateProofreading(branch=self.b, domain=self.d, language=self.l, person=self.pr)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]