-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
122 lines (103 loc) · 3.81 KB
/
main.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
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# 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.
#
import os
import webapp2
from webapp2_extras import sessions
#This is needed to configure the session secret key
#Runs first in the whole application
myconfig_dict = {}
myconfig_dict['webapp2_extras.sessions'] = {
'secret_key': 'my-super-secret-key-bladibladibladibladibla',
}
session_password = 'wachtwoord'
# login
class BaseHandler(webapp2.RequestHandler):
def dispatch(self):
# Get a session store for this request.
self.session_store = sessions.get_store(request=self.request)
try:
# Dispatch the request.
webapp2.RequestHandler.dispatch(self)
finally:
# Save all sessions.
self.session_store.save_sessions(self.response)
@webapp2.cached_property
def session(self):
# Returns a session using the default cookie key.
return self.session_store.get_session()
# jinja
import jinja2
jinja_environment = jinja2.Environment(autoescape=True,
loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'views')))
# web requests
class MainPage(BaseHandler):
def get(self):
pw = self.session.get('password')
if pw != session_password :
self.redirect('/login')
else :
template_values = {}
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(template_values))
class SubPage(BaseHandler):
def get(self):
pw = self.session.get('password')
if pw != session_password :
self.redirect('/login')
else :
template_values = {}
template = jinja_environment.get_template('subpage.html')
self.response.out.write(template.render(template_values))
class LogIn(BaseHandler):
def get(self):
if self.session.get('password'):
del self.session['password']
if not self.session.get('referrer'):
self.session['referrer'] = \
self.request.environ['HTTP_REFERER'] \
if 'HTTP_REFERER' in self.request.environ \
else '/'
template_values = {
}
template = jinja_environment.get_template('login.html')
self.response.out.write(template.render(template_values))
def post(self):
password = self.request.get('PasswordIsNeededForAccess')
if password == session_password :
self.session['password'] = password
#logging.info("%s just logged in" % user)
self.redirect('/')
else :
template_values = { 'message':'Nope! That wasn\'t it' }
template = jinja_environment.get_template('login.html')
self.response.out.write(template.render(template_values))
'''class ResultPage(webapp2.RequestHandler):
def post(self):
pw = self.request.get('InputPassword1')
temp = ''
if pw==password :
temp = 'index.html'
else :
temp = 'wrong.html'
template_values = {}
template = jinja_environment.get_template(temp)
self.response.out.write(template.render(template_values))'''
app = webapp2.WSGIApplication([
('/', MainPage),
('/sub', SubPage),
('/login',LogIn)
], config = myconfig_dict, debug=True)