Repository navigation
Expand file tree
/
Copy pathbase.py
More file actions
76 lines (66 loc) · 2.73 KB
/
Copy pathbase.py
File metadata and controls
76 lines (66 loc) · 2.73 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
from bottle import response, request
from datetime import datetime
from mining.utils import slugfy, parse_dumps
def base():
response.set_header('charset', 'utf-8')
response.content_type = 'application/json'
def get(mongodb, collection, slug, field={'key': 'slug'}):
base()
if slug:
data = mongodb[collection].find_one({field['key']: slug})
if data:
data.pop('_id', None)
else:
data = {}
return json.dumps(data, default=parse_dumps)
else:
data = mongodb[collection].find()
response = []
for d in data:
d.pop('_id', None)
response.append(d)
return json.dumps(response, default=parse_dumps)
def post(mongodb, collection, opt={}, field={'key': 'slug', 'value': 'name'}):
base()
data = request.json
data[field['key']] = data[field['value']]
if field.get('key', '') == 'slug':
data[field['key']] = slugfy(data[field['value']])
data = dict(data.items() + opt.items())
get = mongodb[collection].find({field['key']: data[field['key']]})
if get.count() == 0:
mongodb[collection].insert(data)
del data['_id']
return json.dumps(data, default=parse_dumps)
return {'status': 'error', 'message': 'Object exist, please send PUT!'}
def put(mongodb, collection, slug, opt={}, field={'key': 'slug'},
request_json=request.json):
base()
data = request_json or request.json or {}
data[field['key']] = slug
data = dict(data.items() + opt.items())
if 'lastupdate' in data and isinstance(data.get('lastupdate'),
basestring):
data['lastupdate'] = datetime.strptime(data.get('lastupdate'),
'%Y-%m-%d %H:%M:%S')
if 'start_process' in data and isinstance(data.get('start_process'),
basestring):
data['start_process'] = datetime.strptime(data.get('start_process'),
'%Y-%m-%d %H:%M:%S')
get = mongodb[collection].find_one({field['key']: slug})
if get:
mongodb[collection].update({field['key']: slug}, {'$set': data})
return json.dumps(data, default=parse_dumps)
return {'status': 'error',
'message': 'Object not exist, please send POST to create!'}
def delete(mongodb, collection, slug, field={"key": "slug"}):
base()
get = mongodb[collection].find_one({field['key']: slug})
if get:
mongodb[collection].remove({field['key']: slug})
return {'status': 'success'}
return {'status': 'error',
'message': 'Object not exist, please send POST to create!'}