-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCookUpDBServlet.py
More file actions
90 lines (73 loc) · 2.93 KB
/
CookUpDBServlet.py
File metadata and controls
90 lines (73 loc) · 2.93 KB
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
import pdb
import sys
import web
import json
import logging
import MealMatcher
import DBMockUp
urls = (
"/CookUpDB", "CookUpDBServlet"
)
format_string = '%(asctime)s %(funcName)s:%(lineno)s %(levelname)s - %(message)s'
logging.basicConfig(filename='/tmp/CookUpDBServlet.log',level=logging.DEBUG, format=format_string)#,stream=logging.StreamHandler(sys.stdout))
class CookUpDBServlet(object):
def __init__(self):
self.log = logging.getLogger()
self.matcher = MealMatcher.MealMatcher(self.log)
self.db_mockup = DBMockUp.DBMockUp()
self.actions = {
1:self.search_meal,
2:self.create_order,
3:self.close_order,
}
def POST(self):
web.header("Content-type", "application/json; charset=utf-8")
in_json=None
in_data=web.data()
try:
in_json=json.loads(in_data)
except:
return self.return_error("not a valid JSON: %s" % in_data)
self.log.debug('received JSON: %s' % in_json)
# check if it's create (no buyer yet) or edit (buyer available)
if 'action' not in in_json:
return self.return_error('action not in json' % in_json)
if 'data' not in in_json or len(in_json['data'])==0:
return self.return_error('data not in json' % in_json)
action_id=int(in_json['action'])
if action_id not in self.actions:
return self.return_error("not a valid action: %s" % action_id)
self.log.debug('action: %s' % action_id)
out = {"nothing":"done"}
try:
out = self.actions[action_id](in_json['data'])
except Exception,e:
print e
return self.return_error('action failed, reason: %s' % e)
web.OK()
return json.dumps(out)
def return_error(self, message):
web.notfound()
self.log.error(message)
return '{"log_msg":"%s"}' % message
def search_meal(self,in_json):
for kl in ['meal','location','cook']:
if kl not in in_json:
raise Exception('JSON not complete, %s missing' % kl)
self.log.debug('search for meal: %s' % in_json)
found_meal = self.matcher.get_meal_offer(in_json['meal'], in_json['location'], in_json['cook'])
self.log.debug('found meals: %s' % found_meal)
return found_meal
def create_order(self, in_json):
self.log.debug('create order: %s' % in_json)
created_order = self.db_mockup.create_order(in_json)
self.log.debug('created order: %s' % created_order)
return created_order
def close_order(self, in_json):
self.log.debug('close order: %s' % in_json)
closed_order = self.db_mockup.set_order_buyer(in_json['id'],in_json['buyer'])
self.log.debug('closed order: %s' % closed_order)
return closed_order
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()