Advertisement
Guest User

Listname

a guest
Feb 18th, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.47 KB | None | 0 0
  1. class ListNewTest(unittest.TestCase):
  2.  
  3.     def setUp(self):
  4.         self.form_data = {
  5.             'listname' : 'test_list1',
  6.             'mail_host': 'mailman.most-desirable.org',
  7.             'list_owner': 'james@example.com',
  8.             'advertised': True,
  9.             'description': 'The Most Desirable organization',
  10.         }
  11.         self.domain_choices = ['mailman.most-desirable.org']
  12.  
  13.     def test_form_fields_listname(self):
  14.         form = ListNew(self.form_data)
  15.         self.assertTrue(form.is_valid())    
  16.  
  17.     def test_form_fields_listname_invalid(self):
  18.         self.form_data['listname'] = 'test$list1'
  19.         form = ListNew(self.form_data)
  20.         self.assertFalse(form.is_valid())
  21. -----------------------------------------------------------------------
  22.  
  23. class ListNew(FieldsetForm):
  24.  
  25.     """
  26.    Form fields to add a new list. Languages are hard coded which should
  27.    be replaced by a REST lookup of available languages.
  28.    """
  29.     listname = forms.CharField(
  30.         label=_('List Name'),
  31.         required=True,
  32.         error_messages={'required': _('Please enter a name for your list.'),
  33.                         'invalid': _('Please enter a valid list name.')})
  34.     mail_host = forms.ChoiceField()
  35.     list_owner = forms.EmailField(
  36.         label=_('Inital list owner address'),
  37.         error_messages={
  38.             'required': _("Please enter the list owner's email address.")},
  39.         required=True)
  40.     advertised = forms.ChoiceField(
  41.         widget=forms.RadioSelect(),
  42.         label=_('Advertise this list?'),
  43.         error_messages={
  44.             'required': _("Please choose a list type.")},
  45.         required=True,
  46.         choices=(
  47.             (True, _("Advertise this list in list index")),
  48.             (False, _("Hide this list in list index"))))
  49.     description = forms.CharField(
  50.         label=_('Description'),
  51.         required=True)
  52.  
  53.     def __init__(self, domain_choices, *args, **kwargs):
  54.         super(ListNew, self).__init__(*args, **kwargs)
  55.         self.fields["mail_host"] = forms.ChoiceField(
  56.             widget=forms.Select(),
  57.             label=_('Mail Host'),
  58.             required=True,
  59.             choices=domain_choices,
  60.             error_messages={'required': _("Choose an existing Domain."),
  61.                             'invalid': "ERROR-todo_forms.py"})
  62.         if len(domain_choices) < 2:
  63.             self.fields["mail_host"].help_text = _(
  64.                 "Site admin has not created any domains")
  65.             # if len(choices) < 2:
  66.             #    help_text=_("No domains available: " +
  67.             #                "The site admin must create new domains " +
  68.             #                "before you will be able to create a list")
  69.  
  70.     def clean_listname(self):
  71.         listname = self.cleaned_data.get('listname')
  72.         try:
  73.             validate_slug(listname)
  74.         except:
  75.             raise forms.ValidationError(_("Please enter a valid listname"))
  76.         return listname
  77.  
  78.     class Meta:
  79.  
  80.         """
  81.        Class to handle the automatic insertion of fieldsets and divs.
  82.  
  83.        To use it: add a list for each wished fieldset. The first item in
  84.        the list should be the wished name of the fieldset, the following
  85.        the fields that should be included in the fieldset.
  86.        """
  87.         layout = [["List Details",
  88.                    "listname",
  89.                    "mail_host",
  90.                    "list_owner",
  91.                    "description",
  92.                    "advertised"], ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement