Conditions | 12 |
Total Lines | 66 |
Code Lines | 41 |
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 build.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 -*- |
||
43 | def __call__(self): |
||
44 | form = self.request.form |
||
45 | |||
46 | # Form submit toggle |
||
47 | form_submitted = form.get("submitted", False) |
||
48 | |||
49 | # Buttons |
||
50 | form_continue = form.get("button_continue", False) |
||
51 | form_cancel = form.get("button_cancel", False) |
||
52 | |||
53 | # Is Rejection Workflow Enabled |
||
54 | if not self.is_rejection_workflow_enabled: |
||
55 | return self.redirect(message=_("Rejection workflow is not enabled"), |
||
56 | level="warning") |
||
57 | |||
58 | # Get the objects from request |
||
59 | samples = self.get_samples_from_request() |
||
60 | |||
61 | # No Samples selected |
||
62 | if not samples: |
||
63 | return self.redirect(message=_("No items selected"), |
||
64 | level="warning") |
||
65 | |||
66 | # Handle rejection |
||
67 | if form_submitted and form_continue: |
||
68 | logger.info("*** REJECT SAMPLES ***") |
||
69 | processed = [] |
||
70 | for sample in form.get("samples", []): |
||
71 | sample_uid = sample.get("uid", "") |
||
72 | reasons = sample.get("reasons", []) |
||
73 | other = sample.get("other_reasons", "") |
||
74 | if not sample_uid: |
||
75 | continue |
||
76 | |||
77 | # Omit if no rejection reason specified |
||
78 | if not any([reasons, other]): |
||
79 | continue |
||
80 | |||
81 | # This is quite bizarre! |
||
82 | # AR's Rejection reasons is a RecordsField, but with one |
||
83 | # record only, that contains both predefined and other reasons. |
||
84 | obj = api.get_object_by_uid(sample_uid) |
||
85 | rejection_reasons = { |
||
86 | "other": other, |
||
87 | "selected": reasons } |
||
88 | obj.setRejectionReasons([rejection_reasons]) |
||
89 | wf.doActionFor(obj, "reject") |
||
90 | processed.append(obj) |
||
91 | |||
92 | # Client needs to be notified? |
||
93 | if sample.get("notify", "") == "on": |
||
94 | notify_rejection(obj) |
||
95 | |||
96 | if not processed: |
||
97 | return self.redirect(message=_("No samples were rejected")) |
||
98 | |||
99 | message = _("Rejected {} samples: {}").format( |
||
100 | len(processed), ", ".join(map(api.get_id, processed))) |
||
101 | return self.redirect(message=message) |
||
102 | |||
103 | # Handle cancel |
||
104 | if form_submitted and form_cancel: |
||
105 | logger.info("*** CANCEL REJECTION ***") |
||
106 | return self.redirect(message=_("Rejection cancelled")) |
||
107 | |||
108 | return self.template() |
||
109 | |||
163 |