-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.py
108 lines (104 loc) · 3.54 KB
/
base.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''Base handler module
'''
import tornado.web
import tornado.httpserver
import tornado.httpclient
import tornado.locale
import re, urlparse
try:
from bson.objectid import ObjectId
except ImportError as error:
print error.message
raise ImportError
def error_message(message):
'''returns error message in a dictionary
'''
return dict({'status':'ERROR', 'message':message})
def result_message(result):
'''returns result message in a dictionary
'''
return dict({'status':'OK', 'data':result})
def parse_rest(uri):
'''The function parses uri and returns the rest object
'''
rest = {}
#/item/id/UPDATE POST
if re.match(r'/[A-Za-z]+/[A-Za-z0-9]+/(UPDATE|DELETE)', uri):
uri_list = uri.split('/')
rest['action'] = uri_list[3].split('?')[0]
rest['params'] = uri_list[2]
rest['entity'] = uri_list[1]
#/item/id/resources GET
elif re.match(r'/[A-Za-z]+/[A-Za-z0-9]+/[A-Za-z]+', uri):
uri_list = uri.split('/')
rest['action'] = 'FIND_RESOURCE'
rest['resources'] = uri_list[3]
rest['params'] = uri_list[2]
rest['entity'] = uri_list[1]
#/item/NEW POST
elif re.match(r'/[A-Za-z]+/NEW', uri):
rest['action'] = 'NEW'
rest['entity'] = uri.split('/')[1]
#/item/LIST GET
elif re.match(r'/[A-Za-z]+/LIST', uri):
rest['action'] = 'LIST'
rest['entity'] = uri.split('/')[1]
rest['params'] = []
#/item/id GET
elif re.match(r'/[A-Za-z]+/[A-Za-z0-9]+', uri):
uri_list = uri.split('/')
rest['action'] = 'FIND_ONE'
rest['entity'] = uri_list[1]
rest['params'] = [{'_id': ObjectId(uri_list[2])}]
#/item/?param1=v1¶m2=v2&... GET
elif re.match(re.compile(r'/[A-Za-z]+/\?(.*)'), uri):
query = urlparse.urlparse(uri).query
rest['action'] = 'QUERY'
rest['entity'] = uri.split('/')[1]
rest['params'] = [{k:v[0]} for k, v in urlparse.parse_qs(query).items()]
return rest
class BaseHandler(tornado.web.RequestHandler):
'''This is the base handler class that can be overwrited
in a other handler classes
'''
@property
def rest(self):
'''This function as a property returns the rest object
'''
return parse_rest(self.request.uri)
def get(self):
'''Action GET, this can be overwrited in concrete class
'''
try:
#rest = parse_rest(self.request.uri)
rest = self.rest
#LIST
if rest['action'] == 'LIST':
pass
#FIND_ONE | QUERY
if rest['action'] == 'FIND_ONE' or rest['action'] == 'QUERY':
params = rest['params']
pass
#FIND_RESOURCE
if rest['action'] == 'FIND_RESOURCE':
pass
except Exception as error:
self.finish(error_message(error.message))
def post(self):
'''Action POST, this can be overwrited in concrete class
'''
try:
###process request parameters###
rest = self.rest
# NEW
if rest['action'] == 'NEW':
pass
# UPDATE | DELETE
elif rest['action'] == 'DELETE':
pass
elif rest['action'] == 'UPDATE':
pass
except Exception as error:
self.finish(error_message(error.message))