-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
127 lines (100 loc) · 4.16 KB
/
util.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
import psycopg2
import os
import requests
dbconn = psycopg2.connect(database="kryss",
host="localhost",
user="la1k",
password=os.environ['dbpass'],
port="5432")
def get_transactions(number = 100, username = None):
cursor = dbconn.cursor()
if username == None:
cursor.execute("""SELECT "ID", "sum", "slug", "time" FROM "public"."transaction_names" ORDER BY "ID" DESC LIMIT %s""", [number])
else:
cursor.execute("""SELECT "ID", "sum", "slug", "time" FROM "public"."transaction_names" WHERE "slug" = %s ORDER BY "ID" DESC LIMIT %s""", [username, number])
transactions = cursor.fetchall()
cursor.close()
return transactions
def see_all_trans():
cursor = dbconn.cursor()
cursor.execute(""" SELECT * FROM transactions""")
low_balance = cursor.fetchall()
cursor.close()
return low_balance
def data_in():
conn = requests.Session()
try:
conn.get(url="https://ufs.samfundet.no/", auth=('mortekho', os.environ['ufspass']), timeout=5)
data = conn.get(url='https://ufs.samfundet.no/ark/api/accounts/', auth=('mortekho', os.environ['ufspass']), timeout=5).json()
except:
return 1
cursor = dbconn.cursor()
for i in data:
cursor.execute("INSERT INTO users VALUES (%s, %s, %s, %s, %s) ON CONFLICT (id) DO UPDATE SET slug=%s, callsign=%s, ignore_limit=%s, balance=%s", (data[i]["id"], data[i]["slug"], data[i]["short_name"], data[i]["ignore_block_limit"], data[i]["balance"], data[i]["slug"], data[i]["short_name"], data[i]["ignore_block_limit"], data[i]["balance"]))
dbconn.commit()
cursor.close()
return 1
def make_unblocked(username=None):
cursor = dbconn.cursor()
if username==None:
cursor.execute(f"""UPDATE "public"."users" SET is_blocked = false WHERE "balance" >= 0""")
else:
cursor.execute(f"""UPDATE "public"."users" SET is_blocked = false WHERE "slug" = '{username}'""")
dbconn.commit()
cursor.close()
def make_blocked():
cursor = dbconn.cursor()
for i in find_low_bal():
if i[1] == True:
cursor.execute(f"""UPDATE "public"."users" SET is_blocked = false WHERE id = {i[0]}""")
else:
cursor.execute(f"""UPDATE "public"."users" SET is_blocked = true WHERE id = {i[0]}""")
dbconn.commit()
cursor.close()
def find_low_bal():
cursor = dbconn.cursor()
cursor.execute("""SELECT "id", "ignore_limit", "balance" FROM "public"."users" WHERE "balance" < 0""")
low_balance = cursor.fetchall()
cursor.close()
return low_balance
def get_user_from_nfc_or_username(nfc=-1, username=""):
cursor = dbconn.cursor()
if nfc == -1 and username != "":
cursor.execute("""SELECT "id","slug","callsign","ignore_limit","balance","usage","nfc", "is_blocked" FROM "public"."users" WHERE "slug" = %s """, [username])
elif nfc != -1 and username == "":
cursor.execute("""SELECT "id","slug","callsign","ignore_limit","balance","usage","nfc", "is_blocked" FROM "public"."users" WHERE "nfc" = '%s' """, [int(nfc)])
else:
cursor.close()
return -3
ret = cursor.fetchall()
cursor.close()
if len(ret) == 1:
return ret
elif len(ret) == 0:
return -1
else:
return -2
def update_usage(nfc, sum):
cursor = dbconn.cursor()
preUsage = get_user_from_nfc_or_username(nfc=nfc)[0][5]
if preUsage == None:
preUsage = 0
cursor.execute(f"""UPDATE "public"."users" SET usage = {sum+preUsage} WHERE nfc = '{nfc}'""")
dbconn.commit()
cursor.close()
cursor.close()
def write_transaction(user, sum):
if sum == 0:
return 1
cursor = dbconn.cursor()
cursor.execute("""INSERT INTO transactions ("sum", "user") VALUES (%s, %s)""", (sum, user[0][0]))
dbconn.commit()
cursor.close()
return 1
def nfc_reg(username, nfc):
cursor = dbconn.cursor()
cursor.execute(f"""UPDATE "public"."users" SET nfc = {nfc} WHERE slug = '{username}'""")
dbconn.commit()
cursor.close()
#To inprove run in a cronjob :/
data_in()