| Conditions | 11 |
| Total Lines | 66 |
| Code Lines | 40 |
| 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 bika.lims.browser.analysisrequest.reject_samples.RejectSamplesView.__call__() 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 | # -*- coding: utf-8 -*- |
||
| 57 | def __call__(self): |
||
| 58 | form = self.request.form |
||
| 59 | |||
| 60 | # Form submit toggle |
||
| 61 | form_submitted = form.get("submitted", False) |
||
| 62 | |||
| 63 | # Buttons |
||
| 64 | form_continue = form.get("button_continue", False) |
||
| 65 | form_cancel = form.get("button_cancel", False) |
||
| 66 | |||
| 67 | # Is Rejection Workflow Enabled |
||
| 68 | if not self.is_rejection_workflow_enabled: |
||
| 69 | return self.redirect(message=_("Rejection workflow is not enabled"), |
||
| 70 | level="warning") |
||
| 71 | |||
| 72 | # Get the objects from request |
||
| 73 | samples = self.get_samples_from_request() |
||
| 74 | |||
| 75 | # No Samples selected |
||
| 76 | if not samples: |
||
| 77 | return self.redirect(message=_("No items selected"), |
||
| 78 | level="warning") |
||
| 79 | |||
| 80 | # Handle rejection |
||
| 81 | if form_submitted and form_continue: |
||
| 82 | logger.info("*** REJECT SAMPLES ***") |
||
| 83 | processed = [] |
||
| 84 | for sample in form.get("samples", []): |
||
| 85 | sample_uid = sample.get("uid", "") |
||
| 86 | reasons = sample.get("reasons", []) |
||
| 87 | other = sample.get("other_reasons", "") |
||
| 88 | if not sample_uid: |
||
| 89 | continue |
||
| 90 | |||
| 91 | # Omit if no rejection reason specified |
||
| 92 | if not any([reasons, other]): |
||
| 93 | continue |
||
| 94 | |||
| 95 | # This is quite bizarre! |
||
| 96 | # AR's Rejection reasons is a RecordsField, but with one |
||
| 97 | # record only, that contains both predefined and other reasons. |
||
| 98 | obj = api.get_object_by_uid(sample_uid) |
||
| 99 | rejection_reasons = { |
||
| 100 | "other": other, |
||
| 101 | "selected": reasons |
||
| 102 | } |
||
| 103 | obj.setRejectionReasons([rejection_reasons]) |
||
| 104 | |||
| 105 | # Reject the sample |
||
| 106 | notify = sample.get("notify", "") == "on" |
||
| 107 | do_rejection(obj, notify=notify) |
||
| 108 | processed.append(obj) |
||
| 109 | |||
| 110 | if not processed: |
||
| 111 | return self.redirect(message=_("No samples were rejected")) |
||
| 112 | |||
| 113 | message = _("Rejected {} samples: {}").format( |
||
| 114 | len(processed), ", ".join(map(api.get_id, processed))) |
||
| 115 | return self.redirect(message=message) |
||
| 116 | |||
| 117 | # Handle cancel |
||
| 118 | if form_submitted and form_cancel: |
||
| 119 | logger.info("*** CANCEL REJECTION ***") |
||
| 120 | return self.redirect(message=_("Rejection cancelled")) |
||
| 121 | |||
| 122 | return self.template() |
||
| 123 | |||
| 177 |