-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
319 lines (251 loc) · 7.89 KB
/
models.py
File metadata and controls
319 lines (251 loc) · 7.89 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import json
import datetime
import traceback
import os
import sqlite3
import hashlib
class db:
def __init__(self) -> None:
"""
Function to initialize the database
"""
self.conn=sqlite3.connect('./users.db')
self.conn.execute('CREATE TABLE IF NOT EXISTS USERS (username TEXT NOT NULL PRIMARY KEY, email TEXT NOT NULL, pass TEXT NOT NULL,key TEXT)')
self.conn.commit
def insertdb(self,username:str,email:str,passw:str):
"""
Function to insert the user in the database
:param username: str
:param email: str
:param passw: str
:return: str
"""
try:
if self.username_exists(username):
return "Username already exists"
import uuid
__api_key=uuid.uuid4()
self.conn.execute("INSERT INTO USERS(username,email,pass,key)values(?,?,?,?)",(username,email,hashlib.sha256(passw.encode()).hexdigest(),str(__api_key)))
self.conn.commit()
return "User added successfully"
except Exception as e:
(traceback.format_exc())
return "Something went wrong"
def validate(self,username:str,passw:str):
"""
Function to validate the user for login
:param username: str
:param passw: str
:return: bool
"""
try:
res=self.conn.execute("SELECT username,pass from USERS WHERE (username,pass)=(?,?) ",(username,hashlib.sha256(passw.encode()).hexdigest()))
r=res.fetchall()
if len(r)==0:
return False
return True
except Exception as e:
(traceback.format_exc())
return False
def get_api_key(self,username:str,passw:str):
"""
Function to get the api key of the user
:param username: str
:return: str
"""
try:
res=self.conn.execute("SELECT key from USERS WHERE (username,pass)=(?,?)",(username,hashlib.sha256(passw.encode()).hexdigest()))
r=res.fetchall()
if len(r)==0:
return "User not found"
return r
except Exception as e:
return traceback.format_exc()
def username_exists(self,username:str):
"""
Function to check if the username exists
:param username: str
:return: bool
"""
try:
res=self.conn.execute("SELECT * from USERS WHERE username=?",(username,))
r=res.fetchone()
return r is not None
except Exception as e:
print(traceback.format_exc())
return True
# DB=db()
# (DB)
# DB.insertdb('ashu','ashu@gmail.com','ashuashu')
# DB.insertdb('ashu1','ashu1@gmail.com','ashuashu1')
# DB.insertdb('ashu2','ashu2@gmail.com','ashuashu2')
# (DB.insertdb('ashu2','ashu2@gmail.com','ashuashu2'))
# (DB.get_api_key('ashu','ashuashu')[0][0])
# (hashlib.sha256('ashuashu'.encode()).hexdigest())
wdir=os.getcwd()
def create(api_key:str,username:str):
"""
Create a new user
:param api_key: str
:param username: str
:return: bool | exception
"""
try:
directory = './data'
# Check if the directory exists, create it if it does not
if not os.path.exists(directory):
os.makedirs(directory)
filepath = f"{directory}/{api_key[0][0]}.json"
data = {
"username": username,
"notes": {
"count": "0",
"note": {}
},
"reminders": {
"count": "0",
"reminder": {}
}
}
with open(filepath, 'a') as file:
json.dump(data, file)
return True
except Exception as e:
(traceback.format_exc())
return False
# r=create('fa14263c-b79d-4fa7-a6e7-d8a5d1cd5565','ashu')
# (r)
def open_file(filename):
"""
Open a file and return the contents
:param filename: str
:return: str
"""
try:
with open(f"./data/{filename}.json", 'r') as file:
(file)
return json.load(file)
except Exception as e:
(traceback.format_exc())
return False
def save_file(filename,data):
try:
with open(f"./data/{filename}.json", 'w') as file:
json.dump(data, file, indent=4)
return True
except Exception as e:
return False,e
def add_note(api_key:str,body:str, pinned:bool=False, title:str = "Enter Title", date:str=datetime.datetime.now()):
"""
Add a note to the notes file
:param api_key: str
:param body: str
:param pinned: bool
:param title: str
:param date: str
:return: bool | exception
"""
try:
notes = open_file(api_key)
count=int(notes['notes']['count'])+1
note = {
'title': str(title),
'body': str(body),
'date': str(date),
'pinned': str(pinned)
}
notes['notes']['note'][str(count)] = note
notes['notes']['count'] = str(count)
r=save_file(api_key,notes)
# (r)
return True
except Exception as e:
(traceback.format_exc() )
return False
# r=add_note('api_key',"hello world",pinned=True)
# (r)
def update_notes(api_key:str,note_id:str,body:str, date:str, pinned:bool=False, title:str = "Enter Title"):
"""
Update a note in the notes file
:param api_key: str
:param note_id: str count
:param body: str
:param date: str
:param pinned: bool
:param title: str
:return: bool | exception | str
"""
try:
notes = open_file(api_key)
count=int(notes['notes']['count'])
if int(note_id)>count:
# (note_id,count)
return "note not present"
# note_id=notes['notes']['note'][note_id]
# (note_id)
else:
note = {
'title': str(title),
'body': str(body),
'date': str(date),
'pinned': str(pinned),
"last_change":str(datetime.datetime.now())
}
notes = open_file(api_key)
notes['notes']['note'][note_id] = note
r=save_file(api_key,notes)
(r)
return True
except Exception as e:
("update notes :",traceback.format_exc())
return False
# r=update_notes('api_key_here','3',"helo world","2024-06-21 14:56:32.215340",pinned=True)
# (r)
def delete_note(api_key:str,note_id:str):
"""
Delete a note in the notes file
:param api_key: str
:param note_id: str count
:return: bool | exception | str
"""
try:
notes = open_file(api_key)
count=int(notes['notes']['count'])
if int(note_id)>count:
return "note not present"
else:
notes['notes']['note'].pop(note_id)
notes['notes']['count'] = str(int(notes['notes']['count'])-1)
r=save_file(api_key,notes)
return True
except Exception as e:
("delete notes :",traceback.format_exc())
return False
# r=delete_notes('api_key_here','3')
# (r)
def fetch_notes(api_key:str):
"""
Fetch all notes in the notes file
:param api_key: str
:return: dict | exception
"""
try:
notes = open_file(api_key)
(notes)
note=notes['notes']['note']
return note
except Exception as e:
("fetch notes :",traceback.format_exc())
return False
# r=fetch_notes('api_key_here')
# (r)
def is_strong_password(password):
import re
# Minimum length of 8 characters
if len(password) < 8:
return False
# At least one digit
if not re.search(r'\d', password):
return False
# If all conditions are met
return True