def validators(cls): validators = { 'username': speedy_core_accounts_validators.get_username_validators( min_username_length=cls.settings.MIN_USERNAME_LENGTH, max_username_length=cls.settings.MAX_USERNAME_LENGTH, allow_letters_after_digits=False), 'slug': speedy_core_accounts_validators.get_slug_validators( min_username_length=cls.settings.MIN_USERNAME_LENGTH, max_username_length=cls.settings.MAX_USERNAME_LENGTH, min_slug_length=cls.settings.MIN_SLUG_LENGTH, max_slug_length=cls.settings.MAX_SLUG_LENGTH, allow_letters_after_digits=False) + ["validate_slug"], 'date_of_birth': [speedy_core_accounts_validators.validate_date_of_birth_in_model], **{ to_attribute(name='first_name', language_code=language_code): [ speedy_core_accounts_validators.validate_first_name_in_model ] for language_code, language_name in django_settings.LANGUAGES }, **{ to_attribute(name='last_name', language_code=language_code): [ speedy_core_accounts_validators.validate_last_name_in_model ] for language_code, language_name in django_settings.LANGUAGES }, } return validators
def validate_field(field_name, user): if (field_name in ['profile_picture']): speedy_core_accounts_validators.validate_profile_picture( profile_picture=user.photo) elif (field_name in ['profile_description', to_attribute(name='profile_description')]): speedy_match_accounts_validators.validate_profile_description( profile_description=user.speedy_match_profile.profile_description) elif (field_name in ['city', to_attribute(name='city')]): speedy_match_accounts_validators.validate_city(city=user.city) elif (field_name in ['children', to_attribute(name='children')]): speedy_match_accounts_validators.validate_children( children=user.speedy_match_profile.children) elif (field_name in ['more_children', to_attribute(name='more_children')]): speedy_match_accounts_validators.validate_more_children( more_children=user.speedy_match_profile.more_children) elif (field_name in ['match_description', to_attribute(name='match_description')]): speedy_match_accounts_validators.validate_match_description( match_description=user.speedy_match_profile.match_description) elif (field_name in ['height']): speedy_match_accounts_validators.validate_height( height=user.speedy_match_profile.height) elif (field_name in ['diet']): speedy_match_accounts_validators.validate_diet(diet=user.diet) elif (field_name in ['smoking_status']): speedy_match_accounts_validators.validate_smoking_status( smoking_status=user.smoking_status) elif (field_name in ['relationship_status']): speedy_match_accounts_validators.validate_relationship_status( relationship_status=user.relationship_status) elif (field_name in ['gender_to_match']): speedy_match_accounts_validators.validate_gender_to_match( gender_to_match=user.speedy_match_profile.gender_to_match) elif (field_name in ['min_age_to_match']): speedy_match_accounts_validators.validate_min_age_to_match( min_age_to_match=user.speedy_match_profile.min_age_to_match) elif (field_name in ['max_age_to_match']): speedy_match_accounts_validators.validate_max_age_to_match( max_age_to_match=user.speedy_match_profile.max_age_to_match) elif (field_name in ['min_max_age_to_match']): speedy_match_accounts_validators.validate_min_max_age_to_match( min_age_to_match=user.speedy_match_profile.min_age_to_match, max_age_to_match=user.speedy_match_profile.max_age_to_match) elif (field_name in ['diet_match']): speedy_match_accounts_validators.validate_diet_match( diet_match=user.speedy_match_profile.diet_match) elif (field_name in ['smoking_status_match']): speedy_match_accounts_validators.validate_smoking_status_match( smoking_status_match=user.speedy_match_profile.smoking_status_match ) elif (field_name in ['relationship_status_match']): speedy_match_accounts_validators.validate_relationship_status_match( relationship_status_match=user.speedy_match_profile. relationship_status_match) else: raise Exception("Unexpected: field_name={}".format(field_name))
def set_up_required_fields(self): self.required_fields = self.data.keys() - { to_attribute(name="last_name", language_code=self.language_code) } self.assert_registration_form_required_fields( required_fields=self.required_fields)
def get_step_form_fields(step): form_fields = [] for field_name in list(SpeedyMatchSiteProfile.settings.SPEEDY_MATCH_SITE_PROFILE_FORM_FIELDS[step]): if (not (field_name in SpeedyMatchSiteProfile.LOCALIZABLE_FIELDS)): form_fields.append(field_name) else: form_fields.append(to_attribute(name=field_name)) # print(form_fields) # ~~~~ TODO: remove this line! return form_fields
def deactivate(self): self._set_active_languages(languages=[]) self.activation_step = 2 for language_code, language_name in django_settings.LANGUAGES: setattr( self, to_attribute(name='activation_step', language_code=language_code), 2) self.user.save_user_and_profile()
def get_step_form_fields(step): form_fields = [] for field_name in list(SpeedyMatchSiteProfile.settings. SPEEDY_MATCH_SITE_PROFILE_FORM_FIELDS[step]): if (not (field_name in (User.LOCALIZABLE_FIELDS + SpeedyMatchSiteProfile.LOCALIZABLE_FIELDS))): form_fields.append(field_name) else: form_fields.append(to_attribute(name=field_name)) return form_fields
def get_queryset(self): SiteProfile = get_site_profile_model() if (self.request.GET.get('order_by') == 'number_of_friends'): qs = User.objects.all().order_by( F('{}__{}'.format( SpeedyNetSiteProfile.RELATED_NAME, to_attribute(name='number_of_friends'))).desc( nulls_last=True), '-{}__last_visit'.format(SiteProfile.RELATED_NAME)) else: qs = User.objects.all().order_by('-{}__last_visit'.format( SiteProfile.RELATED_NAME)) return qs
def validate_field(field_name, user): if (field_name in ['photo']): validators.validate_photo(photo=user.photo) elif (field_name in ['profile_description', to_attribute(name='profile_description')]): validators.validate_profile_description(profile_description=user.speedy_match_profile.profile_description) elif (field_name in ['city', to_attribute(name='city')]): validators.validate_city(city=user.speedy_match_profile.city) elif (field_name in ['children', to_attribute(name='children')]): validators.validate_children(children=user.speedy_match_profile.children) elif (field_name in ['more_children', to_attribute(name='more_children')]): validators.validate_more_children(more_children=user.speedy_match_profile.more_children) elif (field_name in ['match_description', to_attribute(name='match_description')]): validators.validate_match_description(match_description=user.speedy_match_profile.match_description) elif (field_name in ['height']): validators.validate_height(height=user.speedy_match_profile.height) elif (field_name in ['diet']): validators.validate_diet(diet=user.diet) elif (field_name in ['smoking_status']): validators.validate_smoking_status(smoking_status=user.speedy_match_profile.smoking_status) elif (field_name in ['marital_status']): validators.validate_marital_status(marital_status=user.speedy_match_profile.marital_status) elif (field_name in ['gender_to_match']): validators.validate_gender_to_match(gender_to_match=user.speedy_match_profile.gender_to_match) elif (field_name in ['min_age_match']): validators.validate_min_age_match(min_age_match=user.speedy_match_profile.min_age_match) elif (field_name in ['max_age_match']): validators.validate_max_age_match(max_age_match=user.speedy_match_profile.max_age_match) elif (field_name in ['min_max_age_to_match']): validators.validate_min_max_age_to_match(min_age_match=user.speedy_match_profile.min_age_match, max_age_match=user.speedy_match_profile.max_age_match) elif (field_name in ['diet_match']): validators.validate_diet_match(diet_match=user.speedy_match_profile.diet_match) elif (field_name in ['smoking_status_match']): validators.validate_smoking_status_match(smoking_status_match=user.speedy_match_profile.smoking_status_match) elif (field_name in ['marital_status_match']): validators.validate_marital_status_match(marital_status_match=user.speedy_match_profile.marital_status_match) else: raise Exception("Unexpected: field_name={}".format(field_name))
def assert_user_first_and_last_name_in_all_languages(self, user): self.assertTupleEqual(tuple1=User.NAME_LOCALIZABLE_FIELDS, tuple2=('first_name', 'last_name')) self.assertEqual(first=user.first_name_en, second=user.first_name) self.assertEqual(first=user.first_name_he, second=user.first_name) self.assertEqual(first=user.last_name_en, second=user.last_name) self.assertEqual(first=user.last_name_he, second=user.last_name) field_name_localized_list = list() for base_field_name in User.NAME_LOCALIZABLE_FIELDS: for language_code, language_name in django_settings.LANGUAGES: field_name_localized = to_attribute(name=base_field_name, language_code=language_code) self.assertEqual(first=getattr(user, field_name_localized), second=getattr(user, base_field_name), msg="assert_user_first_and_last_name_in_all_languages::fields don't match ({field_name_localized}, {base_field_name}), user.pk={user_pk}, user.username={user_username}, user.slug={user_slug}, user.name={user_name}".format( field_name_localized=field_name_localized, base_field_name=base_field_name, user_pk=user.pk, user_username=user.username, user_slug=user.slug, user_name=user.name, )) field_name_localized_list.append(field_name_localized) self.assertListEqual(list1=field_name_localized_list, list2=['first_name_en', 'first_name_he', 'last_name_en', 'last_name_he'])
def set_up(self): super().set_up() # _this_field_is_required_error_message_dict = {'en': '___This field is required.', 'he': 'יש להזין תוכן בשדה זה.___'} # ~~~~ TODO: remove this line! _this_field_cannot_be_null_error_message_dict = {'en': 'This field cannot be null.', 'he': 'שדה זה אינו יכול להיות ריק.'} _this_field_cannot_be_blank_error_message_dict = {'en': 'This field cannot be blank.', 'he': 'שדה זה אינו יכול להיות ריק.'} _id_contains_illegal_characters_error_message_dict = {'en': 'id contains illegal characters.', 'he': 'id מכיל תווים לא חוקיים.'} _value_must_be_valid_json_error_message_dict = {'en': 'Value must be valid JSON.', 'he': 'ערך חייב להיות JSON חוקי.'} _invalid_password_error_message_dict = {'en': 'Invalid password.', 'he': 'הסיסמה לא תקינה.'} _password_too_short_error_message_dict = {'en': 'This password is too short. It must contain at least 8 characters.', 'he': 'סיסמה זו קצרה מדי. היא חייבת להכיל לפחות 8 תווים.'} _password_too_long_error_message_dict = {'en': 'This password is too long. It must contain at most 120 characters.', 'he': 'סיסמה זו ארוכה מדי. היא יכולה להכיל 120 תווים לכל היותר.'} _this_username_is_already_taken_error_message_dict = {'en': 'This username is already taken.', 'he': 'שם המשתמש/ת הזה כבר תפוס.'} _enter_a_valid_email_address_error_message_dict = {'en': 'Enter a valid email address.', 'he': 'נא להזין כתובת דואר אלקטרוני חוקית.'} _this_email_is_already_in_use_error_message_dict = {'en': 'This email is already in use.', 'he': 'הדואר האלקטרוני הזה כבר נמצא בשימוש.'} _enter_a_valid_date_error_message_dict = {'en': 'Enter a valid date.', 'he': 'יש להזין תאריך חוקי.'} _please_enter_a_correct_username_and_password_error_message_dict = {'en': 'Please enter a correct username and password. Note that both fields may be case-sensitive.', 'he': 'נא להזין שם משתמש/ת וסיסמה נכונים. נא לשים לב כי שני השדות רגישים לאותיות גדולות/קטנות.'} _your_old_password_was_entered_incorrectly_error_message_dict = {'en': 'Your old password was entered incorrectly. Please enter it again.', 'he': 'סיסמתך הישנה הוזנה בצורה שגויה. נא להזינה שוב.'} _the_two_password_fields_didnt_match_error_message_dict = {'en': "The two password fields didn't match.", 'he': 'שני שדות הסיסמה אינם זהים.'} _entity_username_must_start_with_4_or_more_letters_error_message_dict = {'en': 'Username must start with 4 or more letters, and may contain letters, digits or dashes.', 'he': 'שם המשתמש/ת חייב להתחיל עם 4 אותיות או יותר, ויכול להכיל אותיות, ספרות או מקפים.'} _user_username_must_start_with_4_or_more_letters_error_message_dict = {'en': 'Username must start with 4 or more letters, after which can be any number of digits. You can add dashes between words.', 'he': 'שם המשתמש/ת חייב להתחיל עם 4 אותיות או יותר, לאחר מכן ניתן להוסיף מספר כלשהו של ספרות. ניתן להוסיף מקפים בין מילים.'} _slug_does_not_parse_to_username_error_message_dict = {'en': 'Slug does not parse to username.', 'he': 'slug לא מתאים לשם המשתמש/ת.'} _youve_already_confirmed_this_email_address_error_message_dict = {'en': "You've already confirmed this email address.", 'he': 'כבר אימתת את כתובת הדואר האלקטרוני שלך.'} _youve_confirmed_your_email_address_error_message_dict = {'en': "You've confirmed your email address.", 'he': 'אימתת את כתובת הדואר האלקטרוני שלך.'} _the_email_address_was_deleted_error_message_dict = {'en': 'The email address was deleted.', 'he': 'כתובת הדואר האלקטרוני נמחקה.'} _you_have_changed_your_primary_email_address_error_message_dict = {'en': 'You have made this email address primary.', 'he': 'הפכת את כתובת הדואר האלקטרוני הזאת לראשית.'} _value_is_not_a_valid_choice_error_message_to_format_dict = {'en': 'Value {value} is not a valid choice.', 'he': 'ערך {value} אינו אפשרות חוקית.'} _value_must_be_an_integer_error_message_to_format_dict = {'en': "'{value}' value must be an integer.", 'he': "הערך '{value}' חייב להיות מספר שלם."} _list_contains_items_it_should_contain_no_more_than_3_error_message_to_format_dict = {'en': 'List contains {list_length} items, it should contain no more than 3.', 'he': 'הרשימה מכילה {list_length} פריטים, עליה להכיל לא יותר מ-3.'} # _ensure_this_value_has_at_least_min_length_characters_error_message_to_format_dict = {'en': 'Ensure this value has at least {min_length} characters (it has {value_length}).', 'he': '_____ # ~~~~ TODO'} # ~~~~ TODO # _ensure_this_value_has_at_most_max_length_characters_error_message_to_format_dict = {'en': 'Ensure this value has at most {max_length} characters (it has {value_length}).', 'he': '_____ # ~~~~ TODO'} # ~~~~ TODO _username_must_contain_at_least_min_length_alphanumeric_characters_error_message_to_format_dict = {'en': 'Username must contain at least {min_length} alphanumeric characters (it has {value_length}).', 'he': 'נא לוודא ששם המשתמש/ת מכיל {min_length} תווים אלפאנומריים לפחות (מכיל {value_length}).'} _username_must_contain_at_most_max_length_alphanumeric_characters_error_message_to_format_dict = {'en': 'Username must contain at most {max_length} alphanumeric characters (it has {value_length}).', 'he': 'נא לוודא ששם המשתמש/ת מכיל {max_length} תווים אלפאנומריים לכל היותר (מכיל {value_length}).'} _username_must_contain_at_least_min_length_characters_error_message_to_format_dict = {'en': 'Username must contain at least {min_length} characters (it has {value_length}).', 'he': 'נא לוודא ששם המשתמש/ת מכיל {min_length} תווים לפחות (מכיל {value_length}).'} _username_must_contain_at_most_max_length_characters_error_message_to_format_dict = {'en': 'Username must contain at most {max_length} characters (it has {value_length}).', 'he': 'נא לוודא ששם המשתמש/ת מכיל {max_length} תווים לכל היותר (מכיל {value_length}).'} _a_confirmation_message_was_sent_to_email_address_error_message_to_format_dict = {'en': 'A confirmation message was sent to {email_address}', 'he': 'הודעת אימות נשלחה ל-{email_address}'} _you_cant_change_your_username_error_message_dict_by_gender = { 'en': {gender: "You can't change your username." for gender in User.ALL_GENDERS}, 'he': { User.GENDER_FEMALE_STRING: "לא ניתן לשנות שם משתמשת.", User.GENDER_MALE_STRING: "לא ניתן לשנות שם משתמש.", User.GENDER_OTHER_STRING: "לא ניתן לשנות שם משתמש/ת.", }, } # self._this_field_is_required_error_message = _this_field_is_required_error_message_dict[self.language_code] # ~~~~ TODO: remove this line! self._this_field_cannot_be_null_error_message = _this_field_cannot_be_null_error_message_dict[self.language_code] self._this_field_cannot_be_blank_error_message = _this_field_cannot_be_blank_error_message_dict[self.language_code] self._id_contains_illegal_characters_error_message = _id_contains_illegal_characters_error_message_dict[self.language_code] self._value_must_be_valid_json_error_message = _value_must_be_valid_json_error_message_dict[self.language_code] self._invalid_password_error_message = _invalid_password_error_message_dict[self.language_code] self._password_too_short_error_message = _password_too_short_error_message_dict[self.language_code] self._password_too_long_error_message = _password_too_long_error_message_dict[self.language_code] self._this_username_is_already_taken_error_message = _this_username_is_already_taken_error_message_dict[self.language_code] self._enter_a_valid_email_address_error_message = _enter_a_valid_email_address_error_message_dict[self.language_code] self._this_email_is_already_in_use_error_message = _this_email_is_already_in_use_error_message_dict[self.language_code] self._enter_a_valid_date_error_message = _enter_a_valid_date_error_message_dict[self.language_code] self._please_enter_a_correct_username_and_password_error_message = _please_enter_a_correct_username_and_password_error_message_dict[self.language_code] self._your_old_password_was_entered_incorrectly_error_message = _your_old_password_was_entered_incorrectly_error_message_dict[self.language_code] self._the_two_password_fields_didnt_match_error_message = _the_two_password_fields_didnt_match_error_message_dict[self.language_code] self._entity_username_must_start_with_4_or_more_letters_error_message = _entity_username_must_start_with_4_or_more_letters_error_message_dict[self.language_code] self._user_username_must_start_with_4_or_more_letters_error_message = _user_username_must_start_with_4_or_more_letters_error_message_dict[self.language_code] self._slug_does_not_parse_to_username_error_message = _slug_does_not_parse_to_username_error_message_dict[self.language_code] self._youve_already_confirmed_this_email_address_error_message = _youve_already_confirmed_this_email_address_error_message_dict[self.language_code] self._youve_confirmed_your_email_address_error_message = _youve_confirmed_your_email_address_error_message_dict[self.language_code] self._the_email_address_was_deleted_error_message = _the_email_address_was_deleted_error_message_dict[self.language_code] self._you_have_changed_your_primary_email_address_error_message = _you_have_changed_your_primary_email_address_error_message_dict[self.language_code] self._value_is_not_a_valid_choice_error_message_to_format = _value_is_not_a_valid_choice_error_message_to_format_dict[self.language_code] self._value_must_be_an_integer_error_message_to_format = _value_must_be_an_integer_error_message_to_format_dict[self.language_code] self._list_contains_items_it_should_contain_no_more_than_3_error_message_to_format = _list_contains_items_it_should_contain_no_more_than_3_error_message_to_format_dict[self.language_code] # self._ensure_this_value_has_at_least_min_length_characters_error_message_to_format = _ensure_this_value_has_at_least_min_length_characters_error_message_to_format_dict[self.language_code] # self._ensure_this_value_has_at_most_max_length_characters_error_message_to_format = _ensure_this_value_has_at_most_max_length_characters_error_message_to_format_dict[self.language_code] self._username_must_contain_at_least_min_length_alphanumeric_characters_error_message_to_format = _username_must_contain_at_least_min_length_alphanumeric_characters_error_message_to_format_dict[self.language_code] self._username_must_contain_at_most_max_length_alphanumeric_characters_error_message_to_format = _username_must_contain_at_most_max_length_alphanumeric_characters_error_message_to_format_dict[self.language_code] self._username_must_contain_at_least_min_length_characters_error_message_to_format = _username_must_contain_at_least_min_length_characters_error_message_to_format_dict[self.language_code] self._username_must_contain_at_most_max_length_characters_error_message_to_format = _username_must_contain_at_most_max_length_characters_error_message_to_format_dict[self.language_code] self._a_confirmation_message_was_sent_to_email_address_error_message_to_format = _a_confirmation_message_was_sent_to_email_address_error_message_to_format_dict[self.language_code] self._you_cant_change_your_username_error_message_dict_by_gender = _you_cant_change_your_username_error_message_dict_by_gender[self.language_code] self.assertSetEqual(set1=set(self._you_cant_change_your_username_error_message_dict_by_gender.keys()), set2=set(User.ALL_GENDERS)) self.assertNotEqual(first=set(self._cannot_create_user_without_all_the_required_fields_errors_dict_by_value(value=None).keys()), second=set(self._user_all_the_required_fields_keys())) self.assertEqual(first=len(set(self._cannot_create_user_without_all_the_required_fields_errors_dict_by_value(value=None).keys())), second=9) self.assertEqual(first=len(set(self._user_all_the_required_fields_keys())), second=7) self.assertEqual(first=len(set(self._cannot_create_user_without_all_the_required_fields_errors_dict_by_value(value=None).keys()) - set(self._user_all_the_required_fields_keys())), second=2) self.assertSetEqual(set1=set(self._cannot_create_user_without_all_the_required_fields_errors_dict_by_value(value=None).keys()), set2=set(self._user_all_the_required_fields_keys()) | {'first_name_en', 'first_name_he', 'last_name_en', 'last_name_he'}) self.assertListEqual(list1=self._profile_form_all_the_required_fields_keys(), list2=[field_name for field_name in self._registration_form_all_the_required_fields_keys() if (not (field_name in ['email', 'new_password1']))]) self.assertSetEqual(set1=set(self._registration_form_all_the_required_fields_keys()) - {'email', 'new_password1'}, set2=set(self._profile_form_all_the_required_fields_keys())) self.assertSetEqual(set1=set(self._profile_form_all_the_required_fields_keys()) | {'email', 'new_password1'}, set2=set(self._registration_form_all_the_required_fields_keys())) self.assertNotEqual(first=[to_attribute(name='first_name'), to_attribute(name='last_name')], second=['first_name', 'last_name']) self.assertListEqual(list1=self._user_all_the_required_fields_keys()[:2], list2=[to_attribute(name='first_name'), to_attribute(name='last_name')]) self.assertListEqual(list1=self._registration_form_all_the_required_fields_keys()[:2], list2=[to_attribute(name='first_name'), to_attribute(name='last_name')]) self.assertListEqual(list1=self._profile_form_all_the_required_fields_keys()[:2], list2=[to_attribute(name='first_name'), to_attribute(name='last_name')])
def get_fields(self): return (to_attribute(name='profile_description'), to_attribute(name='city'), 'height', to_attribute(name='children'), to_attribute(name='more_children'), 'diet', 'smoking_status', 'marital_status')
def __init__(self, *args, **kwargs): self.step = kwargs.pop('step', None) super().__init__(*args, **kwargs) # Create the localized city field dynamically. self.fields[to_attribute(name='city')] = forms.CharField(label=_('Where do you live?'), max_length=120, error_messages={'required': _("Please write where you live.")}) # Delete unneeded fields from the form. self.delete_unneeded_fields() # Update fields attributes according to the user's gender and language. if ('profile_picture' in self.fields): self.fields['profile_picture'].widget.attrs['user'] = self.instance.user self.fields['profile_picture'].label = pgettext_lazy(context=self.instance.user.get_gender(), message='Add profile picture') if ('height' in self.fields): self.fields['height'].label = pgettext_lazy(context=self.instance.user.get_gender(), message='My height in centimeters') if ('diet' in self.fields): update_form_field_choices(field=self.fields['diet'], choices=self.instance.user.get_diet_choices_with_description()) if ('smoking_status' in self.fields): update_form_field_choices(field=self.fields['smoking_status'], choices=self.instance.user.get_smoking_status_choices()) if ('relationship_status' in self.fields): update_form_field_choices(field=self.fields['relationship_status'], choices=self.instance.user.get_relationship_status_choices()) if ('diet_match' in self.fields): update_form_field_choices(field=self.fields['diet_match'], choices=self.instance.get_diet_match_choices()) if ('smoking_status_match' in self.fields): update_form_field_choices(field=self.fields['smoking_status_match'], choices=self.instance.get_smoking_status_match_choices()) if ('relationship_status_match' in self.fields): update_form_field_choices(field=self.fields['relationship_status_match'], choices=self.instance.get_relationship_status_match_choices()) if (to_attribute(name='profile_description') in self.fields): self.fields[to_attribute(name='profile_description')].error_messages = {'required': pgettext_lazy(context=self.instance.user.get_gender(), message="Please write a few words about yourself.")} if (to_attribute(name='city') in self.fields): self.fields[to_attribute(name='city')].label = pgettext_lazy(context=self.instance.user.get_gender(), message='Where do you live?') self.fields[to_attribute(name='city')].error_messages = {'required': pgettext_lazy(context=self.instance.user.get_gender(), message="Please write where you live.")} if (to_attribute(name='children') in self.fields): self.fields[to_attribute(name='children')].label = pgettext_lazy(context=self.instance.user.get_gender(), message='Do you have children? How many?') self.fields[to_attribute(name='children')].error_messages = {'required': pgettext_lazy(context=self.instance.user.get_gender(), message="Do you have children? How many?")} if (to_attribute(name='more_children') in self.fields): self.fields[to_attribute(name='more_children')].label = pgettext_lazy(context=self.instance.user.get_gender(), message='Do you want (more) children?') self.fields[to_attribute(name='more_children')].error_messages = {'required': pgettext_lazy(context=self.instance.user.get_gender(), message="Do you want (more) children?")} if (to_attribute(name='match_description') in self.fields): self.fields[to_attribute(name='match_description')].label = pgettext_lazy(context=self.instance.get_match_gender(), message='My ideal match') self.fields[to_attribute(name='match_description')].error_messages = {'required': pgettext_lazy(context=self.instance.get_match_gender(), message="Who is your ideal partner?")} if ('gender_to_match' in self.fields): self.fields['gender_to_match'].label = _('Gender to match') if ('min_age_to_match' in self.fields): self.fields['min_age_to_match'].label = pgettext_lazy(context=self.instance.get_match_gender(), message='Minimal age to match') if ('max_age_to_match' in self.fields): self.fields['max_age_to_match'].label = pgettext_lazy(context=self.instance.get_match_gender(), message='Maximal age to match') # Update the initial value of fields from the User model. for field_name in self.user_fields: if (field_name in self.fields): self.fields[field_name].initial = getattr(self.instance.user, field_name) # Update the fields validators. for field_name, field in self.fields.items(): if (field_name in self.validators): field.validators.extend(self.validators[field_name]) field.required = True # Rearrange the fields. self.order_fields(field_order=self.get_fields())
class SpeedyMatchProfileBaseForm(forms.ModelForm): validators = { 'height': [speedy_match_accounts_validators.validate_height], 'smoking_status': [speedy_match_accounts_validators.validate_smoking_status], 'marital_status': [speedy_match_accounts_validators.validate_marital_status], **{ to_attribute(name='profile_description', language_code=language_code): [ speedy_match_accounts_validators.validate_profile_description ] for language_code, language_name in django_settings.LANGUAGES }, **{ to_attribute(name='city', language_code=language_code): [ speedy_match_accounts_validators.validate_city ] for language_code, language_name in django_settings.LANGUAGES }, **{ to_attribute(name='children', language_code=language_code): [ speedy_match_accounts_validators.validate_children ] for language_code, language_name in django_settings.LANGUAGES }, **{ to_attribute(name='more_children', language_code=language_code): [ speedy_match_accounts_validators.validate_more_children ] for language_code, language_name in django_settings.LANGUAGES }, **{ to_attribute(name='match_description', language_code=language_code): [ speedy_match_accounts_validators.validate_match_description ] for language_code, language_name in django_settings.LANGUAGES }, 'gender_to_match': [speedy_match_accounts_validators.validate_gender_to_match], 'min_age_match': [speedy_match_accounts_validators.validate_min_age_match], 'max_age_match': [speedy_match_accounts_validators.validate_max_age_match], 'diet_match': [speedy_match_accounts_validators.validate_diet_match], 'smoking_status_match': [speedy_match_accounts_validators.validate_smoking_status_match], 'marital_status_match': [speedy_match_accounts_validators.validate_marital_status_match], } # ~~~~ TODO: diet choices depend on the current user's gender. Also same for smoking status and marital status. diet = forms.ChoiceField( choices=User.DIET_VALID_CHOICES, widget=forms.RadioSelect(), label=_('My diet'), validators=[speedy_match_accounts_validators.validate_diet]) photo = forms.ImageField(required=False, widget=CustomPhotoWidget, label=_('Add profile picture')) class Meta: model = SpeedyMatchSiteProfile fields = ( 'photo', *(to_attribute(name='profile_description', language_code=language_code) for language_code, language_name in django_settings.LANGUAGES), *(to_attribute(name='city', language_code=language_code) for language_code, language_name in django_settings.LANGUAGES), 'height', *(to_attribute(name='children', language_code=language_code) for language_code, language_name in django_settings.LANGUAGES), *(to_attribute(name='more_children', language_code=language_code) for language_code, language_name in django_settings.LANGUAGES), 'diet', 'smoking_status', 'marital_status', 'gender_to_match', *(to_attribute(name='match_description', language_code=language_code) for language_code, language_name in django_settings.LANGUAGES), 'min_age_match', 'max_age_match', 'diet_match', 'smoking_status_match', 'marital_status_match', ) widgets = { 'smoking_status': forms.RadioSelect(), 'marital_status': forms.RadioSelect(), **{ to_attribute(name='profile_description', language_code=language_code): forms.Textarea(attrs={ 'rows': 3, 'cols': 25 }) for language_code, language_name in django_settings.LANGUAGES }, **{ to_attribute(name='city', language_code=language_code): forms.TextInput( ) for language_code, language_name in django_settings.LANGUAGES }, **{ to_attribute(name='children', language_code=language_code): forms.TextInput( ) for language_code, language_name in django_settings.LANGUAGES }, **{ to_attribute(name='more_children', language_code=language_code): forms.TextInput( ) for language_code, language_name in django_settings.LANGUAGES }, **{ to_attribute(name='match_description', language_code=language_code): forms.Textarea(attrs={ 'rows': 3, 'cols': 25 }) for language_code, language_name in django_settings.LANGUAGES }, 'diet_match': CustomJsonWidget(choices=User.DIET_VALID_CHOICES), 'smoking_status_match': CustomJsonWidget( choices=SpeedyMatchSiteProfile.SMOKING_STATUS_VALID_CHOICES), 'marital_status_match': CustomJsonWidget( choices=SpeedyMatchSiteProfile.MARITAL_STATUS_VALID_CHOICES), } def __init__(self, *args, **kwargs): self.step = kwargs.pop('step', None) super().__init__(*args, **kwargs) fields = self.get_fields() # print(fields) # ~~~~ TODO: remove this line! fields_for_deletion = set(self.fields.keys()) - set(fields) for field_for_deletion in fields_for_deletion: del self.fields[field_for_deletion] if ('gender_to_match' in self.fields): self.fields['gender_to_match'] = forms.MultipleChoiceField( choices=User.GENDER_CHOICES, widget=forms.CheckboxSelectMultiple) if ('photo' in self.fields): self.fields['photo'].widget.attrs['user'] = self.instance.user if ('diet' in self.fields): self.fields[ 'diet'].widget.choices = self.instance.user.get_diet_choices() self.fields[ 'diet'].initial = self.instance.user.diet # ~~~~ TODO: diet, smoking_status and marital_status - this line is required if the field is in class User - not in class SpeedyMatchSiteProfile. if ('smoking_status' in self.fields): self.fields[ 'smoking_status'].widget.choices = self.instance.get_smoking_status_choices( ) if ('marital_status' in self.fields): self.fields[ 'marital_status'].widget.choices = self.instance.get_marital_status_choices( ) # ~~~~ TODO: diet match choices gender is the desired match gender - either male, female or other. If more than one gender option is selected, then other. Same is for smoking status and marital status. if ('diet_match' in self.fields): self.fields[ 'diet_match'].widget.choices = self.instance.get_diet_match_choices( ) if ('smoking_status_match' in self.fields): self.fields[ 'smoking_status_match'].widget.choices = self.instance.get_smoking_status_match_choices( ) if ('marital_status_match' in self.fields): self.fields[ 'marital_status_match'].widget.choices = self.instance.get_marital_status_match_choices( ) for field_name, field in self.fields.items(): if (field_name in self.validators): field.validators.extend(self.validators[field_name]) field.required = True def clean_photo(self): photo = self.files.get('photo') if (not (photo)): photo = self.instance.user.photo speedy_match_accounts_validators.validate_photo(photo=photo) return self.cleaned_data def clean_gender_to_match(self): return [int(value) for value in self.cleaned_data['gender_to_match']] def clean(self): if (('min_age_match' in self.fields) and ('max_age_match' in self.fields)): min_age_match = self.cleaned_data.get('min_age_match') max_age_match = self.cleaned_data.get('max_age_match') speedy_match_accounts_validators.validate_min_max_age_to_match( min_age_match=min_age_match, max_age_match=max_age_match) return self.cleaned_data def save(self, commit=True): if ((commit) and ('photo' in self.fields)): if (self.files): user_image = Image(owner=self.instance.user, file=self.files['photo']) user_image.save() self.instance.user.photo = user_image self.instance.user.save() if ((commit) and ('diet' in self.fields)): self.instance.user.diet = self.cleaned_data['diet'] self.instance.user.save() super().save(commit=commit) if (commit): activation_step = self.instance.activation_step step, errors = self.instance.validate_profile_and_activate() self.instance.activation_step = min(activation_step + 1, step) if (self.instance.activation_step >= len(SpeedyMatchSiteProfile.settings. SPEEDY_MATCH_SITE_PROFILE_FORM_FIELDS) - 1): self.instance.activation_step = len( SpeedyMatchSiteProfile.settings. SPEEDY_MATCH_SITE_PROFILE_FORM_FIELDS) self.instance.save(update_fields={'activation_step'}) return self.instance def get_fields(self): # This function is not defined in this base (abstract) form. raise NotImplementedError()
class Meta: model = SpeedyMatchSiteProfile fields = ( 'photo', *(to_attribute(name='profile_description', language_code=language_code) for language_code, language_name in django_settings.LANGUAGES), *(to_attribute(name='city', language_code=language_code) for language_code, language_name in django_settings.LANGUAGES), 'height', *(to_attribute(name='children', language_code=language_code) for language_code, language_name in django_settings.LANGUAGES), *(to_attribute(name='more_children', language_code=language_code) for language_code, language_name in django_settings.LANGUAGES), 'diet', 'smoking_status', 'marital_status', 'gender_to_match', *(to_attribute(name='match_description', language_code=language_code) for language_code, language_name in django_settings.LANGUAGES), 'min_age_match', 'max_age_match', 'diet_match', 'smoking_status_match', 'marital_status_match', ) widgets = { 'smoking_status': forms.RadioSelect(), 'marital_status': forms.RadioSelect(), **{ to_attribute(name='profile_description', language_code=language_code): forms.Textarea(attrs={ 'rows': 3, 'cols': 25 }) for language_code, language_name in django_settings.LANGUAGES }, **{ to_attribute(name='city', language_code=language_code): forms.TextInput( ) for language_code, language_name in django_settings.LANGUAGES }, **{ to_attribute(name='children', language_code=language_code): forms.TextInput( ) for language_code, language_name in django_settings.LANGUAGES }, **{ to_attribute(name='more_children', language_code=language_code): forms.TextInput( ) for language_code, language_name in django_settings.LANGUAGES }, **{ to_attribute(name='match_description', language_code=language_code): forms.Textarea(attrs={ 'rows': 3, 'cols': 25 }) for language_code, language_name in django_settings.LANGUAGES }, 'diet_match': CustomJsonWidget(choices=User.DIET_VALID_CHOICES), 'smoking_status_match': CustomJsonWidget( choices=SpeedyMatchSiteProfile.SMOKING_STATUS_VALID_CHOICES), 'marital_status_match': CustomJsonWidget( choices=SpeedyMatchSiteProfile.MARITAL_STATUS_VALID_CHOICES), }
class SpeedyMatchProfileBaseForm(DeleteUnneededFieldsMixin, forms.ModelForm): # Fields from the User model. user_fields = ( 'diet', 'smoking_status', 'relationship_status', *(to_attribute(name='city', language_code=language_code) for language_code, language_name in django_settings.LANGUAGES), ) # Fields validators. validators = { 'height': [speedy_match_accounts_validators.validate_height], 'diet': [speedy_match_accounts_validators.validate_diet], 'smoking_status': [speedy_match_accounts_validators.validate_smoking_status], 'relationship_status': [speedy_match_accounts_validators.validate_relationship_status], **{to_attribute(name='profile_description', language_code=language_code): [speedy_match_accounts_validators.validate_profile_description] for language_code, language_name in django_settings.LANGUAGES}, **{to_attribute(name='city', language_code=language_code): [speedy_match_accounts_validators.validate_city] for language_code, language_name in django_settings.LANGUAGES}, **{to_attribute(name='children', language_code=language_code): [speedy_match_accounts_validators.validate_children] for language_code, language_name in django_settings.LANGUAGES}, **{to_attribute(name='more_children', language_code=language_code): [speedy_match_accounts_validators.validate_more_children] for language_code, language_name in django_settings.LANGUAGES}, **{to_attribute(name='match_description', language_code=language_code): [speedy_match_accounts_validators.validate_match_description] for language_code, language_name in django_settings.LANGUAGES}, 'gender_to_match': [speedy_match_accounts_validators.validate_gender_to_match], 'min_age_to_match': [speedy_match_accounts_validators.validate_min_age_to_match], 'max_age_to_match': [speedy_match_accounts_validators.validate_max_age_to_match], 'diet_match': [speedy_match_accounts_validators.validate_diet_match], 'smoking_status_match': [speedy_match_accounts_validators.validate_smoking_status_match], 'relationship_status_match': [speedy_match_accounts_validators.validate_relationship_status_match], } # Fields who are not in the SpeedyMatchSiteProfile model. profile_picture = forms.ImageField(required=False, widget=speedy_core_accounts_forms.CustomPhotoWidget, label=_('Add profile picture'), error_messages={'required': _("A profile picture is required.")}) diet = forms.ChoiceField(widget=forms.RadioSelect(), label=_('My diet'), error_messages={'required': _("Your diet is required.")}) smoking_status = forms.ChoiceField(widget=forms.RadioSelect(), label=_('My smoking status'), error_messages={'required': _("Your smoking status is required.")}) relationship_status = forms.ChoiceField(widget=forms.RadioSelect(), label=_('My relationship status'), error_messages={'required': _("Your relationship status is required.")}) gender_to_match = forms.MultipleChoiceField(choices=User.GENDER_CHOICES, widget=forms.CheckboxSelectMultiple, error_messages={'required': _("Gender to match is required.")}) class Meta: model = SpeedyMatchSiteProfile fields = ( 'profile_picture', *(to_attribute(name='profile_description', language_code=language_code) for language_code, language_name in django_settings.LANGUAGES), 'height', *(to_attribute(name='children', language_code=language_code) for language_code, language_name in django_settings.LANGUAGES), *(to_attribute(name='more_children', language_code=language_code) for language_code, language_name in django_settings.LANGUAGES), 'diet', 'smoking_status', 'relationship_status', 'gender_to_match', *(to_attribute(name='match_description', language_code=language_code) for language_code, language_name in django_settings.LANGUAGES), 'min_age_to_match', 'max_age_to_match', 'diet_match', 'smoking_status_match', 'relationship_status_match', ) widgets = { 'smoking_status': forms.RadioSelect(), 'relationship_status': forms.RadioSelect(), **{to_attribute(name='profile_description', language_code=language_code): forms.Textarea(attrs={'rows': 6, 'cols': 40}) for language_code, language_name in django_settings.LANGUAGES}, **{to_attribute(name='city', language_code=language_code): forms.TextInput() for language_code, language_name in django_settings.LANGUAGES}, **{to_attribute(name='children', language_code=language_code): forms.TextInput() for language_code, language_name in django_settings.LANGUAGES}, **{to_attribute(name='more_children', language_code=language_code): forms.TextInput() for language_code, language_name in django_settings.LANGUAGES}, **{to_attribute(name='match_description', language_code=language_code): forms.Textarea(attrs={'rows': 6, 'cols': 40}) for language_code, language_name in django_settings.LANGUAGES}, 'diet_match': CustomJsonWidget(choices=User.DIET_VALID_CHOICES), 'smoking_status_match': CustomJsonWidget(choices=User.SMOKING_STATUS_VALID_CHOICES), 'relationship_status_match': CustomJsonWidget(choices=User.RELATIONSHIP_STATUS_VALID_CHOICES), } error_messages = { 'profile_picture': { 'required': _("A profile picture is required."), }, 'height': { 'required': _("Your height is required."), }, 'diet': { 'required': _("Your diet is required."), }, 'smoking_status': { 'required': _("Your smoking status is required."), }, 'relationship_status': { 'required': _("Your relationship status is required."), }, **{to_attribute(name='profile_description', language_code=language_code): { 'required': _("Please write a few words about yourself."), } for language_code, language_name in django_settings.LANGUAGES}, **{to_attribute(name='city', language_code=language_code): { 'required': _("Please write where you live."), } for language_code, language_name in django_settings.LANGUAGES}, **{to_attribute(name='children', language_code=language_code): { 'required': _("Do you have children? How many?"), } for language_code, language_name in django_settings.LANGUAGES}, **{to_attribute(name='more_children', language_code=language_code): { 'required': _("Do you want (more) children?"), } for language_code, language_name in django_settings.LANGUAGES}, **{to_attribute(name='match_description', language_code=language_code): { 'required': _("Who is your ideal partner?"), } for language_code, language_name in django_settings.LANGUAGES}, 'gender_to_match': { 'required': _("Gender to match is required."), }, 'min_age_to_match': { 'required': _("Minimal age to match is required."), }, 'max_age_to_match': { 'required': _("Maximal age to match is required."), }, 'diet_match': { 'required': _("Diet match is required."), }, 'smoking_status_match': { 'required': _("Smoking status match is required."), }, 'relationship_status_match': { 'required': _("Relationship status match is required."), }, } def __init__(self, *args, **kwargs): self.step = kwargs.pop('step', None) super().__init__(*args, **kwargs) # Create the localized city field dynamically. self.fields[to_attribute(name='city')] = forms.CharField(label=_('Where do you live?'), max_length=120, error_messages={'required': _("Please write where you live.")}) # Delete unneeded fields from the form. self.delete_unneeded_fields() # Update fields attributes according to the user's gender and language. if ('profile_picture' in self.fields): self.fields['profile_picture'].widget.attrs['user'] = self.instance.user self.fields['profile_picture'].label = pgettext_lazy(context=self.instance.user.get_gender(), message='Add profile picture') if ('height' in self.fields): self.fields['height'].label = pgettext_lazy(context=self.instance.user.get_gender(), message='My height in centimeters') if ('diet' in self.fields): update_form_field_choices(field=self.fields['diet'], choices=self.instance.user.get_diet_choices_with_description()) if ('smoking_status' in self.fields): update_form_field_choices(field=self.fields['smoking_status'], choices=self.instance.user.get_smoking_status_choices()) if ('relationship_status' in self.fields): update_form_field_choices(field=self.fields['relationship_status'], choices=self.instance.user.get_relationship_status_choices()) if ('diet_match' in self.fields): update_form_field_choices(field=self.fields['diet_match'], choices=self.instance.get_diet_match_choices()) if ('smoking_status_match' in self.fields): update_form_field_choices(field=self.fields['smoking_status_match'], choices=self.instance.get_smoking_status_match_choices()) if ('relationship_status_match' in self.fields): update_form_field_choices(field=self.fields['relationship_status_match'], choices=self.instance.get_relationship_status_match_choices()) if (to_attribute(name='profile_description') in self.fields): self.fields[to_attribute(name='profile_description')].error_messages = {'required': pgettext_lazy(context=self.instance.user.get_gender(), message="Please write a few words about yourself.")} if (to_attribute(name='city') in self.fields): self.fields[to_attribute(name='city')].label = pgettext_lazy(context=self.instance.user.get_gender(), message='Where do you live?') self.fields[to_attribute(name='city')].error_messages = {'required': pgettext_lazy(context=self.instance.user.get_gender(), message="Please write where you live.")} if (to_attribute(name='children') in self.fields): self.fields[to_attribute(name='children')].label = pgettext_lazy(context=self.instance.user.get_gender(), message='Do you have children? How many?') self.fields[to_attribute(name='children')].error_messages = {'required': pgettext_lazy(context=self.instance.user.get_gender(), message="Do you have children? How many?")} if (to_attribute(name='more_children') in self.fields): self.fields[to_attribute(name='more_children')].label = pgettext_lazy(context=self.instance.user.get_gender(), message='Do you want (more) children?') self.fields[to_attribute(name='more_children')].error_messages = {'required': pgettext_lazy(context=self.instance.user.get_gender(), message="Do you want (more) children?")} if (to_attribute(name='match_description') in self.fields): self.fields[to_attribute(name='match_description')].label = pgettext_lazy(context=self.instance.get_match_gender(), message='My ideal match') self.fields[to_attribute(name='match_description')].error_messages = {'required': pgettext_lazy(context=self.instance.get_match_gender(), message="Who is your ideal partner?")} if ('gender_to_match' in self.fields): self.fields['gender_to_match'].label = _('Gender to match') if ('min_age_to_match' in self.fields): self.fields['min_age_to_match'].label = pgettext_lazy(context=self.instance.get_match_gender(), message='Minimal age to match') if ('max_age_to_match' in self.fields): self.fields['max_age_to_match'].label = pgettext_lazy(context=self.instance.get_match_gender(), message='Maximal age to match') # Update the initial value of fields from the User model. for field_name in self.user_fields: if (field_name in self.fields): self.fields[field_name].initial = getattr(self.instance.user, field_name) # Update the fields validators. for field_name, field in self.fields.items(): if (field_name in self.validators): field.validators.extend(self.validators[field_name]) field.required = True # Rearrange the fields. self.order_fields(field_order=self.get_fields()) def clean_profile_picture(self): profile_picture = self.files.get('profile_picture') if (profile_picture): user_image = Image(owner=self.instance.user, file=profile_picture) user_image.save() self.instance.user._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.user, profile_picture=profile_picture, test_new_profile_picture=True) else: profile_picture = self.instance.user.photo speedy_core_accounts_validators.validate_profile_picture_for_user(user=self.instance.user, profile_picture=profile_picture, test_new_profile_picture=False) return self.cleaned_data.get('profile_picture') def clean_gender_to_match(self): return [int(value) for value in self.cleaned_data['gender_to_match']] def clean(self): if (('min_age_to_match' in self.fields) and ('max_age_to_match' in self.fields)): min_age_to_match = self.cleaned_data.get('min_age_to_match') max_age_to_match = self.cleaned_data.get('max_age_to_match') speedy_match_accounts_validators.validate_min_max_age_to_match(min_age_to_match=min_age_to_match, max_age_to_match=max_age_to_match) return self.cleaned_data def save(self, commit=True): if (commit): user_profile = SpeedyMatchSiteProfile.objects.get(pk=self.instance.pk) if (not (self.instance.height == user_profile.height)): site = Site.objects.get_current() logger.info('User changed height on {site_name}, user={user}, new height={new_height}, old height={old_height} (registered {registered_days_ago} days ago)'.format( site_name=_(site.name), user=self.instance.user, new_height=self.instance.height, old_height=user_profile.height, registered_days_ago=(now() - self.instance.user.date_created).days, )) if ('profile_picture' in self.fields): profile_picture = self.files.get('profile_picture') if (profile_picture): self.instance.user.photo = self.instance.user._new_profile_picture self.instance.profile_picture_months_offset = 5 for field_name in self.user_fields: if (field_name in self.fields): setattr(self.instance.user, field_name, self.cleaned_data[field_name]) self.instance.user.save() super().save(commit=commit) if (commit): if (not (self.instance.not_allowed_to_use_speedy_match)): if (self.instance.activation_step >= len(SpeedyMatchSiteProfile.settings.SPEEDY_MATCH_SITE_PROFILE_FORM_FIELDS) - 1): if (not (SpeedyMatchSiteProfile.settings.MIN_HEIGHT_TO_MATCH <= self.instance.height <= SpeedyMatchSiteProfile.settings.MAX_HEIGHT_TO_MATCH)): self.instance.not_allowed_to_use_speedy_match = True self.instance.save() logger.warning('User {user} is not allowed to use Speedy Match (height={height}, registered {registered_days_ago} days ago).'.format( user=self.instance.user, height=self.instance.height, registered_days_ago=(now() - self.instance.user.date_created).days, )) activation_step = self.instance.activation_step step, errors = self.instance.validate_profile_and_activate(commit=False) if (self.step == activation_step): self.instance.activation_step = min(activation_step + 1, step) else: self.instance.activation_step = min(activation_step, step) if (self.instance.activation_step >= len(SpeedyMatchSiteProfile.settings.SPEEDY_MATCH_SITE_PROFILE_FORM_FIELDS)): self.instance.activation_step = len(SpeedyMatchSiteProfile.settings.SPEEDY_MATCH_SITE_PROFILE_FORM_FIELDS) self.instance.validate_profile_and_activate(commit=True) self.instance.save() return self.instance def get_fields(self): # This function is not defined in this base (abstract) form. raise NotImplementedError() def get_visible_fields(self): # This function is not defined in this base (abstract) form. raise NotImplementedError() def get_hidden_fields(self): fields = self.get_fields() visible_fields = self.get_visible_fields() return (field_name for field_name in fields if (not (field_name in visible_fields)))
class Meta: model = SpeedyMatchSiteProfile fields = ( 'profile_picture', *(to_attribute(name='profile_description', language_code=language_code) for language_code, language_name in django_settings.LANGUAGES), 'height', *(to_attribute(name='children', language_code=language_code) for language_code, language_name in django_settings.LANGUAGES), *(to_attribute(name='more_children', language_code=language_code) for language_code, language_name in django_settings.LANGUAGES), 'diet', 'smoking_status', 'relationship_status', 'gender_to_match', *(to_attribute(name='match_description', language_code=language_code) for language_code, language_name in django_settings.LANGUAGES), 'min_age_to_match', 'max_age_to_match', 'diet_match', 'smoking_status_match', 'relationship_status_match', ) widgets = { 'smoking_status': forms.RadioSelect(), 'relationship_status': forms.RadioSelect(), **{to_attribute(name='profile_description', language_code=language_code): forms.Textarea(attrs={'rows': 6, 'cols': 40}) for language_code, language_name in django_settings.LANGUAGES}, **{to_attribute(name='city', language_code=language_code): forms.TextInput() for language_code, language_name in django_settings.LANGUAGES}, **{to_attribute(name='children', language_code=language_code): forms.TextInput() for language_code, language_name in django_settings.LANGUAGES}, **{to_attribute(name='more_children', language_code=language_code): forms.TextInput() for language_code, language_name in django_settings.LANGUAGES}, **{to_attribute(name='match_description', language_code=language_code): forms.Textarea(attrs={'rows': 6, 'cols': 40}) for language_code, language_name in django_settings.LANGUAGES}, 'diet_match': CustomJsonWidget(choices=User.DIET_VALID_CHOICES), 'smoking_status_match': CustomJsonWidget(choices=User.SMOKING_STATUS_VALID_CHOICES), 'relationship_status_match': CustomJsonWidget(choices=User.RELATIONSHIP_STATUS_VALID_CHOICES), } error_messages = { 'profile_picture': { 'required': _("A profile picture is required."), }, 'height': { 'required': _("Your height is required."), }, 'diet': { 'required': _("Your diet is required."), }, 'smoking_status': { 'required': _("Your smoking status is required."), }, 'relationship_status': { 'required': _("Your relationship status is required."), }, **{to_attribute(name='profile_description', language_code=language_code): { 'required': _("Please write a few words about yourself."), } for language_code, language_name in django_settings.LANGUAGES}, **{to_attribute(name='city', language_code=language_code): { 'required': _("Please write where you live."), } for language_code, language_name in django_settings.LANGUAGES}, **{to_attribute(name='children', language_code=language_code): { 'required': _("Do you have children? How many?"), } for language_code, language_name in django_settings.LANGUAGES}, **{to_attribute(name='more_children', language_code=language_code): { 'required': _("Do you want (more) children?"), } for language_code, language_name in django_settings.LANGUAGES}, **{to_attribute(name='match_description', language_code=language_code): { 'required': _("Who is your ideal partner?"), } for language_code, language_name in django_settings.LANGUAGES}, 'gender_to_match': { 'required': _("Gender to match is required."), }, 'min_age_to_match': { 'required': _("Minimal age to match is required."), }, 'max_age_to_match': { 'required': _("Maximal age to match is required."), }, 'diet_match': { 'required': _("Diet match is required."), }, 'smoking_status_match': { 'required': _("Smoking status match is required."), }, 'relationship_status_match': { 'required': _("Relationship status match is required."), }, }
def get_field_pairs(self): return ((to_attribute(name='profile_description'), ), (to_attribute(name='city'), 'height'), (to_attribute(name='children'), to_attribute(name='more_children')), ('diet', 'smoking_status'), ('relationship_status', ))
def get_field_pairs(self): return (('gender_to_match', to_attribute(name='match_description')), ('min_age_to_match', 'max_age_to_match'), ('diet_match', 'smoking_status_match'), ('relationship_status_match', ))
def get_field_pairs(self): return ((to_attribute(name='first_name'), to_attribute(name='last_name')), ('slug', ), ('gender', 'date_of_birth'), ('profile_picture', ))
def get_fields(self): return ('gender_to_match', to_attribute(name='match_description'), 'min_age_match', 'max_age_match', 'diet_match', 'smoking_status_match', 'marital_status_match')
def set_up(self): super().set_up() # _this_field_is_required_error_message_dict = {'en': '___This field is required.', 'he': 'יש להזין תוכן בשדה זה.___'} # ~~~~ TODO: remove this line! _this_field_cannot_be_null_error_message_dict = {'en': 'This field cannot be null.', 'he': 'שדה זה אינו יכול להיות ריק.'} _this_field_cannot_be_blank_error_message_dict = {'en': 'This field cannot be blank.', 'he': 'שדה זה אינו יכול להיות ריק.'} _id_contains_illegal_characters_error_message_dict = {'en': 'id contains illegal characters.', 'he': 'id מכיל תווים לא חוקיים.'} _value_must_be_valid_json_error_message_dict = {'en': 'Value must be valid JSON.', 'he': 'ערך חייב להיות JSON חוקי.'} _invalid_password_error_message_dict = {'en': 'Invalid password.', 'he': 'הסיסמה לא תקינה.'} _password_too_short_error_message_dict = {'en': 'This password is too short. It must contain at least 8 characters.', 'he': 'סיסמה זו קצרה מדי. היא חייבת להכיל לפחות 8 תווים.'} _password_too_long_error_message_dict = {'en': 'This password is too long. It must contain at most 120 characters.', 'he': 'סיסמה זו ארוכה מדי. היא יכולה להכיל 120 תווים לכל היותר.'} _this_username_is_already_taken_error_message_dict = {'en': 'This username is already taken.', 'he': 'שם המשתמש/ת הזה כבר תפוס.'} _enter_a_valid_email_address_error_message_dict = {'en': 'Enter a valid email address.', 'he': 'נא להזין כתובת דואר אלקטרוני חוקית.'} _this_email_is_already_in_use_error_message_dict = {'en': 'This email is already in use.', 'he': 'הדואר האלקטרוני הזה כבר נמצא בשימוש.'} _enter_a_valid_date_error_message_dict = {'en': 'Enter a valid date.', 'he': 'יש להזין תאריך חוקי.'} _please_enter_a_correct_username_and_password_error_message_dict = {'en': 'Please enter a correct username and password. Note that both fields may be case-sensitive.', 'he': 'נא להזין שם משתמש/ת וסיסמה נכונים. נא לשים לב כי שני השדות רגישים לאותיות גדולות/קטנות.'} _your_old_password_was_entered_incorrectly_error_message_dict = {'en': 'Your old password was entered incorrectly. Please enter it again.', 'he': 'סיסמתך הישנה הוזנה בצורה שגויה. נא להזינה שוב.'} _the_two_password_fields_didnt_match_error_message_dict = {'en': "The two password fields didn’t match.", 'he': 'שני שדות הסיסמה אינם זהים.'} _entity_username_must_start_with_4_or_more_letters_error_message_dict = {'en': 'Username must start with 4 or more letters, and may contain letters, digits or dashes.', 'he': 'שם המשתמש/ת חייב להתחיל עם 4 אותיות או יותר, ויכול להכיל אותיות, ספרות או מקפים. שם המשתמש/ת חייב להיות באנגלית.'} _user_username_must_start_with_4_or_more_letters_error_message_dict = {'en': 'Username must start with 4 or more letters, after which can be any number of digits. You can add dashes between words.', 'he': 'שם המשתמש/ת חייב להתחיל עם 4 אותיות או יותר, לאחר מכן ניתן להוסיף מספר כלשהו של ספרות. ניתן להוסיף מקפים בין מילים. שם המשתמש/ת חייב להיות באנגלית.'} _slug_does_not_parse_to_username_error_message_dict = {'en': 'Slug does not parse to username.', 'he': 'slug לא מתאים לשם המשתמש/ת.'} _youve_already_confirmed_this_email_address_error_message_dict = {'en': "You've already confirmed this email address.", 'he': 'כבר אימתת את כתובת הדואר האלקטרוני שלך.'} _invalid_confirmation_link_error_message_dict = {'en': "Invalid confirmation link.", 'he': 'קישור אימות לא חוקי.'} _youve_confirmed_your_email_address_message_dict = {'en': "You've confirmed your email address.", 'he': 'אימתת את כתובת הדואר האלקטרוני שלך.'} _the_email_address_was_deleted_error_message_dict = {'en': 'The email address was deleted.', 'he': 'כתובת הדואר האלקטרוני נמחקה.'} _you_have_changed_your_primary_email_address_error_message_dict = {'en': 'You have made this email address primary.', 'he': 'הפכת את כתובת הדואר האלקטרוני הזאת לראשית.'} _username_is_required_error_message_dict = {'en': 'Username is required.', 'he': 'שם המשתמש/ת נדרש.'} _password_reset_on_speedy_net_subject_dict = {'en': "Password Reset on Speedy Net", 'he': "איפוס סיסמה בספידי נט"} _password_reset_on_speedy_match_subject_dict = {'en': "Password Reset on Speedy Match", 'he': "איפוס סיסמה בספידי מץ'"} _value_is_not_a_valid_choice_error_message_to_format_dict = {'en': 'Value {value} is not a valid choice.', 'he': 'ערך {value} אינו אפשרות חוקית.'} _value_must_be_an_integer_error_message_to_format_dict = {'en': "“{value}” value must be an integer.", 'he': "הערך '{value}' חייב להיות מספר שלם."} _username_must_contain_at_least_min_length_alphanumeric_characters_error_message_to_format_dict = {'en': 'Username must contain at least {min_length} alphanumeric characters (it has {value_length}).', 'he': 'נא לוודא ששם המשתמש/ת מכיל {min_length} תווים אלפאנומריים לפחות (מכיל {value_length}). שם המשתמש/ת חייב להיות באנגלית.'} _username_must_contain_at_most_max_length_alphanumeric_characters_error_message_to_format_dict = {'en': 'Username must contain at most {max_length} alphanumeric characters (it has {value_length}).', 'he': 'נא לוודא ששם המשתמש/ת מכיל {max_length} תווים אלפאנומריים לכל היותר (מכיל {value_length}). שם המשתמש/ת חייב להיות באנגלית.'} _username_must_contain_at_least_min_length_characters_error_message_to_format_dict = {'en': 'Username must contain at least {min_length} characters (it has {value_length}).', 'he': 'נא לוודא ששם המשתמש/ת מכיל {min_length} תווים לפחות (מכיל {value_length}).'} _username_must_contain_at_most_max_length_characters_error_message_to_format_dict = {'en': 'Username must contain at most {max_length} characters (it has {value_length}).', 'he': 'נא לוודא ששם המשתמש/ת מכיל {max_length} תווים לכל היותר (מכיל {value_length}).'} _a_confirmation_message_was_sent_to_email_address_error_message_to_format_dict = {'en': 'A confirmation message was sent to {email_address}', 'he': 'הודעת אימות נשלחה ל-{email_address}'} _you_cant_change_your_username_error_message_dict_by_gender = { 'en': { **{gender: "You can't change your username." for gender in User.ALL_GENDERS}, }, 'he': { User.GENDER_FEMALE_STRING: "לא ניתן לשנות שם משתמשת.", User.GENDER_MALE_STRING: "לא ניתן לשנות שם משתמש.", User.GENDER_OTHER_STRING: "לא ניתן לשנות שם משתמש/ת.", }, } _confirm_your_email_address_on_speedy_net_subject_dict_by_gender = { 'en': { **{gender: "Confirm your email address on Speedy Net" for gender in User.ALL_GENDERS}, }, 'he': { User.GENDER_FEMALE_STRING: "אמתי את כתובת הדואר האלקטרוני שלך בספידי נט", User.GENDER_MALE_STRING: "אמת את כתובת הדואר האלקטרוני שלך בספידי נט", User.GENDER_OTHER_STRING: "אמת/י את כתובת הדואר האלקטרוני שלך בספידי נט", }, } _confirm_your_email_address_on_speedy_match_subject_dict_by_gender = { 'en': { **{gender: "Confirm your email address on Speedy Match" for gender in User.ALL_GENDERS}, }, 'he': { User.GENDER_FEMALE_STRING: "אמתי את כתובת הדואר האלקטרוני שלך בספידי מץ'", User.GENDER_MALE_STRING: "אמת את כתובת הדואר האלקטרוני שלך בספידי מץ'", User.GENDER_OTHER_STRING: "אמת/י את כתובת הדואר האלקטרוני שלך בספידי מץ'", }, } # self._this_field_is_required_error_message = _this_field_is_required_error_message_dict[self.language_code] # ~~~~ TODO: remove this line! self._this_field_cannot_be_null_error_message = _this_field_cannot_be_null_error_message_dict[self.language_code] self._this_field_cannot_be_blank_error_message = _this_field_cannot_be_blank_error_message_dict[self.language_code] self._id_contains_illegal_characters_error_message = _id_contains_illegal_characters_error_message_dict[self.language_code] self._value_must_be_valid_json_error_message = _value_must_be_valid_json_error_message_dict[self.language_code] self._invalid_password_error_message = _invalid_password_error_message_dict[self.language_code] self._password_too_short_error_message = _password_too_short_error_message_dict[self.language_code] self._password_too_long_error_message = _password_too_long_error_message_dict[self.language_code] self._this_username_is_already_taken_error_message = _this_username_is_already_taken_error_message_dict[self.language_code] self._enter_a_valid_email_address_error_message = _enter_a_valid_email_address_error_message_dict[self.language_code] self._this_email_is_already_in_use_error_message = _this_email_is_already_in_use_error_message_dict[self.language_code] self._enter_a_valid_date_error_message = _enter_a_valid_date_error_message_dict[self.language_code] self._please_enter_a_correct_username_and_password_error_message = _please_enter_a_correct_username_and_password_error_message_dict[self.language_code] self._your_old_password_was_entered_incorrectly_error_message = _your_old_password_was_entered_incorrectly_error_message_dict[self.language_code] self._the_two_password_fields_didnt_match_error_message = _the_two_password_fields_didnt_match_error_message_dict[self.language_code] self._entity_username_must_start_with_4_or_more_letters_error_message = _entity_username_must_start_with_4_or_more_letters_error_message_dict[self.language_code] self._user_username_must_start_with_4_or_more_letters_error_message = _user_username_must_start_with_4_or_more_letters_error_message_dict[self.language_code] self._slug_does_not_parse_to_username_error_message = _slug_does_not_parse_to_username_error_message_dict[self.language_code] self._youve_already_confirmed_this_email_address_error_message = _youve_already_confirmed_this_email_address_error_message_dict[self.language_code] self._invalid_confirmation_link_error_message = _invalid_confirmation_link_error_message_dict[self.language_code] self._youve_confirmed_your_email_address_message = _youve_confirmed_your_email_address_message_dict[self.language_code] self._the_email_address_was_deleted_error_message = _the_email_address_was_deleted_error_message_dict[self.language_code] self._you_have_changed_your_primary_email_address_error_message = _you_have_changed_your_primary_email_address_error_message_dict[self.language_code] self._username_is_required_error_message = _username_is_required_error_message_dict[self.language_code] self._password_reset_on_speedy_net_subject = _password_reset_on_speedy_net_subject_dict[self.language_code] self._password_reset_on_speedy_match_subject = _password_reset_on_speedy_match_subject_dict[self.language_code] self._value_is_not_a_valid_choice_error_message_to_format = _value_is_not_a_valid_choice_error_message_to_format_dict[self.language_code] self._value_must_be_an_integer_error_message_to_format = _value_must_be_an_integer_error_message_to_format_dict[self.language_code] self._username_must_contain_at_least_min_length_alphanumeric_characters_error_message_to_format = _username_must_contain_at_least_min_length_alphanumeric_characters_error_message_to_format_dict[self.language_code] self._username_must_contain_at_most_max_length_alphanumeric_characters_error_message_to_format = _username_must_contain_at_most_max_length_alphanumeric_characters_error_message_to_format_dict[self.language_code] self._username_must_contain_at_least_min_length_characters_error_message_to_format = _username_must_contain_at_least_min_length_characters_error_message_to_format_dict[self.language_code] self._username_must_contain_at_most_max_length_characters_error_message_to_format = _username_must_contain_at_most_max_length_characters_error_message_to_format_dict[self.language_code] self._a_confirmation_message_was_sent_to_email_address_error_message_to_format = _a_confirmation_message_was_sent_to_email_address_error_message_to_format_dict[self.language_code] self._you_cant_change_your_username_error_message_dict_by_gender = _you_cant_change_your_username_error_message_dict_by_gender[self.language_code] self._confirm_your_email_address_on_speedy_net_subject_dict_by_gender = _confirm_your_email_address_on_speedy_net_subject_dict_by_gender[self.language_code] self._confirm_your_email_address_on_speedy_match_subject_dict_by_gender = _confirm_your_email_address_on_speedy_match_subject_dict_by_gender[self.language_code] self.assertSetEqual(set1=set(self._you_cant_change_your_username_error_message_dict_by_gender.keys()), set2=set(User.ALL_GENDERS)) self.assertSetEqual(set1=set(self._confirm_your_email_address_on_speedy_net_subject_dict_by_gender.keys()), set2=set(User.ALL_GENDERS)) self.assertSetEqual(set1=set(self._confirm_your_email_address_on_speedy_match_subject_dict_by_gender.keys()), set2=set(User.ALL_GENDERS)) self.assertEqual(first=len(set(self._you_cant_change_your_username_error_message_dict_by_gender.keys())), second=3) self.assertEqual(first=len(set(self._confirm_your_email_address_on_speedy_net_subject_dict_by_gender.keys())), second=3) self.assertEqual(first=len(set(self._confirm_your_email_address_on_speedy_match_subject_dict_by_gender.keys())), second=3) self.assertEqual(first=len(set(self._user_all_the_required_fields_keys())), second=7) self.assertEqual(first=set(self._cannot_create_user_without_all_the_required_fields_errors_dict_by_value(value=None).keys()), second=set(self._user_all_the_required_fields_keys())) self.assertEqual(first=len(set(self._cannot_create_user_without_all_the_required_fields_errors_dict_by_value(value=None).keys())), second=7) self.assertEqual(first=len(set(self._cannot_create_user_without_all_the_required_fields_errors_dict_by_value(value=None).keys()) - set(self._user_all_the_required_fields_keys())), second=0) self.assertSetEqual(set1=set(self._cannot_create_user_without_all_the_required_fields_errors_dict_by_value(value=None).keys()), set2=set(self._user_all_the_required_fields_keys()) | {'first_name_en', 'first_name_he'}) self.assertEqual(first=set(self._cannot_create_user_without_all_the_required_fields_errors_dict_by_value(value='').keys()), second=set(self._user_all_the_required_fields_keys())) self.assertEqual(first=len(set(self._cannot_create_user_without_all_the_required_fields_errors_dict_by_value(value='').keys())), second=7) self.assertEqual(first=len(set(self._cannot_create_user_without_all_the_required_fields_errors_dict_by_value(value='').keys()) - set(self._user_all_the_required_fields_keys())), second=0) self.assertSetEqual(set1=set(self._cannot_create_user_without_all_the_required_fields_errors_dict_by_value(value='').keys()), set2=set(self._user_all_the_required_fields_keys()) | {'first_name_en', 'first_name_he'}) self.assertListEqual(list1=self._profile_form_all_the_required_fields_keys(), list2=[field_name for field_name in self._registration_form_all_the_required_fields_keys() if (not (field_name in ['email', 'new_password1']))]) self.assertSetEqual(set1=set(self._registration_form_all_the_required_fields_keys()) - {'email', 'new_password1'}, set2=set(self._profile_form_all_the_required_fields_keys())) self.assertSetEqual(set1=set(self._profile_form_all_the_required_fields_keys()) | {'email', 'new_password1'}, set2=set(self._registration_form_all_the_required_fields_keys())) self.assertNotEqual(first=[to_attribute(name='first_name')], second=['first_name']) self.assertNotEqual(first=[to_attribute(name='first_name'), to_attribute(name='last_name')], second=['first_name', 'last_name']) self.assertListEqual(list1=self._user_all_the_required_fields_keys()[:2], list2=[to_attribute(name='first_name', language_code=language_code) for language_code, language_name in django_settings.LANGUAGES]) self.assertListEqual(list1=self._user_all_the_required_fields_keys()[:2], list2=[to_attribute(name='first_name', language_code='en'), to_attribute(name='first_name', language_code='he')]) self.assertListEqual(list1=self._user_all_the_required_fields_keys()[:2], list2=['first_name_en', 'first_name_he']) self.assertListEqual(list1=self._registration_form_all_the_required_fields_keys()[:1], list2=[to_attribute(name='first_name')]) self.assertListEqual(list1=self._profile_form_all_the_required_fields_keys()[:1], list2=[to_attribute(name='first_name')])
def get_localized_field(self, base_field_name, language_code): return to_attribute(name=base_field_name, language_code=language_code or self.language_code)