class OrganisationRegionAdminCreateForm(EmailForwardMixin, OrganisationRegionAdminForm): """ A form for a regional admin to create new centers the selectable countries are limited to the countries from the regions with additionally email_forward field """ email_forward = EmailFieldLower( required=True, label=_("Email forwarding address"), help_text=_( 'The primary email forwarding address for the organisation'), widget=bootstrap.EmailInput()) def clean(self): super().clean() # check email forward return self.check_email_forward() def save(self, commit=True): """ creating a new center with a forward address """ instance = super().save(commit) return self.save_email_forward(instance)
class SelfUserEmailAddForm(forms.Form): email = EmailFieldLower(max_length=254, label=_('Email address'), required=False) user = forms.IntegerField(widget=forms.HiddenInput()) error_messages = { 'duplicate_email': _("The email address \"%(email)s\" is already in use."), } def clean_email(self): # Check if email is unique, email = self.cleaned_data["email"] if not email: raise forms.ValidationError(_('This field is required.')) qs = UserEmail.objects.filter(email=email) if qs.exists(): raise forms.ValidationError( self.error_messages['duplicate_email'] % {'email': email}) return email def save(self): cd = self.cleaned_data user_email = UserEmail(email=cd['email'], user_id=cd['user']) user_email.save() return user_email
class AdminEmailForwardInlineForm(BaseTabularInlineForm): forward = EmailFieldLower(max_length=254, label=_('Email forwarding address')) class Meta: model = EmailForward fields = ('forward', 'primary') widgets = {'primary': bootstrap.CheckboxInput()}
class EmailForwardOnlyInlineForm(BaseTabularInlineForm): """ form without a primary field """ forward = EmailFieldLower(max_length=254, label=_('Email forwarding address')) class Meta: model = EmailForward fields = ('forward', )
class EmailForwardForm(BaseForm): forward = EmailFieldLower(max_length=254, label=_('Email forwarding address')) class Meta: model = EmailForward fields = ('forward', 'email') widgets = { 'email': forms.HiddenInput(), } def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs)
class EmailForwardInlineForm(BaseTabularInlineForm): """ form without a primary field and with a list of readonly primary emails """ forward = EmailFieldLower(max_length=254, label=_('Email forwarding address')) class Meta: model = EmailForward fields = ('forward', ) def template(self): return 'emails/email_forward_tabular.html'
class OrganisationCountryForm(BaseForm): email_value = EmailFieldLower(required=True, label=_("Email address")) country_groups = ModelMultipleChoiceField( queryset=CountryGroup.objects.all(), required=False, widget=bootstrap.CheckboxSelectMultiple(), label=_("Country Groups")) class Meta: model = OrganisationCountry fields = ('association', 'homepage', 'country_groups', 'country', 'is_active') widgets = { 'homepage': bootstrap.TextInput(attrs={'size': 50}), 'association': bootstrap.Select(), 'country': bootstrap.Select2(), } def __init__(self, *args, **kwargs): self.user = kwargs.pop('user') # remove custom user keyword super().__init__(*args, **kwargs) if self.instance.email: self.fields['email_value'].initial = force_str(self.instance.email) else: self.fields['email_value'].initial = SSO_ORGANISATION_EMAIL_DOMAIN if self.instance.pk is not None and self.instance.country: # readonly field for the update form self.fields['country_text'] = bootstrap.ReadOnlyField( initial=force_str(self.instance.country), label=_("Country")) self.fields['association_text'] = bootstrap.ReadOnlyField( initial=force_str(self.instance.association), label=_("Association")) del self.fields['association'] del self.fields['country'] else: self.fields[ 'association'].queryset = self.user.get_administrable_associations( ) def clean_email_value(self): """ the new email address must be ending with SSO_ORGANISATION_EMAIL_DOMAIN """ email_value = self.cleaned_data['email_value'] if SSO_ORGANISATION_EMAIL_DOMAIN and email_value[ -len(SSO_ORGANISATION_EMAIL_DOMAIN ):] != SSO_ORGANISATION_EMAIL_DOMAIN: msg = _( 'The email address of the center must be ending with %(domain)s' ) % { 'domain': SSO_ORGANISATION_EMAIL_DOMAIN } raise ValidationError(msg) if Email.objects.filter(email=email_value).exclude( organisationcountry=self.instance).exists(): msg = _('The email address already exists') raise ValidationError(msg) return email_value def save(self, commit=True): instance = super().save(commit) if 'email_value' in self.changed_data: if self.instance.email: self.instance.email.email = self.cleaned_data['email_value'] self.instance.email.save() else: # create email object email = Email(email_type=COUNTRY_EMAIL_TYPE, permission=PERM_DWB, email=self.cleaned_data['email_value']) email.save() instance.email = email instance.save() return instance
class AdminRegionForm(BaseForm): email_value = EmailFieldLower(required=True, label=_("Email address")) organisation_country = ModelChoiceField(queryset=None, required=True, label=_("country (org.)"), widget=bootstrap.Select2()) class Meta: model = AdminRegion fields = ('name', 'homepage', 'organisation_country', 'is_active') widgets = { 'homepage': bootstrap.TextInput(attrs={'size': 50}), 'organisation_country': bootstrap.Select2(), 'name': bootstrap.TextInput(attrs={'size': 50}), } def __init__(self, *args, **kwargs): self.user = kwargs.pop('user') # remove custom user keyword super().__init__(*args, **kwargs) self.fields[ 'organisation_country'].queryset = self.user.get_administrable_region_countries( ) if self.instance.email: self.fields['email_value'].initial = force_str(self.instance.email) else: self.fields['email_value'].initial = SSO_ORGANISATION_EMAIL_DOMAIN def clean_email_value(self): """ the new email address must be ending with SSO_ORGANISATION_EMAIL_DOMAIN """ email_value = self.cleaned_data['email_value'] if SSO_ORGANISATION_EMAIL_DOMAIN and email_value[ -len(SSO_ORGANISATION_EMAIL_DOMAIN ):] != SSO_ORGANISATION_EMAIL_DOMAIN: msg = _( 'The email address of the center must be ending with %(domain)s' ) % { 'domain': SSO_ORGANISATION_EMAIL_DOMAIN } raise ValidationError(msg) if Email.objects.filter(email=email_value).exclude( adminregion=self.instance).exists(): msg = _('The email address already exists') raise ValidationError(msg) return email_value def save(self, commit=True): instance = super().save(commit) if 'email_value' in self.changed_data: if self.instance.email: self.instance.email.email = self.cleaned_data['email_value'] self.instance.email.save() else: # create email object email = Email(email_type=REGION_EMAIL_TYPE, permission=PERM_DWB, email=self.cleaned_data['email_value']) email.save() instance.email = email instance.save() return instance
class OrganisationEmailAdminForm(OrganisationBaseForm): """ A Form for Admins of Organisations. The Form includes an additional Email Field for creating an Email objects """ email_type = CENTER_EMAIL_TYPE permission = PERM_EVERYBODY email_value = EmailFieldLower(required=True, label=_("Email address")) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) if self.instance.email: self.fields['email_value'].initial = force_str(self.instance.email) else: self.fields['email_value'].initial = SSO_ORGANISATION_EMAIL_DOMAIN def clean_email_value(self): """ the new email address must be ending with SSO_ORGANISATION_EMAIL_DOMAIN """ email_value = self.cleaned_data['email_value'] if SSO_ORGANISATION_EMAIL_DOMAIN and ( email_value[-len(SSO_ORGANISATION_EMAIL_DOMAIN):] != SSO_ORGANISATION_EMAIL_DOMAIN): msg = _( 'The email address of the center must be ending with %(domain)s' ) % { 'domain': SSO_ORGANISATION_EMAIL_DOMAIN } raise ValidationError(msg) if Email.objects.filter(email=email_value).exclude( organisation=self.instance).exists(): msg = _('The email address already exists') raise ValidationError(msg) return email_value def save(self, commit=True): """ save the email address or create a new email object if it does not exist """ if self.instance.email: old_email_value = self.instance.email.email else: old_email_value = None new_email_value = self.cleaned_data['email_value'] if 'email_value' in self.changed_data: if self.instance.email: self.instance.email.email = new_email_value self.instance.email.save() else: # create email object email = Email(email_type=self.email_type, permission=self.permission, email=new_email_value) email.save() self.instance.email = email instance = super().save(commit) # enable brand specific modification update_or_create_organisation_account.send_robust( sender=self.__class__, organisation=self.instance, old_email_value=old_email_value, new_email_value=new_email_value, user=self.user) return instance
class EmailAliasInlineForm(BaseTabularInlineForm): alias = EmailFieldLower(max_length=254, label=_('Alias address')) class Meta: model = EmailAlias fields = ('alias', )
class GroupEmailForm(BaseForm): email_value = EmailFieldLower(required=True, label=_("Email address"), widget=bootstrap.EmailInput()) permission = forms.ChoiceField(label=_('Permission'), choices=Email.PERMISSION_CHOICES, widget=bootstrap.Select()) is_active = forms.BooleanField(required=False, label=_("Active"), widget=bootstrap.CheckboxInput()) class Meta: model = GroupEmail fields = ['homepage', 'name', 'is_guide_email'] widgets = { 'homepage': bootstrap.TextInput(attrs={'size': 50}), 'name': bootstrap.TextInput(attrs={'size': 50}), } def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) try: email = self.instance.email self.fields['is_active'].initial = email.is_active self.fields['email_value'].initial = force_str(email) self.fields['permission'].initial = email.permission except ObjectDoesNotExist: self.fields['is_active'].initial = True def clean_email_value(self): """ the new email address must .. """ email_value = self.cleaned_data['email_value'] if Email.objects.filter(email=email_value).exclude( groupemail=self.instance).exists(): msg = _('The email address already exists') raise ValidationError(msg) return email_value def save(self, commit=True): cd = self.changed_data if 'email_value' in cd or 'permission' in cd or 'is_active' in cd: created = False try: email = self.instance.email except ObjectDoesNotExist: email = Email(email_type=GROUP_EMAIL_TYPE) created = True email.is_active = self.cleaned_data['is_active'] email.email = self.cleaned_data['email_value'] email.permission = self.cleaned_data['permission'] email.save() if created: self.instance.email = email instance = super().save(commit) return instance