Exemple #1
0
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()
Exemple #2
0
class RegistrationForm(AddAttributesToFieldsMixin, CleanEmailMixin, CleanNewPasswordMixin, CleanDateOfBirthMixin, LocalizedFirstLastNameMixin, forms.ModelForm):
    email = forms.EmailField(label=_('Your email'))
    new_password1 = forms.CharField(label=_("New password"), strip=False, widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = ('email', 'slug', 'new_password1', 'gender', 'date_of_birth')

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['slug'].label = _('New username')
        self.fields['date_of_birth'].input_formats = django_settings.DATE_FIELD_FORMATS
        self.helper = FormHelperWithDefaults()
        self.helper.add_input(Submit('submit', _('Create an account'), css_class='btn-lg btn-arrow-right'))

    def save(self, commit=True):
        user = super().save(commit=False)
        user.set_password(raw_password=self.cleaned_data["new_password1"])
        for language_code, language_name in django_settings.LANGUAGES:
            for loc_field in self.get_localizable_fields():
                setattr(user, self.get_localized_field(base_field_name=loc_field, language_code=language_code), self.cleaned_data[self.get_localized_field(base_field_name=loc_field, language_code=self.language_code)])
        if (commit):
            user.save()
            user.email_addresses.create(email=self.cleaned_data['email'], is_confirmed=False, is_primary=True)
        return user
Exemple #3
0
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')))
Exemple #4
0
class ProfileNotificationsForm(forms.ModelForm):
    _profile_model = get_site_profile_model(profile_model=None)
    _profile_fields = ()

    class Meta:
        model = User
        fields = ('notify_on_message',)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for field in self._profile_model._meta.fields:
            if (field.name in self._profile_fields):
                self.fields[field.name] = field.formfield()
                self.fields[field.name].initial = getattr(self.instance.profile, field.name)
        self.helper = FormHelperWithDefaults()
        self.helper.add_input(Submit('submit', pgettext_lazy(context=self.instance.get_gender(), message='Save Changes')))

    def save(self, commit=True):
        for field_name in self.fields.keys():
            if (field_name in self._profile_fields):
                setattr(self.instance.profile, field_name, self.cleaned_data[field_name])
        r = super().save(commit=commit)
        if (commit):
            self.instance.profile.save()
        return r
Exemple #5
0
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
Exemple #6
0
 def helper(self):
     helper = FormHelperWithDefaults()
     helper.add_input(
         Submit(
             'submit',
             pgettext_lazy(context=self.defaults['user'].get_gender(),
                           message='Add')))
     return helper
Exemple #7
0
 def helper(self):
     helper = FormHelperWithDefaults()
     helper.add_input(
         Submit(
             'submit',
             pgettext_lazy(context=self.user.get_gender(),
                           message='Change Password')))
     return helper
Exemple #8
0
class ProfilePrivacyForm(forms.ModelForm):
    class Meta:
        fields = ('access_dob_day_month', 'access_dob_year')
        model = User

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelperWithDefaults()
        self.helper.add_input(Submit('submit', pgettext_lazy(context=self.instance.get_gender(), message='Save Changes')))
Exemple #9
0
class PasswordChangeForm(AddAttributesToFieldsMixin, CleanNewPasswordMixin,
                         auth_forms.PasswordChangeForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelperWithDefaults()
        self.helper.add_input(Hidden('_form', 'password'))
        self.helper.add_input(
            Submit(
                'submit',
                pgettext_lazy(context=self.user.get_gender(),
                              message='Change Password')))
Exemple #10
0
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')))
Exemple #11
0
class SiteProfileDeactivationForm(AddAttributesToFieldsMixin, forms.Form):
    password = forms.CharField(label=_('Your password'), strip=False, widget=forms.PasswordInput)

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user')
        super().__init__(*args, **kwargs)
        site = Site.objects.get_current()
        self.helper = FormHelperWithDefaults()
        self.helper.add_input(Submit('submit', pgettext_lazy(context=self.user.get_gender(), message='Deactivate your {site_name} account').format(site_name=_(site.name)), css_class='btn-danger'))

    def clean_password(self):
        password = self.cleaned_data['password']
        if (not (self.user.check_password(raw_password=password))):
            raise ValidationError(_('Invalid password.'))
        return password
Exemple #12
0
class SiteProfileActivationForm(forms.ModelForm):
    class Meta:
        model = get_site_profile_model(profile_model=None)
        fields = ()

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        site = Site.objects.get_current()
        self.helper = FormHelperWithDefaults()
        self.helper.add_input(Submit('submit', pgettext_lazy(context=self.instance.user.get_gender(), message='Activate your {site_name} account').format(site_name=_(site.name))))

    def save(self, commit=True):
        if (commit):
            self.instance.activate()
        return super().save(commit=commit)
Exemple #13
0
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
Exemple #14
0
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')))
Exemple #15
0
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)
Exemple #16
0
 def helper(self):
     helper = FormHelperWithDefaults()
     helper.add_input(Submit('submit', pgettext_lazy(context=self.defaults['user'].get_gender(), message='Add')))
     return helper
Exemple #17
0
class PasswordChangeForm(AddAttributesToFieldsMixin, CleanNewPasswordMixin, auth_forms.PasswordChangeForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelperWithDefaults()
        self.helper.add_input(Hidden('_form', 'password'))
        self.helper.add_input(Submit('submit', _('Change')))
Exemple #18
0
 def helper(self):
     helper = FormHelperWithDefaults()
     helper.add_input(Submit('submit', _('Submit')))
     return helper
Exemple #19
0
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)