mirror of
https://github.com/jwinterm/modoboa.git
synced 2026-11-30 11:53:53 +01:00
* Updated *Settings* section * Users can now change their first/last name (see #268) * *admin* and *userprefs* templates moved to their respective application folders (see #245)
71 lines
3.0 KiB
Python
71 lines
3.0 KiB
Python
from django import forms
|
|
from modoboa.admin.models import User, Domain, Mailbox
|
|
from django.utils.translation import ugettext as _, ugettext_lazy
|
|
from modoboa.lib.emailutils import split_mailbox
|
|
from modoboa.lib import parameters
|
|
|
|
class BadDestination(Exception):
|
|
pass
|
|
|
|
class ProfileForm(forms.ModelForm):
|
|
oldpassword = forms.CharField(label=ugettext_lazy("Old password"), required=False,
|
|
widget=forms.PasswordInput)
|
|
newpassword = forms.CharField(label=ugettext_lazy("New password"), required=False,
|
|
widget=forms.PasswordInput)
|
|
confirmation = forms.CharField(label=ugettext_lazy("Confirmation"), required=False,
|
|
widget=forms.PasswordInput)
|
|
|
|
class Meta:
|
|
model = User
|
|
fields = ("first_name", "last_name")
|
|
|
|
def __init__(self, update_password, *args, **kwargs):
|
|
super(ProfileForm, self).__init__(*args, **kwargs)
|
|
if not update_password:
|
|
del self.fields["oldpassword"]
|
|
del self.fields["newpassword"]
|
|
del self.fields["confirmation"]
|
|
|
|
def clean_oldpassword(self):
|
|
if self.cleaned_data["oldpassword"] == "":
|
|
return self.cleaned_data["oldpassword"]
|
|
|
|
if parameters.get_admin("AUTHENTICATION_TYPE", app="admin") != "local":
|
|
return self.cleaned_data["oldpassword"]
|
|
|
|
if not self.instance.check_password(self.cleaned_data["oldpassword"]):
|
|
raise forms.ValidationError(_("Old password mismatchs"))
|
|
return self.cleaned_data["oldpassword"]
|
|
|
|
def clean_confirmation(self):
|
|
if self.cleaned_data["newpassword"] != self.cleaned_data["confirmation"]:
|
|
raise forms.ValidationError(_("Passwords mismatch"))
|
|
return self.cleaned_data["confirmation"]
|
|
|
|
def save(self, commit=True):
|
|
user = super(ProfileForm, self).save(commit=False)
|
|
if commit:
|
|
if self.cleaned_data.has_key("confirmation") and \
|
|
self.cleaned_data["confirmation"] != "":
|
|
user.set_password(self.cleaned_data["confirmation"], self.cleaned_data["oldpassword"])
|
|
user.save()
|
|
return user
|
|
|
|
class ForwardForm(forms.Form):
|
|
dest = forms.CharField(label=ugettext_lazy("Destination(s)"), widget=forms.Textarea)
|
|
keepcopies = forms.BooleanField(label=ugettext_lazy("Keep local copies"), required=False)
|
|
|
|
def parse_dest(self):
|
|
self.dests = []
|
|
for d in self.cleaned_data["dest"].strip().split(","):
|
|
local_part, domname = split_mailbox(d)
|
|
if not local_part or not domname or not len(domname):
|
|
raise BadDestination("Invalid mailbox syntax for %s" % d)
|
|
try:
|
|
mb = Domain.objects.get(name=domname)
|
|
except Domain.DoesNotExist:
|
|
self.dests += [d]
|
|
else:
|
|
raise BadDestination(_("You can't define a forward to a local destination. Please ask your administrator to create an alias instead."))
|
|
|