2014-05-19 22:28:25 +02:00
|
|
|
from django import forms
|
2023-02-18 22:46:48 +01:00
|
|
|
from django.contrib import admin
|
2015-10-11 15:30:23 +02:00
|
|
|
from django.forms.utils import flatatt
|
2023-02-18 22:46:48 +01:00
|
|
|
from django.utils.html import format_html
|
|
|
|
from django.utils.translation import gettext as _
|
2014-05-18 20:02:39 +02:00
|
|
|
|
2023-02-18 22:46:48 +01:00
|
|
|
from .models import MailAddress, MailAddressForward, MailAddressMailbox, Mailbox
|
2014-05-19 22:28:25 +02:00
|
|
|
|
|
|
|
PASSWORD_MISMATCH_ERROR = _("Passwords don't match")
|
|
|
|
|
|
|
|
|
|
|
|
class ReadOnlyPasswordHashWidget(forms.Widget):
|
2024-08-23 18:01:27 +02:00
|
|
|
def render(self, name, value, attrs, renderer=None):
|
2014-05-19 22:28:25 +02:00
|
|
|
final_attrs = self.build_attrs(attrs)
|
2023-02-18 22:46:48 +01:00
|
|
|
summary = format_html("<strong>{0}</strong>: {1} ", _("Hash"), value)
|
2014-05-19 22:28:25 +02:00
|
|
|
return format_html("<div{0}>{1}</div>", flatatt(final_attrs), summary)
|
|
|
|
|
|
|
|
|
|
|
|
class ReadOnlyPasswordHashField(forms.Field):
|
|
|
|
widget = ReadOnlyPasswordHashWidget
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
kwargs.setdefault("required", False)
|
|
|
|
super(ReadOnlyPasswordHashField, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def bound_data(self, data, initial):
|
|
|
|
return initial
|
|
|
|
|
2015-12-19 20:11:23 +01:00
|
|
|
def has_changed(self, initial, data):
|
2014-05-19 22:28:25 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
class MailboxCreationForm(forms.ModelForm):
|
|
|
|
"""
|
|
|
|
A form for creating mailboxes.
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
|
|
|
|
password1 = forms.CharField(label=_("Password"), widget=forms.PasswordInput)
|
|
|
|
password2 = forms.CharField(label=_("Password (again)"), widget=forms.PasswordInput)
|
2014-05-19 22:28:25 +02:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Mailbox
|
2023-02-18 22:46:48 +01:00
|
|
|
fields = ("osuser",)
|
2014-05-19 22:28:25 +02:00
|
|
|
|
|
|
|
def clean_password2(self):
|
|
|
|
"""
|
|
|
|
Check that the two password entries match.
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
password1 = self.cleaned_data.get("password1")
|
|
|
|
password2 = self.cleaned_data.get("password2")
|
2014-05-19 22:28:25 +02:00
|
|
|
if password1 and password2 and password1 != password2:
|
|
|
|
raise forms.ValidationError(PASSWORD_MISMATCH_ERROR)
|
|
|
|
return password2
|
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
"""
|
|
|
|
Save the provided password in hashed format.
|
|
|
|
|
|
|
|
"""
|
|
|
|
mailbox = super(MailboxCreationForm, self).save(commit=False)
|
2023-02-18 22:46:48 +01:00
|
|
|
mailbox.username = Mailbox.objects.get_next_mailbox_name(mailbox.osuser)
|
|
|
|
mailbox.set_password(self.cleaned_data["password1"])
|
2014-05-19 22:28:25 +02:00
|
|
|
if commit:
|
|
|
|
mailbox.save()
|
|
|
|
return mailbox
|
|
|
|
|
|
|
|
|
|
|
|
class MailboxChangeForm(forms.ModelForm):
|
|
|
|
"""
|
|
|
|
A form for updating mailboxes.
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
|
2014-05-19 22:28:25 +02:00
|
|
|
password = ReadOnlyPasswordHashField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Mailbox
|
2023-02-18 22:46:48 +01:00
|
|
|
fields = ("username", "password", "osuser", "active")
|
2014-05-19 22:28:25 +02:00
|
|
|
|
|
|
|
def clean_password(self):
|
2023-02-18 22:46:48 +01:00
|
|
|
return self.initial["password"]
|
2014-05-19 22:28:25 +02:00
|
|
|
|
|
|
|
|
2014-05-24 14:58:54 +02:00
|
|
|
class ActivationChangeMixin(object):
|
|
|
|
def activate(self, request, queryset):
|
|
|
|
queryset.update(active=True)
|
|
|
|
|
|
|
|
def deactivate(self, request, queryset):
|
|
|
|
queryset.update(active=False)
|
|
|
|
|
2023-02-18 22:46:48 +01:00
|
|
|
activate.short_description = _("Activate")
|
|
|
|
deactivate.short_description = _("Deactivate")
|
2014-05-24 14:58:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
class MailboxAdmin(ActivationChangeMixin, admin.ModelAdmin):
|
2014-05-19 22:28:25 +02:00
|
|
|
"""
|
|
|
|
Custom admin page for mailboxes.
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
|
2014-05-19 22:28:25 +02:00
|
|
|
form = MailboxChangeForm
|
|
|
|
add_form = MailboxCreationForm
|
|
|
|
|
2023-02-18 22:46:48 +01:00
|
|
|
actions = ["activate", "deactivate"]
|
2014-05-24 14:58:54 +02:00
|
|
|
|
2023-02-18 22:46:48 +01:00
|
|
|
list_display = ("username", "osuser", "active")
|
|
|
|
list_filter = ("active", "osuser")
|
|
|
|
fieldsets = ((None, {"fields": ("osuser", "username", "password", "active")}),)
|
2014-05-19 22:28:25 +02:00
|
|
|
add_fieldsets = (
|
2023-02-18 22:46:48 +01:00
|
|
|
(None, {"classes": ("wide",), "fields": ("osuser", "password1", "password2")}),
|
2014-05-19 22:28:25 +02:00
|
|
|
)
|
2023-02-18 22:46:48 +01:00
|
|
|
search_fields = ("username",)
|
|
|
|
ordering = ("username",)
|
2014-05-19 22:28:25 +02:00
|
|
|
filter_horizontal = ()
|
|
|
|
|
|
|
|
def get_fieldsets(self, request, obj=None):
|
|
|
|
if not obj:
|
|
|
|
return self.add_fieldsets
|
|
|
|
return super(MailboxAdmin, self).get_fieldsets(request, obj)
|
|
|
|
|
|
|
|
def get_form(self, request, obj=None, **kwargs):
|
|
|
|
"""
|
|
|
|
Use special form during mailbox creation.
|
|
|
|
|
|
|
|
"""
|
|
|
|
defaults = {}
|
|
|
|
if obj is None:
|
2023-02-18 22:46:48 +01:00
|
|
|
defaults.update(
|
|
|
|
{
|
|
|
|
"form": self.add_form,
|
|
|
|
"fields": admin.options.flatten_fieldsets(self.add_fieldsets),
|
|
|
|
}
|
|
|
|
)
|
2014-05-19 22:28:25 +02:00
|
|
|
defaults.update(kwargs)
|
|
|
|
return super(MailboxAdmin, self).get_form(request, obj, **defaults)
|
|
|
|
|
|
|
|
|
2014-05-24 14:58:54 +02:00
|
|
|
class MailAddressMailboxInline(admin.TabularInline):
|
|
|
|
model = MailAddressMailbox
|
|
|
|
|
|
|
|
|
|
|
|
class MailAddressForwardInline(admin.TabularInline):
|
|
|
|
model = MailAddressForward
|
|
|
|
|
|
|
|
|
|
|
|
class MailAddressAdmin(ActivationChangeMixin, admin.ModelAdmin):
|
2023-02-18 22:46:48 +01:00
|
|
|
actions = ["activate", "deactivate"]
|
2014-05-24 14:58:54 +02:00
|
|
|
|
2023-02-18 22:46:48 +01:00
|
|
|
list_display = ("__str__", "mailaddressmailbox", "active")
|
|
|
|
list_filter = ("active", "domain")
|
2014-05-24 14:58:54 +02:00
|
|
|
|
|
|
|
inlines = [MailAddressMailboxInline, MailAddressForwardInline]
|
|
|
|
|
|
|
|
|
2014-05-19 22:28:25 +02:00
|
|
|
admin.site.register(Mailbox, MailboxAdmin)
|
2014-05-24 14:58:54 +02:00
|
|
|
admin.site.register(MailAddress, MailAddressAdmin)
|