Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added tagging functionality and filtering by date, amount, payer, tag #557

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion ihatemoney/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from werkzeug.security import check_password_hash, generate_password_hash
from wtforms.fields.core import SelectField, SelectMultipleField
from wtforms.fields.html5 import DateField, DecimalField, URLField
from wtforms.fields.simple import BooleanField, PasswordField, StringField, SubmitField
from wtforms.fields.simple import BooleanField, HiddenField, PasswordField, StringField, SubmitField
from wtforms.validators import (
DataRequired,
Email,
Expand Down Expand Up @@ -197,6 +197,7 @@ class ResetPasswordForm(FlaskForm):
class BillForm(FlaskForm):
date = DateField(_("Date"), validators=[DataRequired()], default=datetime.now)
what = StringField(_("What?"), validators=[DataRequired()])
tag = HiddenField(_("Tag"), default="")
payer = SelectField(_("Payer"), validators=[DataRequired()], coerce=int)
amount = CalculatorStringField(_("Amount paid"), validators=[DataRequired()])
external_link = URLField(
Expand All @@ -214,6 +215,9 @@ def save(self, bill, project):
bill.payer_id = self.payer.data
bill.amount = self.amount.data
bill.what = self.what.data
tag = list(set(part[1:] for part in bill.what.split() if part.startswith('#')))
if tag:
bill.tag = tag[0]
Glandos marked this conversation as resolved.
Show resolved Hide resolved
bill.external_link = self.external_link.data
bill.date = self.date.data
bill.owers = [Person.query.get(ower, project) for ower in self.payed_for.data]
Expand All @@ -223,6 +227,7 @@ def fake_form(self, bill, project):
bill.payer_id = self.payer
bill.amount = self.amount
bill.what = self.what
bill.tag = self.tag
bill.external_link = ""
bill.date = self.date
bill.owers = [Person.query.get(ower, project) for ower in self.payed_for]
Expand All @@ -233,6 +238,7 @@ def fill(self, bill):
self.payer.data = bill.payer_id
self.amount.data = bill.amount
self.what.data = bill.what
self.tag.data = bill.tag
self.external_link.data = bill.external_link
self.date.data = bill.date
self.payed_for.data = [int(ower.id) for ower in bill.owers]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""added tag column to bill model

Revision ID: 49fc258cebf5
Revises: cb038f79982e
Create Date: 2020-04-23 13:13:08.094805

"""

# revision identifiers, used by Alembic.
revision = '49fc258cebf5'
down_revision = 'cb038f79982e'

from alembic import op
import sqlalchemy as sa


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('bill', sa.Column('tag', sa.UnicodeText(), nullable=True))
op.add_column('bill_version', sa.Column('tag', sa.UnicodeText(), autoincrement=False, nullable=True))
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('bill_version', 'tag')
op.drop_column('bill', 'tag')
# ### end Alembic commands ###
2 changes: 2 additions & 0 deletions ihatemoney/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ def delete(self, project, id):
date = db.Column(db.Date, default=datetime.now)
creation_date = db.Column(db.Date, default=datetime.now)
what = db.Column(db.UnicodeText)
tag = db.Column(db.UnicodeText)
Glandos marked this conversation as resolved.
Show resolved Hide resolved
external_link = db.Column(db.UnicodeText)

archive = db.Column(db.Integer, db.ForeignKey("archive.id"))
Expand All @@ -444,6 +445,7 @@ def _to_serialize(self):
"date": self.date,
"creation_date": self.creation_date,
"what": self.what,
"tag": self.tag,
"external_link": self.external_link,
}

Expand Down
8 changes: 8 additions & 0 deletions ihatemoney/static/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ body {
width: 5em;
}

#bill-search {
color: white;
background: #8f9296;
margin-top: 10px;
border-radius: .25rem;
padding: .375rem .75rem;
}

.invites textarea {
width: 800px;
height: 100px;
Expand Down
67 changes: 66 additions & 1 deletion ihatemoney/templates/list_bills.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,44 @@
});
});

// remove duplicate tags
var usedTags = {};
$("select[name='tag-search-select'] > option").each(function() {
if (usedTags[this.text]) {
$(this).remove();
} else {
usedTags[this.text] = this.value;
}
});

// add default values to payer select
$('#payer-search-select').prepend(new Option('', '', true, true));

// add default values to date select
$('#date-search-select').prepend(new Option('', '', true, true));

$('#btn-bill-filter').click(function() {
var dateValue = $('#date-search-select').val();
var payerValue = $('#payer-search-select option:selected').val();
var amountValue = $('#amount-search-select').val();
if (amountValue.substr(amountValue.length - 3) === '.00') {
amountValue = amountValue.substr(0, amountValue.length - 3)
}
var amountWithZero = amountValue + ".0"
var tagValue = $('#tag-search-select').val();

var matching = $('#bill_table tbody tr').filter(function(){
return (payerValue != "" && $(this).attr('payer') !== payerValue) || $(this).attr('date') !== dateValue || (amountValue != "" && $(this).attr('amount') !== amountValue && $(this).attr('amount') !== amountWithZero) || (tagValue != "" && $(this).attr('tag') !== tagValue);
});

matching.hide();
$('#bill_table tbody tr').not(matching).show(200);
});

$('#btn-bill-showall').click(function() {
$('#bill_table tbody tr').show(200);
});

var highlight_owers = function(){
var ower_ids = $(this).attr("owers").split(',');
var payer_id = $(this).attr("payer");
Expand Down Expand Up @@ -110,11 +148,38 @@ <h3 class="modal-title">{{ _('Add a bill') }}</h3>
{% if bills.total > 0 %}
<div class="clearfix"></div>

<div id="bill-search">
<span>Filter by:</span>
<form class="form-inline mt-2">
<div class="form-group mr-sm-2">
<label for="date-select" class="mr-sm-2">{{ _("Date") }}</label>
{{ bill_form.date(id="date-search-select", class="form-control custom-select", placeholder='') | safe }}
<label for="payer-select" class="mr-sm-2" style="margin-left: 8px">{{ _("Payer") }}</label>
{{ bill_form.payer(id="payer-search-select", class="form-control custom-select", placeholder='') | safe }}
<label for="amount-select" class="mr-sm-2" style="margin-left: 8px">{{ _("Amount") }}</label>
{{ bill_form.amount(id="amount-search-select", class="form-control", style="width: 100px", placeholder='') | safe }}
<label for="tag-select" class="mr-sm-2" style="margin-left: 8px">{{ _("Tag") }}</label>
<select name="tag-search-select" id="tag-search-select" style="height: 38px; margin-right: 8px">
<option value=""></option>
{% for bill in bills.items %}
{% if bill.tag %}
<option value="{{bill.tag}}">{{bill.tag}}</option>
{% endif %}
{% endfor %}}
</select>
</div>
<div class="form-group">
<button id="btn-bill-filter" type="button" class="btn btn-secondary mr-sm-2">{{ _("Apply Filter") }}</button>
<button id="btn-bill-showall" type="button" class="btn btn-secondary">{{ _("Clear Filters") }}</button>
</div>
</form>
</div>

<table id="bill_table" class="col table table-striped table-hover table-responsive-sm">
<thead><tr><th>{{ _("When?") }}</th><th>{{ _("Who paid?") }}</<th><th>{{ _("For what?") }}</th><th>{{ _("For whom?") }}</th><th>{{ _("How much?") }}</th><th>{{ _("Actions") }}</th></tr></thead>
<tbody>
{% for bill in bills.items %}
<tr owers="{{bill.owers|join(',','id')}}" payer="{{bill.payer.id}}">
<tr owers="{{bill.owers|join(',','id')}}" payer="{{bill.payer.id}}" date="{{bill.date}}" amount="{{bill.amount}}" tag="{{bill.tag}}">
<td>
<span data-toggle="tooltip" data-placement="top"
title="{{ _('Added on %(date)s', date=bill.creation_date if bill.creation_date else bill.date) }}">
Expand Down
12 changes: 12 additions & 0 deletions ihatemoney/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,7 @@ def test_import_new_project(self):
{
"date": "2017-01-01",
"what": "refund",
"tag": "",
"amount": 13.33,
"payer_name": "tata",
"payer_weight": 1.0,
Expand All @@ -1219,6 +1220,7 @@ def test_import_new_project(self):
{
"date": "2016-12-31",
"what": "red wine",
"tag": "",
"amount": 200.0,
"payer_name": "fred",
"payer_weight": 1.0,
Expand All @@ -1227,6 +1229,7 @@ def test_import_new_project(self):
{
"date": "2016-12-31",
"what": "fromage a raclette",
"tag": "",
"amount": 10.0,
"payer_name": "alexis",
"payer_weight": 2.0,
Expand Down Expand Up @@ -1286,6 +1289,7 @@ def test_import_partial_project(self):
data={
"date": "2016-12-31",
"what": "red wine",
"tag": "",
"payer": 2,
"payed_for": [1, 3],
"amount": "200",
Expand All @@ -1296,6 +1300,7 @@ def test_import_partial_project(self):
{
"date": "2017-01-01",
"what": "refund",
"tag": "",
"amount": 13.33,
"payer_name": "tata",
"payer_weight": 1.0,
Expand All @@ -1304,6 +1309,7 @@ def test_import_partial_project(self):
{ # This expense does not have to be present twice.
"date": "2016-12-31",
"what": "red wine",
"tag": "",
"amount": 200.0,
"payer_name": "fred",
"payer_weight": 1.0,
Expand All @@ -1312,6 +1318,7 @@ def test_import_partial_project(self):
{
"date": "2016-12-31",
"what": "fromage a raclette",
"tag": "",
"amount": 10.0,
"payer_name": "alexis",
"payer_weight": 2.0,
Expand Down Expand Up @@ -1376,6 +1383,7 @@ def test_import_wrong_json(self):
{ # amount missing
"date": "2017-01-01",
"what": "refund",
"tag": "",
"payer_name": "tata",
"payer_weight": 1.0,
"owers": ["fred"],
Expand Down Expand Up @@ -1780,6 +1788,7 @@ def test_bills(self):
}

got = json.loads(req.data.decode("utf-8"))
del got["tag"]
self.assertEqual(
datetime.date.today(),
datetime.datetime.strptime(got["creation_date"], "%Y-%m-%d").date(),
Expand Down Expand Up @@ -1854,6 +1863,7 @@ def test_bills(self):
datetime.datetime.strptime(got["creation_date"], "%Y-%m-%d").date(),
)
del got["creation_date"]
del got["tag"]
self.assertDictEqual(expected, got)

# delete a bill
Expand Down Expand Up @@ -1930,6 +1940,7 @@ def test_bills_with_calculation(self):
datetime.datetime.strptime(got["creation_date"], "%Y-%m-%d").date(),
)
del got["creation_date"]
del got["tag"]
self.assertDictEqual(expected, got)

# should raise errors
Expand Down Expand Up @@ -2071,6 +2082,7 @@ def test_weighted_bills(self):
datetime.datetime.strptime(got["creation_date"], "%Y-%m-%d").date(),
)
del got["creation_date"]
del got["tag"]
self.assertDictEqual(expected, got)

# getting it should return a 404
Expand Down
3 changes: 2 additions & 1 deletion ihatemoney/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ def import_project(file, project):
json_file = json.load(file)

# Check if JSON is correct
attr = ["what", "payer_name", "payer_weight", "amount", "date", "owers"]
attr = ["what", "tag", "payer_name", "payer_weight", "amount", "date", "owers"]
attr.sort()
for e in json_file:
if len(e) != len(attr):
Expand Down Expand Up @@ -481,6 +481,7 @@ def import_project(file, project):
bill = Bill()
form = get_billform_for(project)
form.what = b["what"]
form.tag = b["tag"]
form.amount = b["amount"]
form.date = parse(b["date"])
form.payer = id_dict[b["payer_name"]]
Expand Down