/
daily.py
67 lines (48 loc) · 1.68 KB
/
daily.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
from flask import json
from flask.ext.restful import abort, Resource, marshal_with
from sqlalchemy import func
import requests
from resources.RecipeApi import recipe_fields
from settings.db import session
from models.RecipeModel import Recipe
headers = {
"X-Parse-Application-Id": "SkwXEUQtF8aakn2cUYQi17yXXM0pB0xWT3AGrxiy",
"X-Parse-REST-API-Key": "JMCstdhoTIV46FEzeeF9Z86lCrxecbRYX9nqBxLo",
"Content-Type": "application/json"
}
parse_url = "https://api.parse.com/1/push"
class RandomRecipeFinder:
def reset(self):
for recipe in session.query(Recipe).all():
recipe.done = False
session.commit()
def find(self):
recipe = session.query(Recipe).filter(Recipe.done == False).order_by(func.random()).first()
if not recipe:
abort(500, message="Every recipe was done. Nice.")
recipe.done = True
session.commit()
return recipe
class Parse:
def send_push(self, id):
data = json.dumps({"data":{"recipe": "{}".format(id)}, "where":{}})
r = requests.post(parse_url, headers=headers, data=data)
print r.content
class DailyRecipeSender(Resource):
@marshal_with(recipe_fields)
def get(self):
random = RandomRecipeFinder()
recipe = random.find()
parse = Parse()
parse.send_push(recipe.id)
return recipe
class DailyRecipeLeft(Resource):
@marshal_with(recipe_fields)
def get(self):
recipes = session.query(Recipe).filter(Recipe.done == False).all()
return recipes
class DailyRecipeReset(Resource):
def get(self):
random = RandomRecipeFinder()
random.reset()
return {"message": "ok"}, 200