Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.44 KB | None | 0 0
  1. class LineItem():
  2.  
  3. '''think you're supposed to have a docstring here describing the class'''
  4.  
  5. def get_mom_yoy(self, model):
  6. mom = get_mom(model[0].month_1, model[0].month_2)
  7. yoy = get_mom(model[0].month_1, model[0].month_13)
  8. return mom, yoy
  9.  
  10. def round_if_not_none(self, item):
  11. if item is not None:
  12. return round(item, 1)
  13. else:
  14. return item
  15.  
  16. def get_last_twelve_months(self, model):
  17. twelve_months_list = \
  18. list(model.values_list(
  19. 'month_13', 'month_12', 'month_11', 'month_10', 'month_9', 'month_8',
  20. 'month_7', 'month_6', 'month_5', 'month_4', 'month_3',
  21. 'month_2', 'month_1'
  22. )[0])
  23. twelve_months_dates = []
  24. for i in reversed(range(1,14)):
  25. twelve_months_dates.append(
  26. datetime.strftime(
  27. datetime.now() - dateutil.relativedelta.relativedelta(months=i), '%b\'%y'
  28. )
  29. )
  30. twelve_months_list = [0 if x is None else round(x,1) for x in twelve_months_list]
  31. twelve_months_average = round(sum(twelve_months_list)/len(twelve_months_list),1)
  32.  
  33. return twelve_months_list, twelve_months_average, twelve_months_dates
  34.  
  35. def get_last_three_months(self, model):
  36. three_months_list = \
  37. list(model.values_list(
  38. 'week_12', 'week_11', 'week_10', 'week_9', 'week_8',
  39. 'week_7', 'week_6', 'week_5', 'week_4', 'week_3',
  40. 'week_2', 'week_1'
  41. )[0])
  42. three_months_dates = []
  43. for day in reversed(range(7, 7*4*3+14, 7)):
  44. stop_day = datetime.now() - dateutil.relativedelta.relativedelta(days=day-7)
  45. start_day = datetime.now() - dateutil.relativedelta.relativedelta(days=day)
  46. to_append = str(custom_strftime('%b {S}', start_day) + ' to ' + custom_strftime('%b {S}', stop_day))
  47. three_months_dates.append(to_append)
  48. three_months_list = [0 if x is None else round(x,1) for x in three_months_list]
  49. three_months_average = round(sum(three_months_list)/len(three_months_list),1)
  50.  
  51. return three_months_list, three_months_average, three_months_dates
  52.  
  53. def get_last_month(self, model):
  54. day_list = []
  55. for i in reversed(range(1,32)):
  56. day_list.append('last_month_day_'+str(i))
  57. last_month_list = list(model.values_list(*day_list)[0])
  58. last_month_list = [0 if x is None else round(x,1) for x in last_month_list]
  59. last_month_average = round(sum(last_month_list)/len(last_month_list),1)
  60.  
  61. last_month_dates = []
  62. start_date = datetime.now() - dateutil.relativedelta.relativedelta(months=1)
  63. for week in calendar.monthcalendar(start_date.year, start_date.month):
  64. for w in week:
  65. if w != 0: last_month_dates.append(start_date.replace(day=w))
  66. last_month_dates = [custom_strftime('%b {S}', d) for d in last_month_dates]
  67.  
  68. return last_month_list, last_month_average, last_month_dates
  69.  
  70. def get_this_month(self, model):
  71. day_list = []
  72. for i in reversed(range(1, datetime.now().day)):
  73. day_list.append('day_'+str(i))
  74. this_month_list = list(model.values_list(*day_list)[0])
  75. this_month_list = [0 if x is None else round(x,1) for x in this_month_list]
  76. this_month_average = round(sum(this_month_list)/len(this_month_list),1)
  77.  
  78. this_month_dates = []
  79. start_date = datetime.now().replace(day=1)
  80. for i in range(1, datetime.now().day):
  81. this_month_dates.append(datetime.now().replace(day=i))
  82. this_month_dates = [custom_strftime('%b {S}', d) for d in this_month_dates]
  83.  
  84. return this_month_list, this_month_average, this_month_dates
  85.  
  86. def get_averages(self, model):
  87. if current_region:
  88. base_model = RetailCycleTimesAverages.objects.all()
  89. try: average = self.round_if_not_none(base_model.get(
  90. model_name=model.model.__name__,
  91. object_type = 'region',
  92. ).month_1)
  93. except ObjectDoesNotExist: average = 'N/A'
  94. elif current_branch:
  95. base_model = RegionalCycleTimesAverages.objects.all()
  96. if self.branch:
  97. try: average = self.round_if_not_none(base_model.get(
  98. model_name=base_model.__class__.__name__,
  99. object_type = 'branch',
  100. region = self.branch.region
  101. ).month_1)
  102. except ObjectDoesNotExist: return 'N/A'
  103. return average
  104.  
  105. def get_ranks(self, model):
  106. return 'N/A'
  107.  
  108. def __init__(self, metric_name):
  109. if current_region:
  110. self.model = model_dict[metric_name].filter(region=current_region)
  111. elif current_branch:
  112. self.model = model_dict[metric_name].filter(branch=current_branch)
  113. elif current_rollup:
  114. self.model = model_dict[metric_name].filter(rollup=current_rollup)
  115. self.mtd = self.round_if_not_none(self.model[0].mtd)
  116. self.month_1 = self.round_if_not_none(self.model[0].month_1)
  117. self.twelve_months_list ,self.twelve_months_average, self.twelve_months_dates = \
  118. self.get_last_twelve_months(self.model)
  119. self.three_months_list, self.three_months_average, self.three_months_dates = \
  120. self.get_last_three_months(self.model)
  121. self.last_month_list, self.last_month_average, self.last_month_dates = \
  122. self.get_last_month(self.model)
  123. self.this_month_list, self.this_month_average, self.this_month_dates = \
  124. self.get_this_month(self.model)
  125. self.ytd = self.round_if_not_none(self.model[0].ytd)
  126. self.mom, self.yoy = self.get_mom_yoy(self.model)
  127. self.average = self.get_averages(self.model)
  128. self.rank = self.get_ranks(self.model)
  129.  
  130. def __str__(self):
  131. metric_name = self.metric_name
  132. return 'LineItem: ' + str(metric_name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement