Skip to content

Commit

Permalink
multiple improvements across all files #1 #3 #4
Browse files Browse the repository at this point in the history
  • Loading branch information
michivonah committed Apr 7, 2023
1 parent 76ebe64 commit 2e2efd3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 31 deletions.
5 changes: 3 additions & 2 deletions DB/create_views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ Michi von Ah - April 2023

DROP VIEW IF EXISTS "alltickets";
CREATE VIEW "alltickets" AS
SELECT ticketid AS "Ticketnumber", ticket."name" AS "Ticketname", ticket."description" AS "Description", "customer"."name" AS "Customer", "user"."username" AS "Assigned to" FROM ticket
SELECT ticketid AS "Ticketnumber", ticket."name" AS "Ticketname", ticket."description" AS "Description", "customer"."name" AS "Customer", "user"."username" AS "Assigned to", "status"."name" AS "Status" FROM ticket
JOIN customer ON customerid = fk_customerid
JOIN "user" ON userid = fk_userid;
JOIN "user" ON userid = fk_userid
JOIN status ON statusid = fk_statusid;

DROP VIEW IF EXISTS "alltickets-count";
CREATE VIEW "alltickets-count" AS
Expand Down
12 changes: 11 additions & 1 deletion dbfunctions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import streamlit as st
import psycopg2
import pandas as pd

# Create a connection to database
#@st.cache_resource
Expand All @@ -18,4 +19,13 @@ def executeQuery(query):
conn = connectDatabase()
conn.execute(query)
result = conn.fetchall()
return result
return result

#@st.cache_data(ttl=30)
def loadTable(query):
conn = connectDatabase()
conn.execute(query)
result = conn.fetchall()
colnames = [desc[0] for desc in conn.description]
df = pd.DataFrame(result, columns=colnames)
return df
14 changes: 7 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
branding.loadBranding()

def createUser(email, password, username):
db = dbfunctions.connectDatabase()
db.execute(f"INSERT INTO \"user\" (username, password, mail, fk_usergroupid) VALUES ('{username}', '{password}', '{email}', 2);")
dbfunctions.executeQuery(f"INSERT INTO \"user\" (username, password, mail, fk_usergroupid) VALUES ('{username}', '{password}', '{email}', 2);")
userdata = dbfunctions.executeQuery(f"SELECT DISTINCT username, password, userid from \"user\" WHERE mail = '{email}';")
st.session_state['loginSucceed'] = True
st.session_state['username'] = username
st.session_state['userid'] = userdata[0][2]
st.sidebar.success('Account created.', icon="✅")

def collectUserinfo(email, password):
Expand All @@ -24,16 +25,16 @@ def collectUserinfo(email, password):
def loginUser(email, password):
userdata = dbfunctions.executeQuery(f"SELECT DISTINCT username, password, userid from \"user\" WHERE mail = '{email}';")
if userdata:
for value in userdata:
username = value[0]
correctPW = value[1]
userid = value[2]
username = userdata[0][0]
correctPW = userdata[0][1]
userid = userdata[0][2]
if password == correctPW:
st.session_state['loginSucceed'] = True
st.session_state['username'] = username
st.session_state['userid'] = userid
st.info('Welcome back', icon="👋🏻")
else:
st.session_state['loginSucceed'] = False
st.warning('Wrong password')
else:
createUser(email, password, email)
Expand Down Expand Up @@ -67,6 +68,5 @@ def loginUser(email, password):
st.session_state['email'] = email
passwordHashed = hashlib.sha256(password.encode())
st.session_state['password'] = passwordHashed.hexdigest()
st.session_state['loginSucceed'] = False
loginUser(email, st.session_state.password)

30 changes: 9 additions & 21 deletions pages/tickets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import streamlit as st
import pandas as pd
import dbfunctions
import branding

Expand All @@ -9,32 +8,23 @@
def createTicket(name, desc):
dbfunctions.executeQuery(f"INSERT INTO ticket (name, description, fk_statusid, fk_userid, fk_customerid) VALUES ('{name}', '{desc}', 1, 1, 1);")

# load tickets from database
#@st.cache_data(ttl=30)
def loadTickets():
db = dbfunctions.connectDatabase()
db.execute("SELECT * FROM alltickets")
result = db.fetchall()
colnames = [desc[0] for desc in db.description]
df = pd.DataFrame(result, columns=colnames)
return df

st.write("""
# Tickets
""")

ticketList, newTicket = st.tabs(["Tickets", "Create new ticket"])
ticketList, myTickets, newTicket = st.tabs(["All Tickets", "My Tickets", "Create new ticket"])

with ticketList:
st.write("""
Here you can see all your tickets:
""")
st.dataframe(loadTickets())
showClosedTickets = st.checkbox('Show completed tickets')
if showClosedTickets:
st.dataframe(dbfunctions.loadTable("SELECT * FROM alltickets ORDER BY \"Ticketnumber\""), use_container_width=True)
else:
st.dataframe(dbfunctions.loadTable("SELECT * FROM alltickets WHERE \"Status\" = 'Open' ORDER BY \"Ticketnumber\""), use_container_width=True)

with myTickets:
st.dataframe(dbfunctions.loadTable(f"SELECT * FROM alltickets WHERE \"Status\" = 'Open' AND \"Assigned to\" = '{st.session_state.username}' ORDER BY \"Ticketnumber\""), use_container_width=True)

with newTicket:
st.write("""
Here you can create a new ticket:
""")
with st.container():
newTicketname = st.text_input('Ticketname')
customerList = dbfunctions.executeQuery(f"SELECT \"name\" FROM customer;")
Expand All @@ -47,5 +37,3 @@ def loadTickets():
if createTicketBtn:
createTicket(newTicketname, ticketDescription)



0 comments on commit 2e2efd3

Please sign in to comment.