Conditions | 10 |
Total Lines | 61 |
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 UserServiceWizard.done() 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 | from django.shortcuts import render_to_response |
||
86 | def done(self, form_list, **kwargs): |
||
87 | """ |
||
88 | Save info to the DB |
||
89 | The process is : |
||
90 | 1) get the infos for the Trigger from step 0, 2, 4 |
||
91 | 2) save it to TriggerService |
||
92 | 3) get the infos from the "Provider" and "Consumer" services |
||
93 | at step 1 and 3 |
||
94 | 4) save all of them |
||
95 | """ |
||
96 | # get the datas from the form for TriggerService |
||
97 | i = 0 |
||
98 | for form in form_list: |
||
99 | # cleaning |
||
100 | data = form.cleaned_data |
||
101 | # get the service we selected at step 0 : provider |
||
102 | if i == 0: |
||
103 | trigger_provider = UserService.objects.get( |
||
104 | name=data.get('provider'), |
||
105 | user=self.request.user.id) |
||
106 | model_provider = get_service(data.get('provider'), 'models') |
||
107 | # get the service we selected at step 2 : consumer |
||
108 | elif i == 2: |
||
109 | trigger_consumer = UserService.objects.get( |
||
110 | name=data.get('consumer'), |
||
111 | user=self.request.user.id) |
||
112 | model_consumer = get_service(data.get('consumer'), 'models') |
||
113 | # get the description we gave for the trigger |
||
114 | elif i == 4: |
||
115 | trigger_description = data.get('description') |
||
116 | i += 1 |
||
117 | |||
118 | # save the trigger |
||
119 | trigger = TriggerService( |
||
120 | provider=trigger_provider, consumer=trigger_consumer, |
||
121 | user=self.request.user, status=True, |
||
122 | description=trigger_description) |
||
123 | trigger.save() |
||
124 | |||
125 | model_fields = {} |
||
126 | # get the datas from the form for Service related |
||
127 | # save the related models to provider and consumer |
||
128 | i = 0 |
||
129 | for form in form_list: |
||
130 | model_fields = {} |
||
131 | data = form.cleaned_data |
||
132 | # get the data for the provider service |
||
133 | if i == 1: |
||
134 | for field in data: |
||
135 | model_fields.update({field: data[field]}) |
||
136 | model_fields.update({'trigger_id': trigger.id, 'status': True}) |
||
137 | model_provider.objects.create(**model_fields) |
||
138 | # get the data for the consumer service |
||
139 | elif i == 3: |
||
140 | for field in data: |
||
141 | model_fields.update({field: data[field]}) |
||
142 | model_fields.update({'trigger_id': trigger.id, 'status': True}) |
||
143 | model_consumer.objects.create(**model_fields) |
||
144 | i += 1 |
||
145 | |||
146 | return HttpResponseRedirect(reverse('base')) |
||
147 | |||
163 |