From 81493afa53a1a1d5ff4b417d05febf9f9e2a172b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Thu, 23 Jul 2020 00:28:47 +0200 Subject: Restructure --- app/__init__.py | 32 --- app/forms.py | 59 ---- app/login.py | 21 -- app/model.py | 205 -------------- app/utils.py | 117 -------- app/views/__init__.py | 61 ----- app/views/categories.py | 31 --- app/views/consts.py | 174 ------------ app/views/expenses.py | 224 ---------------- app/views/stats.py | 56 ---- app/views/user.py | 89 ------ kosten.fcgi | 2 +- kosten/__init__.py | 1 + kosten/app/__init__.py | 32 +++ kosten/app/forms.py | 59 ++++ kosten/app/login.py | 21 ++ kosten/app/model.py | 205 ++++++++++++++ kosten/app/utils.py | 117 ++++++++ kosten/app/views/__init__.py | 61 +++++ kosten/app/views/categories.py | 31 +++ kosten/app/views/consts.py | 174 ++++++++++++ kosten/app/views/expenses.py | 224 ++++++++++++++++ kosten/app/views/stats.py | 56 ++++ kosten/app/views/user.py | 89 ++++++ kosten/static/css/style.css | 338 +++++++++++++++++++++++ kosten/static/images/add.png | Bin 0 -> 800 bytes kosten/static/images/arrow_left.png | Bin 0 -> 9190 bytes kosten/static/images/arrow_right.png | Bin 0 -> 7846 bytes kosten/static/images/closed.png | Bin 0 -> 301 bytes kosten/static/images/closed.svg | 73 +++++ kosten/static/images/currency.png | Bin 0 -> 3225 bytes kosten/static/images/img01.gif | Bin 0 -> 3840 bytes kosten/static/images/img02.gif | Bin 0 -> 4787 bytes kosten/static/images/img03.gif | Bin 0 -> 227 bytes kosten/static/images/img04.gif | Bin 0 -> 92 bytes kosten/static/images/lupe.png | Bin 0 -> 475 bytes kosten/static/images/minus.png | Bin 0 -> 3406 bytes kosten/static/images/open.png | Bin 0 -> 301 bytes kosten/static/images/open.svg | 73 +++++ kosten/static/images/undo.png | Bin 0 -> 649 bytes kosten/static/js/kosten.js | 280 +++++++++++++++++++ kosten/static/js/lib/highstock-4.2.2.js | 431 ++++++++++++++++++++++++++++++ kosten/static/js/lib/jquery-1.11.2.min.js | 4 + kosten/templates/404.jinja | 10 + kosten/templates/categories/manage.jinja | 19 ++ kosten/templates/consts/add.jinja | 13 + kosten/templates/consts/edit.jinja | 14 + kosten/templates/consts/list.jinja | 24 ++ kosten/templates/consts/show.jinja | 39 +++ kosten/templates/expenses/add.jinja | 13 + kosten/templates/expenses/edit.jinja | 14 + kosten/templates/expenses/search.jinja | 25 ++ kosten/templates/expenses/show.jinja | 64 +++++ kosten/templates/js.jinja | 25 ++ kosten/templates/layout.jinja | 81 ++++++ kosten/templates/macros.jinja | 39 +++ kosten/templates/menu.jinja | 14 + kosten/templates/stats/const_dialog.jinja | 8 + kosten/templates/stats/show.jinja | 12 + kosten/templates/user/cpw.jinja | 13 + kosten/templates/user/login.jinja | 13 + run.sh | 2 +- static/css/style.css | 338 ----------------------- static/images/add.png | Bin 800 -> 0 bytes static/images/arrow_left.png | Bin 9190 -> 0 bytes static/images/arrow_right.png | Bin 7846 -> 0 bytes static/images/closed.png | Bin 301 -> 0 bytes static/images/closed.svg | 73 ----- static/images/currency.png | Bin 3225 -> 0 bytes static/images/img01.gif | Bin 3840 -> 0 bytes static/images/img02.gif | Bin 4787 -> 0 bytes static/images/img03.gif | Bin 227 -> 0 bytes static/images/img04.gif | Bin 92 -> 0 bytes static/images/lupe.png | Bin 475 -> 0 bytes static/images/minus.png | Bin 3406 -> 0 bytes static/images/open.png | Bin 301 -> 0 bytes static/images/open.svg | 73 ----- static/images/undo.png | Bin 649 -> 0 bytes static/js/kosten.js | 280 ------------------- static/js/lib/highstock-4.2.2.js | 431 ------------------------------ static/js/lib/jquery-1.11.2.min.js | 4 - templates/404.jinja | 10 - templates/categories/manage.jinja | 19 -- templates/consts/add.jinja | 13 - templates/consts/edit.jinja | 14 - templates/consts/list.jinja | 24 -- templates/consts/show.jinja | 39 --- templates/expenses/add.jinja | 13 - templates/expenses/edit.jinja | 14 - templates/expenses/search.jinja | 25 -- templates/expenses/show.jinja | 64 ----- templates/js.jinja | 25 -- templates/layout.jinja | 81 ------ templates/macros.jinja | 39 --- templates/menu.jinja | 14 - templates/stats/const_dialog.jinja | 8 - templates/stats/show.jinja | 12 - templates/user/cpw.jinja | 13 - templates/user/login.jinja | 13 - 99 files changed, 2711 insertions(+), 2710 deletions(-) delete mode 100644 app/__init__.py delete mode 100644 app/forms.py delete mode 100644 app/login.py delete mode 100644 app/model.py delete mode 100644 app/utils.py delete mode 100644 app/views/__init__.py delete mode 100644 app/views/categories.py delete mode 100644 app/views/consts.py delete mode 100644 app/views/expenses.py delete mode 100644 app/views/stats.py delete mode 100644 app/views/user.py create mode 100644 kosten/__init__.py create mode 100644 kosten/app/__init__.py create mode 100644 kosten/app/forms.py create mode 100644 kosten/app/login.py create mode 100644 kosten/app/model.py create mode 100644 kosten/app/utils.py create mode 100644 kosten/app/views/__init__.py create mode 100644 kosten/app/views/categories.py create mode 100644 kosten/app/views/consts.py create mode 100644 kosten/app/views/expenses.py create mode 100644 kosten/app/views/stats.py create mode 100644 kosten/app/views/user.py create mode 100644 kosten/static/css/style.css create mode 100644 kosten/static/images/add.png create mode 100644 kosten/static/images/arrow_left.png create mode 100644 kosten/static/images/arrow_right.png create mode 100644 kosten/static/images/closed.png create mode 100644 kosten/static/images/closed.svg create mode 100644 kosten/static/images/currency.png create mode 100644 kosten/static/images/img01.gif create mode 100644 kosten/static/images/img02.gif create mode 100644 kosten/static/images/img03.gif create mode 100644 kosten/static/images/img04.gif create mode 100644 kosten/static/images/lupe.png create mode 100644 kosten/static/images/minus.png create mode 100644 kosten/static/images/open.png create mode 100644 kosten/static/images/open.svg create mode 100644 kosten/static/images/undo.png create mode 100644 kosten/static/js/kosten.js create mode 100644 kosten/static/js/lib/highstock-4.2.2.js create mode 100644 kosten/static/js/lib/jquery-1.11.2.min.js create mode 100644 kosten/templates/404.jinja create mode 100644 kosten/templates/categories/manage.jinja create mode 100644 kosten/templates/consts/add.jinja create mode 100644 kosten/templates/consts/edit.jinja create mode 100644 kosten/templates/consts/list.jinja create mode 100644 kosten/templates/consts/show.jinja create mode 100644 kosten/templates/expenses/add.jinja create mode 100644 kosten/templates/expenses/edit.jinja create mode 100644 kosten/templates/expenses/search.jinja create mode 100644 kosten/templates/expenses/show.jinja create mode 100644 kosten/templates/js.jinja create mode 100644 kosten/templates/layout.jinja create mode 100644 kosten/templates/macros.jinja create mode 100644 kosten/templates/menu.jinja create mode 100644 kosten/templates/stats/const_dialog.jinja create mode 100644 kosten/templates/stats/show.jinja create mode 100644 kosten/templates/user/cpw.jinja create mode 100644 kosten/templates/user/login.jinja delete mode 100644 static/css/style.css delete mode 100644 static/images/add.png delete mode 100644 static/images/arrow_left.png delete mode 100644 static/images/arrow_right.png delete mode 100644 static/images/closed.png delete mode 100644 static/images/closed.svg delete mode 100644 static/images/currency.png delete mode 100644 static/images/img01.gif delete mode 100644 static/images/img02.gif delete mode 100644 static/images/img03.gif delete mode 100644 static/images/img04.gif delete mode 100644 static/images/lupe.png delete mode 100644 static/images/minus.png delete mode 100644 static/images/open.png delete mode 100644 static/images/open.svg delete mode 100644 static/images/undo.png delete mode 100644 static/js/kosten.js delete mode 100644 static/js/lib/highstock-4.2.2.js delete mode 100644 static/js/lib/jquery-1.11.2.min.js delete mode 100644 templates/404.jinja delete mode 100644 templates/categories/manage.jinja delete mode 100644 templates/consts/add.jinja delete mode 100644 templates/consts/edit.jinja delete mode 100644 templates/consts/list.jinja delete mode 100644 templates/consts/show.jinja delete mode 100644 templates/expenses/add.jinja delete mode 100644 templates/expenses/edit.jinja delete mode 100644 templates/expenses/search.jinja delete mode 100644 templates/expenses/show.jinja delete mode 100644 templates/js.jinja delete mode 100644 templates/layout.jinja delete mode 100644 templates/macros.jinja delete mode 100644 templates/menu.jinja delete mode 100644 templates/stats/const_dialog.jinja delete mode 100644 templates/stats/show.jinja delete mode 100644 templates/user/cpw.jinja delete mode 100644 templates/user/login.jinja diff --git a/app/__init__.py b/app/__init__.py deleted file mode 100644 index 7c6408a..0000000 --- a/app/__init__.py +++ /dev/null @@ -1,32 +0,0 @@ -from flask import Flask - -# create app -app = Flask('kosten', instance_relative_config = True) - -# force autoescape in all files -app.jinja_env.autoescape = True - -app.jinja_env.lstrip_blocks = True -app.jinja_env.trim_blocks = True - -# load config -app.config.from_object('settings') -app.config.from_pyfile('settings.py', silent = True) - -from .model import db -from .login import login_manager -from . import views - -# commands -@app.cli.command() -def create(): - db.create_all() - -@app.cli.command() -def drop(): - db.drop_all() - -@app.cli.command() -def compile(): - """Compiles all templates.""" - app.jinja_env.compile_templates('comp', zip = None) diff --git a/app/forms.py b/app/forms.py deleted file mode 100644 index b7cbebf..0000000 --- a/app/forms.py +++ /dev/null @@ -1,59 +0,0 @@ -# -*- encoding: utf-8 -*- -import flask -from flask_wtf import FlaskForm -from wtforms.fields import BooleanField, StringField, HiddenField, PasswordField, DecimalField as WTFDecimalField, DateField as HTML4DateField -from wtforms.fields.html5 import DateField, IntegerField -from wtforms import validators, ValidationError, Form as WTForm - -from wtforms.ext.sqlalchemy.fields import QuerySelectField - -from . import app - -@app.template_test('hidden') -def is_hidden_field(f): - return isinstance(f, HiddenField) - -class DecimalField(WTFDecimalField): - def __init__(self, *args, **kwargs): - render_kw = kwargs.setdefault('render_kw', dict()) - render_kw.setdefault('inputmethod', 'decimal') - - super().__init__(*args, **kwargs) - - def process_formdata(self, valuelist): - if valuelist: - value = valuelist[0].replace(',','.') - super().process_formdata([value]) - -class MonthField(HTML4DateField): - def __init__(self, label, validators, format='%m.%Y', **kwargs): - super().__init__(label, validators, format, **kwargs) - -req = [validators.input_required()] - -class Form(FlaskForm): - class Meta: - locales = ['de_DE', 'de'] - - def __init__ (self, *args, **kwargs): - self._msg = kwargs.pop('flash', "Fehler im Formular!") - super().__init__(*args, **kwargs) - - def flash(self): - flask.flash(self._msg, 'error') - - def flash_validate (self): - if not self.validate(): - self.flash() - return False - - return True - - def validate_on_submit (self): - return self.is_submitted() and self.flash_validate() - - def _get_translations(self): - # use WTForms builtin translation support instead of the flask-babael - # stuff added by flask-wtf - # FIXME: remove this, if flask-babel is used in the app - return WTForm._get_translations(self) diff --git a/app/login.py b/app/login.py deleted file mode 100644 index 850cc8a..0000000 --- a/app/login.py +++ /dev/null @@ -1,21 +0,0 @@ -from flask_login import LoginManager, UserMixin -from passlib.apps import custom_app_context as pwd_context -from . import app, model - -# just for exporting -from flask_login import login_user, logout_user, login_required, current_user - -login_manager = LoginManager() -login_manager.init_app(app) -login_manager.login_message = "Bitte einloggen!" - -class User (model.User, UserMixin): - def check_password(self, pwd): - return pwd_context.verify(pwd, self.pwd) - - def set_password(self, pwd): - self.pwd = pwd_context.encrypt(pwd) - -@login_manager.user_loader -def load_user(id): - return User.get(id) diff --git a/app/model.py b/app/model.py deleted file mode 100644 index 4663685..0000000 --- a/app/model.py +++ /dev/null @@ -1,205 +0,0 @@ -from flask_sqlalchemy import SQLAlchemy -from sqlalchemy import sql -from sqlalchemy.ext.declarative import declared_attr - -import datetime -import decimal -from functools import partial -from collections import namedtuple - -from . import app - -db = SQLAlchemy(app) - -__all__ = ['db', \ - 'Category', 'SingleExpense', 'ConstExpense', \ - 'CatExpense', 'MonthExpense'] - -Column = db.Column -ReqColumn = partial(Column, nullable = False) -ExpNum = db.Numeric(scale = 2, precision = 10) - -def to_exp(d): - """Converts decimal into expense""" - return d.quantize(decimal.Decimal('.01'), rounding = decimal.ROUND_UP) - -# -# Database Entities -# -class Model (db.Model): - """Abstract base class for all models. - Adds an id PK and several convenience accessors. - """ - - __abstract__ = True - - id = Column(db.Integer, primary_key=True) - - @declared_attr - def __tablename__ (cls): - return cls.__name__.lower() - - @classmethod - def get_by (cls, *args, **kwargs): - return cls.query.filter_by(*args, **kwargs).first() - - @classmethod - def get_by_or_404 (cls, *args, **kwargs): - return cls.query.filter_by(*args, **kwargs).first_or_404() - - @classmethod - def get (cls, *args, **kwargs): - return cls.query.get(*args, **kwargs) - - @classmethod - def get_or_404 (cls, *args, **kwargs): - return cls.query.get_or_404(*args, **kwargs) - - -class User (Model): - # NB: This is abstract, the flesh is added in login.py - - __abstract__ = True - name = ReqColumn(db.Unicode(50), unique = True) - pwd = ReqColumn(db.Unicode(255)) - description = Column(db.Unicode(100)) - - -class UserModel (Model): - """Abstract base class for tables that have a user column.""" - __abstract__ = True - - @declared_attr - def user_id(cls): - return ReqColumn(db.Integer, db.ForeignKey('user.id')) - - @declared_attr - def user(cls): - return db.relationship('User') - - @classmethod - def of (cls, user): - return cls.query.filter_by(user = user) - - -class Category (UserModel): - name = ReqColumn(db.Unicode(50)) - parent_id = Column(db.Integer, db.ForeignKey('category.id')) - - children = db.relationship('Category', - backref=db.backref('parent', remote_side="Category.id")) - - def __init__ (self, name, user, parent_id = None): - Model.__init__(self) - self.name = name - self.user = user - self.parent_id = parent_id - - def __repr__ (self): - if self.parent: - return '' % (self.name, self.parent.name) - else: - return '' % self.name - - -class CategoryModel (db.Model): - """Abstract base class for expenses: Adds the common fields - and establishes the connection to `Category`. - """ - __abstract__ = True - - @declared_attr - def category_id(cls): - return ReqColumn(db.Integer, db.ForeignKey(Category.id)) - - @declared_attr - def category(cls): - return db.relationship(Category, innerjoin = True) - - -class SingleExpense (UserModel, CategoryModel): - description = Column(db.Unicode(50)) - expense = ReqColumn(ExpNum) - year = ReqColumn(db.Integer) - month = ReqColumn(db.SmallInteger) - day = ReqColumn(db.SmallInteger) - - @classmethod - def of_month (cls, user, month, year): - return cls.of(user).filter_by(month = month, year = year) - - @property - def date (self): - return datetime.date(self.year, self.month, self.day) - - @date.setter - def date (self, d): - self.year = d.year - self.month = d.month - self.day = d.day - - -class ConstExpense (UserModel, CategoryModel): - description = Column(db.Unicode(50)) - expense = ReqColumn(ExpNum) - months = ReqColumn(db.SmallInteger) - start = ReqColumn(db.Date, index = True) - end = ReqColumn(db.Date, index = True) - prev_id = Column(db.Integer, db.ForeignKey('constexpense.id')) - - prev = db.relationship('ConstExpense', remote_side = 'ConstExpense.id', uselist = False, - backref=db.backref('next', uselist = False)) - - @property - def monthly(self): - return to_exp(self.expense / self.months) - - @classmethod - def of_month (cls, user, month, year): - d = datetime.date(year, month, 1) - return cls.of(user).filter(sql.between(d, cls.start, cls.end)) - -# -# Work entities (not stored in DB) -# -class CatExpense (namedtuple('CatExpense', 'cat sum exps')): - __slots__ = () - - @property - def all (self): - return self.exps.order_by(SingleExpense.day).all() - -class MonthExpense (namedtuple('MonthExpense', 'user date catexps')): - - def __init__ (self, *args, **kwargs): - self._consts = None - - @property - def consts (self): - if self._consts is None: - self._consts = ConstExpense.of_month(self.user, self.date.month, self.date.year).all() - - return self._consts - - @property - def constsum (self): - s = sum(c.monthly for c in self.consts) - return s or 0 - - @property - def sum (self): - return self.constsum + sum(x.sum for x in self.catexps) - - @property - def all (self): - return SingleExpense.of_month(self.user, self.date.month, self.date.year).order_by(SingleExpense.day).all() - - def __str__ (self): - return '' % (self.user.name, self.date, self.sum) - -# -# Extra indices have to be here -# - -db.Index('idx_single_date', SingleExpense.user_id, SingleExpense.year, SingleExpense.month) -db.Index('idx_start_end', ConstExpense.user_id, ConstExpense.start, ConstExpense.end) diff --git a/app/utils.py b/app/utils.py deleted file mode 100644 index 73f2b51..0000000 --- a/app/utils.py +++ /dev/null @@ -1,117 +0,0 @@ -from functools import wraps -from flask import flash, request, render_template, url_for -from flask import redirect as _redirect - -from .login import current_user - -import datetime -today = datetime.date.today - -def _gen_tpl(endpoint): - return endpoint.replace('.', '/') + '.jinja' - -def templated(template=None): - """Marks a view as being rendered by a template. The view then shall - return a dictionary holding the parameters for the template. Ig this - is not the case, the response is returned unchanged. This is needed - to support `redirect` and similar. - - The correct template is deducted as: - - when passed nothing: the name of the view - - when passed a string '.bla', the endpoint 'bla' in the current - blueprint - - when passed any other string: this string (VERBATIM!) - - Except for the last case, the hierarchy of blueprint and view is taken - as directories in the template directory. And '.jinja' is appended. - - If the first argument is a function, this is taken as 'None' to allow: - >>> @templated - ... def foo(): - ... ... - - (else it would have to be ``@templated()``). - """ - - fun = None - if template is not None and callable(template): - # a function was passed in - fun = template - template = None - - def decorator(f): - @wraps(f) - def decorated_function(*args, **kwargs): - if template is None: - template_name = _gen_tpl(request.endpoint) - elif template[0] == '.' and request.blueprint is not None: - template_name = _gen_tpl(request.blueprint + template) - else: - template_name = template - - ctx = f(*args, **kwargs) - if ctx is None: - ctx = {} - elif not isinstance(ctx, dict): - return ctx - return render_template(template_name, **ctx) - return decorated_function - - if fun is None: - return decorator - else: - return decorator(fun) - -def redirect (target, **kwargs): - """Convenience wrapper for `flask.redirect`. It applies `url_for` - on the target, which also gets passed all arguments. - - Special argument '_code' to set the HTTP-Code. - """ - code = kwargs.pop('_code', None) - url = url_for(target, **kwargs) - - if code is None: - return _redirect(url) - else: - return _redirect(url, code) - -def assert_authorisation(constructor, param): - """Asserts that the current user has the right to load some specific data. - - This is done by using the argument with keyword `param` and pass it - to `constructor`. If the resulting object has an attribute `user_id`, - this is checked to be equal to `current_user.id`. - - Usage example:: - - @route('/job/') - @assert_authorisation(Job, 'id') - def show_job(id): - # this is only executed if Job(id).user_id == current_user.id - - """ - def decorator(f): - @wraps(f) - def decorated_function(*args, **kwargs): - p = kwargs.get(param, None) - - if p is None: - raise TypeError("Keyword %s expected but not received." % param) - - obj = constructor(p) - if obj is None: - flash("Eintrag existiert nicht!", 'error') - return redirect('index') - - if not hasattr(obj, 'user_id'): - return f(*args, **kwargs) - - # explicitly use user_id to avoid having to load the user object - if obj.user_id != current_user.id: - flash("Nicht erlaubte Operation!", 'error') - return redirect('index') - else: - return f(*args, **kwargs) - return decorated_function - return decorator diff --git a/app/views/__init__.py b/app/views/__init__.py deleted file mode 100644 index 6b432e8..0000000 --- a/app/views/__init__.py +++ /dev/null @@ -1,61 +0,0 @@ -from flask import render_template, request, url_for -import flask - -from .. import app, db - -# -# Some general imports -# -from ..login import current_user, login_required -from ..utils import today, templated, redirect, assert_authorisation -from flask import Blueprint, flash - -__all__ = [ - 'db', 'app', - 'current_user', 'login_required', - 'assert_authorisation', 'templated', 'today', 'redirect', - 'Blueprint', 'flash', - 'request', 'url_for' -] - -# check for mobile visitors -mobile_checks = ['J2ME', 'Opera Mini'] - -app.add_template_global(zip) -app.add_template_global(current_user) - -@app.before_request -def handle_mobile(): - ua = request.environ.get('HTTP_USER_AGENT', '') - - flask.g.is_mobile = any((x in ua) for x in mobile_checks) - -@app.template_filter('static_url') -def static_url(s, **kwargs): - return url_for('static', filename=s, **kwargs) - -@app.template_filter('eur') -def eur(s): - return ('%s EUR' % s) - -@app.template_filter('date') -def format_date(s, format='%Y/%m'): - if hasattr(s, 'date'): - return s.date.strftime(format) - else: - return s.strftime(format) - -@app.errorhandler(404) -def page_not_found (error): - return render_template('404.jinja', page = request.path), 404 - -# Now import the views -from . import categories, consts, expenses, user, stats - -app.register_blueprint(expenses.mod) -app.register_blueprint(user.mod, url_prefix='/user') -app.register_blueprint(consts.mod, url_prefix='/const') -app.register_blueprint(categories.mod, url_prefix='/cat') -app.register_blueprint(stats.mod, url_prefix='/stats') - -app.add_url_rule('/', endpoint = 'index', build_only = True) diff --git a/app/views/categories.py b/app/views/categories.py deleted file mode 100644 index 47379ba..0000000 --- a/app/views/categories.py +++ /dev/null @@ -1,31 +0,0 @@ -from . import Blueprint, db, \ - current_user, login_required, \ - templated, redirect, request - -from ..model import Category - -mod = Blueprint('categories', __name__) - -@mod.route('/', methods=('GET', 'POST')) -@login_required -@templated -def manage(): - """Workhorse: List and edit/create. For historic reasons, - everything is done in JavaScript. - - NB: No deletion possible yet. - """ - if request.method == 'GET': - categories = Category.of(current_user).order_by(Category.name).all() - - return { 'cats' : categories } - else: - for id, name in request.form.items(): - if id.startswith('n-'): - db.session.add(Category(name = name, user = current_user)) - else: - Category.get(id).name = name - - db.session.commit() - - return redirect('.manage') diff --git a/app/views/consts.py b/app/views/consts.py deleted file mode 100644 index 5d6598d..0000000 --- a/app/views/consts.py +++ /dev/null @@ -1,174 +0,0 @@ -# -*- coding: utf-8 -*- -from . import Blueprint, flash, db, \ - current_user, login_required, \ - assert_authorisation, templated, redirect, request, \ - today - -from ..model import Category, ConstExpense -from .. import forms as F - -from sqlalchemy import sql -from functools import partial - -mod = Blueprint('consts', __name__) -assert_authorisation = partial(assert_authorisation, ConstExpense.get) - -def one_year(d): - """Returns the date d', such that [d, d'] spans exactly one year. - In effect, this is d + 11 months (NB: not 12!)""" - if d.month == 1: - return d.replace(month = 12) - else: - return d.replace(month = d.month - 1, year = d.year + 1) - -# -# Form -# -class ConstForm(F.Form): - start = F.MonthField('Beginn', F.req, - default=lambda: today()) - - end = F.MonthField('Ende', F.req, - default=lambda: one_year(today()), - description='(einschließlich)') - - months = F.IntegerField('Zahlungsrythmus', F.req, - description='Monate') - - expense = F.DecimalField('Betrag', F.req, - description='EUR', - places=2) - - description = F.StringField('Beschreibung', F.req) - - category = F.QuerySelectField('Kategorie', - get_label='name', - get_pk=lambda c: c.id) - - prev = F.QuerySelectField('Vorgänger', - get_label='description', - allow_blank=True, - get_pk=lambda p: p.id) - - def __init__(self, cur=None, obj=None): - obj = cur if obj is None else obj - super().__init__(obj=obj) - self.category.query = Category.of(current_user).order_by(Category.name) - - # 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) - -# -# Views -# -@mod.route('/') -@login_required -@templated -def list (): - """List all constant expenses.""" - d = today().replace(day = 1) - - expenses = ConstExpense.of(current_user).order_by(ConstExpense.description).all() - - current = [] - old = [] - future = [] - last_month = [] - - for e in expenses: - if e.start <= d: - if e.end >= d: - current.append(e) - 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) - else: - old.append(e) - else: - future.append(e) - - return { 'current': current, 'old': old, 'future': future, 'last_month': last_month } - - -@mod.route('/') -@login_required -@assert_authorisation('id') -@templated -def show(id): - """Show a specific constant expense.""" - return { 'exp': ConstExpense.get(id) } - - -@mod.route('/edit/', methods=('GET', 'POST')) -@login_required -@assert_authorisation('id') -@templated -def edit(id): - """Edit a specific constant expense. This includes deletion.""" - exp = ConstExpense.get(id) - form = ConstForm(exp) - - if form.is_submitted(): - if 'deleteB' in request.form: - db.session.delete(exp) - db.session.commit() - return redirect('.list') - - elif form.flash_validate(): # change - form.populate_obj(exp) - db.session.commit() - flash("Eintrag geändert.") - return redirect('.show', id = id) - - return { 'form': form } - - -@mod.route('/add/', methods=('GET', 'POST')) -@login_required -@templated -def add(): - """Add a new constant expense.""" - exp = ConstExpense() - - form = ConstForm() - - if form.validate_on_submit(): - form.populate_obj(exp) - exp.user = current_user - db.session.add(exp) - db.session.commit() - flash("Eintrag hinzugefügt.") - return redirect('.show', id = exp.id) - - return { 'form': form } - - -@mod.route('/add/from/') -@login_required -@assert_authorisation('other') -@templated('.add') -def add_from(other): - """Copy `other` and create a new expense based on it.""" - exp = ConstExpense() # needed to initialize 'CE.next' - - other = ConstExpense.get(other) - - # get form with data from other - form = ConstForm(obj = other) - - # replace some fields to be more meaningful - start = max(form.end.data, today()) - form.start.data = start - form.end.data = one_year(start) - if not other.next: form.prev.data = other - - return { 'form': form } diff --git a/app/views/expenses.py b/app/views/expenses.py deleted file mode 100644 index 90c8ffd..0000000 --- a/app/views/expenses.py +++ /dev/null @@ -1,224 +0,0 @@ -# -*- coding: utf-8 -*- -from . import Blueprint, flash, db, \ - current_user, login_required, \ - assert_authorisation, templated, redirect, request, url_for, today - -from flask import Markup - -from ..model import Category, SingleExpense, CatExpense, MonthExpense -from .. import forms as F - -import datetime -from sqlalchemy import func -from functools import partial - -assert_authorisation = partial(assert_authorisation, SingleExpense.get) -mod = Blueprint('expenses', __name__) - -# -# Form -# -class ExpenseForm(F.Form): - date = F.DateField('Datum', F.req, - default=lambda: today()) - - expense = F.DecimalField('Betrag', F.req, - description='EUR', - places=2) - - description = F.StringField('Beschreibung') - - category = F.QuerySelectField('Kategorie', - get_label='name', - get_pk=lambda c: c.id) - - def __init__(self, obj = None, description_req = True): - super().__init__(obj = obj) - self.category.query = Category.of(current_user).order_by(Category.name) - - if description_req: - self.description.validators.extend(F.req) - -# -# Utilities -# -def calc_month_exp(year, month): - """Returns the `MonthExpense` for the given month.""" - ssum = func.sum(SingleExpense.expense) - query = SingleExpense.of_month(current_user, month, year) - - result = query.group_by(SingleExpense.category_id).\ - values(SingleExpense.category_id, ssum) - - exps = [CatExpense(Category.get(c), s, query.filter(SingleExpense.category_id == c)) for c,s in result] - - return MonthExpense(current_user, datetime.date(year, month, 1), exps) - - -def pie_data(exp): - """Generates the dictionary needed to show the pie diagram. - The resulting dict is category → sum of expenses. - """ - expenses = {} - for c in exp.catexps: - expenses[c.cat.name] = float(c.sum) - - for c in Category.of(current_user).order_by(Category.name).all(): - yield (c.name, expenses.get(c.name, 0.0)) - - -def calc_month_and_pie(year, month): - exp = calc_month_exp(year,month) - pie = pie_data(exp) - return (exp, dict(pie)) - - -def entry_flash(msg, exp): - """When changing/adding an entry, a message is shown.""" - url = url_for('.edit', id = exp.id) - link = "%s" % (url, exp.description) - flash(Markup(msg % link)) - -DATE_FORMAT='%Y%m%d' -def parse_date(value): - try: - dt = datetime.datetime.strptime(value, DATE_FORMAT) - except ValueError: - return today() - else: - return dt.date() - -def gen_date(value): - return value.strftime(DATE_FORMAT) - -# -# Template additions -# -@mod.app_template_filter() -def prev_date(exp): - if exp.date.month == 1: - return exp.date.replace(year = exp.date.year - 1, month = 12) - else: - return exp.date.replace(month = exp.date.month - 1) - - -@mod.app_template_filter() -def next_date(exp): - if exp.date.month == 12: - return exp.date.replace(year = exp.date.year + 1, month = 1) - else: - return exp.date.replace(month = exp.date.month + 1) - - -@mod.app_template_test('last_date') -def is_last(exp): - return exp.date >= today().replace(day = 1) - -# -# Views -# -@mod.route('/') -@login_required -@templated -def show(): - """Show this and the last month.""" - d = today() - - first, pfirst = calc_month_and_pie(d.year, d.month) - if d.month == 1: - second, psecond = calc_month_and_pie(d.year - 1, 12) - else: - second, psecond = calc_month_and_pie(d.year, d.month - 1) - - return { 'exps' : [first, second], 'pies': [pfirst, psecond] } - - -@mod.route('//') -@login_required -@templated('.show') -def show_date(year, month): - """Show the expenses of the specified month.""" - c,p = calc_month_and_pie(year, month) - return { 'exps' : [c], 'pies' : [p] } - -# shortcut to allow calling the above route, when year/month is a string -mod.add_url_rule('/', endpoint = 'show_date_str', build_only = True) - - -@mod.route('/edit/', methods=('GET', 'POST')) -@login_required -@assert_authorisation('id') -@templated -def edit(id): - """Edit a single expense, given by `id`.""" - exp = SingleExpense.get(id) - form = ExpenseForm(exp) - - if form.is_submitted(): - if 'deleteB' in request.form: - db.session.delete(exp) - - elif form.flash_validate(): # change - form.populate_obj(exp) - - else: - return { 'form': form } - - db.session.commit() - entry_flash("Eintrag %s geändert.", exp) - return redirect('index') - - return { 'form': form } - - -@mod.route('/add', methods=('GET', 'POST')) -@login_required -@templated -def add(): - """Add a new expense.""" - form = ExpenseForm(description_req=False) - - if request.method == 'GET' and 'date' in request.args: - form.date.data = parse_date(request.args['date']) - - if form.validate_on_submit(): - if not form.description.data.strip(): - form.description.data = form.category.data.name - - exp = SingleExpense() - - form.populate_obj(exp) - exp.user = current_user - - db.session.add(exp) - db.session.commit() - - entry_flash("Neuer Eintrag %s hinzugefügt.", exp) - - return redirect('.add', date = gen_date(exp.date) if exp.date != today() else None) - - return { 'form': form } - -@mod.route('/search', methods=('POST', 'GET')) -@login_required -@templated -def search(): - try: - query = request.form['search'].strip() - except KeyError: - flash("Ungültige Suchanfrage") - return redirect('index') - - if not query: - flash("Leere Suche") - return redirect('index') - - exps = SingleExpense.of(current_user).filter(SingleExpense.description.ilike(query))\ - .order_by(SingleExpense.year.desc(), SingleExpense.month, SingleExpense.day, SingleExpense.description)\ - .all() - - if not exps: - flash("Keine Ergebnisse") - return redirect('index') - - return { 'exps': exps } diff --git a/app/views/stats.py b/app/views/stats.py deleted file mode 100644 index 9ff81a1..0000000 --- a/app/views/stats.py +++ /dev/null @@ -1,56 +0,0 @@ -from . import Blueprint, flash, db, \ - current_user, login_required, \ - assert_authorisation, templated, redirect, request, \ - today - -from .. import forms as F -from ..model import ConstExpense, SingleExpense -import sqlalchemy as sql -import calendar -from collections import defaultdict -from datetime import date -from flask import jsonify - -mod = Blueprint('stats', __name__) - -def next_date(d): - if d.month == 12: - return d.replace(year = d.year + 1, month = 1) - else: - return d.replace(month = d.month + 1) - -def date_to_ms(d): - return calendar.timegm(d.timetuple()) * 1000 - -@mod.route('/_const//') -@login_required -@templated -def const_dialog(year,month): - consts = ConstExpense.of_month(current_user, month, year).order_by(ConstExpense.description) - - return { 'consts': consts } - - -@mod.route('/') -@login_required -@templated -def show(): - # easy way: fetch them all and then do some computation - consts = defaultdict(int) - t = today().replace(day = 1) - for e in ConstExpense.of(current_user): - cur = e.start - end = min(e.end, t) - while cur <= end: - consts[date_to_ms(cur)] += e.monthly - cur = next_date(cur) - - consts = list(sorted(consts.items())) - - expQuery = SingleExpense.of(current_user)\ - .group_by(SingleExpense.year, SingleExpense.month)\ - .values(SingleExpense.year, SingleExpense.month, sql.func.sum(SingleExpense.expense)) - - expenses = list(sorted((date_to_ms(date(year,month,1)), exp) for (year, month, exp) in expQuery)) - - return { 'consts': consts, 'expenses' : expenses } diff --git a/app/views/user.py b/app/views/user.py deleted file mode 100644 index 9b75af8..0000000 --- a/app/views/user.py +++ /dev/null @@ -1,89 +0,0 @@ -# -*- encoding: utf-8 -*- -from . import Blueprint, flash, db, \ - current_user, login_required, \ - templated, redirect, request, url_for - -from .. import forms as F -from ..login import login_user, logout_user, login_manager, User - -import flask - -mod = Blueprint('user', __name__) - -# -# Forms -# -class LoginForm(F.Form): - username = F.StringField('Username', F.req) - pwd = F.PasswordField('Passwort', F.req) - remember = F.BooleanField('Eingeloggt bleiben?') - - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.user = None - - def validate(self): - rv = super().validate() - if not rv: - return False - - user = User.get_by(name = self.username.data) - if user is None or not user.check_password(self.pwd.data): - return False - - self.user = user - return True - - -class ChangePwdForm(F.Form): - old = F.PasswordField('Passwort', F.req) - new = F.PasswordField('Neues Passwort', F.req + [F.validators.EqualTo('confirm', 'Passwörter stimmen nicht überein')]) - confirm = F.PasswordField('Wdh. neues Passwort', F.req) - - def validate_old(self, field): - if not current_user.check_password(field.data): - raise F.ValidationError("Falsches Passwort") - - @property - def newpwd(self): - return self.new.data - -# -# Views -# -@mod.route('/login', methods=('GET', 'POST')) -@templated -def login(): - """Log the user in.""" - form = LoginForm(flash="Login fehlgeschlagen!") - if form.validate_on_submit(): - login_user(form.user, remember=form.remember.data) - # we explicitly need flask's variant as we redirect to a URI - return flask.redirect(request.args.get('next') or url_for('index')) - return { 'form': form } - - -@mod.route('/logout') -def logout(): - """Log the user out.""" - logout_user() - return redirect('.login') - - -@mod.route('/cpw', methods=('GET', 'POST')) -@login_required -@templated -def cpw(): - """Change the password of the user.""" - form = ChangePwdForm() - - if form.validate_on_submit(): - current_user.set_password(form.newpwd) - db.session.commit() - flash("Passwort geändert.") - return redirect('index') - - return { 'form': form } - -# set this, so the user is redirected to the correct view, when not logged in -login_manager.login_view = 'user.login' diff --git a/kosten.fcgi b/kosten.fcgi index 86f7e52..eb9917f 100755 --- a/kosten.fcgi +++ b/kosten.fcgi @@ -1,7 +1,7 @@ #!/usr/bin/env python3 from flipflop import WSGIServer -from app import app +from kosten import app if __name__ == "__main__": WSGIServer(app).run() diff --git a/kosten/__init__.py b/kosten/__init__.py new file mode 100644 index 0000000..c07c459 --- /dev/null +++ b/kosten/__init__.py @@ -0,0 +1 @@ +from .app import app diff --git a/kosten/app/__init__.py b/kosten/app/__init__.py new file mode 100644 index 0000000..7c6408a --- /dev/null +++ b/kosten/app/__init__.py @@ -0,0 +1,32 @@ +from flask import Flask + +# create app +app = Flask('kosten', instance_relative_config = True) + +# force autoescape in all files +app.jinja_env.autoescape = True + +app.jinja_env.lstrip_blocks = True +app.jinja_env.trim_blocks = True + +# load config +app.config.from_object('settings') +app.config.from_pyfile('settings.py', silent = True) + +from .model import db +from .login import login_manager +from . import views + +# commands +@app.cli.command() +def create(): + db.create_all() + +@app.cli.command() +def drop(): + db.drop_all() + +@app.cli.command() +def compile(): + """Compiles all templates.""" + app.jinja_env.compile_templates('comp', zip = None) diff --git a/kosten/app/forms.py b/kosten/app/forms.py new file mode 100644 index 0000000..b7cbebf --- /dev/null +++ b/kosten/app/forms.py @@ -0,0 +1,59 @@ +# -*- encoding: utf-8 -*- +import flask +from flask_wtf import FlaskForm +from wtforms.fields import BooleanField, StringField, HiddenField, PasswordField, DecimalField as WTFDecimalField, DateField as HTML4DateField +from wtforms.fields.html5 import DateField, IntegerField +from wtforms import validators, ValidationError, Form as WTForm + +from wtforms.ext.sqlalchemy.fields import QuerySelectField + +from . import app + +@app.template_test('hidden') +def is_hidden_field(f): + return isinstance(f, HiddenField) + +class DecimalField(WTFDecimalField): + def __init__(self, *args, **kwargs): + render_kw = kwargs.setdefault('render_kw', dict()) + render_kw.setdefault('inputmethod', 'decimal') + + super().__init__(*args, **kwargs) + + def process_formdata(self, valuelist): + if valuelist: + value = valuelist[0].replace(',','.') + super().process_formdata([value]) + +class MonthField(HTML4DateField): + def __init__(self, label, validators, format='%m.%Y', **kwargs): + super().__init__(label, validators, format, **kwargs) + +req = [validators.input_required()] + +class Form(FlaskForm): + class Meta: + locales = ['de_DE', 'de'] + + def __init__ (self, *args, **kwargs): + self._msg = kwargs.pop('flash', "Fehler im Formular!") + super().__init__(*args, **kwargs) + + def flash(self): + flask.flash(self._msg, 'error') + + def flash_validate (self): + if not self.validate(): + self.flash() + return False + + return True + + def validate_on_submit (self): + return self.is_submitted() and self.flash_validate() + + def _get_translations(self): + # use WTForms builtin translation support instead of the flask-babael + # stuff added by flask-wtf + # FIXME: remove this, if flask-babel is used in the app + return WTForm._get_translations(self) diff --git a/kosten/app/login.py b/kosten/app/login.py new file mode 100644 index 0000000..850cc8a --- /dev/null +++ b/kosten/app/login.py @@ -0,0 +1,21 @@ +from flask_login import LoginManager, UserMixin +from passlib.apps import custom_app_context as pwd_context +from . import app, model + +# just for exporting +from flask_login import login_user, logout_user, login_required, current_user + +login_manager = LoginManager() +login_manager.init_app(app) +login_manager.login_message = "Bitte einloggen!" + +class User (model.User, UserMixin): + def check_password(self, pwd): + return pwd_context.verify(pwd, self.pwd) + + def set_password(self, pwd): + self.pwd = pwd_context.encrypt(pwd) + +@login_manager.user_loader +def load_user(id): + return User.get(id) diff --git a/kosten/app/model.py b/kosten/app/model.py new file mode 100644 index 0000000..4663685 --- /dev/null +++ b/kosten/app/model.py @@ -0,0 +1,205 @@ +from flask_sqlalchemy import SQLAlchemy +from sqlalchemy import sql +from sqlalchemy.ext.declarative import declared_attr + +import datetime +import decimal +from functools import partial +from collections import namedtuple + +from . import app + +db = SQLAlchemy(app) + +__all__ = ['db', \ + 'Category', 'SingleExpense', 'ConstExpense', \ + 'CatExpense', 'MonthExpense'] + +Column = db.Column +ReqColumn = partial(Column, nullable = False) +ExpNum = db.Numeric(scale = 2, precision = 10) + +def to_exp(d): + """Converts decimal into expense""" + return d.quantize(decimal.Decimal('.01'), rounding = decimal.ROUND_UP) + +# +# Database Entities +# +class Model (db.Model): + """Abstract base class for all models. + Adds an id PK and several convenience accessors. + """ + + __abstract__ = True + + id = Column(db.Integer, primary_key=True) + + @declared_attr + def __tablename__ (cls): + return cls.__name__.lower() + + @classmethod + def get_by (cls, *args, **kwargs): + return cls.query.filter_by(*args, **kwargs).first() + + @classmethod + def get_by_or_404 (cls, *args, **kwargs): + return cls.query.filter_by(*args, **kwargs).first_or_404() + + @classmethod + def get (cls, *args, **kwargs): + return cls.query.get(*args, **kwargs) + + @classmethod + def get_or_404 (cls, *args, **kwargs): + return cls.query.get_or_404(*args, **kwargs) + + +class User (Model): + # NB: This is abstract, the flesh is added in login.py + + __abstract__ = True + name = ReqColumn(db.Unicode(50), unique = True) + pwd = ReqColumn(db.Unicode(255)) + description = Column(db.Unicode(100)) + + +class UserModel (Model): + """Abstract base class for tables that have a user column.""" + __abstract__ = True + + @declared_attr + def user_id(cls): + return ReqColumn(db.Integer, db.ForeignKey('user.id')) + + @declared_attr + def user(cls): + return db.relationship('User') + + @classmethod + def of (cls, user): + return cls.query.filter_by(user = user) + + +class Category (UserModel): + name = ReqColumn(db.Unicode(50)) + parent_id = Column(db.Integer, db.ForeignKey('category.id')) + + children = db.relationship('Category', + backref=db.backref('parent', remote_side="Category.id")) + + def __init__ (self, name, user, parent_id = None): + Model.__init__(self) + self.name = name + self.user = user + self.parent_id = parent_id + + def __repr__ (self): + if self.parent: + return '' % (self.name, self.parent.name) + else: + return '' % self.name + + +class CategoryModel (db.Model): + """Abstract base class for expenses: Adds the common fields + and establishes the connection to `Category`. + """ + __abstract__ = True + + @declared_attr + def category_id(cls): + return ReqColumn(db.Integer, db.ForeignKey(Category.id)) + + @declared_attr + def category(cls): + return db.relationship(Category, innerjoin = True) + + +class SingleExpense (UserModel, CategoryModel): + description = Column(db.Unicode(50)) + expense = ReqColumn(ExpNum) + year = ReqColumn(db.Integer) + month = ReqColumn(db.SmallInteger) + day = ReqColumn(db.SmallInteger) + + @classmethod + def of_month (cls, user, month, year): + return cls.of(user).filter_by(month = month, year = year) + + @property + def date (self): + return datetime.date(self.year, self.month, self.day) + + @date.setter + def date (self, d): + self.year = d.year + self.month = d.month + self.day = d.day + + +class ConstExpense (UserModel, CategoryModel): + description = Column(db.Unicode(50)) + expense = ReqColumn(ExpNum) + months = ReqColumn(db.SmallInteger) + start = ReqColumn(db.Date, index = True) + end = ReqColumn(db.Date, index = True) + prev_id = Column(db.Integer, db.ForeignKey('constexpense.id')) + + prev = db.relationship('ConstExpense', remote_side = 'ConstExpense.id', uselist = False, + backref=db.backref('next', uselist = False)) + + @property + def monthly(self): + return to_exp(self.expense / self.months) + + @classmethod + def of_month (cls, user, month, year): + d = datetime.date(year, month, 1) + return cls.of(user).filter(sql.between(d, cls.start, cls.end)) + +# +# Work entities (not stored in DB) +# +class CatExpense (namedtuple('CatExpense', 'cat sum exps')): + __slots__ = () + + @property + def all (self): + return self.exps.order_by(SingleExpense.day).all() + +class MonthExpense (namedtuple('MonthExpense', 'user date catexps')): + + def __init__ (self, *args, **kwargs): + self._consts = None + + @property + def consts (self): + if self._consts is None: + self._consts = ConstExpense.of_month(self.user, self.date.month, self.date.year).all() + + return self._consts + + @property + def constsum (self): + s = sum(c.monthly for c in self.consts) + return s or 0 + + @property + def sum (self): + return self.constsum + sum(x.sum for x in self.catexps) + + @property + def all (self): + return SingleExpense.of_month(self.user, self.date.month, self.date.year).order_by(SingleExpense.day).all() + + def __str__ (self): + return '' % (self.user.name, self.date, self.sum) + +# +# Extra indices have to be here +# + +db.Index('idx_single_date', SingleExpense.user_id, SingleExpense.year, SingleExpense.month) +db.Index('idx_start_end', ConstExpense.user_id, ConstExpense.start, ConstExpense.end) diff --git a/kosten/app/utils.py b/kosten/app/utils.py new file mode 100644 index 0000000..73f2b51 --- /dev/null +++ b/kosten/app/utils.py @@ -0,0 +1,117 @@ +from functools import wraps +from flask import flash, request, render_template, url_for +from flask import redirect as _redirect + +from .login import current_user + +import datetime +today = datetime.date.today + +def _gen_tpl(endpoint): + return endpoint.replace('.', '/') + '.jinja' + +def templated(template=None): + """Marks a view as being rendered by a template. The view then shall + return a dictionary holding the parameters for the template. Ig this + is not the case, the response is returned unchanged. This is needed + to support `redirect` and similar. + + The correct template is deducted as: + - when passed nothing: the name of the view + - when passed a string '.bla', the endpoint 'bla' in the current + blueprint + - when passed any other string: this string (VERBATIM!) + + Except for the last case, the hierarchy of blueprint and view is taken + as directories in the template directory. And '.jinja' is appended. + + If the first argument is a function, this is taken as 'None' to allow: + >>> @templated + ... def foo(): + ... ... + + (else it would have to be ``@templated()``). + """ + + fun = None + if template is not None and callable(template): + # a function was passed in + fun = template + template = None + + def decorator(f): + @wraps(f) + def decorated_function(*args, **kwargs): + if template is None: + template_name = _gen_tpl(request.endpoint) + elif template[0] == '.' and request.blueprint is not None: + template_name = _gen_tpl(request.blueprint + template) + else: + template_name = template + + ctx = f(*args, **kwargs) + if ctx is None: + ctx = {} + elif not isinstance(ctx, dict): + return ctx + return render_template(template_name, **ctx) + return decorated_function + + if fun is None: + return decorator + else: + return decorator(fun) + +def redirect (target, **kwargs): + """Convenience wrapper for `flask.redirect`. It applies `url_for` + on the target, which also gets passed all arguments. + + Special argument '_code' to set the HTTP-Code. + """ + code = kwargs.pop('_code', None) + url = url_for(target, **kwargs) + + if code is None: + return _redirect(url) + else: + return _redirect(url, code) + +def assert_authorisation(constructor, param): + """Asserts that the current user has the right to load some specific data. + + This is done by using the argument with keyword `param` and pass it + to `constructor`. If the resulting object has an attribute `user_id`, + this is checked to be equal to `current_user.id`. + + Usage example:: + + @route('/job/') + @assert_authorisation(Job, 'id') + def show_job(id): + # this is only executed if Job(id).user_id == current_user.id + + """ + def decorator(f): + @wraps(f) + def decorated_function(*args, **kwargs): + p = kwargs.get(param, None) + + if p is None: + raise TypeError("Keyword %s expected but not received." % param) + + obj = constructor(p) + if obj is None: + flash("Eintrag existiert nicht!", 'error') + return redirect('index') + + if not hasattr(obj, 'user_id'): + return f(*args, **kwargs) + + # explicitly use user_id to avoid having to load the user object + if obj.user_id != current_user.id: + flash("Nicht erlaubte Operation!", 'error') + return redirect('index') + else: + return f(*args, **kwargs) + return decorated_function + return decorator diff --git a/kosten/app/views/__init__.py b/kosten/app/views/__init__.py new file mode 100644 index 0000000..6b432e8 --- /dev/null +++ b/kosten/app/views/__init__.py @@ -0,0 +1,61 @@ +from flask import render_template, request, url_for +import flask + +from .. import app, db + +# +# Some general imports +# +from ..login import current_user, login_required +from ..utils import today, templated, redirect, assert_authorisation +from flask import Blueprint, flash + +__all__ = [ + 'db', 'app', + 'current_user', 'login_required', + 'assert_authorisation', 'templated', 'today', 'redirect', + 'Blueprint', 'flash', + 'request', 'url_for' +] + +# check for mobile visitors +mobile_checks = ['J2ME', 'Opera Mini'] + +app.add_template_global(zip) +app.add_template_global(current_user) + +@app.before_request +def handle_mobile(): + ua = request.environ.get('HTTP_USER_AGENT', '') + + flask.g.is_mobile = any((x in ua) for x in mobile_checks) + +@app.template_filter('static_url') +def static_url(s, **kwargs): + return url_for('static', filename=s, **kwargs) + +@app.template_filter('eur') +def eur(s): + return ('%s EUR' % s) + +@app.template_filter('date') +def format_date(s, format='%Y/%m'): + if hasattr(s, 'date'): + return s.date.strftime(format) + else: + return s.strftime(format) + +@app.errorhandler(404) +def page_not_found (error): + return render_template('404.jinja', page = request.path), 404 + +# Now import the views +from . import categories, consts, expenses, user, stats + +app.register_blueprint(expenses.mod) +app.register_blueprint(user.mod, url_prefix='/user') +app.register_blueprint(consts.mod, url_prefix='/const') +app.register_blueprint(categories.mod, url_prefix='/cat') +app.register_blueprint(stats.mod, url_prefix='/stats') + +app.add_url_rule('/', endpoint = 'index', build_only = True) diff --git a/kosten/app/views/categories.py b/kosten/app/views/categories.py new file mode 100644 index 0000000..47379ba --- /dev/null +++ b/kosten/app/views/categories.py @@ -0,0 +1,31 @@ +from . import Blueprint, db, \ + current_user, login_required, \ + templated, redirect, request + +from ..model import Category + +mod = Blueprint('categories', __name__) + +@mod.route('/', methods=('GET', 'POST')) +@login_required +@templated +def manage(): + """Workhorse: List and edit/create. For historic reasons, + everything is done in JavaScript. + + NB: No deletion possible yet. + """ + if request.method == 'GET': + categories = Category.of(current_user).order_by(Category.name).all() + + return { 'cats' : categories } + else: + for id, name in request.form.items(): + if id.startswith('n-'): + db.session.add(Category(name = name, user = current_user)) + else: + Category.get(id).name = name + + db.session.commit() + + return redirect('.manage') diff --git a/kosten/app/views/consts.py b/kosten/app/views/consts.py new file mode 100644 index 0000000..5d6598d --- /dev/null +++ b/kosten/app/views/consts.py @@ -0,0 +1,174 @@ +# -*- coding: utf-8 -*- +from . import Blueprint, flash, db, \ + current_user, login_required, \ + assert_authorisation, templated, redirect, request, \ + today + +from ..model import Category, ConstExpense +from .. import forms as F + +from sqlalchemy import sql +from functools import partial + +mod = Blueprint('consts', __name__) +assert_authorisation = partial(assert_authorisation, ConstExpense.get) + +def one_year(d): + """Returns the date d', such that [d, d'] spans exactly one year. + In effect, this is d + 11 months (NB: not 12!)""" + if d.month == 1: + return d.replace(month = 12) + else: + return d.replace(month = d.month - 1, year = d.year + 1) + +# +# Form +# +class ConstForm(F.Form): + start = F.MonthField('Beginn', F.req, + default=lambda: today()) + + end = F.MonthField('Ende', F.req, + default=lambda: one_year(today()), + description='(einschließlich)') + + months = F.IntegerField('Zahlungsrythmus', F.req, + description='Monate') + + expense = F.DecimalField('Betrag', F.req, + description='EUR', + places=2) + + description = F.StringField('Beschreibung', F.req) + + category = F.QuerySelectField('Kategorie', + get_label='name', + get_pk=lambda c: c.id) + + prev = F.QuerySelectField('Vorgänger', + get_label='description', + allow_blank=True, + get_pk=lambda p: p.id) + + def __init__(self, cur=None, obj=None): + obj = cur if obj is None else obj + super().__init__(obj=obj) + self.category.query = Category.of(current_user).order_by(Category.name) + + # 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) + +# +# Views +# +@mod.route('/') +@login_required +@templated +def list (): + """List all constant expenses.""" + d = today().replace(day = 1) + + expenses = ConstExpense.of(current_user).order_by(ConstExpense.description).all() + + current = [] + old = [] + future = [] + last_month = [] + + for e in expenses: + if e.start <= d: + if e.end >= d: + current.append(e) + 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) + else: + old.append(e) + else: + future.append(e) + + return { 'current': current, 'old': old, 'future': future, 'last_month': last_month } + + +@mod.route('/') +@login_required +@assert_authorisation('id') +@templated +def show(id): + """Show a specific constant expense.""" + return { 'exp': ConstExpense.get(id) } + + +@mod.route('/edit/', methods=('GET', 'POST')) +@login_required +@assert_authorisation('id') +@templated +def edit(id): + """Edit a specific constant expense. This includes deletion.""" + exp = ConstExpense.get(id) + form = ConstForm(exp) + + if form.is_submitted(): + if 'deleteB' in request.form: + db.session.delete(exp) + db.session.commit() + return redirect('.list') + + elif form.flash_validate(): # change + form.populate_obj(exp) + db.session.commit() + flash("Eintrag geändert.") + return redirect('.show', id = id) + + return { 'form': form } + + +@mod.route('/add/', methods=('GET', 'POST')) +@login_required +@templated +def add(): + """Add a new constant expense.""" + exp = ConstExpense() + + form = ConstForm() + + if form.validate_on_submit(): + form.populate_obj(exp) + exp.user = current_user + db.session.add(exp) + db.session.commit() + flash("Eintrag hinzugefügt.") + return redirect('.show', id = exp.id) + + return { 'form': form } + + +@mod.route('/add/from/') +@login_required +@assert_authorisation('other') +@templated('.add') +def add_from(other): + """Copy `other` and create a new expense based on it.""" + exp = ConstExpense() # needed to initialize 'CE.next' + + other = ConstExpense.get(other) + + # get form with data from other + form = ConstForm(obj = other) + + # replace some fields to be more meaningful + start = max(form.end.data, today()) + form.start.data = start + form.end.data = one_year(start) + if not other.next: form.prev.data = other + +