-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.py
109 lines (90 loc) · 3.28 KB
/
control.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
#JWT
import jwt
#
import subprocess
from send_email.formats.verification_code import verification_code_msg
from send_email.message import Message, verificationTextFormatter
from send_email.send_email import sendEmail
class Controls:
pass
class TokenControls(Controls):
def __init__(self,secret_key):
self.__secret_key = secret_key
def generateToken(self,**kwargs:str):
"""
To generate token, specify the key and values
Example: key_one="the value_one",key_two="value_two"
"""
token = jwt.encode(kwargs,self.__secret_key,algorithm="HS256")
return token
def getTokenValues(self,token:str):
"""
Return Dict value of JWT or return None if the token is invalid
"""
try:
values = jwt.decode(token,self.__secret_key,algorithms=["HS256"])
return values
except:
return None
class BasicControls(Controls):
def sanitizeEmail(self,email:str):
"""
Sanitize email by removing invalid characters
"""
invalids = [" ","\"","\\", "/", "<", ">","|","\t",":"]
for x in invalids:
email = email.strip(x)
return email.strip()
def bytesToGb(self,bytesize):
return bytesize / (1024*1024*1024)
def bytesToMb(self,bytesize):
return bytesize / (1024*1024)
def removeStaticDirectory(self,directory):
"This remove static"
filelocation = ""
if "static" in directory:
for i in directory.split("/")[1:]:
filelocation+=i+"/"
filelocation = filelocation[:-1]
else:
filelocation = directory
return filelocation
def getFileNameFromDirectory(self,directory):
directory = self.removeStaticDirectory(directory)
return directory.split("/")[-1]
class PagingControl(Controls):
def generatePagination(self,count,limit):
return [str(x+1 )for x in range((count//limit)+(1 if count%limit != 0 else 0))]
def generateResultBadge(self,count,limit,offset,search):
return {
"start": offset + 1,
"end":offset+limit if offset+limit < count else count,
"count":count if count > 0 else None,
"search":search
}
def generateOffset(self,page,count,limit):
offset = 0
if page:
try:
offset = limit*(int(page)-1)
except:
if page == "end":
offset = limit*(int(count//limit))
return offset
class ProcessControl():
def runSubprocess(self,stmt):
process = subprocess.Popen(stmt, stdout=subprocess.PIPE)
while True:
data = process.stdout.read(4000)
if len(data) == 0:
break
class SendEmailControl():
def sendForgotPwdVerification(self,ver_code,email):
request_details = "You are requesting to forgot your password"
request_code = ver_code
message = Message()
message.subject = "Bleepy Forgot Password Verification"
message.receiver_email = email
message.text = verificationTextFormatter(request_details,request_code)
message.html = verification_code_msg(message.subject,request_details,request_code)
return sendEmail(message)