-
Notifications
You must be signed in to change notification settings - Fork 0
/
reports_sql.py
113 lines (76 loc) · 2.63 KB
/
reports_sql.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
#!/usr/bin/env python
from datetime import datetime
from sys import stderr
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
import configs
from database import Chats, Reports
DATABASE_URL = configs.DATABASE_URL
# Report user with given reporter/reported net_id and type and comment
def report_user(reporter, reported, rep_comment):
try:
engine = create_engine(DATABASE_URL)
Session = sessionmaker(bind=engine)
session = Session()
chat_id = (session.query(Chats.chat_id)
.filter((Chats.net_id1 == reporter) | (Chats.net_id2 == reporter))
.filter((Chats.net_id1 == reported) | (Chats.net_id2 == reported))
.one_or_none())
now = str(datetime.now())
new_report = Reports(report_id=chat_id[0],
reporter_net_id=reporter,
reported_net_id=reported,
comment=rep_comment,
date_time=now)
session.add(new_report)
session.commit()
session.close()
engine.dispose()
except Exception as ex:
print(ex, file=stderr)
exit(1)
def report_exist(chat_id):
try:
engine = create_engine(DATABASE_URL)
Session = sessionmaker(bind=engine)
session = Session()
report_id = (session.query(Reports)
.filter(Reports.report_id == chat_id)
.one_or_none())
session.close()
engine.dispose()
return report_id
except Exception as ex:
print(ex, file=stderr)
exit(1)
def get_all_reports():
try:
engine = create_engine(DATABASE_URL)
Session = sessionmaker(bind=engine)
session = Session()
reports = (session.query(Reports.report_id, Reports.reported_net_id, Reports.comment)
.all())
session.close()
engine.dispose()
return reports
except Exception as ex:
print(ex, file=stderr)
exit(1)
def dismiss_report(chat_id):
try:
engine = create_engine(DATABASE_URL)
Session = sessionmaker(bind=engine)
session = Session()
session.query(Reports).filter(Reports.report_id == chat_id).delete()
session.commit()
session.close()
engine.dispose()
except Exception as ex:
print(ex, file=stderr)
exit(1)
# unit test
def main():
report_user('Tester', 'Bully', 'This person was mean to me.')
# -----------------------------------------------------------------------
if __name__ == '__main__':
main()