2015-10-30 12:09:01 +00:00
|
|
|
import textwrap
|
2014-10-06 14:57:02 +00:00
|
|
|
from functools import partial
|
|
|
|
|
|
|
|
from django import forms
|
2014-09-06 10:56:30 +00:00
|
|
|
from django.contrib.admin import helpers
|
2014-11-24 14:39:41 +00:00
|
|
|
from django.core import validators
|
2014-09-06 10:56:30 +00:00
|
|
|
from django.forms.models import modelformset_factory, BaseModelFormSet
|
2023-11-17 12:25:13 +00:00
|
|
|
from django.template import Template, Context
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2014-10-06 14:57:02 +00:00
|
|
|
|
2015-04-27 12:24:17 +00:00
|
|
|
from orchestra.forms.widgets import SpanWidget
|
2014-11-24 14:39:41 +00:00
|
|
|
|
2014-10-06 14:57:02 +00:00
|
|
|
from ..core.validators import validate_password
|
2014-07-24 15:43:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AdminFormMixin(object):
|
|
|
|
""" Provides a method for rendering a form just like in Django Admin """
|
|
|
|
def as_admin(self):
|
|
|
|
prepopulated_fields = {}
|
|
|
|
fieldsets = [
|
2015-07-20 12:51:30 +00:00
|
|
|
(None, {
|
|
|
|
'fields': list(self.fields.keys())
|
|
|
|
}),
|
2014-07-24 15:43:23 +00:00
|
|
|
]
|
2014-09-06 10:56:30 +00:00
|
|
|
adminform = helpers.AdminForm(self, fieldsets, prepopulated_fields)
|
2014-07-24 15:43:23 +00:00
|
|
|
template = Template(
|
|
|
|
'{% for fieldset in adminform %}'
|
2014-09-02 15:48:07 +00:00
|
|
|
' {% include "admin/includes/fieldset.html" %}'
|
2014-07-24 15:43:23 +00:00
|
|
|
'{% endfor %}'
|
|
|
|
)
|
2021-05-24 09:19:30 +00:00
|
|
|
context = {
|
2015-07-20 12:51:30 +00:00
|
|
|
'adminform': adminform
|
2021-05-24 09:19:30 +00:00
|
|
|
}
|
2023-11-17 12:25:13 +00:00
|
|
|
return template.render(Context(context))
|
2014-09-06 10:56:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AdminFormSet(BaseModelFormSet):
|
|
|
|
def as_admin(self):
|
2015-10-30 12:09:01 +00:00
|
|
|
template = Template(textwrap.dedent("""\
|
|
|
|
<div class="inline-group">
|
|
|
|
<div class="tabular inline-related last-related">
|
|
|
|
{{ formset.management_form }}
|
|
|
|
<fieldset class="module">
|
|
|
|
{{ formset.non_form_errors.as_ul }}
|
|
|
|
<table id="formset" class="form">
|
|
|
|
{% for form in formset.forms %}
|
|
|
|
{% if forloop.first %}
|
|
|
|
<thead><tr>
|
|
|
|
{% for field in form.visible_fields %}
|
|
|
|
<th>{{ field.label|capfirst }}</th>
|
|
|
|
{% endfor %}
|
|
|
|
</tr></thead>
|
|
|
|
{% endif %}
|
2016-10-25 09:50:50 +00:00
|
|
|
<tr class="{% cycle 'row1' 'row2' %}">
|
2015-10-30 12:09:01 +00:00
|
|
|
{% for field in form.visible_fields %}
|
|
|
|
<td>
|
|
|
|
{# Include the hidden fields in the form #}
|
|
|
|
{% if forloop.first %}
|
|
|
|
{% for hidden in form.hidden_fields %}
|
|
|
|
{{ hidden }}
|
|
|
|
{% endfor %}
|
|
|
|
{% endif %}
|
|
|
|
{{ field.errors.as_ul }}
|
|
|
|
{{ field }}
|
|
|
|
</td>
|
|
|
|
{% endfor %}
|
|
|
|
</tr>
|
|
|
|
{% endfor %}
|
|
|
|
</table>
|
|
|
|
</fieldset>
|
|
|
|
</div>
|
|
|
|
</div>""")
|
2014-09-06 10:56:30 +00:00
|
|
|
)
|
2021-05-24 09:19:30 +00:00
|
|
|
context = {
|
2015-10-30 12:09:01 +00:00
|
|
|
'formset': self
|
2021-05-24 09:19:30 +00:00
|
|
|
}
|
2023-11-17 12:25:13 +00:00
|
|
|
return template.render(Context(context))
|
2014-09-06 10:56:30 +00:00
|
|
|
|
|
|
|
|
2014-10-06 14:57:02 +00:00
|
|
|
class AdminPasswordChangeForm(forms.Form):
|
|
|
|
"""
|
|
|
|
A form used to change the password of a user in the admin interface.
|
|
|
|
"""
|
|
|
|
error_messages = {
|
|
|
|
'password_mismatch': _("The two password fields didn't match."),
|
|
|
|
'password_missing': _("No password has been provided."),
|
2016-05-11 12:56:10 +00:00
|
|
|
'bad_hash': _("Invalid password format or unknown hashing algorithm."),
|
2014-10-06 14:57:02 +00:00
|
|
|
}
|
|
|
|
required_css_class = 'required'
|
2016-05-11 12:56:10 +00:00
|
|
|
password = forms.CharField(label=_("Password"), required=False,
|
|
|
|
widget=forms.TextInput(attrs={'size':'120'}))
|
2014-10-06 14:57:02 +00:00
|
|
|
password1 = forms.CharField(label=_("Password"), widget=forms.PasswordInput,
|
|
|
|
required=False, validators=[validate_password])
|
|
|
|
password2 = forms.CharField(label=_("Password (again)"), widget=forms.PasswordInput,
|
|
|
|
required=False)
|
2021-05-24 09:19:30 +00:00
|
|
|
|
2014-10-06 14:57:02 +00:00
|
|
|
def __init__(self, user, *args, **kwargs):
|
|
|
|
self.related = kwargs.pop('related', [])
|
2016-05-11 12:56:10 +00:00
|
|
|
self.raw = kwargs.pop('raw', False)
|
2014-10-06 14:57:02 +00:00
|
|
|
self.user = user
|
2016-05-11 12:56:10 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.password_provided = False
|
2014-10-06 14:57:02 +00:00
|
|
|
for ix, rel in enumerate(self.related):
|
2016-05-11 12:56:10 +00:00
|
|
|
self.fields['password_%i' % ix] = forms.CharField(label=_("Password"), required=False,
|
|
|
|
widget=forms.TextInput(attrs={'size':'120'}))
|
|
|
|
setattr(self, 'clean_password_%i' % ix, partial(self.clean_password, ix=ix))
|
2015-04-27 12:24:17 +00:00
|
|
|
self.fields['password1_%i' % ix] = forms.CharField(label=_("Password"),
|
|
|
|
widget=forms.PasswordInput, required=False)
|
|
|
|
self.fields['password2_%i' % ix] = forms.CharField(label=_("Password (again)"),
|
|
|
|
widget=forms.PasswordInput, required=False)
|
2014-10-06 14:57:02 +00:00
|
|
|
setattr(self, 'clean_password2_%i' % ix, partial(self.clean_password2, ix=ix))
|
2021-05-24 09:19:30 +00:00
|
|
|
|
2014-10-06 14:57:02 +00:00
|
|
|
def clean_password2(self, ix=''):
|
|
|
|
if ix != '':
|
|
|
|
ix = '_%i' % ix
|
|
|
|
password1 = self.cleaned_data.get('password1%s' % ix)
|
|
|
|
password2 = self.cleaned_data.get('password2%s' % ix)
|
|
|
|
if password1 and password2:
|
2016-05-11 12:56:10 +00:00
|
|
|
self.password_provided = True
|
2014-10-06 14:57:02 +00:00
|
|
|
if password1 != password2:
|
|
|
|
raise forms.ValidationError(
|
|
|
|
self.error_messages['password_mismatch'],
|
|
|
|
code='password_mismatch',
|
|
|
|
)
|
|
|
|
elif password1 or password2:
|
2016-05-11 12:56:10 +00:00
|
|
|
self.password_provided = True
|
2014-10-06 14:57:02 +00:00
|
|
|
raise forms.ValidationError(
|
|
|
|
self.error_messages['password_mismatch'],
|
|
|
|
code='password_mismatch',
|
|
|
|
)
|
|
|
|
return password2
|
2021-05-24 09:19:30 +00:00
|
|
|
|
2016-05-11 12:56:10 +00:00
|
|
|
def clean_password(self, ix=''):
|
|
|
|
if ix != '':
|
|
|
|
ix = '_%i' % ix
|
|
|
|
password = self.cleaned_data.get('password%s' % ix)
|
|
|
|
if password:
|
2016-10-11 09:12:25 +00:00
|
|
|
# lazy loading because of passlib
|
|
|
|
from django.contrib.auth.hashers import identify_hasher
|
2016-05-11 12:56:10 +00:00
|
|
|
self.password_provided = True
|
|
|
|
try:
|
2016-10-11 09:12:25 +00:00
|
|
|
identify_hasher(password)
|
2016-05-11 12:56:10 +00:00
|
|
|
except ValueError:
|
|
|
|
raise forms.ValidationError(
|
|
|
|
self.error_messages['bad_hash'],
|
|
|
|
code='bad_hash',
|
|
|
|
)
|
|
|
|
return password
|
2021-05-24 09:19:30 +00:00
|
|
|
|
2014-10-06 14:57:02 +00:00
|
|
|
def clean(self):
|
2016-05-11 12:56:10 +00:00
|
|
|
if not self.password_provided:
|
|
|
|
raise forms.ValidationError(
|
|
|
|
self.error_messages['password_missing'],
|
|
|
|
code='password_missing',
|
|
|
|
)
|
2021-05-24 09:19:30 +00:00
|
|
|
|
2014-10-06 14:57:02 +00:00
|
|
|
def save(self, commit=True):
|
|
|
|
"""
|
|
|
|
Saves the new password.
|
|
|
|
"""
|
2016-05-11 12:56:10 +00:00
|
|
|
field_name = 'password' if self.raw else 'password1'
|
|
|
|
password = self.cleaned_data[field_name]
|
2014-10-06 14:57:02 +00:00
|
|
|
if password:
|
2016-05-11 12:56:10 +00:00
|
|
|
if self.raw:
|
2016-10-11 09:12:25 +00:00
|
|
|
self.user.password = password
|
2016-05-11 12:56:10 +00:00
|
|
|
else:
|
|
|
|
self.user.set_password(password)
|
2014-10-06 14:57:02 +00:00
|
|
|
if commit:
|
2015-04-02 16:14:55 +00:00
|
|
|
try:
|
|
|
|
self.user.save(update_fields=['password'])
|
|
|
|
except ValueError:
|
|
|
|
# password is not a field but an attribute
|
|
|
|
self.user.save() # Trigger the backend
|
2014-10-06 14:57:02 +00:00
|
|
|
for ix, rel in enumerate(self.related):
|
2016-05-11 12:56:10 +00:00
|
|
|
password = self.cleaned_data['%s_%s' % (field_name, ix)]
|
2014-10-06 14:57:02 +00:00
|
|
|
if password:
|
2016-05-20 08:29:25 +00:00
|
|
|
if self.raw:
|
2016-05-11 12:56:10 +00:00
|
|
|
rel.password = password
|
|
|
|
else:
|
|
|
|
set_password = getattr(rel, 'set_password')
|
|
|
|
set_password(password)
|
2014-10-06 14:57:02 +00:00
|
|
|
if commit:
|
2014-10-07 13:08:59 +00:00
|
|
|
rel.save(update_fields=['password'])
|
2014-10-06 14:57:02 +00:00
|
|
|
return self.user
|
2021-05-24 09:19:30 +00:00
|
|
|
|
2014-10-06 14:57:02 +00:00
|
|
|
def _get_changed_data(self):
|
2016-05-11 12:56:10 +00:00
|
|
|
data = super().changed_data
|
2014-10-06 14:57:02 +00:00
|
|
|
for name in self.fields.keys():
|
|
|
|
if name not in data:
|
|
|
|
return []
|
|
|
|
return ['password']
|
|
|
|
changed_data = property(_get_changed_data)
|
|
|
|
|
2014-11-24 14:39:41 +00:00
|
|
|
|
|
|
|
class SendEmailForm(forms.Form):
|
|
|
|
email_from = forms.EmailField(label=_("From"),
|
2015-04-27 12:24:17 +00:00
|
|
|
widget=forms.TextInput(attrs={'size': '118'}))
|
|
|
|
to = forms.CharField(label="To", required=False)
|
2014-11-24 14:39:41 +00:00
|
|
|
extra_to = forms.CharField(label="To (extra)", required=False,
|
2015-04-27 12:24:17 +00:00
|
|
|
widget=forms.TextInput(attrs={'size': '118'}))
|
2014-11-24 14:39:41 +00:00
|
|
|
subject = forms.CharField(label=_("Subject"),
|
2015-04-27 12:24:17 +00:00
|
|
|
widget=forms.TextInput(attrs={'size': '118'}))
|
2014-11-24 14:39:41 +00:00
|
|
|
message = forms.CharField(label=_("Message"),
|
2015-04-27 12:24:17 +00:00
|
|
|
widget=forms.Textarea(attrs={'cols': 118, 'rows': 15}))
|
2021-05-24 09:19:30 +00:00
|
|
|
|
2014-11-24 14:39:41 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
2016-05-11 12:56:10 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
2014-11-24 14:39:41 +00:00
|
|
|
initial = kwargs.get('initial')
|
|
|
|
if 'to' in initial:
|
2015-04-27 12:24:17 +00:00
|
|
|
self.fields['to'].widget = SpanWidget(original=initial['to'])
|
2014-11-24 14:39:41 +00:00
|
|
|
else:
|
|
|
|
self.fields.pop('to')
|
2021-05-24 09:19:30 +00:00
|
|
|
|
2014-11-24 14:39:41 +00:00
|
|
|
def clean_comma_separated_emails(self, value):
|
|
|
|
clean_value = []
|
|
|
|
for email in value.split(','):
|
|
|
|
email = email.strip()
|
|
|
|
if email:
|
|
|
|
try:
|
|
|
|
validators.validate_email(email)
|
|
|
|
except validators.ValidationError:
|
|
|
|
raise validators.ValidationError("Comma separated email addresses.")
|
|
|
|
clean_value.append(email)
|
|
|
|
return clean_value
|
2021-05-24 09:19:30 +00:00
|
|
|
|
2014-11-24 14:39:41 +00:00
|
|
|
def clean_extra_to(self):
|
|
|
|
extra_to = self.cleaned_data['extra_to']
|
|
|
|
return self.clean_comma_separated_emails(extra_to)
|