Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class LineItem():
- '''think you're supposed to have a docstring here describing the class'''
- def get_mom_yoy(self, model):
- mom = get_mom(model[0].month_1, model[0].month_2)
- yoy = get_mom(model[0].month_1, model[0].month_13)
- return mom, yoy
- def round_if_not_none(self, item):
- if item is not None:
- return round(item, 1)
- else:
- return item
- def get_last_twelve_months(self, model):
- twelve_months_list = \
- list(model.values_list(
- 'month_13', 'month_12', 'month_11', 'month_10', 'month_9', 'month_8',
- 'month_7', 'month_6', 'month_5', 'month_4', 'month_3',
- 'month_2', 'month_1'
- )[0])
- twelve_months_dates = []
- for i in reversed(range(1,14)):
- twelve_months_dates.append(
- datetime.strftime(
- datetime.now() - dateutil.relativedelta.relativedelta(months=i), '%b\'%y'
- )
- )
- twelve_months_list = [0 if x is None else round(x,1) for x in twelve_months_list]
- twelve_months_average = round(sum(twelve_months_list)/len(twelve_months_list),1)
- return twelve_months_list, twelve_months_average, twelve_months_dates
- def get_last_three_months(self, model):
- three_months_list = \
- list(model.values_list(
- 'week_12', 'week_11', 'week_10', 'week_9', 'week_8',
- 'week_7', 'week_6', 'week_5', 'week_4', 'week_3',
- 'week_2', 'week_1'
- )[0])
- three_months_dates = []
- for day in reversed(range(7, 7*4*3+14, 7)):
- stop_day = datetime.now() - dateutil.relativedelta.relativedelta(days=day-7)
- start_day = datetime.now() - dateutil.relativedelta.relativedelta(days=day)
- to_append = str(custom_strftime('%b {S}', start_day) + ' to ' + custom_strftime('%b {S}', stop_day))
- three_months_dates.append(to_append)
- three_months_list = [0 if x is None else round(x,1) for x in three_months_list]
- three_months_average = round(sum(three_months_list)/len(three_months_list),1)
- return three_months_list, three_months_average, three_months_dates
- def get_last_month(self, model):
- day_list = []
- for i in reversed(range(1,32)):
- day_list.append('last_month_day_'+str(i))
- last_month_list = list(model.values_list(*day_list)[0])
- last_month_list = [0 if x is None else round(x,1) for x in last_month_list]
- last_month_average = round(sum(last_month_list)/len(last_month_list),1)
- last_month_dates = []
- start_date = datetime.now() - dateutil.relativedelta.relativedelta(months=1)
- for week in calendar.monthcalendar(start_date.year, start_date.month):
- for w in week:
- if w != 0: last_month_dates.append(start_date.replace(day=w))
- last_month_dates = [custom_strftime('%b {S}', d) for d in last_month_dates]
- return last_month_list, last_month_average, last_month_dates
- def get_this_month(self, model):
- day_list = []
- for i in reversed(range(1, datetime.now().day)):
- day_list.append('day_'+str(i))
- this_month_list = list(model.values_list(*day_list)[0])
- this_month_list = [0 if x is None else round(x,1) for x in this_month_list]
- this_month_average = round(sum(this_month_list)/len(this_month_list),1)
- this_month_dates = []
- start_date = datetime.now().replace(day=1)
- for i in range(1, datetime.now().day):
- this_month_dates.append(datetime.now().replace(day=i))
- this_month_dates = [custom_strftime('%b {S}', d) for d in this_month_dates]
- return this_month_list, this_month_average, this_month_dates
- def get_averages(self, model):
- if current_region:
- base_model = RetailCycleTimesAverages.objects.all()
- try: average = self.round_if_not_none(base_model.get(
- model_name=model.model.__name__,
- object_type = 'region',
- ).month_1)
- except ObjectDoesNotExist: average = 'N/A'
- elif current_branch:
- base_model = RegionalCycleTimesAverages.objects.all()
- if self.branch:
- try: average = self.round_if_not_none(base_model.get(
- model_name=base_model.__class__.__name__,
- object_type = 'branch',
- region = self.branch.region
- ).month_1)
- except ObjectDoesNotExist: return 'N/A'
- return average
- def get_ranks(self, model):
- return 'N/A'
- def __init__(self, metric_name):
- if current_region:
- self.model = model_dict[metric_name].filter(region=current_region)
- elif current_branch:
- self.model = model_dict[metric_name].filter(branch=current_branch)
- elif current_rollup:
- self.model = model_dict[metric_name].filter(rollup=current_rollup)
- self.mtd = self.round_if_not_none(self.model[0].mtd)
- self.month_1 = self.round_if_not_none(self.model[0].month_1)
- self.twelve_months_list ,self.twelve_months_average, self.twelve_months_dates = \
- self.get_last_twelve_months(self.model)
- self.three_months_list, self.three_months_average, self.three_months_dates = \
- self.get_last_three_months(self.model)
- self.last_month_list, self.last_month_average, self.last_month_dates = \
- self.get_last_month(self.model)
- self.this_month_list, self.this_month_average, self.this_month_dates = \
- self.get_this_month(self.model)
- self.ytd = self.round_if_not_none(self.model[0].ytd)
- self.mom, self.yoy = self.get_mom_yoy(self.model)
- self.average = self.get_averages(self.model)
- self.rank = self.get_ranks(self.model)
- def __str__(self):
- metric_name = self.metric_name
- return 'LineItem: ' + str(metric_name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement