def get(self): user = users.get_current_user() if user is None: self.redirect(users.create_login_url(self.request.uri)) return dtnow = datetime.utcnow() try: month = int(self.request.get('mois')) year = int(self.request.get('annee')) except: month = dtnow.month year = dtnow.year users_expenses = UserExpenses.get_user_expenses(month, year) #months = date_helper.get_all_months() months = range(1, 13) # for the moment the status is only computed when there are only 2 users status = {'maxuser': user, 'minuser': users_helper.get_other_nickname(), 'debt': 0} if len(users_expenses) == 2: status = UserExpenses.compute_balance(users_expenses, month, year) logging.info(status) #month_name = months[month-1]['Name'] template_values = { 'logouturl': users.create_logout_url("/"), 'currentuser': users.get_current_user().nickname(), 'selectedmonth': month, #'selectedmonthname': month_name, 'selectedyear': year, 'month_range': months, 'year_range': range(2009, dtnow.year + 1), 'usersexpenses': users_expenses, 'status_maxuser': status['maxuser'], 'status_minuser': status['minuser'], 'status_debt': '%.2f' % status['debt'], 'nextmonth': date_helper.get_next_month(month), 'nextyear': date_helper.get_year_of_next_month(month, year), 'previousmonth': date_helper.get_previous_month(month), 'previousyear': date_helper.get_year_of_previous_month(month, year) } #session["monthname"] = months[month-1]['Name'] path = os.path.join(os.path.dirname(__file__), '../templates/index.html') cookie_value = "%d#%d"%(month, year) logging.info("_balanceSession = "+ cookie_value) self.response.headers.add_header('Set-Cookie', '_balanceSession=' + cookie_value + ';path=/;max-age=' + str(7*24*3600) + ';') self.response.out.write(template.render(path, template_values))
def post(self): logging.debug("taxratio: " + str(self.request.params["taxratio"])) logging.debug("year: " + str(self.request.params["year"])) ratio = float(self.request.params["taxratio"]) year = int(self.request.params["year"]) user = users.get_current_user().nickname() self.update_tax_ratio(ratio, year, user) # TODO: update all the balances of the year... new_balances = UserExpenses.compute_year_balances(year) self.response.out.write(simplejson.dumps({"taxratio": ratio, "year": year, "newbalances": new_balances}))
def update_tax_ratio(self, ratio, year, user): query = TaxRatio.query(TaxRatio.year == year) update = False for taxratio in query.fetch(2): if taxratio.user == user: taxratio.ratio = ratio else: taxratio.ratio = 1 - ratio logging.debug("saving ratio %s for user %s" % (str(taxratio), user)) taxratio.put() update = True if not update: taxratioa = TaxRatio(user=user, ratio=ratio, year=year) logging.debug("saving %s" % str(taxratioa)) taxratioa.put() taxratiob = TaxRatio(user=users_helper.get_other_nickname(), ratio=1 - ratio, year=year) logging.debug("saving %s" % str(taxratiob)) taxratiob.put() else: # update balance with this new tax ratio for month in range(1, 12): user_expenses = UserExpenses.get_user_expenses(month, year) UserExpenses.compute_balance(user_expenses, month, year)