-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblocking.py
137 lines (97 loc) · 3.58 KB
/
blocking.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
import json
import webapp2
from google.appengine.ext import ndb
from google.appengine.api import users
# This is a simple model that we can use to demo storage & retrieval.
class TestModel(ndb.Model):
title = ndb.StringProperty()
words = ndb.TextProperty()
author = ndb.StringProperty()
# we'll create a simple summary dictionary method here
def to_summary_dict(self):
return {
# "key" is a property we get from ndb.Model - we can use this for easy retrieval of 1 specfic Model
'key': self.key.urlsafe(),
'title': self.title,
'author': self.author
}
# this to_dict method will send *all* of the data for this object
def to_dict(self):
result = self.to_summary_dict()
result['words'] = self.words
return result
# convenience function for retrieving a user, or None if the user isn't logged in
def get_user_email():
user = users.get_current_user()
if user:
return user.email()
else:
return None
# convenience function for sending JSON data to browser
def send_json(request_handler, params):
# params should be a dictionary - we'll just write it out as a JSON object
request_handler.response.out.write(json.dumps(params))
# convenience funtion for sending JSON error msg to browser
def send_error(request_handler, msg):
# we will create a dictionary, and just convert that to a JSON response
request_handler.response.out.write(json.dumps({'error': msg}))
# Handler for displaying user email JSON - URLs for login and logout
class UserHandler(webapp2.RequestHandler):
def dispatch(self):
result = {
'login': users.create_login_url('/'),
'logout': users.create_logout_url('/'),
'user': get_user_email(),
}
send_json(self, result)
# this handler just lists our models in JSON
class ListModelsHandler(webapp2.RequestHandler):
def dispatch(self):
if get_user_email():
# if we get this far, we know we have a valid user
result = {}
result['models'] = []
# retrieve all of the models we have:
for model in TestModel.query().fetch():
result['models'].append(model.to_summary_dict())
send_json(self, result)
else:
send_error(self, 'Please log in.')
# retrieve the parameters from the request, build a model, and store it
class AddModelHandler(webapp2.RequestHandler):
def dispatch(self):
if get_user_email():
rtext = self.request.get('words')
rtitle = self.request.get('title')
if len(rtitle) > 500:
send_error(self, 'Title should be less than 500 characters.')
elif rtitle.strip():
m = TestModel(title=rtitle, words=rtext, author=get_user_email())
m.put()
send_json(self, {'ok': True})
else:
send_error(self, 'Title should not be blank.')
else:
send_error(self, 'Please log in.')
# retrieve the detail for one model
class ModelDetailHandler(webapp2.RequestHandler):
def dispatch(self):
if get_user_email():
# get the key from the request
rkey = self.request.get('key')
# construct an ndb.Key object
key = ndb.Key(urlsafe=rkey)
if key:
# use the ndb.Key object's get() method to retrieve the Model associated with that particular key
m = key.get()
send_json(self, m.to_dict())
else:
send_error(self, 'Model not found.')
else:
send_error('Please log in.')
app = webapp2.WSGIApplication([
('/models', ListModelsHandler),
('/user', UserHandler),
('/add', AddModelHandler),
('/model', ModelDetailHandler),
])