-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_handler.py
144 lines (123 loc) · 4.61 KB
/
db_handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from jinja2 import Environment, FileSystemLoader
from relative_finance import *
from decimal import Decimal
from file_handler import *
import mysql.connector
from expense import *
from user import *
dbconfig_dict = load_dict("dbconfig.json");
finanzministerium = {}
cursor = {}
def initialize_database ():
global finanzministerium
global cursor
dbconfig_dict = load_dict("dbconfig.json");
finanzministerium = mysql.connector.connect(
host = dbconfig_dict["host"],
user = dbconfig_dict["user"],
passwd = dbconfig_dict["passwd"],
database = dbconfig_dict["database"])
cursor = finanzministerium.cursor(prepared=True)
def check_connection ():
global finanzministerium
global cursor
if not finanzministerium.is_connected():
finanzministerium = mysql.connector.connect(
host = dbconfig_dict["host"],
user = dbconfig_dict["user"],
passwd = dbconfig_dict["passwd"],
database = dbconfig_dict["database"])
cursor = finanzministerium.cursor(prepared=True)
def add_user (tag, full_name, chat_id):
check_connection()
cursor.execute("INSERT INTO users (tag, full_name, chat_id) VALUES (%s, %s, %s);",
(tag, full_name, str(chat_id)))
finanzministerium.commit()
def register_message (message_type):
check_connection()
cursor.execute("INSERT INTO messages (type) VALUES (%s);",
(message_type,))
finanzministerium.commit()
def register_expense ():
check_connection()
cursor.execute("INSERT INTO expenses VALUES ();")
finanzministerium.commit()
def get_user (tag):
check_connection()
cursor.execute("SELECT tag, full_name, chat_id FROM users WHERE tag=%s;", (tag,))
result = cursor.fetchall()
if result:
user = User()
user.tag = result[0][0].decode()
user.full_name = result[0][1].decode()
user.chat_id = result[0][2]
return user
else:
return None
def get_status (user_a):
check_connection()
cursor.execute("SELECT user_b, value FROM relative_finance WHERE user_a=%s AND NOT value=0 "
"UNION SELECT user_a AS user_b, -value FROM relative_finance WHERE user_b=%s AND NOT value=0",
(user_a, user_a))
result = cursor.fetchall();
return [RelativeFinance(userB = entry[0].decode(), value=Decimal(entry[1].decode())) for entry in result]
def get_relative_finance (user_a, user_b):
check_connection()
if user_a == user_b:
return Decimal(0)
switched = False
if (user_a > user_b):
# switch users and invert value
tmp = user_a
user_a = user_b
user_b = tmp
switched = True
cursor.execute("SELECT value FROM relative_finance WHERE user_a=%s AND user_b=%s;",
(user_a, user_b))
result = cursor.fetchall();
if len(result) > 0:
if switched:
return Decimal(-1) * Decimal(result[0][0].decode())
else:
return Decimal(result[0][0].decode())
else:
# insert new entry
cursor.execute("INSERT INTO relative_finance (user_a, user_b, value) VALUES (%s, %s, %s)",
(user_a, user_b, str(Decimal(0))))
finanzministerium.commit()
return Decimal(0)
def settle_differences (user_a, user_b):
check_connection()
if user_a == user_b:
return
if (user_a > user_b):
# switch users
tmp = user_a
user_a = user_b
user_b = tmp
cursor.execute("DELETE FROM relative_finance WHERE user_a=%s AND user_b=%s",
(user_a, user_b))
finanzministerium.commit()
def add_simple_expense (expense):
check_connection()
if expense.userA == expense.userB:
return
# user b owes user a value units
if (expense.userA > expense.userB):
# switch users and invert value
old_value = get_relative_finance(expense.userB, expense.userA)
new_value = old_value + expense.value
cursor.execute("UPDATE relative_finance SET value=%s WHERE user_a=%s AND user_b=%s;",
(new_value, expense.userB, expense.userA))
finanzministerium.commit()
else:
# users are in correct order
old_value = get_relative_finance(expense.userA, expense.userB)
new_value = old_value - expense.value
cursor.execute("UPDATE relative_finance SET value=%s WHERE user_a=%s AND user_b=%s;",
(new_value, expense.userA, expense.userB))
finanzministerium.commit()
def add_expense (expense, user_a):
check_connection()
for expense in expense.to_expense_list(user_a):
add_simple_expense(expense)