-
Notifications
You must be signed in to change notification settings - Fork 0
/
myapp.py
109 lines (88 loc) · 3.02 KB
/
myapp.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
import os
import urllib
from google.appengine.api import users
# [START import_ndb]
from google.appengine.ext import ndb
# [END import_ndb]
import jinja2
import webapp2
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir),
autoescape = True)
DEFAULT_COMMENTS_NAME = 'default_comments'
comments_key = ndb.Key('Feedback', DEFAULT_COMMENTS_NAME)
def check_profanity(text_to_check):
connection = urllib.urlopen("http://www.wdyl.com/profanity?q=" + text_to_check)
output = connection.read()
connection.close()
if "true" in output:
return True
else:
return False
class Author(ndb.Model):
"""Submodel for post author."""
identity = ndb.StringProperty(indexed=False)
email = ndb.StringProperty(indexed=False)
class Comment(ndb.Model):
"""Main model for comments."""
author = ndb.StructuredProperty(Author)
title = ndb.StringProperty(indexed=False)
content = ndb.StringProperty(indexed=False)
date = ndb.DateTimeProperty(auto_now_add=True)
class Handler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
def render_str(self, template, **params):
t = jinja_env.get_template(template)
return t.render(params)
def render(self, template, **kw):
self.write(self.render_str(template, **kw))
class MainPage(Handler):
def get(self):
comments_query = Comment.query(ancestor=comments_key).order(-Comment.date)
comments = comments_query.fetch(10)
user = users.get_current_user()
if user:
url = users.create_logout_url(self.request.uri)
url_linktext = 'Logout'
else:
url = users.create_login_url(self.request.uri)
url_linktext = 'Login'
template_values = {
'user': user,
'comments': comments,
'users_prompt': "Google users please",
'url': url,
'url_linktext': url_linktext
}
# template = jinja_env.get_template("comments.html")
# self.response.write(template.render(template_values))
self.render("comments.html", template_values = template_values)
def post(self):
comment = Comment(parent=comments_key)
if users.get_current_user():
comment.author = Author(
identity = users.get_current_user().user_id(),
email = users.get_current_user().email())
comment.content = self.request.get('content')
comment.title = self.request.get('title')
if comment.title and comment.content:
if (check_profanity(comment.title) or check_profanity(comment.content)):
msg = "No profanities please!!!"
title = comment.title
content = comment.content
template_values = {
'error': msg,
'title': title,
'content': content
}
self.render('comments.html', template_values = template_values)
else:
comment.put()
self.redirect('/')
else:
template_values = { 'error': "Please enter title and comment!"}
self.render('comments.html', template_values = template_values)
app = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)