class SpeedyMatchProfileFullSettingsBaseForm(SpeedyMatchProfileBaseForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.helper = FormHelperWithDefaults() self.helper.error_text_inline = False # split into two columns custom_field_names = ('gender_to_match', 'diet_match', 'smoking_status_match', 'relationship_status_match') self.helper.add_layout( Div(*[ Row(*[ # A little hack that forces display of custom widgets Div(Field(field, template='%s/speedy_match_custom_field.html') if (field in custom_field_names) else field, css_class='col-md-6') for field in pair ]) for pair in self.get_field_pairs() ]), ) self.helper.add_input( Submit( 'submit', pgettext_lazy(context=self.instance.user.get_gender(), message='Save Changes'))) def get_field_pairs(self): # This function is not defined in this base (abstract) form. raise NotImplementedError()
class ProfileForm(AddAttributesToFieldsMixin, CleanDateOfBirthMixin, LocalizedFirstLastNameMixin, forms.ModelForm): class Meta: model = User fields = ('date_of_birth', 'photo', 'slug', 'gender') def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['date_of_birth'].input_formats = django_settings.DATE_FIELD_FORMATS self.fields['date_of_birth'].widget.format = django_settings.DEFAULT_DATE_FIELD_FORMAT self.fields['slug'].label = pgettext_lazy(context=self.instance.get_gender(), message='username (slug)') self.helper = FormHelperWithDefaults() # split into two columns field_names = list(self.fields.keys()) self.helper.add_layout(Div(*[ Row(*[ Div(field, css_class='col-md-6') for field in pair]) for pair in zip_longest(field_names[::2], field_names[1::2]) ])) self.helper.add_input(Submit('submit', pgettext_lazy(context=self.instance.get_gender(), message='Save Changes'))) def clean_slug(self): slug = self.cleaned_data.get('slug') username = self.instance.username if (not (normalize_username(username=slug) == username)): raise ValidationError(pgettext_lazy(context=self.instance.get_gender(), message="You can't change your username.")) return slug
class FeedbackForm(ModelFormWithDefaults): class Meta: model = Feedback fields = ('sender_name', 'sender_email', 'text') def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.helper = FormHelperWithDefaults() if (self.defaults.get('sender')): del self.fields['sender_name'] del self.fields['sender_email'] self.helper.add_input( Submit( 'submit', pgettext_lazy(context=self.defaults['sender'].get_gender(), message='Send'))) else: self.fields['sender_name'].required = True self.fields['sender_email'].required = True self.helper.add_layout( Row( Div('sender_name', css_class='col-md-6'), Div('sender_email', css_class='col-md-6'), )) self.helper.add_input(Submit('submit', _('Send')))
class SpeedyMatchProfileFullSettingsBaseForm(SpeedyMatchProfileBaseForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.helper = FormHelperWithDefaults() # split into two columns field_names = list(self.fields.keys()) custom_field_names = ('gender_to_match', 'diet_match', 'smoking_status_match', 'marital_status_match') self.helper.add_layout(Div(*[ Row(*[ # a little hack that forces display of custom widgets Div(Field(field, template='%s/render.html') if (field in custom_field_names) else field, css_class='col-md-6') for field in pair]) for pair in zip_longest(field_names[::2], field_names[1::2]) ])) self.helper.add_input(Submit('submit', pgettext_lazy(context=self.instance.user.get_gender(), message='Save Changes')))
class FeedbackForm(ModelFormWithDefaults): _not_allowed_strings = [] no_bots = forms.CharField(label=_('Type the number "17"'), required=True) class Meta: model = Feedback fields = ('sender_name', 'sender_email', 'text', 'no_bots') def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.helper = FormHelperWithDefaults() if (self.defaults.get('sender')): del self.fields['sender_name'] del self.fields['sender_email'] del self.fields['no_bots'] self.helper.add_input(Submit('submit', pgettext_lazy(context=self.defaults['sender'].get_gender(), message='Send'))) else: self.fields['sender_name'].required = True self.fields['sender_email'].required = True self.helper.add_layout( MultiWidgetField( Row( Div('sender_name', css_class='col-md-6'), Div('sender_email', css_class='col-md-6'), ), 'text', 'no_bots', ), ) self.helper.add_input(Submit('submit', _('Send'))) def clean_text(self): text = self.cleaned_data.get('text') for not_allowed_string in self._not_allowed_strings: if (not_allowed_string in text): raise ValidationError(_("Please contact us by email.")) return text def clean_no_bots(self): no_bots = self.cleaned_data.get('no_bots') if (not (no_bots == "17")): raise ValidationError(_("Not 17.")) return no_bots
class LoginForm(AddAttributesToFieldsMixin, auth_forms.AuthenticationForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.data = self.data.copy() if ('username' in self.data): self.data['username'] = self.data['username'].lower() self.fields['username'].label = _('Email or Username') self.helper = FormHelperWithDefaults() self.helper.add_layout(Div( 'username', 'password', Submit('submit', _('Login')), HTML('<a class="btn btn-link" href="{link}">{text}</a>'.format( link=reverse('accounts:password_reset'), text=_('Forgot your password?'), )), )) def confirm_login_allowed(self, user): return None
class FeedbackForm(ModelFormWithDefaults): class Meta: model = Feedback fields = ('sender_name', 'sender_email', 'text') def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.helper = FormHelperWithDefaults() if (self.defaults.get('sender')): del self.fields['sender_name'] del self.fields['sender_email'] self.helper.add_input(Submit('submit', pgettext_lazy(context=self.defaults['sender'].get_gender(), message='Send'))) else: self.fields['sender_name'].required = True self.fields['sender_email'].required = True self.helper.add_layout(Row( Div('sender_name', css_class='col-md-6'), Div('sender_email', css_class='col-md-6'), )) self.helper.add_input(Submit('submit', _('Send')))
class ProfileForm(AddAttributesToFieldsMixin, CleanDateOfBirthMixin, LocalizedFirstLastNameMixin, forms.ModelForm): class Meta: model = User fields = ('date_of_birth', 'photo', 'slug', 'gender') def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields[ 'date_of_birth'].input_formats = django_settings.DATE_FIELD_FORMATS self.fields[ 'date_of_birth'].widget.format = django_settings.DEFAULT_DATE_FIELD_FORMAT self.fields['slug'].label = pgettext_lazy( context=self.instance.get_gender(), message='username (slug)') self.helper = FormHelperWithDefaults() # split into two columns field_names = list(self.fields.keys()) self.helper.add_layout( Div(*[ Row(*[Div(field, css_class='col-md-6') for field in pair]) for pair in zip_longest(field_names[::2], field_names[1::2]) ]), ) self.helper.add_input( Submit( 'submit', pgettext_lazy(context=self.instance.get_gender(), message='Save Changes'))) def clean_slug(self): slug = self.cleaned_data.get('slug') username = self.instance.username if (not (normalize_username(username=slug) == username)): raise ValidationError( pgettext_lazy(context=self.instance.get_gender(), message="You can't change your username.")) return slug def save(self, commit=True): if (commit): user = User.objects.get(pk=self.instance.pk) if (not (self.instance.date_of_birth == user.date_of_birth)): site = Site.objects.get_current() logger.warning( 'User changed date of birth on {site_name}, user={user}, new date of birth={new_date_of_birth}, old date of birth={old_date_of_birth}' .format(site_name=_(site.name), user=self.instance, new_date_of_birth=self.instance.date_of_birth, old_date_of_birth=user.date_of_birth)) if (not (self.instance.gender == user.gender)): site = Site.objects.get_current() logger.warning( 'User changed gender on {site_name}, user={user}, new gender={new_gender}, old gender={old_gender}' .format(site_name=_(site.name), user=self.instance, new_gender=self.instance.gender, old_gender=user.gender)) if (not (self.instance.username == user.username)): # Error - users can't change their username. site = Site.objects.get_current() logger.error( 'User changed username on {site_name}, user={user}, new username={new_username}, old username={old_username}' .format(site_name=_(site.name), user=self.instance, new_username=self.instance.username, old_username=user.username)) return super().save(commit=commit)
class ProfileForm(AddAttributesToFieldsMixin, CleanDateOfBirthMixin, LocalizedFirstLastNameMixin, forms.ModelForm): profile_picture = forms.ImageField( required=False, widget=CustomPhotoWidget, label=_('Update your profile picture'), error_messages={'required': _("A profile picture is required.")}) class Meta: model = User fields = ('slug', 'gender', 'date_of_birth', 'profile_picture') def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['slug'].label = pgettext_lazy( context=self.instance.get_gender(), message='username (slug)') self.fields[ 'date_of_birth'].input_formats = django_settings.DATE_FIELD_FORMATS self.fields[ 'date_of_birth'].widget.format = django_settings.DEFAULT_DATE_FIELD_FORMAT self.fields['profile_picture'].widget.attrs['user'] = self.instance self.fields['profile_picture'].label = pgettext_lazy( context=self.instance.get_gender(), message='Update your profile picture') self.helper = FormHelperWithDefaults() # split into two columns field_names = list(self.fields.keys()) self.helper.add_layout( Div(*[ Row(*[Div(field, css_class='col-md-6') for field in pair]) for pair in self.get_field_pairs() ]), ) self.helper.add_input( Submit( 'submit', pgettext_lazy(context=self.instance.get_gender(), message='Save Changes'))) def get_field_pairs(self): return ((to_attribute(name='first_name'), to_attribute(name='last_name')), ('slug', ), ('gender', 'date_of_birth'), ('profile_picture', )) def clean_profile_picture(self): profile_picture = self.files.get('profile_picture') if (profile_picture): user_image = Image(owner=self.instance, file=profile_picture) user_image.save() self.instance._new_profile_picture = user_image speedy_core_base_validators.validate_image_file_extension( profile_picture) speedy_core_accounts_validators.validate_profile_picture_for_user( user=self.instance, profile_picture=profile_picture, test_new_profile_picture=True) else: if (self.instance.photo): profile_picture = self.instance.photo speedy_core_accounts_validators.validate_profile_picture_for_user( user=self.instance, profile_picture=profile_picture, test_new_profile_picture=False) return self.cleaned_data.get('profile_picture') def clean_slug(self): slug = self.cleaned_data.get('slug') username = self.instance.username if (not (normalize_username(username=slug) == username)): raise ValidationError( pgettext_lazy(context=self.instance.get_gender(), message="You can't change your username.")) return slug def save(self, commit=True): if (commit): if ('profile_picture' in self.fields): profile_picture = self.files.get('profile_picture') if (profile_picture): self.instance.photo = self.instance._new_profile_picture if (self.instance.speedy_match_profile): self.instance.speedy_match_profile.profile_picture_months_offset = 5 self.instance.speedy_match_profile.save() user = User.objects.get(pk=self.instance.pk) if (not (self.instance.date_of_birth == user.date_of_birth)): site = Site.objects.get_current() logger.warning( 'User changed date of birth on {site_name}, user={user}, new date of birth={new_date_of_birth}, old date of birth={old_date_of_birth} (registered {registered_days_ago} days ago)' .format( site_name=_(site.name), user=self.instance, new_date_of_birth=self.instance.date_of_birth, old_date_of_birth=user.date_of_birth, registered_days_ago=(now() - self.instance.date_created).days, )) if (not (self.instance.gender == user.gender)): site = Site.objects.get_current() logger.warning( 'User changed gender on {site_name}, user={user}, new gender={new_gender}, old gender={old_gender} (registered {registered_days_ago} days ago)' .format( site_name=_(site.name), user=self.instance, new_gender=self.instance.gender, old_gender=user.gender, registered_days_ago=(now() - self.instance.date_created).days, )) if (not (self.instance.username == user.username)): # Error - users can't change their username. site = Site.objects.get_current() logger.error( 'User changed username on {site_name}, user={user}, new username={new_username}, old username={old_username} (registered {registered_days_ago} days ago)' .format( site_name=_(site.name), user=self.instance, new_username=self.instance.username, old_username=user.username, registered_days_ago=(now() - self.instance.date_created).days, )) return super().save(commit=commit)