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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import hashlib
import time
from django.conf import settings
from gm_types.ascle import ERROR
from helios.rpc.exceptions import RPCFaultException
from utils.base import APIView
from utils.channel_package_tools import ChannelPackage
from utils.logger import error_logger
from .tasks import version_release_task, channel_build, channel_build_one
def get_md5_value(src):
md5 = hashlib.md5()
md5.update(src)
md5_digest = md5.hexdigest()
return md5_digest
class Vendor(APIView):
def get(self, request):
"""
获取渠道商列表
:param request:
:return:
"""
query = request.GET.get('q', '')
page = int(request.GET.get('page', 1))
offset = int(request.GET.get('offset', 30))
data = request.rpc.origin['endpoint'](
query=query, start=(page - 1) * offset, offset=offset
).unwrap()
return data
class ChannelList(APIView):
def get(self, request):
"""
渠道列表
:param request:
:return:
"""
page = int(request.GET.get('page', 1))
limit = int(request.GET.get('limit', 10))
filters = self.handle_filter(request.GET.get('filter', ""))
try:
channel_list = self.rpc['venus/sun/channel_build/channel/list'](
offset=(page-1)*limit, limit=limit, filters=filters
).unwrap()
except Exception as e:
error_logger.error(u'获取渠道列表失败:%s', e)
raise e
data = {'total': channel_list.get('total', 0)}
url = settings.APK_DOMAIN + settings.APK_BUILD_DIR + '/' + settings.APK_RELEASE_DIR + \
"/{name}/gm_alpha_{name}.apk?t={timestamp}"
channels = [
{
'id': data.get('id'),
'name': data.get('name'),
'url_name': data.get('url_name'),
'pack': data.get('pack'),
'released_version': data.get('released_version'),
'download_url': url.format(name=data.get('url_name'), timestamp=time.time()),
}
for data in channel_list.get('data', [])]
data.update({'channel': channels})
return self.write_success(message='获取渠道列表成功', data=data)
class ChannelVersionList(APIView):
def get(self, request):
"""
:param request:
:return:
"""
channel_id = request.GET.get('channel_id')
page = int(request.GET.get('page', 1))
limit = int(request.GET.get('limit', 10))
try:
channel_list = self.rpc['venus/sun/channel_build/channel_version/list'](
offset=(page-1)*limit, limit=limit, channel_id=channel_id
).unwrap()
except Exception as e:
error_logger.error(u'获取渠道列表失败:%s', e)
raise e
data = {'total': channel_list.get('total', 0)}
url = settings.APK_DOMAIN + settings.APK_BUILD_DIR + "/{version}/{name}/gm_alpha_{name}.apk?t={timestamp}"
channels = [
{
'version_id': data.get('version_id'),
'channel_id': data.get('channel_id'),
'version': data.get('version'),
'name': data.get('name'),
'url_name': data.get('url_name'),
'packed_time': data.get('packed_time'),
'packed': data.get('packed'),
'release_time': data.get('release_time'),
'is_released': data.get('is_released'),
'download_url': url.format(
version=data.get('version'), name=data.get('url_name'), timestamp=time.time()),
}
for data in channel_list.get('data', [])]
data.update({'channel': channels})
return self.write_success(message='获取渠道列表成功', data=data)
class Channel(APIView):
def get(self, request):
"""
渠道详情
:param request:
:return:
"""
channel_id = request.GET.get('channel_id')
if not channel_id:
error_logger.error(u'参数不完整')
return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整')
try:
channel_data = self.rpc['venus/sun/channel_build/channel/get'](
id=channel_id
).unwrap()
except Exception as e:
error_logger.error(u'获取渠道详情失败:%s', e)
raise e
channel = {
'id': channel_data.get('id'),
'name': channel_data.get('name'),
'url_name': channel_data.get('url_name'),
'pack': channel_data.get('pack'),
'type': channel_data.get('type'),
'released_version': channel_data.get('released_version'),
}
return self.write_success(message='获取渠道详情成功', data=channel)
def post(self, request):
"""
渠道创建 / 编辑
:param request:
:return:
"""
channel_id = request.POST.get('id', None)
channel_name = request.POST.get('name', '')
url_name = request.POST.get('url_name', '')
auto_pack = request.POST.get('pack', True)
channel_type = request.POST.get('type', 2)
if not all([channel_name, url_name, auto_pack, channel_type]):
error_logger.error(u'参数不完整')
return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整')
if channel_id:
channel = self.rpc['venus/sun/channel_build/channel/get'](
id=channel_id
).unwrap()
else:
channel = {}
data = dict()
if channel_name:
data.update({'name': channel_name})
if url_name:
data.update({'url_name': url_name})
if auto_pack:
data.update({'pack': auto_pack})
if channel_type:
data.update({'type': channel_type})
try:
resp = self.rpc['venus/sun/channel_build/channel/edit'](
id=channel_id, data=data
).unwrap()
except Exception as e:
error_logger.error('操作失败%s', e)
return self.write_fail(message='路径名不能重复', code=-1)
version = resp.get('version', '')
if version and channel.get('url_name') != url_name:
channel_build_one.delay(version, url_name)
return self.write_success(message='操作成功', data={})
class ChannelPacked(APIView):
def post(self, request):
"""
当前版本的渠道包发布
:param request:
:return:
"""
version_id = request.POST.get('version_id')
channel_id = request.POST.get('channel_id')
if not all([channel_id, version_id]):
error_logger.error(u'参数不完整')
return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整')
channel = self.rpc['venus/sun/channel_build/channel/get'](
id=channel_id
).unwrap()
version = self.rpc['venus/sun/channel_build/version/get'](
id=version_id
).unwrap()
ChannelPackage.publish_apk(channel.get('url_name'), version.get('version'))
try:
self.rpc['venus/sun/channel_build/version/release'](
version_id=version_id, channel_ids=[channel_id]
).unwrap() #发布
except RPCFaultException as e:
error_logger.error(u'发布失败:%s', e)
return self.write_fail(code=-1, message='发布失败!')
return self.write_success(data={}, message='发布成功!')
class VersionList(APIView):
def get(self, request):
"""
所有版本列表
:param request:
:return:
"""
page = int(request.GET.get('page', 1))
offset = int(request.GET.get('limit', 10))
try:
version_list = self.rpc['venus/sun/channel_build/version/list'](
offset=(page-1)*offset, limit=offset
).unwrap()
except Exception as e:
error_logger.error(u'获取版本列表失败:%s', e)
raise e
data = {'total': version_list.get('total', 0)}
versions = [
{
'version_id': item.get('id'),
'version': item.get('version'),
'md5': item.get('md5'),
'release_time': item.get('release_time'),
} for item in version_list.get('data', [])
]
data.update({'data': versions})
return self.write_success(data=data)
class VersionChannelList(APIView):
def get(self, request):
page = int(request.GET.get('page', 1))
offset = int(request.GET.get('limit', 10))
version_id = request.GET.get('version_id', 10)
try:
version_list = self.rpc['venus/sun/channel_build/version_channel/list'](
offset=(page-1)*offset, limit=offset, version_id=version_id
).unwrap()
except Exception as e:
error_logger.error(u'获取版本列表失败:%s', e)
raise e
data = {'total': version_list.get('total', 0)}
url = settings.APK_DOMAIN + settings.APK_BUILD_DIR + "/{version}/{name}/gm_alpha_{name}.apk?t={timestamp}"
sup_url = ChannelPackage.apk_url(channel='', version=version_list.get('version')) + '?t={timestamp}'
versions = {
'version': [{
'channel_id': item.get('channel_id'),
'version_id': item.get('version_id'),
'version': item.get('version'),
'name': item.get('name'),
'packed_time': item.get('packed_time'),
'packed': item.get('packed'),
'download_url': url.format(
version=item.get('version'), name=item.get('url_name'), timestamp=time.time()),
'release_time': item.get('release_time'),
'is_released': item.get('is_released'),
} for item in version_list.get('data', [])],
'base_apk_url': sup_url.format(timestamp=time.time()),
}
data.update({'data': versions})
return self.write_success(data=data)
class VersionDetailUpdate(APIView):
def get(self, request):
"""
版本详情
:param request:
:return:
"""
version_id = request.GET.get('version_id')
if not version_id:
error_logger.error(u'参数不完整')
return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整')
version_info = self.rpc['venus/sun/channel_build/version/get'](
id=version_id
).unwrap()
data = {
'id': version_info.get('id'),
'version': version_info.get('version'),
'release_time': version_info.get('release_time'),
'md5': version_info.get('md5'),
'description': version_info.get('description'),
}
return self.write_success(data=data, message='操作成功')
def post(self, request):
"""
版本信息编辑 只允许修改描述
:param request:
:return:
"""
version_id = request.POST.get('id')
version = request.POST.get('version')
md5 = request.POST.get('md5')
release_time = request.POST.get('release_time')
desc = request.POST.get('description')
if not version_id:
error_logger.error(u'参数不完整')
return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整')
data = dict()
if version:
data.update({'version': version})
if release_time:
data.update({'release_time': release_time})
if desc:
data.update({'description': desc})
if md5:
data.update({'md5': md5})
try:
self.rpc['venus/sun/channel_build/version/edit'](
id=version_id, data=data
).unwrap()
except Exception as e:
error_logger.error(e)
return self.write_fail(code=-1, message='操作失败')
return self.write_success(data={}, message='操作成功')
class BatchPublishVersion(APIView):
def post(self, request):
"""
批量发布 并邮件通知
:param request:
:return:
"""
version_id = request.POST.get('version_id')
if not version_id:
error_logger.error(u'参数不完整')
return self.write_fail(code=ERROR.ARG_MISS, message='参数不完整')
try:
# 获取当前版本所有已打包渠道
channel_info = self.rpc['venus/sun/channel_build/version/get_channel_info'](
version_id=version_id
).unwrap()
except RPCFaultException as e:
error_logger.error(u'获取渠道失败%s', e)
return self.write_fail(code=-1, message='获取渠道失败!')
packed_channels = list()
for item in channel_info.get('data', []):
if item.get('pack', False):
packed_channels.append(item)
version_release_task.delay(
packed_channels, version_id=version_id, version=channel_info.get('version')
)
return self.write_success(data={}, message='操作成功')
class VersionCreate(APIView):
def post(self, request):
"""
版本创建 供脚本使用
:param request:
:return:
"""
apk = request.FILES.get('file').read()
file_md5 = get_md5_value(apk)
version = request.POST.get('version')
md5 = request.POST.get('md5')
if md5 != file_md5:
raise Exception('md5校验失败, 文件md5{}'.format(file_md5))
data = dict()
if version:
data.update({'version': version})
if md5:
data.update({'md5': md5})
try:
version_id = self.rpc['venus/sun/channel_build/version/edit'](
data=data
).unwrap()
except RPCFaultException as e:
error_logger.error('创建版本失败:%s', e)
raise Exception('创建版本失败!')
channel_info = self.rpc['venus/sun/channel_build/version/get_channel_info'](
version_id=version_id
).unwrap() # 所有渠道的url_name: list()
url_names = []
for item in channel_info.get('data', []):
if item.get('pack', False):
url_names.append(item.get('url_name'))
channel_build.delay(version, url_names, apk)
return {'success': True}