-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublca.py
146 lines (100 loc) · 4.4 KB
/
publca.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
#!/usr/bin/env python
import sys
import os
import logging
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import util
from google.appengine.api import mail
from constants import *
from models import Pub
# This hack is here because we want to accept HTML but not javascript content. Fix for this coming soon.
def CheckRequest(inFunction):
"""Checks for Javascript injection """
def outFunction(*args,**kwargs):
webAppRequest = args[0] # the first argument in get/post is self - This should work - There is probably a better way to do this.
arguments = webAppRequest.request.arguments()
for argument in arguments:
argValue = webAppRequest.request.get(argument)
# Check if trying to access someone elses offers - FIX THIS
if '<script' in argValue or '< script' in argValue:
webAppRequest.redirect('/')
return
return inFunction(*args,**kwargs)
return outFunction
class HomeHandler(webapp.RequestHandler):
def get(self):
logging.info('Cookies: %s - self.request.cookies')
content_template_values = {
}
self.response.out.write(
RenderPage('index.html', content_template_values)
)
class PubHandler(webapp.RequestHandler):
def get(self, pub_id):
pub = Pub.get_by_id(pub_id)
if pub:
content_template_values = {
'title':pub.title,
'author':pub.author,
'content':pub.content,
'date':pub.date_published.date().isoformat()
}
else:
content_template_values = {
'title': 'Sorry No Publication Here',
'author':'No one',
'content':'Try Something else',
'date':'Never'
}
self.response.out.write(
RenderPage('text.html', content_template_values)
)
class FavHandler(webapp.RequestHandler):
def get(self):
pass
class CreateHandler(webapp.RequestHandler):
@CheckRequest
def post(self):
title = self.request.get('title_form')
author = self.request.get('author_form')
content = self.request.get('content_form')
email = self.request.get('email_form')
if title == '':
title = 'Untitled'
if author == '':
author = 'Anonymous'
if content == '':
content = 'Nothing To Say'
content = "<br/>".join(content.split("\n"))
new_pub = Pub.create_pub(title, author, content)
if email:
mail.send_mail(sender="Publca <[email protected]>",
to=email,
subject='Publca - %s'% new_pub.title ,
body= 'publ.ca/%s'% new_pub.pub_id )
self.redirect('/%s'% new_pub.pub_id )
class LandingHandler(webapp.RequestHandler):
def get(self):
self.response.out.write(RenderPage('landing.html', {}))
# Helpers
# NOTE: this is overkill and will be more useful when we have constants that need to be added to each page
def RenderPage(template_file_name, content_template_values):
"""This re-renders the full page with the specified template."""
main_path = os.path.join('templates/%s' % template_file_name)
content_template_values.update(willetJS = WILLET_JS)
content_template_values.update(willetOfferId = WILLET_OFFER_ID)
willetOfferId = 'bbf81b0b8114d7e9'
# Add constant things to content_template_values
return template.render(main_path, content_template_values)
appRoute = webapp.WSGIApplication( [ ('/favicon.ico', FavHandler),
('/create', CreateHandler),
('/write', HomeHandler),
('/', LandingHandler),
(r'/(.*)', PubHandler)
],debug=True)
def main():
run_wsgi_app(appRoute)
if __name__ == '__main__':
main()