VersionManagementController.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import hashlib
  5. import time
  6. from django.db import transaction
  7. from django.views.generic.base import View
  8. from Ansjer.config import BASE_DIR
  9. from Object.TokenObject import TokenObject
  10. from Object.ResponseObject import ResponseObject
  11. from Service.CommonService import CommonService
  12. from Model.models import Equipment_Version, App_Info, AppSetModel, App_Colophon
  13. class VersionManagement(View):
  14. def get(self, request, *args, **kwargs):
  15. request.encoding = 'utf-8'
  16. operation = kwargs.get('operation')
  17. return self.validation(request.GET, request, operation)
  18. def post(self, request, *args, **kwargs):
  19. request.encoding = 'utf-8'
  20. operation = kwargs.get('operation')
  21. return self.validation(request.POST, request, operation)
  22. def validation(self, request_dict, request, operation):
  23. language = request_dict.get('language', 'en')
  24. response = ResponseObject(language, 'pc')
  25. if operation == 'upLoadFile':
  26. return self.upLoadFile(request, request_dict, response)
  27. else:
  28. tko = TokenObject(
  29. request.META.get('HTTP_AUTHORIZATION'),
  30. returntpye='pc')
  31. if tko.code != 0:
  32. return response.json(tko.code)
  33. response.lang = tko.lang
  34. userID = tko.userID
  35. if operation == 'getEquipmentVersionList':
  36. return self.getEquipmentVersionList(request_dict, response)
  37. elif operation == 'editVersionInformation':
  38. return self.editVersionInformation(request_dict, response)
  39. elif operation == 'deleteEquipmentVersion':
  40. return self.deleteEquipmentVersion(request_dict, response)
  41. elif operation == 'getAppVersionList':
  42. return self.getAppVersionList(request_dict, response)
  43. elif operation == 'addOrEditAppInfo':
  44. return self.addOrEditAppInfo(request_dict, response)
  45. elif operation == 'deleteAppVersion':
  46. return self.deleteAppVersion(request_dict, response)
  47. elif operation == 'getAppSet':
  48. return self.getAppSet(request_dict, response)
  49. elif operation == 'editAppSet':
  50. return self.editAppSet(request_dict, response)
  51. elif operation == 'getAppRecordList':
  52. return self.getAppRecordList(request_dict, response)
  53. elif operation == 'getAppBundleIdList':
  54. return self.getAppBundleIdList(request_dict, response)
  55. elif operation == 'addOrEditAppRecord':
  56. return self.addOrEditAppRecord(request_dict, response)
  57. elif operation == 'deleteAppRecord':
  58. return self.deleteAppRecord(request_dict, response)
  59. else:
  60. return response.json(404)
  61. def getEquipmentVersionList(self, request_dict, response):
  62. mci = request_dict.get('mci', None)
  63. lang = request_dict.get('lang', None)
  64. version = request_dict.get('version', None)
  65. pageNo = request_dict.get('pageNo', None)
  66. pageSize = request_dict.get('pageSize', None)
  67. if not all([pageNo, pageSize]):
  68. return response.json(444)
  69. page = int(pageNo)
  70. line = int(pageSize)
  71. try:
  72. equipment_version_qs = Equipment_Version.objects.filter()
  73. if mci:
  74. equipment_version_qs = equipment_version_qs.filter(mci=mci)
  75. if lang:
  76. equipment_version_qs = equipment_version_qs.filter(lang=lang)
  77. if version:
  78. equipment_version_qs = equipment_version_qs.filter(version__contains=version)
  79. total = equipment_version_qs.count()
  80. equipment_version_qs = equipment_version_qs.values()[(page - 1) * line:page * line]
  81. equipment_version_list = CommonService.qs_to_list(equipment_version_qs)
  82. return response.json(0, {'list': equipment_version_list, 'total': total})
  83. except Exception as e:
  84. print(e)
  85. return response.json(500, repr(e))
  86. def upLoadFile(self, request, request_dict, response):
  87. file = request.FILES.get('file', None)
  88. mci = request_dict.get('mci', '')
  89. lang = request_dict.get('lang', '')
  90. ESN = request_dict.get('ESN', '')
  91. max_ver = request_dict.get('max_ver', '')
  92. channel = request_dict.get('channel', '')
  93. resolutionRatio = request_dict.get('resolutionRatio', '')
  94. Description = request_dict.get('Description', '')
  95. status = request_dict.get('status', 0)
  96. fileSize = request_dict.get('fileSize', '')
  97. if not all([file, mci, lang, ESN, max_ver, channel, resolutionRatio]):
  98. return response.json(444)
  99. try:
  100. channel = int(channel)
  101. resolutionRatio = int(resolutionRatio)
  102. status = int(status)
  103. # 文件名为设备版本,最后一个'.'的前面为软件版本,后面为设备规格名称
  104. # V2.2.4.16E201252CA,软件版本:2.2.4,设备规格名称:16E201252CA
  105. file_name = str(file) # 文件名
  106. version = file_name[:file_name.rindex('.')] # 设备版本
  107. rpoint = version.rindex('.')
  108. softwareVersion = version[1:rpoint] # 软件版本
  109. code = version[rpoint+1:] # 设备规格名称
  110. chipModelList2Code = code[:4] # 主芯片
  111. type = code[-3:-1] # 设备机型
  112. companyCode = code[-1:] # 公司代码
  113. fileSize = fileSize
  114. filePath = '/'.join(('static/otapack', mci, lang, file_name))
  115. file_data = file.read()
  116. fileMd5 = hashlib.md5(file_data).hexdigest()
  117. data_dict = {'mci': mci, 'lang': lang, 'ESN': ESN, 'max_ver': max_ver, 'channel': channel,
  118. 'resolutionRatio': resolutionRatio, 'Description': Description, 'status': status,
  119. 'version': version, 'softwareVersion': softwareVersion, 'code': code,
  120. 'chipModelList2Code': chipModelList2Code, 'type': type, 'companyCode': companyCode,
  121. 'fileSize': fileSize, 'filePath': filePath, 'fileMd5': fileMd5}
  122. # Equipment_Version表创建或更新数据
  123. equipment_version_qs = Equipment_Version.objects.filter(code=code, lang=lang)
  124. if not equipment_version_qs.exists():
  125. Equipment_Version.objects.create(eid=CommonService.getUserID(getUser=False, setOTAID=True), **data_dict)
  126. else:
  127. equipment_version_qs.update(**data_dict)
  128. # 上传文件到服务器
  129. upload_path = '/'.join((BASE_DIR, 'static/otapack', mci, lang)).replace('\\', '/') + '/'
  130. if not os.path.exists(upload_path): # 上传目录不存在则创建
  131. os.makedirs(upload_path)
  132. # 文件上传
  133. full_name = upload_path + file_name
  134. if os.path.exists(full_name): # 删除同名文件
  135. os.remove(full_name)
  136. with open(full_name, 'wb+') as write_file:
  137. for chunk in file.chunks():
  138. write_file.write(chunk)
  139. return response.json(0)
  140. except Exception as e:
  141. print(e)
  142. return response.json(500, repr(e))
  143. def editVersionInformation(self, request_dict, response):
  144. eid = request_dict.get('eid', None)
  145. ESN = request_dict.get('ESN', '')
  146. max_ver = request_dict.get('max_ver', '')
  147. status = request_dict.get('status', '')
  148. channel = request_dict.get('channel', '')
  149. resolutionRatio = request_dict.get('resolutionRatio', '')
  150. Description = request_dict.get('Description', '')
  151. if not eid:
  152. return response.json(444)
  153. status = 1 if status == 'true' else 0
  154. try:
  155. equipment_version_qs = Equipment_Version.objects.filter(eid=eid)
  156. if not equipment_version_qs.exists():
  157. return response.json(173)
  158. data_dict = {'ESN': ESN, 'max_ver': max_ver, 'status': status, 'channel': channel,
  159. 'resolutionRatio': resolutionRatio, 'Description': Description}
  160. equipment_version_qs.update(**data_dict)
  161. return response.json(0)
  162. except Exception as e:
  163. print(e)
  164. return response.json(500, repr(e))
  165. def deleteEquipmentVersion(self, request_dict, response):
  166. eid = request_dict.get('eid', None)
  167. if not eid:
  168. return response.json(444)
  169. try:
  170. equipment_version_qs = Equipment_Version.objects.filter(eid=eid)
  171. filePath = equipment_version_qs.values('filePath')[0]['filePath']
  172. equipment_version_qs.delete()
  173. # 删除文件
  174. full_name = '/'.join((BASE_DIR, filePath)).replace('\\', '/')
  175. if os.path.exists(full_name):
  176. os.remove(full_name)
  177. return response.json(0)
  178. except Exception as e:
  179. print(e)
  180. return response.json(500, repr(e))
  181. def getAppVersionList(self, request_dict, response):
  182. app_type = request_dict.get('app_type', None)
  183. appName = request_dict.get('appName', None)
  184. version = request_dict.get('version', None)
  185. pageNo = request_dict.get('pageNo', None)
  186. pageSize = request_dict.get('pageSize', None)
  187. if not all([pageNo, pageSize]):
  188. return response.json(444)
  189. page = int(pageNo)
  190. line = int(pageSize)
  191. try:
  192. app_info_qs = App_Info.objects.filter()
  193. if app_type:
  194. if app_type == 'IOS':
  195. app_type = 1
  196. elif app_type == '安卓':
  197. app_type = 2
  198. else:
  199. app_type = 3
  200. app_info_qs = app_info_qs.filter(app_type=app_type)
  201. if appName:
  202. app_info_qs = app_info_qs.filter(appName__contains=appName)
  203. if version:
  204. app_info_qs = app_info_qs.filter(version__contains=version)
  205. total = app_info_qs.count()
  206. app_info_qs = app_info_qs.values()[(page - 1) * line:page * line]
  207. app_info_list = CommonService.qs_to_list(app_info_qs)
  208. return response.json(0, {'list': app_info_list, 'total': total})
  209. except Exception as e:
  210. print(e)
  211. return response.json(500, repr(e))
  212. def addOrEditAppInfo(self, request_dict, response):
  213. id = request_dict.get('id', None)
  214. appName = request_dict.get('appName', '')
  215. appBundleId = request_dict.get('appBundleId', '')
  216. bundleVersion = request_dict.get('bundleVersion', '')
  217. newAppversion = request_dict.get('newAppversion', '')
  218. minAppversion = request_dict.get('minAppversion', '')
  219. content = request_dict.get('content', '')
  220. app_type = request_dict.get('app_type', '')
  221. downloadLink = request_dict.get('downloadLink', '')
  222. try:
  223. app_type = int(app_type)
  224. data_dict = {'appName': appName, 'appBundleId': appBundleId, 'bundleVersion': bundleVersion,
  225. 'newAppversion': newAppversion, 'minAppversion': minAppversion, 'content': content,
  226. 'app_type': app_type, 'downloadLink': downloadLink}
  227. if not id: # 添加
  228. App_Info.objects.create(**data_dict)
  229. else: # 编辑
  230. app_info_qs = App_Info.objects.filter(id=id)
  231. if not app_info_qs.exists():
  232. return response.json(173)
  233. app_info_qs.update(**data_dict)
  234. return response.json(0)
  235. except Exception as e:
  236. print(e)
  237. return response.json(500, repr(e))
  238. def deleteAppVersion(self, request_dict, response):
  239. appBundleId = request_dict.get('appBundleId', None)
  240. if not appBundleId:
  241. return response.json(444)
  242. try:
  243. App_Info.objects.filter(appBundleId=appBundleId).delete()
  244. AppSetModel.objects.filter(appBundleId=appBundleId).delete()
  245. return response.json(0)
  246. except Exception as e:
  247. print(e)
  248. return response.json(500, repr(e))
  249. def getAppSet(self, request_dict, response):
  250. appBundleId = request_dict.get('appBundleId', None)
  251. if not appBundleId:
  252. return response.json(444)
  253. try:
  254. app_set_qs = AppSetModel.objects.filter(appBundleId=appBundleId).values('content')
  255. if app_set_qs.exists():
  256. content = app_set_qs[0]['content']
  257. return response.json(0, {'content': content})
  258. else:
  259. nowTime = int(time.time())
  260. AppSetModel.objects.create(
  261. appBundleId=appBundleId,
  262. addTime=nowTime,
  263. updTime=nowTime
  264. )
  265. return response.json(0)
  266. except Exception as e:
  267. print(e)
  268. return response.json(500, repr(e))
  269. def editAppSet(self, request_dict, response):
  270. appBundleId = request_dict.get('appBundleId', None)
  271. content = request_dict.get('content', None)
  272. if not all([appBundleId, content]):
  273. return response.json(444)
  274. try:
  275. AppSetModel.objects.filter(appBundleId=appBundleId).update(content=content)
  276. return response.json(0)
  277. except Exception as e:
  278. print(e)
  279. return response.json(500, repr(e))
  280. def getAppRecordList_1(self, request_dict, response):
  281. app_type = request_dict.get('app_type', 'IOS')
  282. pageNo = request_dict.get('pageNo', None)
  283. pageSize = request_dict.get('pageSize', None)
  284. if not all([pageNo, pageSize]):
  285. return response.json(444)
  286. page = int(pageNo)
  287. line = int(pageSize)
  288. try:
  289. if app_type:
  290. if app_type == 'IOS':
  291. app_type = 1
  292. elif app_type == '安卓':
  293. app_type = 2
  294. else:
  295. app_type = 3
  296. app_colophon_qs = App_Colophon.objects.filter(app_id__app_type=app_type).order_by('app_id').values_list('app_id__appBundleId', flat=True).distinct()
  297. if not app_colophon_qs.exists():
  298. return response.json(173)
  299. total = app_colophon_qs.count()
  300. app_colophon_list = list(app_colophon_qs[(page - 1) * line:page * line])
  301. app_info_qs = App_Colophon.objects.filter(app_id__appBundleId__in=app_colophon_list).\
  302. values("id", "lang", "newApp_version", "content","version_time", "app_id__appBundleId", "app_id__appName", "app_id__app_type")
  303. app_info_list = list(app_info_qs)
  304. data_dict = {}
  305. # 组装数据
  306. for app_info in app_info_list:
  307. for app_colophon in app_colophon_list:
  308. if app_colophon not in data_dict.keys():
  309. data_dict[app_colophon] = []
  310. if app_colophon == app_info['app_id__appBundleId']:
  311. data_dict[app_colophon].append(app_info)
  312. for k, v in enumerate(data_dict):
  313. new = sorted(data_dict[v], key=lambda x: x['id'], reverse=True)
  314. data_dict[v] = new
  315. res = {
  316. 'data_dict': data_dict,
  317. 'total': total
  318. }
  319. return response.json(0, res)
  320. except Exception as e:
  321. print(e)
  322. return response.json(500, repr(e))
  323. def getAppRecordList(self, request_dict, response):
  324. app_type = request_dict.get('appType', 'IOS')
  325. queryVersion = request_dict.get('queryVersion', None)
  326. queryAppBundleId = request_dict.get('queryAppBundleId', None)
  327. pageNo = request_dict.get('pageNo', None)
  328. pageSize = request_dict.get('pageSize', None)
  329. if not all([pageNo, pageSize]):
  330. return response.json(444)
  331. page = int(pageNo)
  332. line = int(pageSize)
  333. try:
  334. if app_type == 'IOS':
  335. app_type = 1
  336. elif app_type == '安卓':
  337. app_type = 2
  338. else:
  339. app_type = 3
  340. app_colophon_qs = App_Colophon.objects.filter(app_id__app_type=app_type).order_by('app_id').values_list('app_id__appBundleId', flat=True).distinct()
  341. if not app_colophon_qs.exists():
  342. return response.json(173)
  343. total = app_colophon_qs.count()
  344. app_colophon_list = list(app_colophon_qs[(page - 1) * line:page * line])
  345. app_info_qs = App_Colophon.objects.filter(app_id__appBundleId__in=app_colophon_list).\
  346. values("id", "lang", "newApp_version", "content", "version_time", "app_id__appBundleId", "app_id__appName", "app_id__app_type")
  347. app_info_list = list(app_info_qs)
  348. app_record_list = [] # 响应的app record数据
  349. appBundleId_list = [] # 记录已添加过的appBundleId
  350. # 组装数据
  351. for app_info in app_info_list:
  352. version = app_info['lang'] + app_info['newApp_version']
  353. if app_info['app_id__appBundleId'] not in appBundleId_list:
  354. appBundleId_list.append(app_info['app_id__appBundleId'])
  355. newApp_version_list = [[app_info['lang'], app_info['newApp_version']]]
  356. app_record_dict = {
  357. 'app_id__appBundleId': app_info['app_id__appBundleId'],
  358. 'app_id__appName': app_info['app_id__appName'],
  359. 'app_id__app_type': app_info['app_id__app_type'],
  360. 'version': version,
  361. 'newApp_version_list': newApp_version_list,
  362. 'id': app_info['id'],
  363. 'content': app_info['content'],
  364. 'version_time': time.strftime("%Y-%m-%d", time.localtime(app_info['version_time'])),
  365. }
  366. if queryVersion and queryVersion == version and queryAppBundleId == app_info['app_id__appBundleId']:
  367. app_record_dict['id'] = app_info['id']
  368. app_record_dict['content'] = app_info['content']
  369. app_record_dict['version_time'] = time.strftime("%Y-%m-%d", time.localtime(app_info['version_time']))
  370. app_record_list.append(app_record_dict)
  371. else:
  372. index = appBundleId_list.index(app_info['app_id__appBundleId'])
  373. newApp_version_list = [app_info['lang'], app_info['newApp_version']]
  374. if queryVersion and queryVersion == version and queryAppBundleId == app_info['app_id__appBundleId']:
  375. app_record_dict['id'] = app_info['id']
  376. app_record_dict['content'] = app_info['content']
  377. app_record_dict['version_time'] = time.strftime("%Y-%m-%d", time.localtime(app_info['version_time']))
  378. app_record_dict['version'] = version
  379. app_record_dict['newApp_version_list'].insert(0, newApp_version_list)
  380. else:
  381. app_record_list[index]['newApp_version_list'].append(newApp_version_list)
  382. res = {
  383. 'app_record_list': app_record_list,
  384. 'total': total
  385. }
  386. return response.json(0, res)
  387. except Exception as e:
  388. print(e)
  389. return response.json(500, repr(e))
  390. def getAppBundleIdList(self, request_dict, response):
  391. print('request_dict:', request_dict)
  392. app_type = request_dict.get('appType', 'IOS')
  393. try:
  394. if app_type == 'IOS':
  395. app_type = 1
  396. elif app_type == '安卓':
  397. app_type = 2
  398. else:
  399. app_type = 3
  400. app_info_qs = App_Info.objects.filter(app_type=app_type).values('id', 'appBundleId')
  401. appBundleIdList = list(app_info_qs)
  402. return response.json(0, {'appBundleIdList': appBundleIdList})
  403. except Exception as e:
  404. print(e)
  405. return response.json(500, repr(e))
  406. def addOrEditAppRecord(self, request_dict, response):
  407. print('request_dict:', request_dict)
  408. appBundleId = request_dict.get('app_id__appBundleId', None)
  409. newApp_version = request_dict.get('version', None)
  410. version_time = request_dict.get('version_time', None)
  411. cn_content = request_dict.get('cnContent', None)
  412. en_content = request_dict.get('enContent', None)
  413. content = request_dict.get('content', None)
  414. app_colophon_id = request_dict.get('id', None)
  415. if not all([appBundleId, newApp_version, version_time]):
  416. return response.json(444)
  417. try:
  418. version_time = int(time.mktime(time.strptime(version_time, '%Y-%m-%d'))) # 字符串转时间戳
  419. if app_colophon_id: # 编辑
  420. # 编辑获取的版本信息前两位为语言
  421. lang = newApp_version[:2]
  422. newApp_version = newApp_version[2:]
  423. App_Colophon.objects.filter(id=app_colophon_id).update(lang=lang, newApp_version=newApp_version,
  424. content=content, version_time=version_time)
  425. else: # 添加
  426. app_info_qs = App_Info.objects.filter(appBundleId=appBundleId).values('id')
  427. if not app_info_qs.exists():
  428. return response.json(173)
  429. data_dict = {
  430. 'app_id_id': app_info_qs[0]['id'],
  431. 'newApp_version': newApp_version,
  432. 'version_time': version_time,
  433. 'lang': 'cn',
  434. 'content': cn_content,
  435. }
  436. with transaction.atomic():
  437. # 创建中文内容数据
  438. App_Colophon.objects.create(**data_dict)
  439. # 创建英文内容数据
  440. data_dict['lang'] = 'en'
  441. data_dict['content'] = en_content
  442. App_Colophon.objects.create(**data_dict)
  443. return response.json(0)
  444. except Exception as e:
  445. print(e)
  446. return response.json(500, repr(e))
  447. def deleteAppRecord(self, request_dict, response):
  448. print('request_dict:', request_dict)
  449. app_colophon_id = request_dict.get('app_colophon_id', None)
  450. try:
  451. if not app_colophon_id:
  452. return response.json(444)
  453. app_colophon_qs = App_Colophon.objects.filter(id=app_colophon_id)
  454. if not app_colophon_qs.exists():
  455. return response.json(173)
  456. app_colophon_qs.delete()
  457. return response.json(0)
  458. except Exception as e:
  459. print(e)
  460. return response.json(500, repr(e))