forked from uofa-cmput404/cgi-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_data_from_form.py
108 lines (90 loc) · 2.5 KB
/
get_data_from_form.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
#!/usr/bin/env python3
# Python 3.7 versus Python 3.8
try:
from cgi import escape #v3.7
except:
from html import escape #v3.8
__all__ = ['login_page', 'secret_page', 'after_login_incorrect']
def _wrapper(page):
"""
Wraps some text in common HTML.
"""
return ("""
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, sans-serif;
max-width: 24em;
margin: auto;
color: #333;
background-color: #fdfdfd
}
.spoilers {
color: rgba(0,0,0,0); border-bottom: 1px dashed #ccc
}
.spoilers:hover {
transition: color 250ms;
color: rgba(36, 36, 36, 1)
}
label {
display: flex;
flex-direction: row;
}
label > span {
flex: 0;
}
label> input {
flex: 1;
}
button {
font-size: larger;
float: right;
margin-top: 6px;
}
</style>
</head>
<body>
""" + page + """
</body>
</html>
""")
def login_page():
"""
Returns the HTML for the login page.
"""
return _wrapper(r"""
<h1> Welcome! </h1>
<form method="POST" action="login.py">
<label> <span>Username:</span> <input autofocus type="text" name="username"></label> <br>
<label> <span>Password:</span> <input type="password" name="password"></label>
<button type="submit"> Login! </button>
</form>
""")
def secret_page(username=None, password=None):
"""
Returns the HTML after logged-in.
"""
if username is None or password is None:
raise ValueError("You need to pass both username and password!")
return _wrapper("""
<h1> Welcome, {username}! </h1>
<p> <small> See!
<span class="spoilers"> {password}</span>.
</small>
</p>
""".format(username=escape(username.capitalize()),
password=escape(password)))
def after_login_incorrect():
"""
Returns the HTML when type typed
incorrectly.
"""
return _wrapper(r"""
<h1> Login incorrect :c </h1>
<p> Incorrect username or password (hint: <span class="spoilers"> Check
<code>secret.py</code>!</span>)
<p> <a href="login.py"> Try again. </a>
""")