Conditions | 9 |
Total Lines | 56 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | 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:
1 | # -*- coding: utf-8 -*- |
||
96 | def save(self, commit=True): |
||
97 | """Call appropriate action via current registration backend |
||
98 | |||
99 | Insted of modifing the registration profile, this method call current |
||
100 | registration backend's accept/reject/activate method as requested. |
||
101 | |||
102 | """ |
||
103 | fail_message = 'update' if self.instance.pk else 'create' |
||
104 | opts = self.instance._meta |
||
105 | if self.errors: |
||
106 | raise ValueError("The %s chould not be %s because the data did'nt" |
||
107 | "validate." % (opts.object_name, fail_message)) |
||
108 | action_name = self.cleaned_data['action_name'] |
||
109 | message = self.cleaned_data['message'] |
||
110 | # this is a bit hack. to get request instance in form instance, |
||
111 | # RegistrationAdmin save its request to bundle model instance |
||
112 | _request = getattr( |
||
113 | self.instance, |
||
114 | settings._REGISTRATION_ADMIN_REQ_ATTR_NAME_IN_MODEL_INS |
||
115 | ) |
||
116 | if action_name == 'accept': |
||
117 | self.registration_backend.accept( |
||
118 | self.instance, _request, message=message, |
||
119 | force=True, |
||
120 | ) |
||
121 | elif action_name == 'reject': |
||
122 | self.registration_backend.reject( |
||
123 | self.instance, _request, message=message) |
||
124 | elif action_name == 'activate': |
||
125 | # DO NOT delete profile otherwise Django Admin will raise |
||
126 | # IndexError |
||
127 | self.registration_backend.activate( |
||
128 | self.instance.activation_key, _request, message=message, |
||
129 | no_profile_delete=True, |
||
130 | ) |
||
131 | elif action_name == 'force_activate': |
||
132 | self.registration_backend.accept( |
||
133 | self.instance, _request, send_email=False) |
||
134 | # DO NOT delete profile otherwise Django Admin will raise |
||
135 | # IndexError |
||
136 | self.registration_backend.activate( |
||
137 | self.instance.activation_key, _request, message=message, |
||
138 | no_profile_delete=True, |
||
139 | ) |
||
140 | else: |
||
141 | raise AttributeError( |
||
142 | 'Unknwon action_name "%s" was requested.' % action_name) |
||
143 | if action_name not in ('activate', 'force_activate'): |
||
144 | new_instance = self.instance.__class__.objects.get( |
||
145 | pk=self.instance.pk) |
||
146 | else: |
||
147 | new_instance = self.instance |
||
148 | # the instance has been deleted by activate method however |
||
149 | # ``save()`` method will be called, thus set mock save method |
||
150 | new_instance.save = lambda *args, **kwargs: new_instance |
||
151 | return new_instance |
||
152 | |||
156 |