-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.py
213 lines (193 loc) · 8.72 KB
/
models.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from jdatetime import datetime
from json import loads, dumps
from hashlib import sha256
from random import choice
from requests import get
base_domain = "mobilearn.ir"
class BaseModel:
def __init__(self, db:str):
self.db_path = db
self.db:dict = loads(open(self.db_path).read())
def get(self, guid:str) -> dict :
guids = [self.db[i]['id'] for i in self.db]
return {"status": "OK", "statuscode": 200, "data": {"object": self.db[list(self.db.keys())[guids.index(guid)]]}} if guid in guids else {"status": "ERROR", "statuscode": 404, "data": {"message": "object not found"}}
def getAll(self) -> dict:
return {"status": "OK", "statuscode": 200, "data": {"db": self.db}}
def makeGUID(self, start_char:str, length:int=32) -> str:
choices = [*"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"]
selected = start_char + "".join([choice(choices) for i in range(length - len(start_char))])
while self.get(selected)["statuscode"] != 404 :
# make a new UUID while this UUID is exist in table
selected = start_char + "".join([choice(choices) for i in range(length - len(start_char))])
return selected
def delete(self, guid:str):
if self.db.get(guid) != None:
self.db.pop(guid)
with open(self.db_path, "w") as writer: writer.write(dumps(self.db, indent=4, ensure_ascii=False))
return {"status": "OK", "statuscode": 200, "data": {}}
return {"status": "ERROR", "statuscode": 404, "data": {"message": "object not found"}}
def edit(self, guid:str, key:str, value:any) -> dict:
for user in self.db.keys():
if self.db[user]['id'] == guid:
self.db[user][key] = value
with open(self.db_path, "w") as writer: writer.write(dumps(self.db, indent=4, ensure_ascii=False))
return {"status": "OK", "statuscode": 200, "data": {"object": self.db[user]}}
return {"status": "ERROR", "statuscode": 404, "data": {"message": "object not found"}}
class Manage:
def __init__(self):
self.db = loads(open("static/dbs/manage.json").read())
def addToQueue(self, section:str, key:str, value:any) -> dict:
self.db[section][key].append(value)
with open("static/dbs/manage.json", "w") as writer: writer.write(dumps(self.db, indent=4, ensure_ascii=False))
return dict(status="OK", statuscode=200, data={})
def report(self, ID:str, reporter:str, reason:str) -> dict:
return Manage().addToQueue(self, "verifications", "reports", {
"id": ID,
"reason": reason,
"user": reporter
})
def searched(self, query:str) -> dict:
self.db["statistics"]["searchs"][query] = (self.db["statistics"]["searchs"].get(query) or 0) + 1
with open("static/dbs/manage.json", "w") as writer: writer.write(dumps(self.db, indent=4, ensure_ascii=False))
return dict(status="OK", statuscode=200, data={})
def log(self, text:str) -> dict:
return Manage().addToQueue(self, "records", "logs", {
"text": text,
"datetime": datetime.now().strftime("%Y/%m/%d %H:%M")
})
class User(BaseModel):
def __init__(self):
super().__init__("static/dbs/accounts.json")
def new(self, email:str, password:str) -> dict:
if not email in self.db.keys():
ID = self.makeGUID('u')
self.db[email] = {
"id": ID,
"type": "user",
"name": f"کاربر {len(self.db)+1}",
"email": email,
"password": sha256(password.encode()).hexdigest(),
"wallet": {
"left": 0,
"actions": []
},
"thumbnail": "../static/assets/user.png",
"bookmarks": [],
"registed_courses": [],
"notifs": [],
"cart": [],
"interactions": {
"articles": {
"likes": [],
"dislikes": []
},
"courses": {
"likes": [],
"dislikes": []
},
"comments": {
"likes": [],
"dislikes": []
}
},
"blog": {
"posts": [],
"followers": [],
"followings": []
},
"is_banned": False,
"pub_courses": [],
"cv": "",
"accesses": []
}
with open("static/dbs/accounts.json", "w") as writer: writer.write(dumps(self.db, indent=4, ensure_ascii=False))
return {"status": "OK", "statuscode": 200, "data": {"object": self.db[email]}}
else:
return {"status": "OK", "statuscode": 302, "data": {"message": "user is already exists"}}
class Course(BaseModel):
def __init__(self):
super().__init__("static/dbs/publications.json")
self.All = self.db
self.db = self.db["courses"]
def new(self, inputs):
'''
inputs:
- thumbnail : str
- title : str
- master : str
- price : int
- level : str
- description : str
- category : str
'''
guid = self.makeGUID('c')
self.db[guid] = {
"id": guid,
"thumbnail": inputs["thumbnail"],
"title": inputs["title"],
"master": inputs["master"],
"price": {
"before_discount": inputs["price"],
"after_discount": inputs["price"]
},
"students": 0,
"videos": [],
"comments": [],
"quizzes": [],
"time": "0:00:00",
"level": inputs["level"],
"status": "در حال برگزاری",
"last_update": datetime.now().strftime("%Y/%m/%d %H:%M"),
"shortlink": get(f"https://rizy.ir/api?api=bae7206c5c017f0537a30660fcdec85027fbf686&url={base_domain}/course?id={guid}&format=text").text,
"description": inputs["description"],
"category": input["category"],
"is_verified": False
}
with open("static/dbs/publications.json", "w") as writer: writer.write(dumps(self.db, indent=4, ensure_ascii=False))
Manage().addToQueue("verifications", "new_courses", guid)
def edit(self, guid:str, key:str, value:any) -> dict:
for course in self.db.keys():
if self.db[course]['id'] == guid:
self.db[course][key] = value
with open(self.db_path, "w") as writer: writer.write(dumps({"courses": self.db, "blog": self.All["blog"]}, indent=4, ensure_ascii=False))
return {"status": "OK", "statuscode": 200, "data": {"object": self.db[course]}}
return {"status": "ERROR", "statuscode": 404, "data": {"message": "object not found"}}
class Article(BaseModel):
def __init__(self):
super().__init__("static/dbs/publications.json")
self.All = self.db
self.db = self.db["blog"]
def new(self, inputs):
'''
inputs:
- thumbnail : str
- title : str
- author : str
- html : str
- category : str
'''
guid = self.makeGUID('a')
self.db[guid] = {
"id": guid,
"thumbnail": inputs["thumbnail"],
"title": inputs["title"],
"datetime": datetime.now().strftime("%Y/%m/%d %H:%M"),
"author": inputs["author"],
"views": 1,
"likes": 0,
"dislikes": 0,
"shortlink": get(f"https://rizy.ir/api?api=bae7206c5c017f0537a30660fcdec85027fbf686&url={base_domain}/article?id={guid}&format=text").text,
"comments": [],
"html": inputs["html"],
"category": inputs["category"],
"is_verified": False
}
with open("static/dbs/publications.json", "w") as writer: writer.write(dumps(self.db, indent=4, ensure_ascii=False))
Manage().addToQueue("verifications", "new_articles", guid)
def edit(self, guid:str, key:str, value:any) -> dict:
for article in self.db.keys():
if self.db[article]['id'] == guid:
self.db[article][key] = value
with open(self.db_path, "w") as writer: writer.write(dumps({"courses": self.All["courses"], "blog": self.db}, indent=4, ensure_ascii=False))
return {"status": "OK", "statuscode": 200, "data": {"object": self.db[article]}}
return {"status": "ERROR", "statuscode": 404, "data": {"message": "object not found"}}