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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "chenwei"
# Date: 2018/11/15
import json
from utils.base import APIView, get_offset_count
from utils.logger import error_logger
from alpha_types.venus import USER_EXTRA_TYPE
class TopicListView(APIView):
def get(self, request):
pictorial_id = request.GET.get('pictorial_id', '')
user_id = request.GET.get('user_id', '')
page = int(request.GET.get('page', 1))
limit = int(request.GET.get('size', 10))
filters = json.loads(request.GET.get('filter', "{}"))
sorts_by = list(map(lambda i: int(i), request.GET.getlist('sort_params[]', [3])))
drop_score = request.GET.get('drop_score', None)
product_id = request.GET.get('product_id', None)
if user_id:
filters.update({'user_id': user_id})
if pictorial_id:
filters.update({"pictorial_id": int(pictorial_id)})
if drop_score is not None:
if int(drop_score) == 0:
filters.update({'drop_score': int(drop_score)})
elif int(drop_score) == 1:
filters.update({'drop_score__exclude': int(drop_score)})
if filters.get("is_finished") is not None:
filters["is_finished"] = True if filters.get("is_finished") else False
user_type = filters.pop("user_type") if filters.get("user_type") else None
if user_type:
if int(user_type) == USER_EXTRA_TYPE.COMMON:
filters["is_shadow"] = 0
filters["is_edit"] = 0
elif int(user_type) == USER_EXTRA_TYPE.SHADOW:
filters["is_shadow"] = 1
elif int(user_type) == USER_EXTRA_TYPE.KOL:
filters["is_kol"] = 1
elif int(user_type) == USER_EXTRA_TYPE.EDIT:
filters["is_edit"] = 1
if filters.get("has_product") is not None:
filters["has_product"] = bool(filters["has_product"])
res = self.rpc['physical/search/business/topic'](
offset=(page-1) * limit,
size=limit,
filters=filters,
sorts_by=sorts_by
).unwrap()
topic_ids = res.get("topic_ids", [])
total_count = res.get("total_count", 0)
try:
data = self.rpc['venus/sun/topic/list'](
topic_ids=topic_ids, pictorial_id=pictorial_id, product_id=product_id,
).unwrap()
except Exception as e:
error_logger.error(u'获取帖子列表失败%s', e)
raise
result = {
"data": data.get("data", []),
"total": total_count,
}
return result
class TopicImageListView(APIView):
def get(self, request):
pictorial_id = request.GET.get('pictorial_id', '')
page = int(request.GET.get('page', 1))
limit = int(request.GET.get('limit', 50))
filters = json.loads(request.GET.get('filter', "{}"))
sorts_by = list(map(lambda i: int(i), request.GET.getlist('sort_params[]', [3])))
filters.update({"has_image": True})
if pictorial_id:
filters.update({"pictorial_id": int(pictorial_id)})
res = self.rpc['physical/search/business/topic'](
offset=(page-1) * limit,
size=limit,
filters=filters,
sorts_by=sorts_by
).unwrap()
topic_ids = res.get("topic_ids", [])
total_count = res.get("total_count", 0)
try:
data = self.rpc['venus/community/topic/images_info_dict'](
topic_ids=topic_ids
).unwrap()
except Exception as e:
error_logger.error(u'获取帖子列表失败%s', e)
raise
result = []
for topic_id in topic_ids:
imgs_info = data.get(str(topic_id), [])
if not imgs_info:
continue
result.append({
"id": topic_id,
"img_url": imgs_info[0].get("url", "") + '-w'
})
result = {
"data": result,
"total": total_count,
}
return result
class TopicUpdateOrCreateView(APIView):
def get(self, request):
id = request.GET.get('id')
try:
data = self.rpc['venus/sun/topic/get'](id=id).unwrap()
except Exception as e:
error_logger.error(u'获取%s-帖子信息失败%s' %(id, e))
raise
return {'data': data}
def post(self, request):
id = request.POST.get('id', '')
topic_images = list(map(lambda x: x[:-2], json.loads(request.POST.get('topic_images', '[]'))))
tag_ids = list(map(lambda x: x.split(':')[0], json.loads(request.POST.get('tags', '[]'))))
collection_tag_ids = list(map(lambda x: x.split(':')[0], json.loads(request.POST.get('collection_tags', '[]'))))
body_esthetics_tag_ids = list(
map(lambda x: x.split(':')[0], json.loads(request.POST.get('body_esthetics_tags', '[]'))))
pictorial_ids = list(json.loads(request.POST.get('pictorial_ids', '[]')))
data = {
'topic_images': topic_images,
'video_url': request.POST.get('video_url', ''),
'posting_time': request.POST.get('posting_time'),
'content': request.POST.get('content', ''),
'content_level': request.POST.get('content_level', 0),
'pictorial_ids': pictorial_ids,
'user_id': request.POST.get('user', '').split(':')[0],
'star_id': request.POST.get('star', '').split(':')[0],
'tag_ids': tag_ids,
'collection_tag_ids': collection_tag_ids,
'is_online': int(request.POST.get('is_online', 0)),
'drop_score': int(request.POST.get('drop_score', 0)),
'has_image': 1 if topic_images else 0,
'has_video': 1 if request.POST.get('video_url', '') else 0,
'virtual_vote_num': request.POST.get('virtual_vote_num', ''),
'is_home': int(request.POST.get('is_home', 0)),
'is_recommend': int(request.POST.get('is_recommend', 0)),
'body_esthetics_tag_ids': body_esthetics_tag_ids,
'product_id': request.POST.get('product_id'),
'have_face': int(request.POST.get('have_face', 0)),
'is_finished': int(request.POST.get('is_finished', 1)),
}
try:
self.rpc['venus/sun/topic/edit'](id=id, data=data).unwrap()
except Exception as e:
error_logger.error(u'创建/编辑%s-帖子信息失败%s' % (id, e))
raise
return {
'message': '更新成功'
}
class TopicListBatchUpdate(APIView):
def post(self, request):
ids = request.POST.get('ids', '').split()
updates = self.handle_filter(request.POST.get('updates', "{}"))
extra = json.loads(request.POST.get('extra', '{}'))
try:
self.rpc['venus/sun/topic/batch/update'](updates=updates, ids=ids, extra=extra).unwrap()
except Exception as e:
error_logger.error(u'批量更新帖子失败%s', e)
raise
return {
"message": "更新成功"
}
class TopicRelatePictorialInfo(APIView):
def get(self, request):
id_ = request.GET.get('id')
offset, count = get_offset_count(request)
try:
data = self.rpc['venus/sun/topic/relate_pictorial_info'](id_=id_, offset=offset, count=count).unwrap()
except Exception as e:
error_logger.error(u'获取帖子画报列表失败%s', e)
raise
return {'data': data}
class TopicBallot(APIView):
def post(self, request):
id_ = request.POST.get('id')
pictorial_id = request.POST.get('pictorial_id')
ballot_num = request.POST.get('ballot_num')
try:
self.rpc['venus/sun/topic/ballot'](id_=id_, pictorial_id=pictorial_id, ballot_num=ballot_num).unwrap()
except Exception as e:
error_logger.error(u'批量更新帖子失败%s', e)
raise
return {
"message": "更新成功"
}
class TopicProductAdd(APIView):
"""帖子添加商品关系"""
def post(self, request):
ids = json.loads(request.POST.get('ids', '[]'))
product_id = request.POST.get('product_id')
try:
self.rpc['venus/sun/topic/add_product'](ids=ids, product_id=product_id).unwrap()
except Exception as e:
error_logger.error(u'编辑%s帖子信息失败%s' % (ids, e))
raise
return {
'message': '更新成功'
}
class TopicProductDel(APIView):
"""帖子删除商品关系"""
def post(self, request):
ids = json.loads(request.POST.get('ids', '[]'))
product_id = request.POST.get('product_id')
try:
self.rpc['venus/sun/topic/del_product'](ids=ids, product_id=product_id).unwrap()
except Exception as e:
error_logger.error(u'编辑%s帖子信息失败%s' % (ids, e))
raise
return {
'message': '更新成功'
}
class TopicBallotBatch(APIView):
"""帖子批量投票"""
def post(self, request):
ids = json.loads(request.POST.get('ids', '[]'))
ballot_num = int(request.POST.get('ballot_num', 0))
try:
self.rpc['venus/sun/topic/ballot_batch'](ids=ids, ballot_num=ballot_num).unwrap()
except Exception as e:
error_logger.error(u'编辑%s帖子信息失败%s' % (ids, e))
raise
return {
'message': '更新成功'
}