-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransactions.py
172 lines (129 loc) · 4.29 KB
/
transactions.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from Models import *
from sqlobject import OR, AND, LIKE
import hashlib
def create_user(username, email, password):
try:
check = User.select(OR(User.q.username == username, User.q.email == email))
print (check.count())
if check.count() > 0:
return "invalid"
h = hashlib.md5()
h.update(password.encode())
User(username=username,
email=email,
password=h.hexdigest()
)
return "ok"
except Exception as e:
print(e)
return "failed"
def check_username_password(username, password):
try:
h = hashlib.md5()
h.update(password.encode())
select = User.select(AND(User.q.username == username, User.q.password == h.hexdigest()))
select.getOne()
return "ok"
except Exception as e:
print(e)
return "failed!"
def search_users(username, search_user):
try:
result = []
select = User.select(LIKE(User.q.username, "%{}%".format(search_user)))
# print(123)
select_for_con = UserContacts.select(UserContacts.q.user == User.select(User.q.username == username).getOne())
# print (dir(select_for_con))
# for i in select_for_con:
# print(i)
# print(type(i))
# print(i.user)
for i in select:
flag = False
for j in select_for_con:
print(j.con_user.username)
if j.con_user.username == i.username:
flag = True
break
if flag:
continue
result.append(i.username)
return result
except Exception as e:
print(e)
return "failed"
def add_contacts(username, contact):
try:
user = User.select(User.q.username == username).getOne()
contact_user = User.select(User.q.username == contact).getOne()
UserContacts(user=user, con_user=contact_user)
return "ok"
except Exception as e:
print(e)
return "failed"
def get_contacts_by_username(username):
try:
user = User.select(User.q.username == username).getOne()
user_contacts = UserContacts.select(UserContacts.q.user == user)
result = []
for i in user_contacts:
result.append(i.con_user.username)
return result
except Exception as e:
print(e)
return []
def create_room(username, room_name):
try:
user = User.select(User.q.username == username).getOne()
room = Room(admin=user, name=room_name, numbers=1)
RoomUsers(room=room, users=user)
return 'ok', room
except Exception as e:
print(e)
return "failed", None
def get_rooms(username):
try:
user = User.select(User.q.username == username).getOne()
room_users = RoomUsers.select(RoomUsers.q.users == user)
result = list(room_users)
return result
except Exception as e:
print(e)
return []
def get_room_by_id(room_id):
try:
room = Room.select(Room.q.id == room_id).getOne()
return room
except Exception as e:
print(e)
return 'failed'
def check_user_in_room(username, room_id):
try:
user = User.select(User.q.username == username).getOne()
room = Room.select(Room.q.id == room_id).getOne()
RoomUsers.select(AND(RoomUsers.q.users == user, RoomUsers.q.room == room)).getOne()
return 'ok'
except Exception as e:
print(e)
return 404
def get_contacts_of_room(room_id):
try:
room = Room.select(Room.q.id == room_id).getOne()
room_users = RoomUsers.select(RoomUsers.q.room == room)
# for i in room_users:
# print(i)
return list(room_users)
except Exception as e:
print(e)
return []
def add_to_room(room_id, username):
try:
if check_user_in_room(username, room_id) == 'ok':
return 'already'
room = Room.select(Room.q.id == room_id).getOne()
user = User.select(User.q.username == username).getOne()
RoomUsers(room=room, users=user)
return 'ok'
except Exception as e:
print(e)
return 'failed'