Skip to content

Commit

Permalink
Add json response method to the Base handler class
Browse files Browse the repository at this point in the history
validate method from ngforms now return the dictionary of values
  • Loading branch information
gomezjdaniel committed Feb 1, 2013
1 parent 6f2390d commit 2e22124
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
5 changes: 5 additions & 0 deletions handlers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import webapp2
from webapp2_extras import json

import os

Expand All @@ -9,3 +10,7 @@ def render(self, name, *args, **kwargs):
template = jinja_environment.get_template(os.path.join("templates",
('%s.html' % name) ))
self.response.out.write(template.render(*args, **kwargs))

def json(self, value):
self.response.headers['Content-Type'] = 'application/json; charset=utf-8'
self.response.out.write(json.encode(value))
8 changes: 4 additions & 4 deletions ngforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ def validate(self):
data = json.decode(request.body)

for f in self.fields:
vals = self.validations[f.id]

try:
value = data[f.id]
value = data[f.id].strip()
except KeyError:
value = ''

field_values[f.id] = value
for val in vals:
for val in self.validations[f.id]:
if not val.validate(self):
request.abort(403)

return field_values

@property
def fields(self):
raise NotImplemented()
Expand Down

0 comments on commit 2e22124

Please sign in to comment.