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