expephalon/core/forms/profiles.py

108 lines
5.6 KiB
Python
Raw Normal View History

2020-06-22 04:15:09 +00:00
from django.forms import ModelForm, CharField, BooleanField, ImageField, ModelChoiceField, ModelMultipleChoiceField, BooleanField, ValidationError
from django.contrib.auth import get_user_model
from django.utils.translation import gettext_lazy as _
from django.contrib.auth import get_user_model
from phonenumber_field.formfields import PhoneNumberField
from django_countries.fields import CountryField
from internationalflavor.vat_number.forms import VATNumberFormField
from core.models.local import Currency
from core.models.brands import Brand
from core.models.profiles import ClientGroup
class AdminEditForm(ModelForm):
display_name = CharField(required=False, label=_('Internal Display Name'))
mobile = PhoneNumberField(required=False, label=_('Mobile Number'))
role = CharField(required=False, label=_("Role"))
image = ImageField(required=False, label=_("Image"))
remove_image = BooleanField(required=False, label=_("Remove image from profile?"))
class Meta:
model = get_user_model()
fields = ('first_name', 'last_name', "display_name", "email", 'mobile', "role", "image", "remove_image")
def __init__(self, *args, **kwargs):
self.current_email = kwargs.pop('current_email')
super().__init__(*args, **kwargs)
def clean(self):
cleaned_data = super().clean()
if cleaned_data["email"] != self.current_email and get_user_model().objects.filter(username=cleaned_data["email"]):
self.add_error("email", _("An account with this email address already exists!"))
class AdminCreateForm(ModelForm):
display_name = CharField(required=False, label=_('Internal Display Name'))
mobile = PhoneNumberField(required=False, label=_('Mobile Number'))
role = CharField(required=False, label=_("Role"))
image = ImageField(required=False, label=_("Image"))
class Meta:
model = get_user_model()
fields = ('first_name', 'last_name', "display_name", "email", 'mobile', "role", "image")
def clean(self):
cleaned_data = super().clean()
if get_user_model().objects.filter(username=cleaned_data["email"]):
self.add_error("email", _("An account with this email address already exists!"))
2020-06-03 17:24:26 +00:00
class ClientEditForm(ModelForm):
company = CharField(required=False, label=_('Company Name'))
mobile = PhoneNumberField(required=False, label=_('Mobile Number'))
address1 = CharField(label=_('Address'))
2020-06-03 17:24:26 +00:00
address2 = CharField(label=_('Address'), required=False)
zip = CharField(label=_('ZIP'))
city = CharField(label=_('City'))
2020-06-03 17:24:26 +00:00
state = CharField(label=_('State'), required=False)
country = CountryField().formfield()
vat_id = VATNumberFormField(label=_('VAT Number'), required=False)
company_id = CharField(label=_('Company Registration Number'), required=False)
default_currency = ModelChoiceField(Currency.objects.all(), label=_("Default Currency"))
brands = ModelMultipleChoiceField(Brand.objects.all(), label=_("Associated Brands"))
2020-06-22 04:15:09 +00:00
client_groups = ModelMultipleChoiceField(ClientGroup.objects.all(), label=_("Client Groups"), required=False)
2020-06-03 17:24:26 +00:00
marketing_opt_in = BooleanField(label=_("Opted in to marketing messages"), required=False)
pgp_key = CharField(label=_("GPG encryption key"), required=False)
class Meta:
model = get_user_model()
fields = ('first_name', 'last_name', "company", "email", 'mobile', 'address1', 'address2', 'zip', 'city', 'state', 'country', 'vat_id', 'company_id', 'default_currency', 'brands', 'client_groups', 'marketing_opt_in', 'pgp_key')
2020-06-03 17:24:26 +00:00
def __init__(self, *args, **kwargs):
self.current_email = kwargs.pop('current_email')
super().__init__(*args, **kwargs)
def clean(self):
cleaned_data = super().clean()
if cleaned_data["email"] != self.current_email and get_user_model().objects.filter(username=cleaned_data["email"]):
self.add_error("email", _("An account with this email address already exists!"))
2020-06-03 17:24:26 +00:00
class ClientCreateForm(ModelForm):
company = CharField(required=False, label=_('Company Name'))
mobile = PhoneNumberField(required=False, label=_('Mobile Number'))
address1 = CharField(label=_('Address'))
address2 = CharField(label=_('Address'), required=False)
zip = CharField(label=_('ZIP'))
city = CharField(label=_('City'))
state = CharField(label=_('State'), required=False)
country = CountryField().formfield()
vat_id = VATNumberFormField(label=_('VAT Number'), required=False)
company_id = CharField(label=_('Company Registration Number'), required=False)
default_currency = ModelChoiceField(Currency.objects.all(), label=_("Default Currency"))
brands = ModelMultipleChoiceField(Brand.objects.all(), label=_("Associated Brands"))
2020-06-22 04:15:09 +00:00
client_groups = ModelMultipleChoiceField(ClientGroup.objects.all(), label=_("Client Groups"), required=False)
2020-06-03 17:24:26 +00:00
marketing_opt_in = BooleanField(label=_("Opted in to marketing messages"), required=False)
pgp_key = CharField(label=_("GPG encryption key"), required=False)
send_password = BooleanField(label=_("Send password reset email now?"), required=False)
class Meta:
model = get_user_model()
2020-06-22 04:15:09 +00:00
fields = ('first_name', 'last_name', "company", "email", 'mobile', 'address1', 'address2', 'zip', 'city', 'state', 'country', 'vat_id', 'company_id', 'default_currency', 'brands', 'client_groups', 'marketing_opt_in', 'pgp_key', 'send_password')
def clean(self):
cleaned_data = super().clean()
if get_user_model().objects.filter(username=cleaned_data["email"]):
self.add_error("email", _("An account with this email address already exists!"))