Add tagging functionality

This commit is contained in:
alligu 2020-04-20 17:05:07 -04:00 committed by Nikos Epping
parent 4d6a5aaa27
commit 71de84bc14
No known key found for this signature in database
GPG key ID: 8AAF07BFD862BFBC
5 changed files with 53 additions and 3 deletions

View file

@ -11,7 +11,7 @@ from markupsafe import Markup
from werkzeug.security import check_password_hash, generate_password_hash from werkzeug.security import check_password_hash, generate_password_hash
from wtforms.fields.core import Label, SelectField, SelectMultipleField from wtforms.fields.core import Label, SelectField, SelectMultipleField
from wtforms.fields.html5 import DateField, DecimalField, URLField from wtforms.fields.html5 import DateField, DecimalField, URLField
from wtforms.fields.simple import BooleanField, PasswordField, StringField, SubmitField from wtforms.fields.simple import BooleanField, PasswordField, StringField, SubmitField, HiddenField
from wtforms.validators import ( from wtforms.validators import (
URL, URL,
DataRequired, DataRequired,
@ -310,6 +310,7 @@ class BillForm(FlaskForm):
amount = CalculatorStringField(_("How much?"), validators=[DataRequired()]) amount = CalculatorStringField(_("How much?"), validators=[DataRequired()])
currency_helper = CurrencyConverter() currency_helper = CurrencyConverter()
original_currency = SelectField(_("Currency"), validators=[DataRequired()]) original_currency = SelectField(_("Currency"), validators=[DataRequired()])
tag = HiddenField(_("Tag"), default="")
external_link = URLField( external_link = URLField(
_("External link"), _("External link"),
validators=[Optional(), URL()], validators=[Optional(), URL()],
@ -325,6 +326,9 @@ class BillForm(FlaskForm):
bill.payer_id = self.payer.data bill.payer_id = self.payer.data
bill.amount = self.amount.data bill.amount = self.amount.data
bill.what = self.what.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]
bill.external_link = self.external_link.data bill.external_link = self.external_link.data
bill.date = self.date.data bill.date = self.date.data
bill.owers = [Person.query.get(ower, project) for ower in self.payed_for.data] bill.owers = [Person.query.get(ower, project) for ower in self.payed_for.data]
@ -338,6 +342,7 @@ class BillForm(FlaskForm):
bill.payer_id = self.payer bill.payer_id = self.payer
bill.amount = self.amount bill.amount = self.amount
bill.what = self.what bill.what = self.what
bill.tag = self.tag
bill.external_link = "" bill.external_link = ""
bill.date = self.date bill.date = self.date
bill.owers = [Person.query.get(ower, project) for ower in self.payed_for] bill.owers = [Person.query.get(ower, project) for ower in self.payed_for]
@ -352,6 +357,7 @@ class BillForm(FlaskForm):
self.payer.data = bill.payer_id self.payer.data = bill.payer_id
self.amount.data = bill.amount self.amount.data = bill.amount
self.what.data = bill.what self.what.data = bill.what
self.tag.data = bill.tag
self.external_link.data = bill.external_link self.external_link.data = bill.external_link
self.original_currency.data = bill.original_currency self.original_currency.data = bill.original_currency
self.date.data = bill.date self.date.data = bill.date

View file

@ -0,0 +1,31 @@
"""added tag column to bill model
Revision ID: 49fc258cebf5
Revises: 927ed575acbd
Create Date: 2020-04-23 13:13:08.094805
"""
# revision identifiers, used by Alembic.
revision = "49fc258cebf5"
down_revision = "927ed575acbd"
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 ###

View file

@ -554,6 +554,7 @@ class Bill(db.Model):
date = db.Column(db.Date, default=datetime.now) date = db.Column(db.Date, default=datetime.now)
creation_date = db.Column(db.Date, default=datetime.now) creation_date = db.Column(db.Date, default=datetime.now)
what = db.Column(db.UnicodeText) what = db.Column(db.UnicodeText)
tag = db.Column(db.UnicodeText)
external_link = db.Column(db.UnicodeText) external_link = db.Column(db.UnicodeText)
original_currency = db.Column(db.String(3)) original_currency = db.Column(db.String(3))
@ -571,6 +572,7 @@ class Bill(db.Model):
"date": self.date, "date": self.date,
"creation_date": self.creation_date, "creation_date": self.creation_date,
"what": self.what, "what": self.what,
"tag": self.tag,
"external_link": self.external_link, "external_link": self.external_link,
"original_currency": self.original_currency, "original_currency": self.original_currency,
"converted_amount": self.converted_amount, "converted_amount": self.converted_amount,

View file

@ -21,6 +21,16 @@
}); });
}); });
// 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;
}
});
var highlight_owers = function(){ var highlight_owers = function(){
var ower_ids = $(this).attr("owers").split(','); var ower_ids = $(this).attr("owers").split(',');
var payer_id = $(this).attr("payer"); var payer_id = $(this).attr("payer");
@ -110,7 +120,7 @@
</thead> </thead>
<tbody> <tbody>
{% for bill in bills.items %} {% 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> <td>
<span data-toggle="tooltip" data-placement="top" <span data-toggle="tooltip" data-placement="top"
title="{{ _('Added on %(date)s', date=bill.creation_date|dateformat("long") if bill.creation_date else bill.date|dateformat("long")) }}"> title="{{ _('Added on %(date)s', date=bill.creation_date|dateformat("long") if bill.creation_date else bill.date|dateformat("long")) }}">

View file

@ -462,7 +462,7 @@ def import_project(file, project):
json_file = json.load(file) json_file = json.load(file)
# Check if JSON is correct # Check if JSON is correct
attr = ["what", "payer_name", "payer_weight", "amount", "currency", "date", "owers"] attr = ["what", "tag", "payer_name", "payer_weight", "amount", "currency", "date", "owers"]
attr.sort() attr.sort()
currencies = set() currencies = set()
for e in json_file: for e in json_file:
@ -539,6 +539,7 @@ def import_project(file, project):
bill = Bill() bill = Bill()
form = get_billform_for(project) form = get_billform_for(project)
form.what = b["what"] form.what = b["what"]
form.tag = b["tag"]
form.amount = b["amount"] form.amount = b["amount"]
form.original_currency = b["currency"] form.original_currency = b["currency"]
form.date = parse(b["date"]) form.date = parse(b["date"])