class SocialUserSignupForm(SignupForm): password1 = SetPasswordField( label=_('Password'), required=True, help_text= 'We need you to set a password because we can help you recover your account' ) display_name = forms.CharField(label=_('Display name'), max_length=100, required=True) def __init__(self, *args, **kwargs): super(SocialUserSignupForm, self).__init__(*args, **kwargs) self.fields['email'].initial = self.sociallogin.user.email if self.sociallogin.user.email: self.fields['email'].widget.attrs['readonly'] = True self.fields['display_name'].initial = social_user_display_name( self.sociallogin.user) def save(self, request): # Check if user submitted different email we got from social network (Nasty user) if self.sociallogin.user.email and self.sociallogin.user.email != self.cleaned_data.get( 'email'): self.cleaned_data['email'] = self.sociallogin.user.email user = super(SocialUserSignupForm, self).save(request) user.display_name = self.cleaned_data['display_name'] user.save()
class CustomSignupForm(BaseSignupForm): password1 = SetPasswordField(label=_("Password")) password2 = PasswordField(label=_("Password (again)")) confirmation_key = forms.CharField(max_length=40, required=False, widget=forms.HiddenInput()) def __init__(self, *args, **kwargs): super(CustomSignupForm, self).__init__(*args, **kwargs) if not app_settings.SIGNUP_PASSWORD_VERIFICATION: del self.fields["password2"] def clean(self): super(CustomSignupForm, self).clean() if app_settings.SIGNUP_PASSWORD_VERIFICATION \ and "password1" in self.cleaned_data \ and "password2" in self.cleaned_data: if self.cleaned_data["password1"] \ != self.cleaned_data["password2"]: raise forms.ValidationError( _("You must type the same password each time.")) return self.cleaned_data def save(self, request): adapter = get_adapter() user = adapter.new_user(request) adapter.save_user(request, user, self) self.custom_signup(request, user) # TODO: Move into adapter `save_user` ? setup_user_email(request, user, []) return user
class StripeSubscriptionSignupForm(forms.Form): """ Requires the following packages: * django-crispy-forms * django-floppyforms * django-allauth Necessary settings:: INSTALLED_APPS += ( "floppyforms", "allauth", # registration "allauth.account", # registration ) ACCOUNT_SIGNUP_FORM_CLASS = "djstripe.StripeSubscriptionSignupForm" Necessary URLS:: (r'^accounts/', include('allauth.urls')), """ username = forms.CharField(max_length=30) email = forms.EmailField(max_length=30) password1 = SetPasswordField(label=_("Password")) password2 = PasswordField(label=_("Password (again)")) confirmation_key = forms.CharField(max_length=40, required=False, widget=forms.HiddenInput()) stripe_token = forms.CharField(widget=forms.HiddenInput()) plan = forms.ChoiceField(choices=PLAN_CHOICES) # Stripe nameless fields number = forms.CharField( max_length=20, required=False, widget=StripeWidget(attrs={"data-stripe": "number"})) cvc = forms.CharField( max_length=4, label=_("CVC"), required=False, widget=StripeWidget(attrs={"data-stripe": "cvc"})) exp_month = forms.CharField( max_length=2, required=False, widget=StripeWidget(attrs={"data-stripe": "exp-month"})) exp_year = forms.CharField( max_length=4, required=False, widget=StripeWidget(attrs={"data-stripe": "exp-year"})) def save(self, user): try: customer, created = Customer.get_or_create(user) customer.update_card(self.cleaned_data["stripe_token"]) customer.subscribe(self.cleaned_data["plan"]) except stripe.StripeError as e: # handle error here raise e
def __init__(self, *args, **kwargs): super(CustomResetPasswordKeyForm, self).__init__(*args, **kwargs) self.fields['password1'] = SetPasswordField(label='رمز عبور') self.fields['password1'].widget = forms.PasswordInput( attrs={'class': 'form-control'}) self.fields['password2'] = PasswordField(label='تکرار رمز عبور') self.fields['password2'].widget = forms.PasswordInput( attrs={'class': 'form-control'})
class SetPasswordForm(forms.Form): password1 = SetPasswordField(label=_('New Password')) def __init__(self, *args, **kwargs): self.user = kwargs.pop('user', None) self.temp_key = kwargs.pop("temp_key", None) super(SetPasswordForm, self).__init__(*args, **kwargs) self.fields['password1'].user = self.user def save(self): get_adapter().set_password(self.user, self.cleaned_data['password1'])
def __init__(self, *args, **kwargs): super(CustomChangePasswordForm, self).__init__(*args, **kwargs) self.fields['oldpassword'] = PasswordField(label='رمز قبلی') self.fields['oldpassword'].widget = forms.PasswordInput( attrs={'class': 'form-control'}) self.fields['password1'] = SetPasswordField(label='رمز جدید') self.fields['password1'].widget = forms.PasswordInput( attrs={'class': 'form-control'}) self.fields['password2'] = PasswordField(label='تکرار رمز جدید') self.fields['password2'].widget = forms.PasswordInput( attrs={'class': 'form-control'})
class ChangePasswordForm(forms.Form): oldpassword = PasswordField(label=_("Current Password")) password1 = SetPasswordField(label=_("New Password")) password2 = PasswordField(label=_("New Password (again)")) def clean_password2(self): if ("password1" in self.cleaned_data and "password2" in self.cleaned_data): if (self.cleaned_data["password1"] != self.cleaned_data["password2"]): raise forms.ValidationError( _("You must type the same password" " each time.")) return self.cleaned_data["password2"]
class SocialForm(SignupForm): password1 = SetPasswordField(min_length=6, label='Enterpassword') password2 = PasswordField(min_length=6, label='Rente password again') def clean_password2(self): if ("password1" in self.cleaned_data and "password2" in self.cleaned_data): if (self.cleaned_data["password1"] != self.cleaned_data["password2"]): raise forms.ValidationError(("Password mismatch")) def signup(self, request, user): user.set_password(self.user, self.cleaned_data["password1"]) user.save()
class SignupForm(ProfileForm): email = forms.EmailField(required=True) password1 = SetPasswordField(label="Password", required=True) password2 = PasswordField(label="Password (again)", required=True) def clean_email(self): """ Check if user is already registered and if so raise validation error. It may be considered a security hole to inform if a user is registered or not but it improves usability. """ email = self.cleaned_data['email'] User = get_user_model() if User.objects.filter(email=email).exists(): raise forms.ValidationError('This email is already registered.') return email
class MyCustomChangePasswordForm(ChangePasswordForm): oldpassword = PasswordField(label=("Current Password")) password1 = SetPasswordField(label=("New Password")) password2 = PasswordField(label=("New Password (again)")) def __init__(self, *args, **kwargs): super(MyCustomChangePasswordForm, self).__init__(*args, **kwargs) self.fields['password1'].user = self.user def clean_oldpassword(self): if not self.user.check_password(self.cleaned_data.get("oldpassword")): raise forms.ValidationError(("Please type your current" " password.")) return self.cleaned_data["oldpassword"] def save(self): get_adapter().set_password(self.user, self.cleaned_data["password1"])
class ExtChangePasswordForm(ChangePasswordForm): oldpassword = PasswordField(label="Current Password") password1 = SetPasswordField(label="New Password") password2 = PasswordField(label="New Password (again)") def __init__(self, *args, **kwargs): super(ExtChangePasswordForm, self).__init__(*args, **kwargs) self.helper = FormHelper() self.helper.form_method = "POST" self.helper.form_action = "/accounts/password/change/" self.helper.form_class = 'form' self.helper.form_id = 'auth_changepass' self.helper.layout = Layout( HTML(" {% csrf_token %} "), Div(Field('oldpassword', css_class="input", placeholder="Current Password", autocomplete=False), HTML("<p id='oldpassword_error' class='help is-danger'></p >"), css_class="field"), Div(Field('password1', css_class="input", placeholder="New Password", autocomplete=False), HTML("<p id='password1_error' class='help is-danger'></p >"), css_class="field"), Div(Field('password2', css_class="input", placeholder="New Password (again)", autocomplete=False), HTML("<p id='password2_error' class='help is-danger'></p >"), css_class="field"), Div( HTML( "<a id='pc_loader' class='button is-text is-loading is-hidden'></a>" ), Submit('submit', 'Change Password', css_class="button is-info is-rounded", css_id="pc_submit"))) def save(self): super(ExtChangePasswordForm, self).save()
class ChangePasswordForm(UserForm): oldpassword = PasswordField(label=_('Current Password')) password1 = SetPasswordField(label=_('New Password')) def __init__(self, *args, **kwargs): super(ChangePasswordForm, self).__init__(*args, **kwargs) self.fields['password1'].user = self.user self.fields['oldpassword'].widget.attrs['placeholder'] = '' self.fields['password1'].widget.attrs['placeholder'] = '' def clean_oldpassword(self): if not self.user.check_password(self.cleaned_data.get('oldpassword')): raise forms.ValidationError(_('Please type your current' ' password.')) return self.cleaned_data['oldpassword'] def save(self): get_adapter().set_password(self.user, self.cleaned_data['password1'])
class UserSetPasswordForm(PasswordVerificationMixin, forms.Form): """ See allauth: https://github.com/pennersr/django-allauth/blob/master/allauth/account/forms.py#L54 If we do not want this dependency, we can write our own clean method to ensure the 2 typed-in passwords match. """ password1 = SetPasswordField( label="New Password", help_text=password_validation.password_validators_help_text_html(), ) password2 = PasswordField(label="New Password (again)") def save(self, user): user.set_password(self.cleaned_data["password1"]) user.has_valid_password = True user.save() return user
class CustomResetPasswordKeyForm(ResetPasswordKeyForm): """Reset Password with Key Form. Attributes: helper (TYPE): Description """ password1 = SetPasswordField(label="Nueva contraseña") password2 = PasswordField(label="Nueva contraseña ( Otra vez)") helper = FormHelper() helper.form_tag = False def __init__(self, *args, **kwargs): """Reset Password with Key Form. Args: *args (TYPE): Description **kwargs (TYPE): Description """ super(CustomResetPasswordKeyForm, self).__init__(*args, **kwargs)
class CustomChangePasswordForm1(forms.ModelForm): """Change Password Form. Attributes: helper (TYPE): Description """ password = SetPasswordField(label="Contraseña") repeat_password = PasswordField(label="Confirmar contraseña") helper = FormHelper() helper.form_tag = False helper.layout = Layout( Div( 'password', css_class='col-md-12', style="", ), Div('repeat_password', css_class='col-md-12'), Div('email', css_class='col-md-6', hidden="true"), ) class Meta: """Meta. Attributes: exclude (list): Description model (TYPE): Description """ model = User exclude = ['first_name', 'last_name', 'user_type', 'is_active'] def __init__(self, *args, **kwargs): """.""" super(CustomChangePasswordForm1, self).__init__(*args, **kwargs)
class EmailUserSignupForm(BaseSignupForm): display_name = forms.CharField( label=_('Display name'), required=True, widget=forms.TextInput(attrs={'placeholder': _('Display name')})) password1 = SetPasswordField(label=_('Password'), ) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # If you want to remove placeholder # self.fields['email'].widget.attrs.pop('placeholder') # self.fields['password1'].widget.attrs.pop('placeholder') def save(self, request): adapter = get_adapter(request) user = adapter.new_user(request) adapter.save_user(request, user, self) user.display_name = self.cleaned_data['display_name'] user.save() self.custom_signup(request, user) setup_user_email(request, user, []) return user
class SocialPasswordForm(BaseSignupForm): password1 = SetPasswordField(label=_("Password")) password2 = SetPasswordField(label=_("Confirm Password")) def __init__(self, *args, **kwargs): self.sociallogin = kwargs.pop('sociallogin') user = self.sociallogin.user initial = {'email': user_email(user) or '', 'username': user_username(user) or user_email(user).split('@')[0], 'first_name': user_field(user, 'first_name') or '', 'last_name': user_field(user, 'last_name') or ''} kwargs.update({ 'initial': initial, 'email_required': kwargs.get('email_required', app_settings.EMAIL_REQUIRED)}) super().__init__(*args, **kwargs) self.fields['password1'].widget.attrs = {'class':'form-control'} self.fields['password2'].widget.attrs = {'class': 'form-control'} self.fields['email'].widget = forms.HiddenInput() self.fields['username'].widget = forms.HiddenInput() def save(self, request): adapter = self.get_adapter(request) user = adapter.save_user(request, self.sociallogin, form=self) self.custom_signup(request, user) return user def clean(self): super().clean() if "password1" in self.cleaned_data \ and "password2" in self.cleaned_data: if self.cleaned_data["password1"] \ != self.cleaned_data["password2"]: raise forms.ValidationError(_("You must type the same password" " each time.")) def raise_duplicate_email_error(self): raise forms.ValidationError( _("An account already exists with this e-mail address." " Please sign in to that account first, then connect" " your %s account.") % self.sociallogin.account.get_provider().name) def get_adapter(self, request): return import_attribute(settings.SOCIALACCOUNT_ADAPTER)(request) def custom_signup(self, request, user): password = self.cleaned_data['password1'] user.set_password(password) user.save() # TODO: Couldn't get this to work # class SetPasswordForm(forms.Form): # """ # A form that lets a user change set their password without entering the old # password # """ # error_messages = { # 'password_mismatch': _("The two password fields didn't match."), # } # new_password1 = forms.CharField(label=_("New password"), # widget=forms.PasswordInput) # new_password2 = forms.CharField(label=_("New password confirmation"), # widget=forms.PasswordInput) # # def __init__(self, user, *args, **kwargs): # self.user = user # super(SetPasswordForm, self).__init__(*args, **kwargs) # # def clean_new_password2(self): # password1 = self.cleaned_data.get('new_password1') # password2 = self.cleaned_data.get('new_password2') # if password1 and password2: # if password1 != password2: # raise forms.ValidationError( # self.error_messages['password_mismatch'], # code='password_mismatch', # ) # return password2 # # def save(self, commit=True): # self.user.set_password(self.cleaned_data['new_password1']) # if commit: # self.user.save() # return self.user
class SignupForm(forms.Form): username = forms.CharField(max_length=80, required=True) email = forms.EmailField(required=True) password1 = SetPasswordField() password2 = PasswordField() language = forms.TypedChoiceField( choices=settings.LANGUAGE_CHOICES, widget=forms.Select(attrs={'class': 'input-lg'}), required=True) topic1 = forms.ModelChoiceField( queryset=Topic.objects.all(), widget=forms.Select(attrs={'class': 'input-lg'}), empty_label='Select topic') choice1 = forms.ChoiceField(choices=(('pro', "I agree with this."), ('against', "I disagree with this.")), widget=forms.RadioSelect()) topic2 = forms.ModelChoiceField( queryset=Topic.objects.all(), widget=forms.Select(attrs={'class': 'input-lg'}), empty_label='Select topic') choice2 = forms.ChoiceField(choices=(('pro', "I agree with this."), ('against', "I disagree with this.")), widget=forms.RadioSelect()) topic3 = forms.ModelChoiceField( queryset=Topic.objects.all(), widget=forms.Select(attrs={'class': 'input-lg'}), empty_label='Select topic') choice3 = forms.ChoiceField(choices=(('pro', "I agree with this."), ('against', "I disagree with this.")), widget=forms.RadioSelect()) topic4 = forms.ModelChoiceField( queryset=Topic.objects.all(), widget=forms.Select(attrs={'class': 'input-lg'}), empty_label='Select topic') choice4 = forms.ChoiceField(choices=(('pro', "I agree with this."), ('against', "I disagree with this.")), widget=forms.RadioSelect()) class Meta: model = get_user_model() # use this function for swapping user model fields = ('email', 'username', 'password1', 'password2', 'language') def __init__(self, *args, **kwargs): super(SignupForm, self).__init__(*args, **kwargs) self.helper = FormHelper() self.helper.form_id = 'signup_form' self.helper.label_class = 'col-xs-6' self.helper.field_class = 'col-xs-12' self.helper.form_method = 'post' self.helper.form_action = 'accounts_signup' self.helper.add_input(Submit('submit', 'Sign up')) def signup(self, request, user): user.username = self.cleaned_data['username'] user.email = self.cleaned_data['email'] user.language = self.cleaned_data['language'] topic1 = Topic.objects.get(title=self.cleaned_data['topic1']) choice1 = self.cleaned_data['choice1'] if choice1 == "pro": user.topics_pro.add(topic1) else: user.topics_against.add(topic1) topic2 = Topic.objects.get(title=self.cleaned_data['topic2']) choice2 = self.cleaned_data['choice2'] if choice2 == "pro": user.topics_pro.add(topic2) else: user.topics_against.add(topic2) topic3 = Topic.objects.get(title=self.cleaned_data['topic3']) choice3 = self.cleaned_data['choice3'] if choice3 == "pro": user.topics_pro.add(topic3) else: user.topics_against.add(topic3) topic4 = Topic.objects.get(title=self.cleaned_data['topic4']) choice4 = self.cleaned_data['choice4'] if choice4 == "pro": user.topics_pro.add(topic4) else: user.topics_against.add(topic4) user.save()