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