Add user specific profiles
Some checks failed
Run Tests / run-tests (push) Failing after 58s

This commit is contained in:
2026-04-28 21:04:52 +02:00
parent f750cfa8e1
commit bd9ff7191a
12 changed files with 294 additions and 36 deletions

View File

@@ -7,48 +7,73 @@ from enums import CounterType
logger = logging.getLogger(__name__)
def create_counter(title:str, counter_type:CounterType, counter_color) -> None:
logger.info("Adding counter %s", counter_type)
user_id = int(st.session_state.user_id)
logger.info("Adding counter %s for user %d", counter_type, user_id)
with connection().session as session:
try:
query = text('INSERT INTO counters (name, type, color) VALUES (:title, :type, :color)')
session.execute(query, {'title': title, 'type': counter_type, 'color': counter_color})
session.commit()
query = text('INSERT INTO counters (user_id, name, type, color) VALUES (:user, :title, :type, :color)')
session.execute(query, {
'user': user_id,
'title': title,
'type': counter_type,
'color': counter_color
})
except Exception as e:
logger.error(e)
session.rollback()
def get_counters():
user_id = int(st.session_state.user_id)
try:
return connection().query('SELECT id, name, type, color FROM counters', ttl=0)
return connection().query("""
SELECT id, name, type, color
FROM counters
WHERE user_id = :user
""", params={'user': user_id })
except Exception as e:
logger.error(e)
return st.dataframe()
def increment_counter(counter_id:int) -> None:
logger.info("Incrementing counter %s", counter_id)
user_id = int(st.session_state.user_id)
logger.info("Incrementing counter %d for user %d", counter_id, user_id)
with connection().session as session:
try:
query = text('INSERT INTO entries (counter_id) VALUES (:id)')
session.execute(query, {'id': counter_id})
session.commit()
query = text('INSERT INTO entries (counter_id, user_id) VALUES (:id, :user)')
session.execute(query, {
'id': counter_id,
'user': user_id
})
except Exception as e:
logger.error(e)
session.rollback()
def remove_counter(counter_id:int) -> None:
logger.info("Removing counter %s", counter_id)
user_id = int(st.session_state.user_id)
logger.info("Removing counter %d from user %d", counter_id, user_id)
with connection().session as session:
try:
query = text('DELETE FROM counters WHERE id = :id')
session.execute(query, {'id': counter_id})
session.commit()
query = text('DELETE FROM counters WHERE id = :id AND user_id = :user')
session.execute(query, {
'id': counter_id,
'user': user_id
})
except Exception as e:
logger.error(e)
session.rollback()
def get_counter(counter_id:int):
user_id = int(st.session_state.user_id)
try:
return connection().query('SELECT * FROM counters WHERE id = :id', params={'id': counter_id}, ttl=0).iloc[0]
counters = connection().query("""
SELECT * FROM counters
WHERE id = :id AND user_id = :user
""", params={ 'id': counter_id, 'user': user_id}
)
if counters.empty:
return None
return counters.iloc[0]
except Exception as e:
logger.error(e)
return None