Exemple #1
0
def domain_new(request):
    form_initial = {'site': Site.objects.get_current(request)}
    if request.method == 'POST':
        form = DomainForm(request.POST, initial=form_initial)
        if form.is_valid():
            domain = Domain(mail_host=form.cleaned_data['mail_host'],
                            description=form.cleaned_data['description'],
                            owner=request.user.email)
            try:
                domain.save()
            except MailmanApiError:
                return utils.render_api_error(request)
            except HTTPError as e:
                messages.error(request, e)
            else:
                messages.success(request, _("New Domain registered"))
            MailDomain.objects.get_or_create(
                site=form.cleaned_data['site'],
                mail_domain=form.cleaned_data['mail_host'])
            return redirect("domain_index")
        else:
            messages.error(request, _('Please check the errors below'))
    else:
        form = DomainForm(initial=form_initial)
    return render(request, 'postorius/domain/new.html', {'form': form})
Exemple #2
0
 def test_form_fields_valid(self):
     form = DomainForm({
         'mail_host': 'mailman.most-desirable.org',
         'description': 'The Most Desirable organization',
         'contact_address': '*****@*****.**',
         'site': 1,
     })
     self.assertTrue(form.is_valid())
Exemple #3
0
def domain_edit(request, domain):
    try:
        domain_obj = Domain.objects.get(mail_host=domain)
    except Mailman404Error:
        raise Http404('Domain does not exist')
    form_args = []
    if request.method == 'POST':
        form_args.append(request.POST)
    form_initial = {
        'mail_host': domain,
        'description': domain_obj.description,
        'site': MailDomain.objects.get(mail_domain=domain).site,
    }
    form = DomainForm(*form_args, initial=form_initial)
    form.fields["mail_host"].widget = HiddenInput()

    if request.method == 'POST':
        if form.is_valid():
            domain_obj.description = form.cleaned_data['description']
            try:
                web_host = MailDomain.objects.get(mail_domain=domain)
            except MailDomain.DoesNotExist:
                web_host = MailDomain.objects.create(
                    site=form.cleaned_data['site'], mail_domain=domain)
            else:
                web_host.site = form.cleaned_data['site']
                web_host.save()
            try:
                domain_obj.save()
            except MailmanApiError:
                return utils.render_api_error(request)
            except HTTPError as e:
                messages.error(request, e)
            else:
                messages.success(request, _("Domain %s updated") % domain)
            return redirect("domain_edit", domain=domain)
        else:
            messages.error(request, _('Please check the errors below'))
    return render(request, 'postorius/domain/edit.html', {
        'domain': domain,
        'form': form
    })
 def test_site_field_values(self):
     form = DomainForm()
     self.assertTrue('site' in form.fields.keys())
     self.assertTrue([x for x in form.fields['site'].choices],
                     [(1, 'example.com (example.com)')])
     # Now let's create a new domain and see if it shows up.
     Site.objects.create(domain='mail.most-desirable.org', name='My Domain')
     Site.objects.create(domain='dom.most-desirable.org', name='A Domain')
     # Items are ordered by "name".
     self.assertTrue([x for x in form.fields['site'].choices],
                     [(1, 'A Domain (dom.most-desirable.org)'),
                      (2, 'example.com (example.com)'),
                      (3, 'My Domain (mail.most-desirable.org)')])
     # Initial value should be set to the current site.
     self.assertEqual(form.fields['site'].initial(),
                      Site.objects.get(domain='example.com'))
 def test_error_messages(self):
     form = DomainForm({
         'mail_host': 'mailman.most-desirable.org',
     })
     self.assertFalse(form.is_valid())
     self.assertTrue('site' in form.errors.keys())
     self.assertEqual(form.errors['site'][0], 'Please enter a domain name')
     form = DomainForm({'site': 1})
     self.assertFalse(form.is_valid())
     self.assertTrue('mail_host' in form.errors.keys())
     self.assertEqual(form.errors['mail_host'][0],
                      'Please enter a domain name')
 def test_form_fields_validation(self):
     # With all valid values, the form should be valid.
     form = DomainForm({
         'mail_host': 'mailman.most-desirable.org',
         'description': 'The Most Desirable organization',
         'site': 1,
     })
     self.assertTrue(form.is_valid())
     # With a valid alias_domain the form should be valid.
     form = DomainForm({
         'mail_host': 'mailman.most-desirable.org',
         'description': 'The Most Desirable organization',
         'alias_domain': 'x.most-desirable.org',
         'site': 1,
     })
     self.assertTrue(form.is_valid())
     # Because there is no site_id 2 by default in Django, this form should
     # not be valid.
     form = DomainForm({
         'mail_host': 'mailman.most-desirable.org',
         'description': 'The Most Desirable organization',
         'site': 2,
     })
     self.assertFalse(form.is_valid())
     self.assertTrue('site' in form.errors.keys())
     self.assertEqual(form.errors['site'][0],
                      'Select a valid choice.'
                      ' That choice is not one of the available choices.')
     # Now we use an invalid value for domain name.
     form = DomainForm({
         'mail_host': '*****@*****.**',
         'description': 'The Most Desirable organization',
         'site': 1,
     })
     self.assertFalse(form.is_valid())
     self.assertTrue('mail_host' in form.errors.keys())
     self.assertEqual(form.errors['mail_host'][0],
                      'Please enter a valid domain name')
     # Now we use an invalid value for alias domain.
     form = DomainForm({
         'mail_host': 'mailman.most-desirable.org',
         'description': 'The Most Desirable organization',
         'alias_domain': '*****@*****.**',
         'site': 1,
     })
     self.assertFalse(form.is_valid())
     self.assertTrue('alias_domain' in form.errors.keys())
     self.assertEqual(form.errors['alias_domain'][0],
                      'Please enter a valid domain name or nothing.')
 def test_form_labels(self):
     form = DomainForm()
     self.assertTrue(form.fields['mail_host'].label == 'Mail Host')
     self.assertTrue(form.fields['description'].label == 'Description')
     self.assertTrue(form.fields['alias_domain'].label == 'Alias Domain')
     self.assertTrue(form.fields['site'].label == 'Web Host')
 def test_form_contains_mail_host(self):
     form = DomainForm()
     self.assertTrue('description' in form.fields)
     self.assertTrue('alias_domain' in form.fields)
     self.assertTrue('site' in form.fields)
     self.assertTrue('mail_host' in form.fields)