-
Notifications
You must be signed in to change notification settings - Fork 452
/
templates.py
executable file
·134 lines (110 loc) · 3.28 KB
/
templates.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
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# Copyright 2016 Eddie Antonio Santos <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Roll my own "template system".
"""
import cgi
import cgitb
cgitb.enable()
# 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 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 for the page visited after the user has 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> Pst! I know your password is
<span class="spoilers"> {password}</span>.
</small>
</p>
""".format(username=escape(username.capitalize()),
password=escape(password)))
def after_login_incorrect():
"""
Returns the HTML for the page when the login credentials were 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>
""")
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>
""")