clean up and catch some errors

This commit is contained in:
j3d1 2020-11-07 20:58:15 +01:00
parent 252ac918cc
commit 0889937ea3
5 changed files with 70 additions and 72 deletions

View file

@ -1,7 +1,9 @@
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.forms import ModelForm from django.forms import ModelForm
from django.forms.utils import ErrorList
from django.http import HttpResponseRedirect, Http404 from django.http import HttpResponseRedirect, Http404
from django.shortcuts import render from django.shortcuts import render
from django.db import IntegrityError
from multimail.models import Domain, Mailbox, Alias from multimail.models import Domain, Mailbox, Alias
from multimail.owner import user_from_request from multimail.owner import user_from_request
@ -11,7 +13,7 @@ class DomainForm(ModelForm):
class Meta: class Meta:
model = Domain model = Domain
fields = '__all__' fields = '__all__'
#fields = ['domain'] # fields = ['domain']
class MailboxForm(ModelForm): class MailboxForm(ModelForm):
@ -35,14 +37,18 @@ def edit_domain(request, domain_id):
raise Http404 raise Http404
if request.method == 'POST': if request.method == 'POST':
form = DomainForm(request.POST, instance=domain) form = DomainForm(request.POST, instance=domain)
if form.is_valid(): try:
form.save() if form.is_valid():
return HttpResponseRedirect('/domains/') form.save()
return HttpResponseRedirect('/domains/')
except IntegrityError as e:
form.add_error(None, e)
else: else:
form = DomainForm(instance=domain) 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/') @login_required(login_url='/login/')
@ -50,15 +56,19 @@ def new_domain(request):
user = user_from_request(request) user = user_from_request(request)
if request.method == 'POST': if request.method == 'POST':
form = DomainForm(request.POST) form = DomainForm(request.POST)
if form.is_valid(): try:
domain = form.save() if form.is_valid():
domain.admin.create(admin=user['name'], source=user['source']) domain = form.save()
return HttpResponseRedirect('/domains/') domain.admin.create(admin=user['name'], source=user['source'])
return HttpResponseRedirect('/domains/')
except IntegrityError as e:
form.add_error(None, e)
else: else:
form = DomainForm() form = DomainForm()
return render(request, 'multimail/edit_domain.html', {'form': form}) return render(request, 'multimail/edit.html', {'form': form})
@login_required(login_url='/login/') @login_required(login_url='/login/')
@ -71,14 +81,18 @@ def edit_mailbox(request, mailbox_id):
raise Http404 raise Http404
if request.method == 'POST': if request.method == 'POST':
form = MailboxForm(request.POST, instance=mailbox) form = MailboxForm(request.POST, instance=mailbox)
if form.is_valid(): try:
form.save() if form.is_valid():
return HttpResponseRedirect('/mailboxes/') form.save()
return HttpResponseRedirect('/mailboxes/')
except IntegrityError as e:
form.add_error(None, e)
else: else:
form = MailboxForm(instance=mailbox) 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/') @login_required(login_url='/login/')
@ -87,14 +101,18 @@ def new_mailbox(request):
user = user_from_request(request) user = user_from_request(request)
domains = [o.domain for o in Domain.objects.filter(admin__admin=user['name'], admin__source=user['source'])] domains = [o.domain for o in Domain.objects.filter(admin__admin=user['name'], admin__source=user['source'])]
form = MailboxForm(request.POST) form = MailboxForm(request.POST)
if form.is_valid() and form.domain in domains: try:
form.save() if form.is_valid() and form.domain in domains:
return HttpResponseRedirect('/mailboxes/') form.save()
return HttpResponseRedirect('/mailboxes/')
except IntegrityError as e:
form.add_error(None, e)
else: else:
form = MailboxForm() form = MailboxForm()
return render(request, 'multimail/edit_mailbox.html', {'form': form}) return render(request, 'multimail/edit.html', {'form': form})
@login_required(login_url='/login/') @login_required(login_url='/login/')
@ -107,14 +125,18 @@ def edit_alias(request, alias_id):
raise Http404 raise Http404
if request.method == 'POST': if request.method == 'POST':
form = AliasForm(request.POST, instance=alias) form = AliasForm(request.POST, instance=alias)
if form.is_valid(): try:
form.save() if form.is_valid():
return HttpResponseRedirect('/aliases/') form.save()
return HttpResponseRedirect('/aliases/')
except IntegrityError as e:
form.add_error(None, e)
else: else:
form = AliasForm(instance=alias) 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/') @login_required(login_url='/login/')
@ -123,11 +145,15 @@ def new_alias(request):
user = user_from_request(request) user = user_from_request(request)
domains = [o.domain for o in Domain.objects.filter(admin__admin=user['name'], admin__source=user['source'])] domains = [o.domain for o in Domain.objects.filter(admin__admin=user['name'], admin__source=user['source'])]
form = AliasForm(request.POST) form = AliasForm(request.POST)
if form.is_valid() and form.source_domain in domains: try:
form.save() if form.is_valid() and form.source_domain in domains:
return HttpResponseRedirect('/aliases/') form.save()
return HttpResponseRedirect('/aliases/')
except IntegrityError as e:
form.add_error(None, e)
else: else:
form = AliasForm() form = AliasForm()
return render(request, 'multimail/edit_alias.html', {'form': form}) return render(request, 'multimail/edit.html', {'form': form})

View file

@ -1,20 +1,21 @@
import datetime
from django.db import models from django.db import models
from django.utils import timezone
class Alias(models.Model): class Alias(models.Model):
id = models.AutoField(primary_key=True) 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) source_domain = models.CharField(max_length=255)
destination_username = models.CharField(max_length=64) destination_username = models.CharField(max_length=64)
destination_domain = models.CharField(max_length=255) destination_domain = models.CharField(max_length=255)
enabled = models.IntegerField(blank=True, null=True) enabled = models.BooleanField(default=True)
class Meta: class Meta:
managed = False managed = False
db_table = 'aliases' db_table = 'aliases'
def __str__(self):
return self.source_username + '@' + self.source_domain
class Domain(models.Model): class Domain(models.Model):
domain = models.CharField(max_length=255) domain = models.CharField(max_length=255)
@ -23,6 +24,9 @@ class Domain(models.Model):
managed = False managed = False
db_table = 'domains' db_table = 'domains'
def __str__(self):
return self.domain
class Mailbox(models.Model): class Mailbox(models.Model):
id = models.AutoField(primary_key=True) id = models.AutoField(primary_key=True)
@ -30,13 +34,16 @@ class Mailbox(models.Model):
domain = models.CharField(max_length=255) domain = models.CharField(max_length=255)
password = models.CharField(max_length=255) password = models.CharField(max_length=255)
quota = models.IntegerField(blank=True, null=True) quota = models.IntegerField(blank=True, null=True)
enabled = models.IntegerField(blank=True, null=True) enabled = models.BooleanField(default=True)
sendonly = models.IntegerField(blank=True, null=True) sendonly = models.BooleanField(default=False)
class Meta: class Meta:
managed = False managed = False
db_table = 'mailboxes' db_table = 'mailboxes'
def __str__(self):
return self.username + '@' + self.domain
class TLSPolicy(models.Model): class TLSPolicy(models.Model):
domain = models.CharField(max_length=255) domain = models.CharField(max_length=255)
@ -47,11 +54,15 @@ class TLSPolicy(models.Model):
managed = False managed = False
db_table = 'tlspolicies' db_table = 'tlspolicies'
def __str__(self):
return self.domain
class DomainOwner(models.Model): class DomainOwner(models.Model):
domain = models.ForeignKey(Domain, on_delete=models.CASCADE, related_name='admin') domain = models.ForeignKey(Domain, on_delete=models.CASCADE, related_name='admin')
admin = models.CharField(max_length=200) 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): def __str__(self):
return self.admin return self.admin

View file

@ -2,10 +2,7 @@
{% load bootstrap4 %} {% load bootstrap4 %}
{% block content %} {% block content %}
<h1 class="mt-4">{{ form.domain.value }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %} {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="" method="post" class="form"> <form action="" method="post" class="form">
{% csrf_token %} {% csrf_token %}
{% bootstrap_form form %} {% bootstrap_form form %}
@ -13,5 +10,4 @@
<button type="submit" class="btn btn-primary">Submit</button> <button type="submit" class="btn btn-primary">Submit</button>
{% endbuttons %} {% endbuttons %}
</form> </form>
{% endblock %} {% endblock %}

View file

@ -1,18 +0,0 @@
{% extends 'multimail/base.html' %}
{% load bootstrap4 %}
{% block content %}
<h1 class="mt-4">{{ form.source_username.value }}@{{ form.source_domain.value }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="" method="post" class="form">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
<button type="submit" class="btn btn-primary">Submit</button>
{% endbuttons %}
</form>
{% endblock %}

View file

@ -1,17 +0,0 @@
{% extends 'multimail/base.html' %}
{% load bootstrap4 %}
{% block content %}
<h1 class="mt-4">{{ form.username.value }}@{{ form.domain.value }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="" method="post" class="form">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
<button type="submit" class="btn btn-primary">Submit</button>
{% endbuttons %}
</form>
{% endblock %}