-
Notifications
You must be signed in to change notification settings - Fork 14
/
server.py
154 lines (136 loc) · 5 KB
/
server.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from eve import Eve
from eve.auth import TokenAuth
from flask import redirect, request, Response
from settings import posts, users
import json
import random
import string
class RolesAuth(TokenAuth):
def check_auth(self, token, allowed_roles, resource, method):
# use Eve's own db driver; no additional connections/resources are used
accounts = app.data.driver.db['accounts']
lookup = {'token': token}
if allowed_roles:
# only retrieve a user if his roles match ``allowed_roles``
lookup['roles'] = {'$in': allowed_roles}
account = accounts.find_one(lookup)
return account
def add_token(documents):
for document in documents:
document["token"] = (''.join(random.choice(string.ascii_uppercase)
for x in range(10)))
def filter_hero_image(heroImage):
if 'image' in heroImage:
image = filter_gcs_info(heroImage['image'])
heroImage['image'] = image
return heroImage
def filter_leading_video(leadingVideo):
if 'video' in leadingVideo:
video = filter_gcs_info(leadingVideo['video'])
leadingVideo['video'] = video
return leadingVideo
def filter_gcs_info(gcsObj):
if 'gcsDir' in gcsObj:
del gcsObj['gcsDir']
if 'gcsBucket' in gcsObj:
del gcsObj['gcsBucket']
if 'filename' in gcsObj:
del gcsObj['filename']
return gcsObj
def get_relateds(item, key):
if key in item and item[key]:
headers = dict(request.headers)
tc = app.test_client()
all_relateds = ",".join(map(lambda x: '"' + str(x) + '"',item[key]))
resp = tc.get('meta?where={"_id":{"$in":[' + all_relateds + ']}}', headers=headers)
resp_data = json.loads(resp.get_data().decode())
result = []
for i in item[key]:
for j in resp_data['_items']:
if j['_id'] == str(i):
result.append(j)
continue
item[key] = result
# item[key] = resp_data['_items']
return item
def before_returning_meta(response):
items = response['_items']
for item in items:
if 'brief' in item:
del item['brief']['draft']
del item['brief']['html']
if 'heroImage' in item:
if item['heroImage'] is not None:
item['heroImage'] = filter_hero_image(item['heroImage'])
return response
def get_topic(topic_id):
headers = dict(request.headers)
tc = app.test_client()
resp = tc.get('topics/' + str(topic_id) , headers=headers)
resp_data = json.loads(resp.get_data().decode())
return resp_data
def filter_post(item):
if 'brief' in item:
del item['brief']['draft']
del item['brief']['html']
if 'content' in item:
del item['content']['draft']
del item['content']['html']
if 'heroImage' in item:
if type(item['heroImage']) is dict:
item['heroImage'] = filter_hero_image(item['heroImage'])
if 'leading_video' in item:
if type(item['leading_video']) is dict:
item['leading_video'] = filter_leading_video(item['leading_video'])
return item
def before_returning_posts(response):
items = response['_items']
for item in items:
item = before_returning_post(item)
return items
def before_returning_post(response):
item = filter_post(response)
# check if topic object is to be embedded
topics = str(request.args.get('embedded')).find('topics')
if topics > -1 and 'topics' in item:
item['topics'] = get_topic(item['topics'])
return item
def filter_topics(items):
for item in items:
item = filter_topic(item)
return items
def filter_topic(item):
if 'description' in item:
del item['description']['draft']
del item['description']['apiData']
if 'team_description' in item:
del item['team_description']['draft']
del item['team_description']['apiData']
if 'leading_image' in item:
if type(item['leading_image']) is dict:
item['leading_image'] = filter_hero_image(item['leading_image'])
if 'leading_image_portrait' in item:
if type(item['leading_image_portrait']) is dict:
item['leading_image_portrait'] = filter_hero_image(item['leading_image_portrait'])
if 'leading_video' in item:
if type(item['leading_video']) is dict:
item['leading_video'] = filter_leading_video(item['leading_video'])
return item
def before_returing_topics(response):
items = response['_items']
for item in items:
item = before_returning_topic(item)
return response
def before_returning_topic(response):
item = filter_topic(response)
item = get_relateds(item, 'relateds')
return item
#app = Eve(auth=RolesAuth)
app = Eve()
app.on_fetched_resource_meta += before_returning_meta
app.on_fetched_resource_posts += before_returning_posts
app.on_fetched_item_posts += before_returning_post
app.on_fetched_resource_topics += before_returing_topics
app.on_fetched_item_topics += before_returning_topic
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, threaded=True, debug=True)