Пример #1
0
    def get_user_expenses(cls, month, year):
        """

        """
        first_day = date(year, month, 1)
        if month == 12:
            last_day = date(year + 1, 1, 1)
        else:
            last_day = date(year, month + 1, 1)

        logging.info("load user expenses between %s and %s" %
                     (first_day.strftime('%d %b %Y'), last_day.strftime('%d %b %Y')))

        q = Expenses.query(ancestor=expenses_key(month, year))
        q.filter(ndb.AND(Expenses.date >= first_day, Expenses.date < last_day))

        #query for old data that had no ancestor (before migration to High Replication Datastore)
        q_old = Expenses.query(ndb.AND(Expenses.date >= first_day, Expenses.date < last_day))

        users_expenses = dict()
        keys = set()
        if users_helper.is_autorized():
            # always start with the current user
            currentUser = users.get_current_user().nickname()
            users_expenses[currentUser] = UserExpenses(currentUser)

            x = 0
            #TODO Hem refactor this ugly code
            for exp in q.fetch(500):
                exp.update_urlsafe()
                keys.add(exp.urlsafe)
                user_nickname = exp.user.nickname()
                if user_nickname not in users_expenses.keys():
                    users_expenses[user_nickname] = UserExpenses(user_nickname)
                users_expenses[user_nickname].add(exp)
                x += 1
            logging.info('%d expenses with ancestor', x)

            x = 0
            for exp in q_old.fetch(500):
                exp.update_urlsafe()
                if exp.urlsafe not in keys:
                    user_nickname = exp.user.nickname()
                    if user_nickname not in users_expenses.keys():
                        users_expenses[user_nickname] = UserExpenses(user_nickname)
                    users_expenses[user_nickname].add(exp)
                    x += 1
            logging.info('%d expenses with no ancestor', x)

        for usr in users_expenses.keys():
            users_expenses[usr].taxratio = cls.get_tax_ratio(usr, year)
            users_expenses[usr].expenses = sorted(users_expenses[usr].expenses, key=lambda e: e.date)

        return users_expenses.values()
Пример #2
0
 def get(self):
     # print 'coucou'
     # query = db.GqlQuery('SELECT * FROM Expenses')
     query = Expenses.query()
     expenses = query.fetch(10000)
     self.jsonData = dict()
     self.jsonData["tags"] = dict()
     for exp in expenses:
         for tag in exp.tags:
             if tag != "":
                 self.update_tag(tag, exp.amount, exp.date)
     self.persist()
     self.response.out.write(simplejson.dumps(self.jsonData))
Пример #3
0
    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)