class UserForgetPassword(forms.Form): class Meta(object): model = User fields = ( 'email',) email = EMailField()
class UpdateAccountForm(forms.ModelForm): class Meta(object): model = User user_fields = ( 'first_name', 'last_name', 'email') profile_fields = ( 'phone',) fields = user_fields + profile_fields _instance = None # User fields first_name = FirstNameField() last_name = LastNameField() email = EMailField() password1 = PasswordField() password2 = PasswordField() # Profile fields phone = PhoneField() def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['password1'].widget.attrs['placeholder'] = 'Nouveau mot de passe' self.fields['password2'].widget.attrs['placeholder'] = 'Confirmez le nouveau mot de passe' if 'instance' in kwargs: self._instance = kwargs['instance'] self.fields['phone'].initial = self._instance.profile.phone # else: # raise Exception() def clean(self): email = self.cleaned_data.get('email') if email != self._instance.email and User.objects.filter(email=email).exists(): self.add_error('email', 'Ce courriel est reservé.') password = self.cleaned_data.pop('password1') if password != self.cleaned_data.pop('password2'): self.add_error('password1', '') self.add_error('password2', 'Vérifiez les mots de passe') else: self.cleaned_data['password'] = password if not password: del self.cleaned_data['password'] return self.cleaned_data @property def new_password(self): return self.cleaned_data.get('password')
class OrganisationForm(forms.ModelForm): class Meta(object): model = Organisation organisation_fields = ( 'address', 'city', 'description', 'email', 'jurisdiction', 'license', 'logo', 'legal_name', 'organisation_type', 'phone', 'postcode', 'website', ) extended_fields = ( 'contributor_process', 'rattachement_process', 'referent_process', ) fields = organisation_fields + extended_fields # Organisation fields legal_name = OrganisatioLegalNameField(required=True) logo = OrganisationLogoField() address = AddressField() city = CityField() postcode = PostcodeField() org_phone = PhoneField() email = EMailField() website = WebsiteField() description = DescriptionField() jurisdiction = JurisdictionField() organisation_type = OrganisationTypeField() license = LicenseField() # Extended fields rattachement_process = MemberField() contributor_process = ContributorField() referent_process = ReferentField() def __init__(self, *args, **kwargs): self.include_args = kwargs.pop('include', {}) self.extended = self.include_args.get('extended', False) self.instance = kwargs.get('instance', None) self.user = self.include_args.get('user') super().__init__(*args, **kwargs) if not self.extended: for item in self.Meta.extended_fields: self.fields[item].widget = forms.HiddenInput() if self.instance and self.instance.logo: self.fields['logo'].widget.attrs['value'] = self.instance.logo.url if self.instance and not self.user.profile.is_crige_admin: self.fields['jurisdiction'].queryset = \ Jurisdiction.objects.filter(pk__in=self.instance.jurisdiction and [self.instance.jurisdiction.pk] or []) self.fields['jurisdiction'].widget.attrs['disabled'] = 'disabled' self.fields['jurisdiction'].widget.attrs['class'] = 'disabled' def clean(self): if not self.instance: legal_name = self.cleaned_data.get('legal_name') if Organisation.objects.filter( slug=slugify(legal_name)).count() > 0: self.add_error( 'legal_name', 'Une organisation portant le même nom existe déjà.') if self.instance and not self.user.profile.is_crige_admin: self.cleaned_data['jurisdiction'] = self.instance.jurisdiction return self.cleaned_data
class SignUpForm(forms.Form): class Meta(object): user_fields = ( 'username', 'first_name', 'last_name', 'email', 'password', ) profile_fields = ( 'phone', 'organisation', ) organisation_fields = ( 'address', 'city', 'description', 'org_email', 'jurisdiction', 'license', 'logo', 'new_orga', 'organisation_type', 'org_phone', 'postcode', 'website', ) extended_fields = ( 'contributor', 'referent', ) fields = user_fields + profile_fields + organisation_fields + extended_fields # User fields username = UsernameField(required=True) first_name = FirstNameField(required=True) last_name = LastNameField(required=True) email = EMailField(required=True) password1 = PasswordField(required=True) password2 = PasswordField(required=True) # Profile fields phone = PhoneField(required=False) organisation = ModelOrganisationField( required=False, label='Organisation', queryset=Organisation.objects.filter(is_active=True), empty_label="Je ne suis rattaché à aucune organisation", widget=OrganisationSelect(attrs={"crige": False})) # Organisation fields new_orga = OrganisatioLegalNameField() logo = OrganisationLogoField() address = AddressField() city = CityField() postcode = PostcodeField() org_phone = PhoneField() org_email = EMailField() website = WebsiteField() description = DescriptionField() jurisdiction = JurisdictionField() organisation_type = OrganisationTypeField() license = LicenseField() # Extended fields contributor = ContributorField() referent = ReferentField() terms_and_conditions = forms.BooleanField( label=( '<a data-toggle="modal" data-target="#modal-terms">' "J'ai lu et j'accepte les conditions générales d'utilisation du service." '<a>'), initial=False, required=True, widget=TermsAndConditionsCheckBoxInput( attrs={ 'oninvalid': "this.setCustomValidity('Vous devez accepter les conditions générales d'utilisation.')", 'oninput': "setCustomValidity('')", }, modal={ 'id': 'modal-terms', 'title': '', 'body': '', }, ) ) def __init__(self, *args, **kwargs): self.unlock_terms = kwargs.pop('unlock_terms', False) super().__init__(*args, **kwargs) gdpr = Gdpr.objects.latest('issue_date') self.fields['terms_and_conditions'].widget.modal['title'] = gdpr.title self.fields['terms_and_conditions'].widget.modal['body'] = gdpr.description_as_html self.fields['terms_and_conditions'].required = not self.unlock_terms self.fields['password1'].widget.attrs['placeholder'] = 'Mot de passe' self.fields['password2'].widget.attrs['placeholder'] = 'Confirmez le mot de passe' def clean(self): if not self.unlock_terms and not self.cleaned_data.get('terms_and_conditions'): self.add_error('terms_and_conditions', "Vous devez accepter les conditions générales d'utilisation.") username = self.cleaned_data.get('username') if User.objects.filter(username=username).exists() or CkanHandler.is_user_exists(username): self.add_error('username', "Ce nom d'utilisateur est reservé.") email = self.cleaned_data.get('email') if User.objects.filter(email=email).exists(): self.add_error('email', 'Ce courriel est reservé.') password1 = self.cleaned_data.get('password1') password2 = self.cleaned_data.get('password2') if password1 and password2 and password1 != password2: self.add_error('password2', 'Vérifiez les mots de passes.') self.cleaned_data['password'] = self.cleaned_data.pop('password1') return self.cleaned_data @property def is_member(self): select_organisation = self.cleaned_data.get('organisation') or None create_organisation = self.cleaned_data.get('new_orga') or None return (select_organisation or create_organisation) and True or False @property def is_contributor(self): return self.cleaned_data.get('contributor', False) @property def is_referent(self): return self.cleaned_data.get('referent', False) @property def create_organisation(self): return self.cleaned_data.get('new_orga') @property def cleaned_organisation_data(self): data = dict((item, self.cleaned_data.get(item)) for item in self.Meta.organisation_fields) data['legal_name'] = data.pop('new_orga') return data @property def cleaned_user_data(self): return dict((item, self.cleaned_data.get(item)) for item in self.Meta.user_fields) @property def cleaned_profile_data(self): return dict((item, self.cleaned_data.get(item)) for item in self.Meta.profile_fields)