From 0428cf06b50203432cb39bde95e450a481392797 Mon Sep 17 00:00:00 2001 From: Lucas Verney Date: Tue, 25 Dec 2018 16:50:14 +0100 Subject: [PATCH] Add bill.creation_date field (#327) --- ihatemoney/migrations/alembic.ini | 1 + ...afbf27e6ef20_add_bill_import_date_field.py | 26 +++++++++++++++ ihatemoney/models.py | 5 ++- ihatemoney/templates/list_bills.html | 3 +- ihatemoney/tests/tests.py | 33 +++++++++++++++++-- 5 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 ihatemoney/migrations/versions/afbf27e6ef20_add_bill_import_date_field.py diff --git a/ihatemoney/migrations/alembic.ini b/ihatemoney/migrations/alembic.ini index f8ed4801..5301449b 100644 --- a/ihatemoney/migrations/alembic.ini +++ b/ihatemoney/migrations/alembic.ini @@ -7,6 +7,7 @@ # set to 'true' to run the environment during # the 'revision' command, regardless of autogenerate # revision_environment = false +script_location = . # Logging configuration diff --git a/ihatemoney/migrations/versions/afbf27e6ef20_add_bill_import_date_field.py b/ihatemoney/migrations/versions/afbf27e6ef20_add_bill_import_date_field.py new file mode 100644 index 00000000..41791558 --- /dev/null +++ b/ihatemoney/migrations/versions/afbf27e6ef20_add_bill_import_date_field.py @@ -0,0 +1,26 @@ +"""add bill.import_date field + +Revision ID: afbf27e6ef20 +Revises: b78f8a8bdb16 +Create Date: 2018-02-19 20:29:26.286136 + +""" + +# revision identifiers, used by Alembic. +revision = 'afbf27e6ef20' +down_revision = 'b78f8a8bdb16' + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + ### commands auto generated by Alembic - please adjust! ### + op.add_column('bill', sa.Column('creation_date', sa.Date(), nullable=True)) + ### end Alembic commands ### + + +def downgrade(): + ### commands auto generated by Alembic - please adjust! ### + op.drop_column('bill', 'creation_date') + ### end Alembic commands ### diff --git a/ihatemoney/models.py b/ihatemoney/models.py index c6ce23fb..9e9fbeb8 100644 --- a/ihatemoney/models.py +++ b/ihatemoney/models.py @@ -163,6 +163,7 @@ class Project(db.Model): .filter(Bill.payer_id == Person.id)\ .filter(Person.project_id == Project.id)\ .filter(Project.id == self.id)\ + .order_by(Bill.creation_date.desc())\ .order_by(Bill.date.desc())\ .order_by(Bill.id.desc()) @@ -329,7 +330,8 @@ class Bill(db.Model): query_class = BillQuery - _to_serialize = ("id", "payer_id", "owers", "amount", "date", "what") + _to_serialize = ("id", "payer_id", "owers", "amount", "date", + "creation_date", "what") id = db.Column(db.Integer, primary_key=True) @@ -338,6 +340,7 @@ class Bill(db.Model): amount = db.Column(db.Float) date = db.Column(db.Date, default=datetime.now) + creation_date = db.Column(db.Date, default=datetime.now) what = db.Column(db.UnicodeText) archive = db.Column(db.Integer, db.ForeignKey("archive.id")) diff --git a/ihatemoney/templates/list_bills.html b/ihatemoney/templates/list_bills.html index 81f8cd56..718009b8 100644 --- a/ihatemoney/templates/list_bills.html +++ b/ihatemoney/templates/list_bills.html @@ -98,10 +98,11 @@ {% if bills.count() > 0 %} - + {% for bill in bills %} + diff --git a/ihatemoney/tests/tests.py b/ihatemoney/tests/tests.py index 63a73942..f3687809 100644 --- a/ihatemoney/tests/tests.py +++ b/ihatemoney/tests/tests.py @@ -9,6 +9,7 @@ try: except ImportError: from mock import patch +import datetime import os import json from collections import defaultdict @@ -1271,7 +1272,13 @@ class APITestCase(IhatemoneyTestCase): "date": "2011-08-10", "id": 1} - self.assertDictEqual(expected, json.loads(req.data.decode('utf-8'))) + got = json.loads(req.data.decode('utf-8')) + self.assertEqual( + datetime.date.today(), + datetime.datetime.strptime(got["creation_date"], '%Y-%m-%d').date() + ) + del got["creation_date"] + self.assertDictEqual(expected, got) # the list of bills should length 1 req = self.client.get("/api/projects/raclette/bills", @@ -1303,6 +1310,10 @@ class APITestCase(IhatemoneyTestCase): # check its fields req = self.client.get("/api/projects/raclette/bills/1", headers=self.get_auth("raclette")) + creation_date = datetime.datetime.strptime( + json.loads(req.data.decode('utf-8'))["creation_date"], + '%Y-%m-%d' + ).date() expected = { "what": "beer", @@ -1314,7 +1325,13 @@ class APITestCase(IhatemoneyTestCase): "date": "2011-09-10", "id": 1} - self.assertDictEqual(expected, json.loads(req.data.decode('utf-8'))) + got = json.loads(req.data.decode('utf-8')) + self.assertEqual( + creation_date, + datetime.datetime.strptime(got["creation_date"], '%Y-%m-%d').date() + ) + del got["creation_date"] + self.assertDictEqual(expected, got) # delete a bill req = self.client.delete("/api/projects/raclette/bills/1", @@ -1393,6 +1410,10 @@ class APITestCase(IhatemoneyTestCase): # get this bill details req = self.client.get("/api/projects/raclette/bills/1", headers=self.get_auth("raclette")) + creation_date = datetime.datetime.strptime( + json.loads(req.data.decode('utf-8'))["creation_date"], + '%Y-%m-%d' + ).date() # compare with the added info self.assertStatus(200, req) @@ -1405,7 +1426,13 @@ class APITestCase(IhatemoneyTestCase): "amount": 25.0, "date": "2011-08-10", "id": 1} - self.assertDictEqual(expected, json.loads(req.data.decode('utf-8'))) + got = json.loads(req.data.decode('utf-8')) + self.assertEqual( + creation_date, + datetime.datetime.strptime(got["creation_date"], '%Y-%m-%d').date() + ) + del got["creation_date"] + self.assertDictEqual(expected, got) # getting it should return a 404 req = self.client.get("/api/projects/raclette",
{{ _("When?") }}{{ _("Who paid?") }}{{ _("For what?") }}{{ _("For whom?") }}{{ _("How much?") }}{{ _("Actions") }}
{{ _("Added on") }}{{ _("When?") }}{{ _("Who paid?") }}{{ _("For what?") }}{{ _("For whom?") }}{{ _("How much?") }}{{ _("Actions") }}
{{ bill.creation_date if bill.creation_date else bill.date }} {{ bill.date }} {{ bill.payer }} {{ bill.what }}