Beispiel #1
0
def template_run(request, pk):
    template = get_object_or_404(ExpenseTemplate, pk=pk, user=request.user)
    expense = Expense(vendor=template.vendor, category=template.category)
    if "date" in request.GET:
        expense.date = request.GET["date"]
    else:
        expense.date = today_date()

    if template.type == "count":
        if not request.GET.get("count"):
            count = decimal.Decimal(1)
        else:
            count = parse_amount_input(request.GET["count"])
            if count is None:
                return HttpResponseBadRequest()

        expense.amount = round_money(template.amount * count)
        desc_lines = template.description.strip().split("\n")
        desc_possibilities = len(desc_lines)
        desc = desc_lines[0]
        if count % 1 != 0:
            # Is decimal, use last possibility
            desc = desc_lines[desc_possibilities - 1]
        elif desc_possibilities == 2:
            # 0 → 1, 1 → anything else (English)
            desc = desc_lines[int(count != 1)]
        elif desc_possibilities in {3, 4}:
            # Polish scheme
            if count == 1:
                desc = desc_lines[0]
            else:
                # Expression from gettext, simplified
                desc = desc_lines[1 if (2 <= count % 10 <= 4 and (count % 100 < 10 or count % 100 >= 20)) else 2]

        expense.description = desc.replace("!count!", str(count))
    elif template.type == "description":
        expense.amount = template.amount
        expense.description = template.description.replace("!description!", request.GET["description"])
    elif template.type == "desc_select":
        main_desc, *desc_options = template.description.strip().split("\n")
        desc_id = int(request.GET["desc_id"])
        expense.amount = template.amount
        expense.description = main_desc.strip().replace("!description!", desc_options[desc_id].strip())
    elif template.type == "menu":
        desc_id = int(request.GET["desc_id"])
        desc_options = template.description.strip().split("\n")
        amount_str, desc = desc_options[desc_id].strip().split(" ", 1)
        expense.amount = parse_amount_input(amount_str.strip())
        if expense.amount is None:
            return HttpResponseBadRequest()
        expense.description = desc.strip()
    else:
        expense.amount = template.amount
        expense.description = template.description

    expense.user = request.user
    expense.save()
    return HttpResponseRedirect(expense.get_absolute_url())
Beispiel #2
0
 def handle(self, n, **options):
     for i in range(n):
         o = Expense()
         o.title = silly.a_thing()
         o.amount = "{}.{}".format(random.randint(1, 100),
                                   random.randint(0, 99))
         o.date = silly.datetime().date()
         o.description = "\n".join(
             [silly.paragraph(),
              silly.paragraph(),
              silly.paragraph()])
         o.save()
Beispiel #3
0
    def create_random_expense(self):
        with transaction.atomic():
            o = Expense()
            o.title = silly.a_thing()
            o.amount = "{}.{}".format(random.randint(1, 100),
                                      random.randint(0, 99))
            o.date = silly.datetime().date()
            o.description = "\n".join(
                [silly.paragraph(),
                 silly.paragraph(),
                 silly.paragraph()])
            o.save()

            for j in range(random.randint(0, 5)):
                text = "\n".join(
                    [silly.paragraph(),
                     silly.paragraph(),
                     silly.paragraph()])
                o.comments.create(content=text)