diff --git a/forms.py b/forms.py index aeeee34..5f8da00 100644 --- a/forms.py +++ b/forms.py @@ -20,22 +20,40 @@ from django.utils.translation import gettext as _ from fieldset_forms import FieldsetForm +#Redefined Classes for Validation Purpose +class DomainField(forms.EmailField): + def validate(self, value): + "Check if value consists of a valid email domain." + mail = "mail@"+value + super(forms.EmailField, self).validate(mail) + +class ListNameField(forms.EmailField): + def validate(self, value): + "Check if value consists of a valid email prefix." + mail = value+"@example.net" + super(forms.EmailField, self).validate(mail) + +#Fieldsets for use within the views class DomainNew(FieldsetForm): """ Form field to add a new domain """ - domain_name = forms.CharField( #todo MAIL → add domain, check post ! + domain_name = DomainField( label = _('Domain Name'), - required = True, + error_messages = {'required': _('Please a domain name'), + 'invalid': _('Please enter a valid domain name.')}, + required = True ) contact_address = forms.EmailField( - label = _('Your email address'), + label = _('Your email address'), + required = True, error_messages = {'required': _('Please enter an email address.'), 'invalid': _('Please enter a valid email address.')}) description = forms.CharField( label = _('Description'), - required = False, + required = False ) + class Meta: """ Class to handle the automatic insertion of fieldsets and divs. @@ -87,7 +105,7 @@ ("Turkish", "Turkish"), ("Ukrainian", "Ukrainian"), ("Vietnamese", "Vietnamese")) - listname = forms.CharField( + listname = ListNameField( label = _('List Name'), required = True, error_messages = {'required': _('Please enter a name for your list.'), @@ -98,7 +116,7 @@ label = _('@Domain'), required = True, choices = ( - ("","Please Choose a Domain"), + ("",_("Please Choose a Domain")), ("","-"), ), error_messages = { diff --git a/views.py b/views.py index a782258..5a062dc 100644 --- a/views.py +++ b/views.py @@ -76,28 +76,30 @@ 'message': message}) return _login_decorator -#@login_required +#@login_required #TODO def new_domain(request, template = 'mailman-django/new_domain.html'): if request.method == 'POST': form = DomainNew(request.POST) + try: + c = Client('http://localhost:8001/3.0', API_USER, API_PASS) + except Exception, e: + return HttpResponse(e) if form.is_valid(): domain_name = form.cleaned_data['domain_name'] - try: - c = Client('http://localhost:8001/3.0', API_USER, API_PASS) - except Exception, e: - return HttpResponse(e) domain = c.create_domain(domain_name) - #email_host ; url_host might differ #TODO - domain.contact_address = form.cleaned_data['contact_address'] - domain.description = form.cleaned_data['description'] - + domain.contact_address = form.cleaned_data['contact_address'] + domain.description = form.cleaned_data['description'] else: try: c = Client('http://localhost:8001/3.0', API_USER, API_PASS) - existing_domains = c.domains - except Exception, e: + except Exception, e: return HttpResponse(e) - form = DomainNew() + form = DomainNew() + try: + existing_domains = c.domains + except Exception, e: + return HttpResponse(e) + return render_to_response(template, {'form': form,'domains':existing_domains}) #@login_required