Conditions | 4 |
Total Lines | 119 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 1 |
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:
1 | import uuid |
||
178 | def post(self, request, *args, **kwargs): |
||
179 | form = OrganisationMemberAddForm(data=request.POST) |
||
180 | if form.is_valid(): |
||
181 | existing_user = form.cleaned_data['existing_user'] |
||
182 | new_user = form.cleaned_data['new_user'] |
||
183 | |||
184 | org = Organisation.objects.get(id=self.kwargs['pk']) |
||
185 | host = '{}://{}'.format(settings.SITE_PROTOCOL, |
||
186 | request.META['HTTP_HOST']) |
||
187 | |||
188 | context = { |
||
189 | 'full_name': '%s %s' % (request.user.first_name, |
||
190 | request.user.last_name), |
||
191 | 'org_name': org.name, |
||
192 | 'host': host |
||
193 | } |
||
194 | |||
195 | if existing_user: |
||
196 | # add user to organisation |
||
197 | user = existing_user |
||
198 | org.user.add(user) |
||
199 | org.save() |
||
200 | |||
201 | # set email user's name in context |
||
202 | context['new_member_name'] = '%s %s' % (user.first_name, |
||
203 | user.last_name) |
||
204 | email_context = Context(context) |
||
205 | |||
206 | # send mail to user being added |
||
207 | subject = "You are added in %s organisation" % ( |
||
208 | org.location.name) |
||
209 | email_body = loader.get_template( |
||
210 | 'email_messages/organisation/to_new_member_existing.html').render( |
||
211 | email_context) |
||
212 | text_body = loader.get_template( |
||
213 | 'email_messages/organisation/to_new_member_existing.txt').render(email_context) |
||
214 | |||
215 | send_email_to_id(subject, |
||
216 | body=email_body, |
||
217 | email_id=user.email, |
||
218 | text_body=text_body) |
||
219 | |||
220 | elif new_user: |
||
221 | # generate a random password |
||
222 | random_password = User.objects.make_random_password() |
||
223 | |||
224 | # create a user with the email from form |
||
225 | user = User(username=self.get_username(new_user), |
||
226 | email=new_user, |
||
227 | password=random_password) |
||
228 | |||
229 | # user is inactive initialy |
||
230 | user.is_active = False |
||
231 | user.save() |
||
232 | |||
233 | # add the user to organisation |
||
234 | org.user.add(user.id) |
||
235 | org.save() |
||
236 | |||
237 | # set the email context, the token will be used to generate a unique varification |
||
238 | # link |
||
239 | token = self.get_token(user) |
||
240 | |||
241 | context['new_member_name'] = '%s' % (user.email) |
||
242 | context['token'] = token |
||
243 | context['user'] = user |
||
244 | email_context = Context(context) |
||
245 | |||
246 | # set the meta |
||
247 | subject = "[Python Express]:You are added in %s organisation" % ( |
||
248 | org.location.name) |
||
249 | email_body = loader.get_template( |
||
250 | 'email_messages/organisation/to_new_member.html').render(email_context) |
||
251 | text_body = loader.get_template( |
||
252 | 'email_messages/organisation/to_new_member.txt').render(email_context) |
||
253 | |||
254 | # send the mail to new user |
||
255 | send_email_to_id(subject, |
||
256 | body=email_body, |
||
257 | email_id=new_user, |
||
258 | text_body=text_body) |
||
259 | |||
260 | # These mails will be sent in both cases. |
||
261 | subject = "user %s %s added in %s organisation" % ( |
||
262 | user.first_name, user.last_name, org.location.name) |
||
263 | email_body = loader.get_template( |
||
264 | 'email_messages/organisation/member_addition_to_user.html').render( |
||
265 | email_context) |
||
266 | text_body = loader.get_template( |
||
267 | 'email_messages/organisation/member_addition_to_user.txt').render( |
||
268 | email_context) |
||
269 | |||
270 | # send mail to the user who added the new member |
||
271 | send_email_to_id(subject, |
||
272 | body=email_body, |
||
273 | email_id=request.user.email, |
||
274 | text_body=text_body) |
||
275 | |||
276 | regional_lead = Profile.objects.filter( |
||
277 | interested_locations=org.location, |
||
278 | usertype__slug='lead').values_list('user__email', flat=True) |
||
279 | |||
280 | email_body = loader.get_template( |
||
281 | 'email_messages/organisation/member_addition_to_lead.html').render( |
||
282 | email_context) |
||
283 | text_body = loader.get_template( |
||
284 | 'email_messages/organisation/member_addition_to_lead.txt').render( |
||
285 | email_context) |
||
286 | |||
287 | # send mail to the regional leads |
||
288 | send_email_to_list(subject, |
||
289 | body=email_body, |
||
290 | users_list=regional_lead, |
||
291 | text_body=text_body) |
||
292 | |||
293 | return HttpResponseRedirect(self.success_url) |
||
294 | |||
295 | else: |
||
296 | return render(request, self.template_name, {'form': form}) |
||
297 | |||
341 |