| Conditions | 16 |
| Total Lines | 78 |
| Code Lines | 63 |
| 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.subscribers.pricelist.ObjectModifiedEventHandler() 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 -*- |
||
| 23 | def ObjectModifiedEventHandler(instance, event): |
||
| 24 | """ Various types need automation on edit. |
||
| 25 | """ |
||
| 26 | if not hasattr(instance, 'portal_type'): |
||
| 27 | return |
||
| 28 | |||
| 29 | if instance.portal_type == 'Pricelist': |
||
| 30 | """ Create price list line items |
||
| 31 | """ |
||
| 32 | # Remove existing line items |
||
| 33 | instance.pricelist_lineitems = [] |
||
| 34 | for p in instance.portal_catalog(portal_type=instance.getType(), |
||
| 35 | is_active=True): |
||
| 36 | obj = p.getObject() |
||
| 37 | itemDescription = None |
||
| 38 | itemAccredited = False |
||
| 39 | if instance.getType() == "LabProduct": |
||
| 40 | print_detail = "" |
||
| 41 | if obj.getVolume(): |
||
| 42 | print_detail = print_detail + str(obj.getVolume()) |
||
| 43 | if obj.getUnit(): |
||
| 44 | print_detail = print_detail + str(obj.getUnit()) |
||
| 45 | if obj.getVolume() or obj.getUnit(): |
||
| 46 | print_detail = " (" + print_detail + ")" |
||
| 47 | itemTitle = obj.Title() + print_detail |
||
| 48 | else: |
||
| 49 | itemTitle = obj.Title() |
||
| 50 | cat = None |
||
| 51 | if obj.getPrice(): |
||
| 52 | price = float(obj.getPrice()) |
||
| 53 | totalprice = float(obj.getTotalPrice()) |
||
| 54 | vat = totalprice - price |
||
| 55 | else: |
||
| 56 | price = 0 |
||
| 57 | totalprice = 0 |
||
| 58 | vat = 0 |
||
| 59 | elif instance.getType() == "AnalysisService": |
||
| 60 | |||
| 61 | if str(obj.getUnit()): |
||
| 62 | print_detail = " (" + str(obj.getUnit()) + ")" |
||
| 63 | itemTitle = obj.Title() + print_detail |
||
| 64 | else: |
||
| 65 | itemTitle = obj.Title() |
||
| 66 | itemAccredited = obj.getAccredited() |
||
| 67 | |||
| 68 | cat = obj.getCategoryTitle() |
||
| 69 | if instance.getBulkDiscount(): |
||
| 70 | price = float(obj.getBulkPrice()) |
||
| 71 | vat = get_vat_amount(price, obj.getVAT()) |
||
| 72 | totalprice = price + vat |
||
| 73 | else: |
||
| 74 | if instance.getBulkPrice(): |
||
| 75 | discount = instance.getBulkPrice() |
||
| 76 | price = float(obj.getPrice()) |
||
| 77 | price = apply_discount(price, discount) |
||
| 78 | vat = get_vat_amount(price, obj.getVAT()) |
||
| 79 | totalprice = price + vat |
||
| 80 | elif obj.getPrice(): |
||
| 81 | price = float(obj.getPrice()) |
||
| 82 | vat = get_vat_amount(price, obj.getVAT()) |
||
| 83 | totalprice = price + vat |
||
| 84 | else: |
||
| 85 | totalprice = 0 |
||
| 86 | price = 0 |
||
| 87 | vat = 0 |
||
| 88 | |||
| 89 | if instance.getDescriptions(): |
||
| 90 | itemDescription = obj.Description() |
||
| 91 | |||
| 92 | li = PricelistLineItem() |
||
| 93 | li["title"] = itemTitle |
||
|
|
|||
| 94 | li["ItemDescription"] = itemDescription |
||
| 95 | li["CategoryTitle"] = cat |
||
| 96 | li["Accredited"] = itemAccredited |
||
| 97 | li["Subtotal"] = "%0.2f" % price |
||
| 98 | li["VATAmount"] = "%0.2f" % vat |
||
| 99 | li["Total"] = "%0.2f" % totalprice |
||
| 100 | instance.pricelist_lineitems.append(li) |
||
| 101 |