-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
150 lines (125 loc) · 4.95 KB
/
helpers.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
"""Provides helpers functionality for BLOGz"""
# helpers.py
# 2017, Geoffrey Hadler
import time
from datetime import datetime
import pytz
from tzlocal import get_localzone
from flask import session, request
from sqlalchemy.orm import load_only
from app import valid_session_key, ghDEBUG, bcrypt
from models import BlogzUser
from strings import *
class ValidateSignup:
def __init__(self, strUserName, strUserPass0, strUserPass1, strUserEmail ):
# For Form Data
self.strUserName = strUserName
self.strUserPass = [ strUserPass0, strUserPass1 ]
self.strUserEmail = strUserEmail
# For Errors
self.errUserName = ""
self.errUserPass = [ "", "" ]
self.errUserEmail = ""
# For Status Style
self.ssUserName = ""
self.ssUserPass = [ "", "" ]
self.ssUserEmail = ""
def isValid(self):
# Begin VALIDATION
isSuccess = True
# CHECK for EMPTY
if self.strUserName == "":
isSuccess = False
self.ssUserName = ERRSTR_STATUS_ERROR
self.errUserName += ERRSTR_EMPTY_FIELD
# CHECK for LENGTH
if len(self.strUserName) < 3:
isSuccess = False
self.ssUserName = ERRSTR_STATUS_ERROR
self.errUserName += ERRSTR_SHORT_FIELD_3
if len(self.strUserName) > 20:
isSuccess = False
self.ssUserName = ERRSTR_STATUS_ERROR
self.errUserName += ERRSTR_LONG_FIELD_20
# CHECK for SPACES (find returns -1 if not found)
if self.strUserName.find(" ") > -1:
isSuccess = False
self.ssUserName = ERRSTR_STATUS_ERROR
self.errUserName += ERRSTR_SPACES_FIELD
# CHECK for UNIQUE
if BlogzUser.query.filter_by( handle = self.strUserName ).options( load_only("handle") ).first() != None:
isSuccess = False
self.ssUserName = ERRSTR_STATUS_ERROR
self.errUserName += ERRSTR_USER_EXIST
for i in range(len(self.strUserPass)):
# CHECK for EMPTY
if self.strUserPass[i] == "":
isSuccess = False
self.ssUserPass[i] = ERRSTR_STATUS_ERROR
self.errUserPass[i] += ERRSTR_EMPTY_FIELD
# CHECK for LENGTH
if len(self.strUserPass[i]) < 3:
isSuccess = False
self.ssUserPass[i] = ERRSTR_STATUS_ERROR
self.errUserPass[i] += ERRSTR_SHORT_FIELD_3
if len(self.strUserPass[i]) > 20:
isSuccess = False
self.ssUserPass[i] = ERRSTR_STATUS_ERROR
self.errUserPass[i] += ERRSTR_LONG_FIELD_20
# CHECK for MATCH
if self.strUserPass[0] != self.strUserPass[1]:
isSuccess = False
for i in range(len(self.strUserPass)):
self.ssUserPass[i] = ERRSTR_STATUS_ERROR
self.errUserPass[i] = ERRSTR_MATCH_FIELD
# CHECK for EMPTY
if self.strUserEmail == "":
isSuccess = False
self.ssUserEmail = ERRSTR_STATUS_ERROR
self.errUserEmail = ERRSTR_EMPTY_FIELD
# CHECK for SPACES (find returns -1 if not found)
if self.strUserEmail.find(" ") > -1:
isSuccess = False
self.ssUserEmail = ERRSTR_STATUS_ERROR
self.errUserEmail += ERRSTR_SPACES_FIELD
# CHECK for @
if self.strUserEmail.find("@") == -1:
isSuccess = False
self.ssUserEmail = ERRSTR_STATUS_ERROR
self.errUserEmail += ERRSTR_INVALID_EMAIL
# Check for LENGTH
if len(self.strUserEmail) < 3:
isSuccess = False
self.ssUserEmail = ERRSTR_STATUS_ERROR
self.errUserEmail += ERRSTR_SHORT_FIELD_3
if len(self.strUserEmail) > 127:
isSuccess = False
self.ssUserEmail = ERRSTR_STATUS_ERROR
self.errUserEmail += ERRSTR_LONG_FIELD_127
# CHECK for UNIQUE
if BlogzUser.query.filter_by( handle = self.strUserName ).options( load_only("email") ).first() != None:
isSuccess = False
self.ssUserName = ERRSTR_STATUS_ERROR
self.errUserName += ERRSTR_EMAIL_EXIST
return isSuccess
# some utils
def gh_getLocalTime( utc_dt ):
loc_z = get_localzone()
loc_dt = utc_dt.replace(tzinfo=pytz.utc).astimezone(loc_z)
return loc_z.normalize(loc_dt)
def get_fetch_info():
if ghDEBUG:
utcTime = datetime.utcnow()
return "UTC: " + str(utcTime) + "<br/>" + time.strftime("%Z") + ": " + str(gh_getLocalTime(utcTime))
else:
return ""
def get_current_user():
if valid_session_key in session:
return session['handle']
else:
return request.remote_addr
def get_user_menu():
if valid_session_key in session:
return '[<a href="newpost">new post</a> | <a href="logout">logout</a>]'
else:
return '[<a href="login">login</a> | <a href="signup">register</a>]'