Features: - Added page for an overview of establishments with a possibility to request a membership. - Added page for admins of an establishment to accept or deny those candidates. This is currently only usable via URL, the navigation to this site is not yet implemented in the HTML files. - Added page to add new establishments by providing a name. Improvements: - Better folder structure. - The establishment-specific overview can now be viewed with another URL, as well as some other pages. Bugfixes: - Seriously I don't know anymore what I fixed and what not. But it works just better now :) Future: - Angular has been added to separate the Flask-Backend with the frontend. Angular is currently not connected to the backend, but this will change in the future.
62 lines
3.0 KiB
Python
62 lines
3.0 KiB
Python
from src import db, LOGGER
|
|
from src.models.receipt import Receipt
|
|
from src.models.login_token import LoginToken
|
|
from src.receipts import bp
|
|
from src.receipts.forms import CheckItemsForm, UploadReceiptForm
|
|
from src.utils.pdf_receipt_parser import PDFReceipt
|
|
from src.utils.routes_utils import render_custom_template as render_template
|
|
from flask import abort, request, url_for
|
|
from flask_login import current_user, login_required
|
|
|
|
PDFDir = "./"
|
|
|
|
@bp.route('/upload_receipt', methods=['GET', 'POST'])
|
|
@login_required
|
|
def upload_receipt():
|
|
"""Upload of a receipt."""
|
|
if current_user.is_anonymous:
|
|
abort(403)
|
|
if "establishment" in request.args:
|
|
if LoginToken.query.filter_by(establishment=request.args['establishment'], user=current_user.id).first():
|
|
form = UploadReceiptForm()
|
|
LOGGER.debug(form.pdfReceipt.data)
|
|
if form.is_submitted():
|
|
LOGGER.debug("submitted")
|
|
if form.validate():
|
|
LOGGER.debug("valid")
|
|
else:
|
|
LOGGER.debug(form.errors)
|
|
if form.validate_on_submit():
|
|
receipt = PDFReceipt(form.pdfReceipt.data)
|
|
dbReceipt = Receipt(id = receipt.id, date = receipt.date,
|
|
from_user = LoginToken.query.filter_by(establishment=request.args['establishment'], user=current_user.id).first().token)
|
|
form.pdfReceipt.data.save(PDFDir + f"{str(receipt.date)}_{receipt.id}.pdf")
|
|
db.session.add(dbReceipt)
|
|
db.session.commit()
|
|
return receipt.text.replace("\n", "<br>")
|
|
return render_template("receipts/upload.html", form = form)
|
|
abort(403)
|
|
|
|
@bp.route('/confirm_receipt', methods=['GET', 'POST'])
|
|
@login_required
|
|
def confirm_receipt_items():
|
|
"""Check items from a receipt if they should be accounted for payment."""
|
|
if "receipt" in request.args:
|
|
receipt_details = Receipt.query.get(request.args['receipt'])
|
|
if current_user.is_anonymous and current_user.id == receipt_details.LoginToken.Establishment.owner:
|
|
receipt = PDFReceipt._getPDFReceiptFromFile(PDFDir + f"{receipt.date}_{receipt.id}.pdf")
|
|
form = CheckItemsForm()
|
|
# TODO: Precheck if items are already in database. If yes, check if item is present only once or multiple
|
|
# times and provide dropdown menu if necessary. If not, provide input field.
|
|
temp_choices = []
|
|
for item in receipt.items:
|
|
match item:
|
|
case {"itemname": itemname, "price": price}:
|
|
temp_choices.append((itemname.replace(" ", "_"), f"{itemname, price}"))
|
|
case {"itemname": itemname, "price": price, "amount": amount}:
|
|
temp_choices.append((itemname.replace(" ", "_"), f"{itemname}, {price} * {amount}"))
|
|
form.choices = temp_choices
|
|
if form.validate_on_submit():
|
|
pass # TODO
|
|
return render_template("receipts/confirm_items.html")
|
|
abort(403) |