From 667a7a4285d4e97ed5242e60df909f3ecdd35d59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Mon, 11 Jan 2016 21:42:45 +0100 Subject: Started to include const expense groups --- app/views/consts.py | 64 ++++++++++++++++++++++++++++++--------------- app/views/stats.py | 1 - static/js/kosten.js | 20 +++++++++++++- static/js/kosten.ls | 18 +++++++++++++ templates/consts/edit.jinja | 5 ++++ 5 files changed, 85 insertions(+), 23 deletions(-) diff --git a/app/views/consts.py b/app/views/consts.py index c76cf17..dfc61a2 100644 --- a/app/views/consts.py +++ b/app/views/consts.py @@ -4,7 +4,7 @@ from . import Blueprint, flash, db, \ assert_authorisation, templated, redirect, request, \ today -from ..model import Category, ConstExpense +from ..model import Category, ConstExpense, ConstExpenseGroup from .. import forms as F from sqlalchemy import sql @@ -21,6 +21,19 @@ def one_year(d): else: return d.replace(month = d.month - 1, year = d.year + 1) +def check_group(exp): + if exp.group is None: + group = ConstExpenseGroup( + description = exp.description, + start = exp.start, + end = exp.end, + category = exp.category, + user = current_user) + + exp.description = None + exp.group = group + db.session.add(group) + # # Form # @@ -46,7 +59,7 @@ class ConstForm(F.Form): category = F.QuerySelectField('Kategorie', get_label='name') - prev = F.QuerySelectField('Vorgänger', + group = F.QuerySelectField('Gruppe', get_label='description', allow_blank=True) @@ -54,17 +67,11 @@ class ConstForm(F.Form): obj = cur if obj is None else obj super().__init__(obj=obj) self.category.query = Category.of(current_user).order_by(Category.name) + self.group.query = ConstExpenseGroup.of(current_user).order_by(ConstExpenseGroup.description) - # init prev_list - CE = ConstExpense - - filter = (CE.next == None) - if cur and cur.id is not None: # not empty - filter = sql.or_(CE.next == cur, filter) - filter = sql.and_(filter, CE.id != cur.id) - - self.prev.query = CE.of(current_user).filter(filter).order_by(CE.description) + def get_category_data(self): + return dict((c.id, c.category_id) for c in ConstExpenseGroup.of(current_user)) # # Views @@ -76,25 +83,25 @@ def list (): """List all constant expenses.""" d = today() - expenses = ConstExpense.of(current_user).order_by(ConstExpense.description).all() + groups = ConstExpenseGroup.of(current_user).order_by(ConstExpenseGroup.description).all() current = [] old = [] future = [] last_month = [] - for e in expenses: - if e.start <= d: - if e.end >= d: - current.append(e) + for g in groups: + if g.start <= d: + if g.end >= d: + current.append(g) else: - if (d.month == 1 and e.end.month == 12 and e.end.year == d.year - 1) \ - or (e.end.year == d.year and e.end.month == d.month - 1): - last_month.append(e) + if (d.month == 1 and g.end.month == 12 and g.end.year == d.year - 1) \ + or (g.end.year == d.year and g.end.month == d.month - 1): + last_month.append(g) else: - old.append(e) + old.append(g) else: - future.append(e) + future.append(g) return { 'current': current, 'old': old, 'future': future, 'last_month': last_month } @@ -119,12 +126,25 @@ def edit(id): if form.is_submitted(): if 'deleteB' in request.form: + group = exp.group db.session.delete(exp) + + if not group.expenses: + db.session.delete(group) + db.session.commit() return redirect('.list') elif form.flash_validate(): # change + old_group = exp.group form.populate_obj(exp) + + if exp.group != old_group: + if not old_group.expenses: + db.session.delete(old_group) + + check_group(exp) + db.session.commit() flash("Eintrag geändert.") return redirect('.show', id = id) @@ -144,6 +164,8 @@ def add(): if form.validate_on_submit(): form.populate_obj(exp) exp.user = current_user + check_group(exp) + db.session.add(exp) db.session.commit() flash("Eintrag hinzugefügt.") diff --git a/app/views/stats.py b/app/views/stats.py index 9ff81a1..f386e12 100644 --- a/app/views/stats.py +++ b/app/views/stats.py @@ -9,7 +9,6 @@ import sqlalchemy as sql import calendar from collections import defaultdict from datetime import date -from flask import jsonify mod = Blueprint('stats', __name__) diff --git a/static/js/kosten.js b/static/js/kosten.js index 7d6582c..b1ce683 100644 --- a/static/js/kosten.js +++ b/static/js/kosten.js @@ -1,6 +1,6 @@ // Generated by LiveScript 1.4.0 (function(){ - var jq, setLang, extendDate, addJS, showJS, statJS, catsJS, out$ = typeof exports != 'undefined' && exports || this; + var jq, setLang, editConstsJS, extendDate, addJS, showJS, statJS, catsJS, out$ = typeof exports != 'undefined' && exports || this; jQuery.fn.extend({ copy: function(){ return this.clone().removeAttr('id class'); @@ -59,6 +59,24 @@ $('form.search').hide(); }); }); + out$.editConstJS = editConstJS = jq(function(){ + var group, category; + group = $('select#group'); + category = $('select#category'); + group.change(function(){ + var val, x$; + val = group.find('option:selected').val(); + if (category_data[val] === void 8) { + category.prop('disabled', false); + } else { + x$ = category; + x$.find('option:selected').prop('selected', false); + x$.find("option[value=" + category_data[val] + "]").prop('selected', true); + x$.prop('disabled', true); + } + }); + group.change(); + }); out$.addJS = addJS = jq(function(){ var x$; x$ = $('input[name=date]'); diff --git a/static/js/kosten.ls b/static/js/kosten.ls index ea433a4..f9876f8 100644 --- a/static/js/kosten.ls +++ b/static/js/kosten.ls @@ -58,6 +58,24 @@ $ !-> $ \input.search .focusout !-> $ \form.search .hide! +# Consts +export editConstJS = jq !-> + group = $ \select#group + category = $ \select#category + + group .change !-> + val = group .find \option:selected .val! + if category_data[val] == void # no group selected + category .prop \disabled false + else + category + ..find \option:selected .prop \selected false + ..find "option[value=#{category_data[val]}]" .prop \selected true + ..prop \disabled true + + # initial + group .change! + # Add export addJS = jq !-> $ 'input[name=date]' diff --git a/templates/consts/edit.jinja b/templates/consts/edit.jinja index b313704..de79bdf 100644 --- a/templates/consts/edit.jinja +++ b/templates/consts/edit.jinja @@ -1,6 +1,8 @@ {% extends "layout.jinja" %} {% from "macros.jinja" import render_form %} +{% set JSFunction = "editConstJS" %} + {% block heading %} Konstante Ausgabe bearbeiten {% endblock %} @@ -11,4 +13,7 @@ + {% endblock %} -- cgit v1.2.3