Conditions | 10 |
Total Lines | 70 |
Code Lines | 56 |
Lines | 0 |
Ratio | 0 % |
Tests | 33 |
CRAP Score | 11.2584 |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like byceps.blueprints.common.user.creation.forms.assemble_user_create_form() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | """ |
||
40 | 1 | def assemble_user_create_form( |
|
41 | real_name_required: bool, |
||
42 | terms_consent_required: bool, |
||
43 | required_consent_subjects: Set[Subject], |
||
44 | newsletter_offered: bool, |
||
45 | ): |
||
46 | 1 | class UserCreateForm(LocalizedForm): |
|
47 | 1 | screen_name = StringField('Benutzername', [ |
|
48 | InputRequired(), |
||
49 | Length(min=screen_name_validator.MIN_LENGTH, |
||
50 | max=screen_name_validator.MAX_LENGTH), |
||
51 | ScreenNameValidator(), |
||
52 | ]) |
||
53 | 1 | first_names = StringField('Vorname(n)', [InputRequired(), Length(min=2, max=40)]) |
|
54 | 1 | last_name = StringField('Nachname', [InputRequired(), Length(min=2, max=80)]) |
|
55 | 1 | email_address = StringField('E-Mail-Adresse', [InputRequired(), Length(min=6, max=120)]) |
|
56 | 1 | password = PasswordField('Passwort', [InputRequired(), Length(min=8)]) |
|
57 | 1 | is_bot = BooleanField('Bot') |
|
58 | |||
59 | 1 | @staticmethod |
|
60 | def validate_screen_name(form, field): |
||
61 | 1 | if user_service.is_screen_name_already_assigned(field.data): |
|
62 | raise ValueError('Dieser Benutzername kann nicht verwendet werden.') |
||
63 | |||
64 | 1 | @staticmethod |
|
65 | def validate_email_address(form, field): |
||
66 | 1 | if EMAIL_ADDRESS_PATTERN.search(field.data) is None: |
|
67 | raise ValueError('Die E-Mail-Adresse ist ungültig.') |
||
68 | |||
69 | 1 | if user_service.is_email_address_already_assigned(field.data): |
|
70 | raise ValueError( |
||
71 | 'Diese E-Mail-Adresse kann nicht verwendet werden.' |
||
72 | ) |
||
73 | |||
74 | 1 | @staticmethod |
|
75 | def validate_terms_version_id(form, field): |
||
76 | 1 | try: |
|
77 | 1 | UUID(field.data) |
|
78 | except ValueError: |
||
79 | raise ValueError('Ungültige AGB-Version.') |
||
80 | |||
81 | 1 | @staticmethod |
|
82 | def validate_is_bot(form, field): |
||
83 | 1 | if field.data: |
|
84 | raise ValueError('Bots sind nicht erlaubt.') |
||
85 | |||
86 | 1 | def get_field_for_consent_subject_id(self, subject_id: SubjectID): |
|
87 | name = _generate_consent_subject_field_name(subject_id) |
||
88 | return getattr(self, name) |
||
89 | |||
90 | 1 | if not real_name_required: |
|
91 | del UserCreateForm.first_names |
||
92 | del UserCreateForm.last_name |
||
93 | |||
94 | 1 | if terms_consent_required: |
|
95 | 1 | terms_version_id = HiddenField('AGB-Version', [InputRequired()]) |
|
96 | 1 | consent_to_terms = BooleanField('AGB', [InputRequired()]) |
|
97 | 1 | setattr(UserCreateForm, 'terms_version_id', terms_version_id) |
|
98 | 1 | setattr(UserCreateForm, 'consent_to_terms', consent_to_terms) |
|
99 | |||
100 | 1 | for subject in required_consent_subjects: |
|
101 | 1 | field_name = _generate_consent_subject_field_name(subject.id) |
|
102 | 1 | field = BooleanField('', [InputRequired()]) |
|
103 | 1 | setattr(UserCreateForm, field_name, field) |
|
104 | |||
105 | 1 | if newsletter_offered: |
|
106 | 1 | subscribe_to_newsletter = BooleanField('Newsletter') |
|
107 | 1 | setattr(UserCreateForm, 'subscribe_to_newsletter', subscribe_to_newsletter) |
|
108 | |||
109 | 1 | return UserCreateForm |
|
110 | |||
114 |