Beispiel #1
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 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
    })
Beispiel #2
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'],
                            alias_domain=form.cleaned_data['alias_domain'],
                            owner=request.user.email)
            try:
                domain.save()
            except HTTPError as e:
                form.add_error('mail_host', e.reason)
            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:
        form = DomainForm(initial=form_initial)
    return render(request, 'postorius/domain/new.html', {'form': form})