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.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
@ -35,14 +37,18 @@ def edit_domain(request, domain_id):
raise Http404
if request.method == 'POST':
form = DomainForm(request.POST, instance=domain)
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)
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)
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)
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)
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)
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})

View file

@ -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

View file

@ -2,10 +2,7 @@
{% load bootstrap4 %}
{% block content %}
<h1 class="mt-4">{{ 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 %}
@ -13,5 +10,4 @@
<button type="submit" class="btn btn-primary">Submit</button>
{% endbuttons %}
</form>
{% 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 %}