neuer stand

This commit is contained in:
2022-10-06 00:55:26 +02:00
parent 03a9433149
commit c6dc363c68
4 changed files with 50 additions and 29 deletions

View File

@@ -1,6 +1,12 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block content %} {% block content %}
Logged in as {{session.full_name}} ({{session.username}}) <h1>AStA-Inventarsystem</h1>
<h2>OEs</h2>
<ul>
{% for oe in dbresult %}
<li><a href=/inventory/{{ oe.id }}>{{oe.name}}</a></li>
{% endfor %}
</ul>
<a href=/oes>Show OEs</a>
{% endblock %} {% endblock %}

View File

@@ -1,8 +0,0 @@
{% extends "base.html" %}
{% block content %}
<h1>OEs</h1>
{% for id,name in dbresult %}
<a href=/inventory/{{ id }}>{{name}}</a>
{% endfor %}
{% endblock %}

View File

@@ -2,7 +2,22 @@
{% block content %} {% block content %}
<h1>Inventar</h1> <h1>Inventar</h1>
<h2>{{oe_name}}</h2> <h2>{{oe_name}}</h2>
{% for entry in dbresult %}
{{entry}}<br> <table>
<tr>
{% for entry in table.headers %}
<th>{{entry}}</th>
{% endfor %} {% endfor %}
</tr>
{% for entry in table.rows %}
<tr>
{% for x in entry %}
<td>{{x}}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
{% endblock %} {% endblock %}

View File

@@ -1,5 +1,6 @@
import flask import flask
import psycopg2 import psycopg2
import psycopg2.extras
import functools import functools
from flask import request, session from flask import request, session
from inventorysystem import app from inventorysystem import app
@@ -33,11 +34,6 @@ def permission_required(f, permission, oe=None):
return f(*args, **kwargs) return f(*args, **kwargs)
return inner_function return inner_function
@app.route('/')
@login_required
def index():
return flask.render_template("index.html")
@app.route('/login', methods=["GET", "POST"]) @app.route('/login', methods=["GET", "POST"])
def login(): def login():
@@ -53,29 +49,39 @@ def login():
password = request.form["pass"] password = request.form["pass"]
#FIXME hash password #FIXME hash password
cur = db.cursor() cur = db.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
cur.execute("select id,full_name from users where username=%s and password=%s",(username,password)) cur.execute("select id,full_name from users where username=%s and password=%s",(username,password))
result = cur.fetchall() result = cur.fetchall()
if not result: if not result:
return show_message("Failed to log in, are username and password correct?") return show_message("Failed to log in, are username and password correct?")
else: else:
session["username"] = username session["username"] = username
session["user_id"] = result[0][0] session["user_id"] = result[0]["id"]
session["full_name"] = result[0][1] session["full_name"] = result[0]["full_name"]
return flask.redirect(flask.url_for("index")) return flask.redirect(flask.url_for("index"))
@app.route("/oes") @app.route('/')
@login_required @login_required
def list_oes(): def index():
db = psycopg2.connect(app.config["DSN"]) db = psycopg2.connect(app.config["DSN"])
cur = db.cursor() cur = db.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
cur.execute("select id, name from organizational_units") cur.execute("select id, name from organizational_units")
result = cur.fetchall() result = cur.fetchall()
db.close() db.close()
return flask.render_template("list_oes.html", dbresult=result)
return flask.render_template("index.html", dbresult=result)
def list_of_dicts_to_table(l, headers, default=None):
table = {}
table["headers"] = headers
table["rows"] = []
for d in l:
tmp_list = []
for header in headers:
tmp_list.append(d.get(header,default))
table["rows"].append(tmp_list)
return table
@app.route("/inventory/<int:oe>") @app.route("/inventory/<int:oe>")
def show_inventory(oe): def show_inventory(oe):
@@ -83,14 +89,16 @@ def show_inventory(oe):
return show_message("Permission denied"), 403 return show_message("Permission denied"), 403
db = psycopg2.connect(app.config["DSN"]) db = psycopg2.connect(app.config["DSN"])
cur = db.cursor() cur = db.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
cur.execute("select id,serial,innenauftrag,description,location,purchase_date,old_inventory_id from inventory where oe=%s", (oe,)) cur.execute("select id,serial,innenauftrag,description,location,purchase_date,purchase_price,old_inventory_id from inventory where oe=%s", (oe,))
result = cur.fetchall() result = cur.fetchall()
cur.execute("select name from organizational_units where id=%s", (oe,)) cur.execute("select name from organizational_units where id=%s", (oe,))
oe_name = cur.fetchone()[0] oe_name = cur.fetchone()["name"]
db.close() db.close()
return flask.render_template("show_inventory.html", dbresult=result, oe_name=oe_name) table = list_of_dicts_to_table(result, ["id", "serial", "description", "location", "innenauftrag", "purchase_date", "purchase_price", "old_inventory_id"])
return flask.render_template("show_inventory.html", table=table, oe_name=oe_name)