list create / add item, no interactivity yet

This commit is contained in:
Sam Hadow 2024-02-11 17:38:17 +01:00
parent 4a5b9f3bb9
commit 1151c3e9a2
4 changed files with 116 additions and 0 deletions

View File

@ -60,6 +60,22 @@ def toggle_item_show():
toggle_show(uuid) toggle_show(uuid)
return jsonify({'toggled show/hide ': uuid}), 200 return jsonify({'toggled show/hide ': uuid}), 200
@app.route('/create-list', methods=['POST'])
def createList():
data = request.get_json()
name = data.get('name')
description = data.get('description')
create_list(name, description)
return jsonify({'created list ': name}), 200
@app.route('/add-to-list', methods=['POST'])
def addToList():
date = request.get_json()
list_uuid = data.get('list_uuid')
item_uuid = data.get('item_uuid')
add_to_list(list_uuid, item_uuid)
return jsonify({'list': list_uuid, 'item': item_uuid}), 200
@app.route('/datahistory',methods = ['GET']) @app.route('/datahistory',methods = ['GET'])
def data_history_request(): def data_history_request():
if request.method == 'GET': if request.method == 'GET':

View File

@ -161,6 +161,35 @@ def delete_item(uuid):
connection.commit() connection.commit()
connection.close() connection.close()
def create_list(name, description):
'''
initialize an itemlist in the database
'''
if len(name) <= 50:
connection = connect_db()
cursor = connection.cursor()
cursor.execute("""
INSERT INTO itemlist (uuid, name, description)
VALUES (nextval('uuid_list_sequence'), %s, %s)
""", (name, description))
connection.commit()
connection.close()
return 1
else:
return 0
def add_to_list(list_uuid, item_uuid):
'''
add an item to an itemlist
'''
connection = connect_db()
cursor = connection.cursor()
cursor.execute("""
INSERT INTO listcontent (list_uuid, item_uuid)
VALUES (%s, %s)
""", (list_uuid, item_uuid))
connection.commit()
connection.close()
def export_csv(): def export_csv():
'''join item and history data from database and export it in ./output.csv''' '''join item and history data from database and export it in ./output.csv'''
@ -196,14 +225,28 @@ def initialize():
DROP TABLE IF EXISTS item DROP TABLE IF EXISTS item
""") """)
cursor.execute(""" cursor.execute("""
DROP TABLE IF EXISTS itemlist
""")
cursor.execute("""
DROP TABLE IF EXISTS listcontent
""")
cursor.execute("""
DROP SEQUENCE IF EXISTS uuid_sequence DROP SEQUENCE IF EXISTS uuid_sequence
""") """)
cursor.execute(""" cursor.execute("""
DROP SEQUENCE IF EXISTS uuid_list_sequence
""")
cursor.execute("""
CREATE SEQUENCE uuid_sequence CREATE SEQUENCE uuid_sequence
INCREMENT BY 1 INCREMENT BY 1
START WITH 1 START WITH 1
""") """)
cursor.execute(""" cursor.execute("""
CREATE SEQUENCE uuid_list_sequence
INCREMENT BY 1
START WITH 1
""")
cursor.execute("""
CREATE TABLE item CREATE TABLE item
( (
uuid int, uuid int,
@ -217,6 +260,25 @@ def initialize():
) )
""") """)
cursor.execute(""" cursor.execute("""
CREATE TABLE itemlist
(
uuid int,
name varchar(50),
description text,
primary key (uuid)
)
""")
cursor.execute("""
CREATE TABLE listcontent
(
list_uuid int,
item_uuid int,
foreign key (item_uuid) references item(uuid),
foreign key (list_uuid) references itemlist(uuid),
primary key (list_uuid, item_uuid)
)
""")
cursor.execute("""
CREATE TABLE history CREATE TABLE history
( (
uuid int, uuid int,
@ -248,6 +310,16 @@ def check_schema():
select * select *
from pg_tables from pg_tables
where tablename = 'history' where tablename = 'history'
)
and exists(
select *
from pg_tables
where tablename = 'itemlist'
)
and exists(
select *
from pg_tables
where tablename = 'listcontent'
); );
""") """)
result = cursor.fetchall() result = cursor.fetchall()

View File

@ -18,9 +18,20 @@
<input id="additemid" type="text" placeholder="item id" pattern="[0-9]*"> <input id="additemid" type="text" placeholder="item id" pattern="[0-9]*">
<input id="addattributes" type="text" placeholder="attributes (comma separated)" pattern="[0-9a-zA-Z, ]*"> <input id="addattributes" type="text" placeholder="attributes (comma separated)" pattern="[0-9a-zA-Z, ]*">
<button id="addbutton" type="button">Add</button> <button id="addbutton" type="button">Add</button>
<button id="create_list" type="button">create list</button>
<button id="global_toggleshow" class="global_toggleshow" type="button">show hidden</button> <button id="global_toggleshow" class="global_toggleshow" type="button">show hidden</button>
</div> </div>
<!--popup create new list-->
<div id="createListPopup" class="popup">
<div class="popup-content">
<p>Create new list</p>
<input id="listName" type="text" placeholder="name">
<input id="listDescription" type="text" placeholder="description">
<button id="create_list_cancel" type="button">cancel</button>
</div>
</div>
<!--popup confirmation window--> <!--popup confirmation window-->
<div id="confirmationPopup" class="popup"> <div id="confirmationPopup" class="popup">
<div class="popup-content"> <div class="popup-content">

View File

@ -142,6 +142,23 @@ const apiUrl = `${currentUrl}app/delete`;
} }
} }
// close popups with escape key
document.addEventListener("keydown", (event) => {
if (event.isComposing || event.key === 'Escape') {
Array.from(document.getElementsByClassName("popup")).forEach(function(x) {
x.style.display = 'none';
});
}
});
// create new list
document.getElementById("create_list").addEventListener("click", function () {
createListPopup.style.display = 'flex';
});
document.getElementById("create_list_cancel").addEventListener("click", function () {
createListPopup.style.display = 'none';
});
// delete item, pop-up confirm // delete item, pop-up confirm
document.addEventListener('DOMContentLoaded', function () { document.addEventListener('DOMContentLoaded', function () {