| Conditions | 23 |
| Total Lines | 87 |
| 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:
Complex classes like pyfreebill.management.commands.Command.handle() 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 | # Copyright 2013 Mathias WOLFF |
||
| 26 | def handle(self, *args, **options): |
||
| 27 | for var in args: |
||
| 28 | # try: |
||
| 29 | |||
| 30 | if var == "balance": |
||
| 31 | #print "balance" |
||
| 32 | qs = Company.objects.filter(customer_enabled=True).exclude(email_alert__isnull=True).exclude(email_alert__exact='') |
||
| 33 | for c in qs: |
||
| 34 | #print "name %s - balance %s - email : %s "% (c.name, c.customer_balance, c.email_alert) |
||
| 35 | send_templated_mail( |
||
| 36 | template_name='balance', |
||
| 37 | from_email=settings.EMAIL_HOST_USER, |
||
| 38 | recipient_list=[c.email_alert], |
||
| 39 | context={ |
||
| 40 | 'company':c.name, |
||
| 41 | 'balance':c.customer_balance, |
||
| 42 | 'signature':settings.EMAIL_SIGNATURE |
||
| 43 | }, |
||
| 44 | ) |
||
| 45 | elif var == "lowbalance": |
||
| 46 | print "lowbalance" |
||
| 47 | qs = Company.objects.filter(customer_enabled=True).exclude(email_alert__isnull=True).exclude(email_alert__exact='') |
||
| 48 | for c in qs: |
||
| 49 | """ CREDIT LIMIT ALERT """ |
||
| 50 | print "name %s - balance %s - low_credit_alert : %s - low_credit_alert_sent : %s"% (c.name, c.customer_balance, c.low_credit_alert, c.low_credit_alert_sent) |
||
| 51 | # Check alert status and update if necessary |
||
| 52 | if c.low_credit_alert_sent == True and c.customer_balance > c.low_credit_alert: |
||
| 53 | print "balance > low_credit_alert and low_credit_alert_sent is true" |
||
| 54 | # set low_credit_alert to False |
||
| 55 | c.low_credit_alert_sent = False |
||
| 56 | c.save() |
||
| 57 | elif c.low_credit_alert_sent == False and c.customer_balance > c.low_credit_alert: |
||
| 58 | print "balance > low_credit_alert and low_credit_alert_sent is false" |
||
| 59 | # Nothing to do - good |
||
| 60 | pass |
||
| 61 | elif c.low_credit_alert_sent == False and c.customer_balance <= c.low_credit_alert: |
||
| 62 | print "balance < low_credit_alert and low_credit_alert_sent is false" |
||
| 63 | # set low_credit_alert to True |
||
| 64 | c.low_credit_alert_sent = True |
||
| 65 | c.save() |
||
| 66 | # send alert email |
||
| 67 | send_templated_mail( |
||
| 68 | template_name='lowbalance', |
||
| 69 | from_email=settings.EMAIL_HOST_USER, |
||
| 70 | recipient_list=[c.email_alert], |
||
| 71 | context={ |
||
| 72 | 'company':c.name, |
||
| 73 | 'balance':c.customer_balance, |
||
| 74 | 'creditalert':c.low_credit_alert, |
||
| 75 | 'signature':settings.EMAIL_SIGNATURE |
||
| 76 | }, |
||
| 77 | ) |
||
| 78 | """ CREDIT OVER """ |
||
| 79 | print "name %s - prepaid : %s - balance %s - credit_limit : %s - account_blocked_alert_sent : %s"% (c.name, c.prepaid, c.customer_balance, c.credit_limit, c.account_blocked_alert_sent) |
||
| 80 | # Check alert status and update if necessary |
||
| 81 | if c.account_blocked_alert_sent == True: |
||
| 82 | if ((c.prepaid == False and c.customer_balance > c.credit_limit) or (c.prepaid == True and c.customer_balance > 0)): |
||
| 83 | print "balance > credit_limit or 0 and account_blocked_alert_sent is true" |
||
| 84 | # set account_blocked_alert to False |
||
| 85 | c.account_blocked_alert_sent = False |
||
| 86 | c.save() |
||
| 87 | elif c.account_blocked_alert_sent == False: |
||
| 88 | if ((c.prepaid == False and c.customer_balance <= c.credit_limit) or (c.prepaid == True and c.customer_balance <= 0)): |
||
| 89 | print "balance < credit_limit or 0 and account_blocked_alert_sent is false" |
||
| 90 | # set account_blocked_alert to True |
||
| 91 | c.account_blocked_alert_sent = True |
||
| 92 | c.save() |
||
| 93 | # send alert email |
||
| 94 | send_templated_mail( |
||
| 95 | template_name='nobalance', |
||
| 96 | from_email=settings.EMAIL_HOST_USER, |
||
| 97 | recipient_list=[c.email_alert], |
||
| 98 | context={ |
||
| 99 | 'company':c.name, |
||
| 100 | 'balance':c.customer_balance, |
||
| 101 | 'signature':settings.EMAIL_SIGNATURE |
||
| 102 | }, |
||
| 103 | ) |
||
| 104 | elif var == "custom": |
||
| 105 | pass |
||
| 106 | else: |
||
| 107 | return |
||
| 108 | |||
| 109 | # except: |
||
| 110 | # return CommandError |
||
| 111 | |||
| 112 | self.stdout.write('Successfully alerts ') |
||
| 113 |