Conditions | 8 |
Total Lines | 58 |
Code Lines | 46 |
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:
1 | # -*- coding: utf-8 -*- |
||
81 | def __call__(self): |
||
82 | portal = self.portal |
||
83 | request = self.request |
||
84 | context = self.context |
||
85 | setup = portal.bika_setup |
||
86 | # Allow adding items to this context |
||
87 | context.setConstrainTypesMode(0) |
||
88 | # Collect the products |
||
89 | products = setup.bika_labproducts.objectValues("LabProduct") |
||
90 | |||
91 | # Handle for submission and regular request |
||
92 | if "submit" in request: |
||
93 | portal_factory = getToolByName(context, "portal_factory") |
||
94 | context = portal_factory.doCreate(context, context.id) |
||
95 | context.processForm() |
||
96 | # Clear the old line items |
||
97 | context.supplyorder_lineitems = [] |
||
98 | # Process the order item data |
||
99 | for prodid, qty in request.form.items(): |
||
100 | if prodid.startswith("product_") and qty and float(qty) > 0: |
||
101 | prodid = prodid.replace("product_", "") |
||
102 | product = setup.bika_labproducts[prodid] |
||
103 | context.supplyorder_lineitems.append( |
||
104 | {"Product": prodid, |
||
105 | "Quantity": qty, |
||
106 | "Price": product.getPrice(), |
||
107 | "VAT": product.getVAT()}) |
||
108 | |||
109 | # Redirect to the list of orders |
||
110 | obj_url = context.absolute_url_path() |
||
111 | request.response.redirect(obj_url) |
||
112 | return |
||
113 | else: |
||
114 | self.orderDate = context.Schema()["OrderDate"] |
||
115 | self.contact = context.Schema()["Contact"] |
||
116 | self.subtotal = "%.2f" % context.getSubtotal() |
||
117 | self.vat = "%.2f" % context.getVATAmount() |
||
118 | self.total = "%.2f" % context.getTotal() |
||
119 | # Prepare the products |
||
120 | items = context.supplyorder_lineitems |
||
121 | self.products = [] |
||
122 | products = sorted(products, key=methodcaller("Title")) |
||
123 | for product in products: |
||
124 | item = [o for o in items if o["Product"] == product.getId()] |
||
125 | quantity = item[0]["Quantity"] if len(item) > 0 else 0 |
||
126 | self.products.append({ |
||
127 | "id": product.getId(), |
||
128 | "title": product.Title(), |
||
129 | "description": product.Description(), |
||
130 | "volume": product.getVolume(), |
||
131 | "unit": product.getUnit(), |
||
132 | "price": product.getPrice(), |
||
133 | "vat": "%s%%" % product.getVAT(), |
||
134 | "quantity": quantity, |
||
135 | "total": (float(product.getPrice()) * float(quantity)), |
||
136 | }) |
||
137 | # Render the template |
||
138 | return self.template() |
||
139 | |||
165 |