From 0889937ea33dd1fc21c4971a589b3f7f5e502168 Mon Sep 17 00:00:00 2001 From: jedi Date: Sat, 7 Nov 2020 20:58:15 +0100 Subject: [PATCH] clean up and catch some errors --- backend/multimail/forms.py | 78 ++++++++++++------- backend/multimail/models.py | 25 ++++-- .../multimail/{edit_domain.html => edit.html} | 4 - .../templates/multimail/edit_alias.html | 18 ----- .../templates/multimail/edit_mailbox.html | 17 ---- 5 files changed, 70 insertions(+), 72 deletions(-) rename backend/multimail/templates/multimail/{edit_domain.html => edit.html} (85%) delete mode 100644 backend/multimail/templates/multimail/edit_alias.html delete mode 100644 backend/multimail/templates/multimail/edit_mailbox.html diff --git a/backend/multimail/forms.py b/backend/multimail/forms.py index 98570f2..9c28a74 100644 --- a/backend/multimail/forms.py +++ b/backend/multimail/forms.py @@ -1,7 +1,9 @@ from django.contrib.auth.decorators import login_required from django.forms import ModelForm +from django.forms.utils import ErrorList from django.http import HttpResponseRedirect, Http404 from django.shortcuts import render +from django.db import IntegrityError from multimail.models import Domain, Mailbox, Alias from multimail.owner import user_from_request @@ -11,7 +13,7 @@ class DomainForm(ModelForm): class Meta: model = Domain fields = '__all__' - #fields = ['domain'] + # fields = ['domain'] class MailboxForm(ModelForm): @@ -35,14 +37,18 @@ def edit_domain(request, domain_id): raise Http404 if request.method == 'POST': form = DomainForm(request.POST, instance=domain) - if form.is_valid(): - form.save() - return HttpResponseRedirect('/domains/') + try: + if form.is_valid(): + form.save() + return HttpResponseRedirect('/domains/') + + except IntegrityError as e: + form.add_error(None, e) else: form = DomainForm(instance=domain) - return render(request, 'multimail/edit_domain.html', {'form': form}) + return render(request, 'multimail/edit.html', {'form': form}) @login_required(login_url='/login/') @@ -50,15 +56,19 @@ def new_domain(request): user = user_from_request(request) if request.method == 'POST': form = DomainForm(request.POST) - if form.is_valid(): - domain = form.save() - domain.admin.create(admin=user['name'], source=user['source']) - return HttpResponseRedirect('/domains/') + try: + if form.is_valid(): + domain = form.save() + domain.admin.create(admin=user['name'], source=user['source']) + return HttpResponseRedirect('/domains/') + + except IntegrityError as e: + form.add_error(None, e) else: form = DomainForm() - return render(request, 'multimail/edit_domain.html', {'form': form}) + return render(request, 'multimail/edit.html', {'form': form}) @login_required(login_url='/login/') @@ -71,14 +81,18 @@ def edit_mailbox(request, mailbox_id): raise Http404 if request.method == 'POST': form = MailboxForm(request.POST, instance=mailbox) - if form.is_valid(): - form.save() - return HttpResponseRedirect('/mailboxes/') + try: + if form.is_valid(): + form.save() + return HttpResponseRedirect('/mailboxes/') + + except IntegrityError as e: + form.add_error(None, e) else: form = MailboxForm(instance=mailbox) - return render(request, 'multimail/edit_mailbox.html', {'form': form}) + return render(request, 'multimail/edit.html', {'form': form}) @login_required(login_url='/login/') @@ -87,14 +101,18 @@ def new_mailbox(request): user = user_from_request(request) domains = [o.domain for o in Domain.objects.filter(admin__admin=user['name'], admin__source=user['source'])] form = MailboxForm(request.POST) - if form.is_valid() and form.domain in domains: - form.save() - return HttpResponseRedirect('/mailboxes/') + try: + if form.is_valid() and form.domain in domains: + form.save() + return HttpResponseRedirect('/mailboxes/') + + except IntegrityError as e: + form.add_error(None, e) else: form = MailboxForm() - return render(request, 'multimail/edit_mailbox.html', {'form': form}) + return render(request, 'multimail/edit.html', {'form': form}) @login_required(login_url='/login/') @@ -107,14 +125,18 @@ def edit_alias(request, alias_id): raise Http404 if request.method == 'POST': form = AliasForm(request.POST, instance=alias) - if form.is_valid(): - form.save() - return HttpResponseRedirect('/aliases/') + try: + if form.is_valid(): + form.save() + return HttpResponseRedirect('/aliases/') + + except IntegrityError as e: + form.add_error(None, e) else: form = AliasForm(instance=alias) - return render(request, 'multimail/edit_alias.html', {'form': form}) + return render(request, 'multimail/edit.html', {'form': form}) @login_required(login_url='/login/') @@ -123,11 +145,15 @@ def new_alias(request): user = user_from_request(request) domains = [o.domain for o in Domain.objects.filter(admin__admin=user['name'], admin__source=user['source'])] form = AliasForm(request.POST) - if form.is_valid() and form.source_domain in domains: - form.save() - return HttpResponseRedirect('/aliases/') + try: + if form.is_valid() and form.source_domain in domains: + form.save() + return HttpResponseRedirect('/aliases/') + + except IntegrityError as e: + form.add_error(None, e) else: form = AliasForm() - return render(request, 'multimail/edit_alias.html', {'form': form}) + return render(request, 'multimail/edit.html', {'form': form}) diff --git a/backend/multimail/models.py b/backend/multimail/models.py index 6813fc2..a8d7313 100644 --- a/backend/multimail/models.py +++ b/backend/multimail/models.py @@ -1,20 +1,21 @@ -import datetime from django.db import models -from django.utils import timezone class Alias(models.Model): id = models.AutoField(primary_key=True) - source_username = models.CharField(max_length=64) + source_username = models.CharField(max_length=64, blank=True) source_domain = models.CharField(max_length=255) destination_username = models.CharField(max_length=64) destination_domain = models.CharField(max_length=255) - enabled = models.IntegerField(blank=True, null=True) + enabled = models.BooleanField(default=True) class Meta: managed = False db_table = 'aliases' + def __str__(self): + return self.source_username + '@' + self.source_domain + class Domain(models.Model): domain = models.CharField(max_length=255) @@ -23,6 +24,9 @@ class Domain(models.Model): managed = False db_table = 'domains' + def __str__(self): + return self.domain + class Mailbox(models.Model): id = models.AutoField(primary_key=True) @@ -30,13 +34,16 @@ class Mailbox(models.Model): domain = models.CharField(max_length=255) password = models.CharField(max_length=255) quota = models.IntegerField(blank=True, null=True) - enabled = models.IntegerField(blank=True, null=True) - sendonly = models.IntegerField(blank=True, null=True) + enabled = models.BooleanField(default=True) + sendonly = models.BooleanField(default=False) class Meta: managed = False db_table = 'mailboxes' + def __str__(self): + return self.username + '@' + self.domain + class TLSPolicy(models.Model): domain = models.CharField(max_length=255) @@ -47,11 +54,15 @@ class TLSPolicy(models.Model): managed = False db_table = 'tlspolicies' + def __str__(self): + return self.domain + class DomainOwner(models.Model): domain = models.ForeignKey(Domain, on_delete=models.CASCADE, related_name='admin') admin = models.CharField(max_length=200) - source = models.CharField(max_length=8, choices=[('system','system'), ('ldap', 'ldap'), ('mail', 'mail')], default=0) + source = models.CharField(max_length=8, choices=[('system', 'system'), ('ldap', 'ldap'), ('mail', 'mail')], + default=0) def __str__(self): return self.admin diff --git a/backend/multimail/templates/multimail/edit_domain.html b/backend/multimail/templates/multimail/edit.html similarity index 85% rename from backend/multimail/templates/multimail/edit_domain.html rename to backend/multimail/templates/multimail/edit.html index c70e3c4..d3aa77a 100644 --- a/backend/multimail/templates/multimail/edit_domain.html +++ b/backend/multimail/templates/multimail/edit.html @@ -2,10 +2,7 @@ {% load bootstrap4 %} {% block content %} -

{{ form.domain.value }}

- {% if error_message %}

{{ error_message }}

{% endif %} -
{% csrf_token %} {% bootstrap_form form %} @@ -13,5 +10,4 @@ {% endbuttons %}
- {% endblock %} \ No newline at end of file diff --git a/backend/multimail/templates/multimail/edit_alias.html b/backend/multimail/templates/multimail/edit_alias.html deleted file mode 100644 index eac9c84..0000000 --- a/backend/multimail/templates/multimail/edit_alias.html +++ /dev/null @@ -1,18 +0,0 @@ -{% extends 'multimail/base.html' %} -{% load bootstrap4 %} - -{% block content %} -

{{ form.source_username.value }}@{{ form.source_domain.value }}

- - {% if error_message %}

{{ error_message }}

{% endif %} - - -
- {% csrf_token %} - {% bootstrap_form form %} - {% buttons %} - - {% endbuttons %} -
- -{% endblock %} \ No newline at end of file diff --git a/backend/multimail/templates/multimail/edit_mailbox.html b/backend/multimail/templates/multimail/edit_mailbox.html deleted file mode 100644 index 7815ab6..0000000 --- a/backend/multimail/templates/multimail/edit_mailbox.html +++ /dev/null @@ -1,17 +0,0 @@ -{% extends 'multimail/base.html' %} -{% load bootstrap4 %} - -{% block content %} -

{{ form.username.value }}@{{ form.domain.value }}

- - {% if error_message %}

{{ error_message }}

{% endif %} - -
- {% csrf_token %} - {% bootstrap_form form %} - {% buttons %} - - {% endbuttons %} -
- -{% endblock %} \ No newline at end of file