Conditions | 11 |
Total Lines | 119 |
Code Lines | 83 |
Lines | 0 |
Ratio | 0 % |
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.user.creation.views.create() 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 | """ |
||
80 | @blueprint.route('/', methods=['POST']) |
||
81 | def create(): |
||
82 | """Create a user.""" |
||
83 | _abort_if_user_account_creation_disabled() |
||
84 | |||
85 | terms_document_id = terms_document_service.find_document_id_for_brand( |
||
86 | g.brand_id |
||
87 | ) |
||
88 | |||
89 | privacy_policy_consent_subject_id = ( |
||
90 | _find_privacy_policy_consent_subject_id() |
||
91 | ) |
||
92 | |||
93 | newsletter_list_id = _find_newsletter_list_for_brand() |
||
94 | |||
95 | real_name_required = _is_real_name_required() |
||
96 | terms_consent_required = (terms_document_id is not None) |
||
97 | privacy_policy_consent_required \ |
||
98 | = (privacy_policy_consent_subject_id is not None) |
||
99 | newsletter_offered = (newsletter_list_id is not None) |
||
100 | |||
101 | form = UserCreateForm(request.form) |
||
102 | |||
103 | _adjust_create_form( |
||
104 | form, |
||
105 | real_name_required, |
||
106 | terms_consent_required, |
||
107 | privacy_policy_consent_required, |
||
108 | newsletter_offered, |
||
109 | ) |
||
110 | |||
111 | if not form.validate(): |
||
112 | return create_form(form) |
||
113 | |||
114 | screen_name = form.screen_name.data.strip() |
||
115 | email_address = form.email_address.data.strip().lower() |
||
116 | password = form.password.data |
||
117 | |||
118 | now_utc = datetime.utcnow() |
||
119 | |||
120 | if user_service.is_screen_name_already_assigned(screen_name): |
||
121 | flash_error( |
||
122 | 'Dieser Benutzername ist bereits einem Benutzerkonto zugeordnet.' |
||
123 | ) |
||
124 | return create_form(form) |
||
125 | |||
126 | if user_service.is_email_address_already_assigned(email_address): |
||
127 | flash_error( |
||
128 | 'Diese E-Mail-Adresse ist bereits einem Benutzerkonto zugeordnet.' |
||
129 | ) |
||
130 | return create_form(form) |
||
131 | |||
132 | if real_name_required: |
||
133 | first_names = form.first_names.data.strip() |
||
134 | last_name = form.last_name.data.strip() |
||
135 | else: |
||
136 | first_names = None |
||
137 | last_name = None |
||
138 | |||
139 | terms_consent = None |
||
140 | if terms_consent_required: |
||
141 | terms_version_id = form.terms_version_id.data |
||
142 | consent_to_terms = form.consent_to_terms.data |
||
143 | |||
144 | terms_version = terms_version_service.find_version(terms_version_id) |
||
145 | if terms_version.document_id != terms_document_id: |
||
146 | abort(400, 'Die AGB-Version gehört nicht zu dieser Veranstaltung.') |
||
147 | |||
148 | terms_consent = Consent( |
||
149 | user_id=None, # not available at this point |
||
150 | subject_id=terms_version.consent_subject_id, |
||
151 | expressed_at=now_utc, |
||
152 | ) |
||
153 | |||
154 | privacy_policy_consent = None |
||
155 | if privacy_policy_consent_required: |
||
156 | privacy_policy_consent = Consent( |
||
157 | user_id=None, # not available at this point |
||
158 | subject_id=privacy_policy_consent_subject_id, |
||
159 | expressed_at=now_utc, |
||
160 | ) |
||
161 | |||
162 | newsletter_subscription = None |
||
163 | if newsletter_offered: |
||
164 | subscribe_to_newsletter = form.subscribe_to_newsletter.data |
||
165 | if subscribe_to_newsletter: |
||
166 | newsletter_subscription = NewsletterSubscription( |
||
167 | user_id=None, # not available at this point |
||
168 | list_id=newsletter_list_id, |
||
169 | expressed_at=now_utc, |
||
170 | ) |
||
171 | |||
172 | try: |
||
173 | user, event = user_creation_service.create_user( |
||
174 | screen_name, |
||
175 | email_address, |
||
176 | password, |
||
177 | first_names, |
||
178 | last_name, |
||
179 | g.site_id, |
||
180 | terms_consent=terms_consent, |
||
181 | privacy_policy_consent=privacy_policy_consent, |
||
182 | newsletter_subscription=newsletter_subscription, |
||
183 | ) |
||
184 | except user_creation_service.UserCreationFailed: |
||
185 | flash_error( |
||
186 | f'Das Benutzerkonto für "{screen_name}" konnte nicht angelegt werden.' |
||
187 | ) |
||
188 | return create_form(form) |
||
189 | |||
190 | flash_success( |
||
191 | f'Das Benutzerkonto für "{user.screen_name}" wurde angelegt. ' |
||
192 | 'Bevor du dich damit anmelden kannst, muss zunächst der Link in ' |
||
193 | 'der an die angegebene Adresse verschickten E-Mail besucht werden.', |
||
194 | ) |
||
195 | |||
196 | signals.account_created.send(None, event=event) |
||
197 | |||
198 | return redirect_to('authentication.login_form') |
||
199 | |||
277 |