Conditions | 13 |
Total Lines | 69 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 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 workshop_reminder() 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 | import datetime |
||
61 | @task |
||
62 | def workshop_reminder(days, intro=None, feedback=None): |
||
63 | """ |
||
64 | send workshop reminder and intro email to all register user |
||
65 | :return: |
||
66 | """ |
||
67 | if feedback: |
||
68 | today = datetime.datetime.today() |
||
69 | if days: |
||
70 | workshop_date = today + datetime.timedelta(days=2) |
||
71 | workshops = Workshop.objects.filter(expected_date=workshop_date) |
||
72 | |||
73 | for workshop in workshops: |
||
74 | context_dict = { |
||
75 | 'date': workshop_date, |
||
76 | 'workshop_title': workshop.workshop_section.name, |
||
77 | } |
||
78 | email_context = Context(context_dict) |
||
79 | organisation = workshop.requester |
||
80 | for requester in workshop.requester.user.all(): |
||
81 | feedback_emails( |
||
82 | organisation, email_context, requester.email) |
||
83 | for presenter in workshop.presenter.all(): |
||
84 | feedback_emails( |
||
85 | organisation, email_context, presenter.email) |
||
86 | else: |
||
87 | workshop_date = datetime.date.today() + datetime.timedelta(days=days) |
||
88 | workshops = Workshop.objects.select_related( |
||
89 | 'workshop_section').filter( |
||
90 | expected_date=workshop_date).filter( |
||
91 | is_active=True) |
||
92 | |||
93 | for workshop in workshops: |
||
94 | organisation = workshop.requester |
||
95 | |||
96 | context_dict = { |
||
97 | 'date': workshop_date, |
||
98 | 'workshop_title': workshop.workshop_section.name, |
||
99 | } |
||
100 | if workshop.get_presenter_list: |
||
101 | context_dict['poc_list'] = workshop.requester.user.all() |
||
102 | context_dict['presenter_list'] = workshop.presenter.all() |
||
103 | |||
104 | if intro: |
||
105 | |||
106 | # Below loop is to send email individually |
||
107 | email_context = Context(context_dict) |
||
108 | for requester in workshop.requester.user.all(): |
||
109 | intro_emails( |
||
110 | organisation, email_context, requester.email) |
||
111 | for presenter in workshop.presenter.all(): |
||
112 | intro_emails( |
||
113 | organisation, email_context, presenter.email) |
||
114 | else: |
||
115 | context_dict['workshop_title'] = workshop.workshop_section.name |
||
116 | context_dict['workshop_date'] = workshop.expected_date |
||
117 | context_dict['trainer'] = workshop.presenter.all() |
||
118 | context_dict['venue'] = """{} , {}""".format( |
||
119 | workshop.requester.name, |
||
120 | workshop.requester.full_address) |
||
121 | context_dict['organiser'] = workshop.requester.name |
||
122 | email_context = Context(context_dict) |
||
123 | for requester in workshop.requester.user.all(): |
||
124 | remainder_email( |
||
125 | organisation, email_context, requester.email) |
||
126 | for presenter in workshop.presenter.all(): |
||
127 | remainder_email( |
||
128 | organisation, email_context, presenter.email) |
||
129 | return True |
||
130 |