diff --git a/README.md b/README.md index dc3233b..c1c26da 100644 --- a/README.md +++ b/README.md @@ -50,3 +50,4 @@ ssh -nNTvL 5432:fsmi-db.fsmi.org:5432 fsmi-login.fsmi.uni-karlsruhe.de - [ ] Make it possible to edit entries - [ ] Improve project structure - [ ] Figure out/Add documentation about building `entry.js` +- [ ] Use `flask.flash` for error messages diff --git a/frontend/Entry.elm b/frontend/Entry.elm index 78b73c6..bbd7a93 100644 --- a/frontend/Entry.elm +++ b/frontend/Entry.elm @@ -339,7 +339,9 @@ viewSearchResult model = , td [] [ text model.locationName ] , td [] [ text <| showBool model.available ] , td [] - [ button [ onClick <| GotoItemEditor model ] [ text "Als Vorlage verwenden" ] + [ Html.form [ onSubmit <| GotoItemEditor model ] + [ button [] [ text "Als Vorlage verwenden" ] + ] ] ] diff --git a/jon/db/__init__.py b/jon/db/__init__.py index 4a3a945..19e1c79 100644 --- a/jon/db/__init__.py +++ b/jon/db/__init__.py @@ -7,8 +7,6 @@ from psycopg2.extras import RealDictCursor def get_db(): if "db" not in g: - # TODO: Make this configurable and use a default that works - # on the pool computers. g.db = psycopg2.connect(current_app.config["DB_CONNECTION_STRING"]) run_query_on(g.db, "add_views.sql", None) diff --git a/jon/db/entry/add_item_and_snack_entry.sql b/jon/db/entry/add_item_and_snack_entry.sql new file mode 100644 index 0000000..e973272 --- /dev/null +++ b/jon/db/entry/add_item_and_snack_entry.sql @@ -0,0 +1,26 @@ +DO $$ +DECLARE new_item_id integer; +DECLARE new_snack_id integer; +BEGIN + +-- Create a new inventory line +INSERT INTO garfield.inventory_items +(item_barcode, name, item_group, location, tax_group, sales_units, unit_price) +VALUES +(%(barcode)s, %(name)s, %(group_id)s, %(location_id)s, %(tax_group_id)s, %(sales_units)s, %(net_unit_price)s) +RETURNING item_id INTO new_item_id; + +-- Delete any old snacks with the same barcode in the given location +PERFORM garfield.snack_delete(snack_id) +FROM garfield.snacks +WHERE snack_barcode = %(barcode)s + AND location_id = %(location_id)s; + +-- Create a new snack entry... +SELECT garfield.snack_create(%(name)s, %(barcode)s, %(gross_unit_price)s, %(tax_group_id)s, %(location_id)s) +INTO new_snack_id; + +-- ... and map it to the new inventory line +PERFORM garfield.inventory_map_snack(new_snack_id, new_item_id); + +END$$ diff --git a/jon/entry.py b/jon/entry.py index 9b067f5..7b6c780 100644 --- a/jon/entry.py +++ b/jon/entry.py @@ -1,4 +1,4 @@ -from flask import Blueprint, redirect, render_template, request, session +from flask import Blueprint, flash, redirect, render_template, request, session from . import db @@ -16,12 +16,37 @@ def index(): ) +@bp.post("/add-new-items") +def add_new_entries(): + print(session) + + i_know_what_im_doing = "i-know-what-im-doing" in request.form + if not i_know_what_im_doing: + return "Du weißt nicht was du tust", 400 + + orders = session.get("cart", default=[]) + if not orders: + return "Keine Aufträge", 404 + + # I'm aware of execute_many and extras.execute_values but we don't need to + # optimize here (yet?). This way it's a bit easier to use anyways. + for order in orders: + with db.run_query("entry/add_item_and_snack_entry.sql", order) as cursor: + pass + db.get_db().commit() + + # Reset the cart + session["cart"] = [] + + return redirect(request.referrer) + + @bp.post("/delete-order") def delete_order(): try: order_index = int(request.form["order-index"]) except: - return f"Incomplete or mistyped form", 400 + return "Incomplete or mistyped form", 400 cart = session.get("cart", default=[]) del cart[order_index] diff --git a/jon/templates/entry/index.html b/jon/templates/entry/index.html index fcb5d1b..e08c717 100644 --- a/jon/templates/entry/index.html +++ b/jon/templates/entry/index.html @@ -34,4 +34,9 @@ {% endfor %} +
{% endblock %}