-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
81 lines (63 loc) · 2.64 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
import bcrypt
# from models.peewee_models import User, Reminder, Task, Tag, Category
from typing import Any, Dict
import peewee
import re
def create_table_name(model_class):
model_name = model_class.__name__
return model_name.lower() + ' table'
def hash_password(input_password: str):
byte_passwd = input_password.encode('utf-8')
hashed_password = bcrypt.hashpw(byte_passwd, bcrypt.gensalt())
hashed_password = hashed_password.decode('utf-8')
return hashed_password
def format_uuid(uuid_str):
if len(uuid_str) == 32 and uuid_str.isalnum():
formatted_uuid = f"{uuid_str[:8]}-{uuid_str[8:12]}-{uuid_str[12:16]}-{uuid_str[16:20]}-{uuid_str[20:]}"
return formatted_uuid
else:
raise ValueError("Invalid UUID string format")
# TODO Exception Handlers
def validate_email(email):
emailRegex = re.compile(r'''(
[a-zA-Z0-9._%+-]+ #username
@ # @ symbol
[a-zA-A0-9.-]+ # domain name
(\.[a-zA-Z]{2,4}) # dot-something
)''', re.VERBOSE)
bool(emailRegex.match(email))
# TODO function to abstract response ?
# TODO common function for repo queries
def safe_query(query) -> Any:
try:
if isinstance(query, peewee.ModelInsert):
query.execute() # Insert operation
return {"success": True, "message": "Insert successful"}
elif isinstance(query, peewee.ModelUpdate):
rows_updated = query.execute() # Update operation
return {"success": True, "message": f"Updated {rows_updated} rows"}
elif isinstance(query, peewee.ModelDelete):
rows_deleted = query.execute() # Delete operation
return {"success": True, "message": f"Deleted {rows_deleted} rows"}
else:
try:
result = query.get() # Select operation
return {"success": True, "data": result}
except peewee.DoesNotExist as e:
return {"success": False, "message": "No matching records found"}
except peewee.PeeweeException:
return {"success": False, "message": "Query failed"}
# # Example usage:
# user = User(username="john")
# result = safe_query(user.save(), User) # Insert
# print(result)
# user.username = "new_john"
# query = User.update(username="new_john").where(User.username == "john")
# result = safe_query(query, User) # Update
# print(result)
# query = User.delete().where(User.username == "new_john")
# result = safe_query(query, User) # Delete
# print(result)
# query = User.select().where(User.username == "new_john")
# result = safe_query(query, User) # Select
# print(result)