-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit2.py
138 lines (121 loc) · 4.53 KB
/
unit2.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
# custom imports
from handlers import Validators, Handler, Hashers
from models import User
from google.appengine.ext import db
import hashlib
###############################################################################
# Unit 2
###############################################################################
class Rot13Handler(Handler, Validators):
def get(self):
return self.render( 'rot13form.html', message="" )
def post(self):
smalla = ord('a')
smallz = ord('z')
capA = ord('A')
capZ = ord('Z')
user_message = self.request.get('text')
rotted = ""
for l in user_message:
lord = ord(l)
rotord = lord + 13
small_thresh = rotord - smallz
cap_thresh = rotord - capZ
if lord >= smalla and lord <= smallz or lord >= capA and lord <= capZ:
if rotord > smallz:
rotted+=chr(smalla + small_thresh -1 )
elif rotord > capZ and lord < capZ:
rotted+=chr(capA + cap_thresh -1)
else:
rotted+=chr(rotord)
else:
rotted+=l
return self.render( 'rot13form.html', message=self.escape_html(rotted) )
class SignupHandler(Handler, Validators, Hashers):
def write_form(self,
username = "",
username_error = "",
password_error = "",
verify_error = "",
email = "",
email_error = "",
):
self.render( 'signup_form.html',
username= username,
username_error= username_error,
password_error= password_error,
verify_error= verify_error,
email= email,
email_error= email_error,
)
def get(self):
#self.response.headers['Content-Type'] = 'text/plain'
self.write_form()
def post(self):
status = True
username = self.request.get('username')
password = self.request.get('password')
verify = self.request.get('verify')
email = self.request.get('email')
if not self.valid_username(username):
status = False
username_error = "Username is invalid"
else:
user = User.by_name(username)
if user:
status = False
username_error = "Username already exists"
else:
username_error = ""
if not self.valid_password(password):
status = False
password_error = "Password is invalid"
verify_error = ""
elif verify != password:
verify_error = "Passwords does not match"
password_error = ""
status = False
else:
password_error = ""
verify_error = ""
if not self.valid_email(email):
status = False
email_error = "Email is invalid"
else:
email_error = ""
if not status:
return self.write_form( self.escape_html(username),
self.escape_html(username_error),
self.escape_html(password_error),
self.escape_html(verify_error),
self.escape_html(email),
self.escape_html(email_error),
)
else:
user = User.register(username, password, email)
user.put()
self.set_secure_cookie('user_id', str(user.key().id()) )
self.redirect("/blog/welcome")
class SignupSuccessHandler(Handler, Validators, Hashers):
def get(self):
if self.user:
self.render("welcome.html", username=self.user.username)
else:
self.redirect("/blog/signup")
class LoginHandler(Handler, Validators, Hashers):
def get(self):
self.render('login.html')
def post(self):
username = self.request.get('username')
password = self.request.get('password')
user = User.login(username, password)
if user:
self.login(user)
self.redirect("/blog/welcome")
else:
error = "username and password doesn't match"
self.render('login.html', username=username, error=error)
class LogoutHandler(Handler):
def get(self):
self.logout()
self.redirect('/blog/signup')