def post(self):
        """Handle end month and create new month"""

        action = self.request.get("action")

        # create new month
        if action == "new":
            try:
                # get & validate info in request
                people_key_strings = self.request.get_all("people")
                if len(people_key_strings) == 0:
                    raise ValueError(
                        "There must be at least one person in a month.")
                month = Month.new_month(people_key_strings)
            except ValueError as error:
                self.render_new_month(error=error.message)
            else:
                self.redirect("/month/" + month.key.urlsafe())

        # just end month
        elif action == "end":
            month = Month.end_month()
            if month:
                self.redirect("/month/" + month.key.urlsafe())

        # invalid action
        else:
            self.render_new_month(error="Invalid action")
    def render_new_month(self, **kwargs):
        # new month info
        now = datetime.now()
        month = now.strftime("%B %Y")
        start_date = now.strftime("%d/%m/%y")
        people = Person.get_all()

        # prev month
        prev_month = Month.get_current_month()
        if prev_month and not prev_month.time_end:
            # convert people key to name
            prev_month.time_to_end = now
            prev_month.people_name = []
            for person_key in prev_month.people:
                for person in people:
                    if person.key == person_key:
                        prev_month.people_name.append(person.name)
            prev_month.people_name = ", ".join(prev_month.people_name)

        # render html
        self.render("newmonth.html",
                    month=month,
                    start_date=start_date,
                    page_title=self.PAGE_TITLE,
                    people=people,
                    prev_month=prev_month,
                    **kwargs)
Beispiel #3
0
    def buildJoinedModels(self, model, row):
        keys = row.keys()
        model.tipoRenda = TipoRenda(
            id=row['tipo_renda_id'],
            desc=row['desc'] if 'desc' in keys else None,
            auto=row['auto'] if 'auto' in keys else None)

        model.month = Month(id=row['month_id'] if 'month_id' in keys else None,
                            month=row['month'] if 'month' in keys else None)
Beispiel #4
0
    def get_person_info(self):
        try:
            key = Key(urlsafe=self.request.get("key"))
            person = key.get()

            # get total spend
            months = list(Month.query().filter(Month.people == key).fetch())
            total_spend = sum(month.average for month in months) * 1000

            # get payments made for every one
            money_usages = list(
                MoneyUsage.query().filter(MoneyUsage.person == key).fetch())
            payment = sum(money_usage.money_spend
                          for money_usage in money_usages) * 1000

            # last month
            if person.last_money_usage:
                last_money_usage = person.last_money_usage.get()
                last_month = last_money_usage.month.get()

                last_month_str = last_month.to_string_long()
                next_month_left = last_money_usage.next_month_left * 1000
                in_current_month = last_month.key == Month.get_current_month_key(
                )
            else:
                last_month_str = next_month_left = in_current_month = "N/A"

            # write
            s = ";".join(
                map(unicode, [
                    person.name,
                    key.id(), total_spend,
                    len(months), payment, last_month_str, next_month_left,
                    in_current_month
                ]))
            self.write(s)

        except Exception as e:
            print(e)
            self.response.status = 409
            self.write(
                "Can not resolve this buyer's key. Please reload this page or try again later"
            )
Beispiel #5
0
 def get(self):
     if not is_login(self):
         self.response.set_cookie("redirect", "/admin")
         self.redirect("/admin/login")
     else:
         self.render(
             "admin.html",
             app_name=app_config["app-name"],
             account=AdminAccount.get(),
             people=Person.get_all(),
             months=Month.get_all(),
         )
Beispiel #6
0
 def buildJoinedModels(self, model, row):
     keys = row.keys()
     model.despesa = Despesa(
         id = row['id'] if 'id' in keys else None, 
         desc = row['desc'] if 'desc' in keys else None, 
         val = row['val'] if 'val' in keys else None, 
         paidVal = row['paid_val'] if 'paid_val' in keys else None, 
         paid = row['paid'] if 'paid' in keys else None, 
         month = Month(
             id = row['month_id'] if 'month_id' in keys else None,
             month = row['month'] if 'month' in keys else None
         )
     )
Beispiel #7
0
    def __init__(self, user, month = None):
        if month and type(month).__name__ == 'Month':
            self.month = month
        else:
            self.month = Month()
            if not month:
                self.month.month = self.getCurrentMonth()
            elif type(month).__name__ == 'date':
                self.month.month = month.replace(day = 1)
            else:
                self.month.month = self.convertMonthInputDate(month)

        self.month.user = user
        super().__init__(month = self.month)
        self.monthDAO = MonthDAO()
Beispiel #8
0
 def get(self, *args):
     """Render home page"""
     if args[0] is None:
         self.redirect("/home")
     else:
         self.render("home.html", months=Month.get_all(), page_title=self.PAGE_TITLE)
Beispiel #9
0
 def buildJoinedModels(self, model, row):
     despesa = Despesa(id=row['id'],
                       month=Month(id=row['month_id'],
                                   month=row['despesa_month']))
     despesaTemp = DespesaTemp(despesa=despesa)
     model.despesaTemp = despesaTemp