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!")) class ClientEditForm(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")) client_groups = ModelMultipleChoiceField(ClientGroup.objects.all(), label=_("Client Groups"), required=False) 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') 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 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")) client_groups = ModelMultipleChoiceField(ClientGroup.objects.all(), label=_("Client Groups"), required=False) 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() 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!"))