def get_last_twelve_months_tag_expenses(self): last_year = date_helper.get_last_year_date() logging.info("last year = %d" % last_year.year) month_year = last_year.year * 12 + last_year.month logging.info("month year = %d" % month_year) self.compute_labels(last_year) q = Tag.query(ancestor=Tag.get_tag_parent_key(self.category)) tags = q.fetch(1000) amounts = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] for tag in tags: if tag.amount is not None: index = tag.monthyear - month_year if index not in range(0, 12): continue logging.info("tag=%s amount=%f index=%d" % (tag.name, tag.amount, index)) amounts[index] += tag.amount self.totalamount += tag.amount self.compute_max_amount(amounts)
def get_last_twelve_months_category_expenses(self): last_year = date_helper.get_last_year_date() self.compute_labels(last_year) #TODO refactor #query = db.GqlQuery("SELECT * FROM Expenses WHERE category = :1 AND date >= :2 AND exptype = 1", self.category, last_year) #expenses = query.fetch(5000) q = Expenses.query() q.filter(ndb.AND(Expenses.category == self.category, Expenses.date >= last_year, Expenses.exptype == 1)) expenses = q.fetch(5000) amounts = [] for i in range(0, 12): amounts.append(0) for exp in expenses: index = date_helper.get_month_index(exp.date) if index != -1: amounts[index] += exp.amount self.totalamount += exp.amount self.compute_max_amount(amounts)