/
NewRecipeHandler.py
50 lines (35 loc) · 1.47 KB
/
NewRecipeHandler.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
import os
import logging
from google.appengine.api import users
from google.appengine.ext import webapp, db, blobstore
from google.appengine.ext.db import Key
from google.appengine.ext.webapp import template
from django.template.context import RequestContext
from models import Ingredient, QuantifiedIngredient, Recipe, Image
from django.utils import simplejson
from helpers import allmonths, to_dict
################################################################################
class NewRecipeHandler(webapp.RequestHandler):
def property_to_bool(self, name):
value = self.request.get(name)
if value == 'on': return True
return False
def post(self):
if not users.is_current_user_admin():
return self.redirect('/')
# Extract values from json
json = simplejson.loads(self.request.get('json'))
# Create a recipe
therecipe = Recipe()
therecipe.set_from_dict(json)
self.redirect('/recipe/%s' % therecipe.key())
def get(self):
if not users.is_current_user_admin():
return self.redirect(users.create_login_url(self.request.url))
r = Recipe().to_dict()
path = os.path.join(os.path.dirname(__file__), 'newrecipe.html')
template_values = RequestContext(self.request, {
'json' : simplejson.dumps(r),
'image_upload_url' : '/image',
})
self.response.out.write(template.render(path, template_values))