EvaluationActivityController.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. @Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
  5. @AUTHOR: ASJRD018
  6. @NAME: AnsjerFormal
  7. @software: PyCharm
  8. @DATE: 2024年7月29日15:51:04
  9. @Version: python3.6
  10. @MODIFY DECORD:ansjer dev
  11. """
  12. import time
  13. from django.views.generic.base import View
  14. from Model.models import FreeEvaluationActivity, ActivityTime, ActivityUser, Device_User
  15. from Object.AWS.AmazonS3Util import AmazonS3Util
  16. from Object.ResponseObject import ResponseObject
  17. from Object.TokenObject import TokenObject
  18. from Ansjer.config import SERVER_DOMAIN
  19. from Ansjer.cn_config.config_formal import SECRET_ACCESS_KEY, ACCESS_KEY_ID, REGION_NAME
  20. from django.db import transaction
  21. class EvaluationActivityView(View):
  22. def get(self, request, *args, **kwargs):
  23. request.encoding = 'utf-8'
  24. operation = kwargs.get('operation')
  25. return self.validation(request.GET, request, operation)
  26. def post(self, request, *args, **kwargs):
  27. request.encoding = 'utf-8'
  28. operation = kwargs.get('operation')
  29. return self.validation(request.POST, request, operation)
  30. def validation(self, request_dict, request, operation):
  31. language = request_dict.get('language', 'en')
  32. response = ResponseObject(language, 'pc')
  33. tko = TokenObject(request.META.get('HTTP_AUTHORIZATION'), returntpye='pc')
  34. if tko.code != 0:
  35. return response.json(tko.code)
  36. response.lang = tko.lang
  37. user_id = tko.userID
  38. if operation == 'getActivity':
  39. return self.get_activity(user_id, request_dict, response)
  40. elif operation == 'getActivityList':
  41. return self.get_activity_list(request_dict, response)
  42. elif operation == 'addOrEditActivity':
  43. return self.add_or_edit_activity(request, request_dict, response)
  44. elif operation == 'getActivityUser':
  45. return self.get_user_list(request_dict, response)
  46. elif operation == 'addActivityUser':
  47. return self.add_activity_user(user_id, request_dict, response)
  48. elif operation == 'editActivityUser':
  49. return self.edit_activity_user(request_dict, response)
  50. else:
  51. return response.json(404)
  52. @staticmethod
  53. def get_activity(user_id, request_dict, response):
  54. try:
  55. now_time = int(time.time())
  56. activity_qs = FreeEvaluationActivity.objects.filter(is_show=1).values('activity_name', 'carousel_image_url',
  57. 'details_image_url', 'issue', 'id',
  58. 'product_number', 'original_price')
  59. if activity_qs.exists():
  60. activity = activity_qs[0]
  61. time_qs = ActivityTime.objects.filter(activity_id=activity['id']).values('node_content', 'start_time',
  62. 'end_time').order_by('sort')
  63. activity['activity_start_time'] = time_qs.first()['start_time']
  64. activity['activity_end_time'] = time_qs.last()['end_time']
  65. activity['activity_process'] = list(time_qs)
  66. user_qs = ActivityUser.objects.filter(activity_id=activity['id'])
  67. activity['user_count'] = user_qs.count()
  68. if user_qs.exists():
  69. user = user_qs.filter(user_id=user_id)
  70. if user.exists():
  71. activity['activity_status'] = 2 # 1:可报名;2:已报名;3:报名截止;4:活动结束
  72. else:
  73. activity['activity_status'] = 1
  74. user_qs = user_qs.filter(is_selected=1).values('user_name', 'phone', 'user_id')
  75. for item in user_qs:
  76. users = Device_User.objects.filter(userID=item['user_id']).values('userIconPath')
  77. if users.exists():
  78. user_icon_path = str(users[0]['userIconPath'])
  79. if user_icon_path and user_icon_path.find('static/') != -1:
  80. user_icon_path = user_icon_path.replace('static/', '').replace('\\', '/')
  81. item['user_icon'] = SERVER_DOMAIN + 'account/getAvatar/' + user_icon_path
  82. else:
  83. item['user_icon'] = ''
  84. else:
  85. item['user_icon'] = ''
  86. item['phone'] = item['phone'][:3] + "****" + item['phone'][7:]
  87. activity['activity_user'] = list(user_qs)
  88. else:
  89. activity['activity_status'] = 1
  90. activity['activity_user'] = []
  91. if now_time > time_qs.first()['end_time']:
  92. activity['activity_status'] = 3
  93. if now_time > time_qs.last()['end_time']:
  94. activity['activity_status'] = 4
  95. return response.json(0, activity)
  96. else:
  97. return response.json(0, {})
  98. except Exception as e:
  99. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  100. @staticmethod
  101. def add_activity_user(user_id, request_dict, response):
  102. activity_id = request_dict.get('activity_id', None)
  103. user_name = request_dict.get('user_name', None)
  104. phone = request_dict.get('phone', None)
  105. address = request_dict.get('address', None)
  106. sex = request_dict.get('sex', None)
  107. age = request_dict.get('age', None)
  108. usage_environment = request_dict.get('usage_environment', None)
  109. is_reports = request_dict.get('is_reports', None)
  110. if not all([activity_id, user_name, phone, address, sex, age, usage_environment, is_reports]):
  111. return response.json(404)
  112. now_time = int(time.time())
  113. try:
  114. user = ActivityUser.objects.filter(user_id=user_id, activity_id=activity_id)
  115. if user.exists():
  116. user.update(phone=phone, address=address, sex=sex, user_name=user_name, is_reports=is_reports, age=age,
  117. usage_environment=usage_environment, update_time=now_time)
  118. else:
  119. ActivityUser.objects.create(activity_id=activity_id, user_id=user_id, phone=phone, address=address,
  120. sex=sex, age=age, usage_environment=usage_environment, user_name=user_name,
  121. is_reports=is_reports, created_time=now_time, update_time=now_time)
  122. return response.json(0)
  123. except Exception as e:
  124. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  125. @staticmethod
  126. def add_or_edit_activity(request, request_dict, response):
  127. activity_id = request_dict.get('activity_id', None)
  128. activity_name = request_dict.get('activity_name', None)
  129. carousel_image = request.FILES.get('carousel_image', None)
  130. details_image = request.FILES.get('details_image', None)
  131. issue = request_dict.get('issue', None)
  132. product_number = request_dict.get('product_number', None)
  133. original_price = request_dict.get('original_price', None)
  134. activity_process = request_dict.get('activity_process', None)
  135. is_show = request_dict.get('is_show', None)
  136. if not all([activity_name, issue, product_number, original_price, is_show, activity_process]):
  137. return response.json(404)
  138. now_time = int(time.time())
  139. activity_process = eval(activity_process)
  140. try:
  141. bucket = "ansjerfilemanager"
  142. s3_obj = AmazonS3Util(ACCESS_KEY_ID, SECRET_ACCESS_KEY, REGION_NAME)
  143. s3_url = 'https://{}.s3.{}.amazonaws.com.cn/'.format(bucket, REGION_NAME)
  144. if carousel_image:
  145. carousel_image_path = '前端/EvaluationActivity/carousel_image_{}.jpg'.format(issue)
  146. s3_obj.upload_file_obj(
  147. bucket,
  148. carousel_image_path,
  149. carousel_image,
  150. {"ContentType": carousel_image.content_type, "ACL": "public-read"},
  151. )
  152. carousel_image_url = s3_url + carousel_image_path
  153. if activity_id:
  154. FreeEvaluationActivity.objects.filter(id=activity_id).update(carousel_image_url=carousel_image_url)
  155. if details_image:
  156. details_image_path = '前端/EvaluationActivity/details_image_{}.jpg'.format(issue)
  157. s3_obj.upload_file_obj(
  158. bucket,
  159. details_image_path,
  160. details_image,
  161. {"ContentType": details_image.content_type, "ACL": "public-read"},
  162. )
  163. details_image_url = s3_url + details_image_path
  164. if activity_id:
  165. FreeEvaluationActivity.objects.filter(id=activity_id).update(details_image_url=details_image_url)
  166. with transaction.atomic():
  167. if activity_id: # 编辑活动
  168. FreeEvaluationActivity.objects.filter(id=activity_id).update(issue=issue,
  169. product_number=product_number,
  170. original_price=original_price,
  171. is_show=is_show, update_time=now_time)
  172. ActivityTime.objects.filter(activity_id=activity_id).delete()
  173. for index, item in enumerate(activity_process):
  174. ActivityTime.objects.create(activity_id=activity_id, node_content=item['node_content'],
  175. start_time=item['start_time'], end_time=item['end_time'],
  176. sort=index)
  177. else: # 增加活动
  178. if not all([carousel_image, details_image]):
  179. return response.json(404)
  180. activity_qs = FreeEvaluationActivity.objects.create(activity_name=activity_name,
  181. product_number=product_number,
  182. carousel_image_url=carousel_image_url,
  183. is_show=is_show, issue=issue,
  184. original_price=original_price,
  185. details_image_url=details_image_url,
  186. created_time=now_time, update_time=now_time)
  187. for index, item in enumerate(activity_process):
  188. ActivityTime.objects.create(activity_id=activity_qs.id, node_content=item['node_content'],
  189. start_time=item['start_time'], end_time=item['end_time'],
  190. sort=index)
  191. return response.json(0)
  192. except Exception as e:
  193. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  194. @staticmethod
  195. def get_activity_list(request_dict, response):
  196. page = request_dict.get('page', None)
  197. line = request_dict.get('line', None)
  198. if not all([page, line]):
  199. return response.json(444)
  200. try:
  201. page = int(page)
  202. line = int(line)
  203. activity_qs = FreeEvaluationActivity.objects.all()
  204. count = activity_qs.count()
  205. activity_qs = activity_qs.values('id', 'activity_name', 'carousel_image_url', 'details_image_url', 'issue',
  206. 'is_show', 'product_number', 'original_price')[
  207. (page - 1) * line: page * line]
  208. for item in activity_qs:
  209. time_qs = ActivityTime.objects.filter(activity_id=item['id']).values('node_content', 'start_time',
  210. 'end_time').order_by('sort')
  211. item['activity_process'] = list(time_qs)
  212. return response.json(0, {'list': list(activity_qs), 'count': count})
  213. except Exception as e:
  214. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  215. @staticmethod
  216. def get_user_list(request_dict, response):
  217. activity_id = request_dict.get('activity_id', None)
  218. user_name = request_dict.get('user_name', None)
  219. phone = request_dict.get('phone', None)
  220. page = request_dict.get('page', None)
  221. line = request_dict.get('line', None)
  222. if not all([activity_id, page, line]):
  223. return response.json(444)
  224. try:
  225. page = int(page)
  226. line = int(line)
  227. user_qs = ActivityUser.objects.filter(activity_id=activity_id)
  228. if user_name:
  229. user_qs = user_qs.filter(user_name=user_name)
  230. if phone:
  231. user_qs = user_qs.filter(phone=phone)
  232. count = user_qs.count()
  233. user_qs = user_qs.values('id', 'user_name', 'phone', 'age', 'address', 'sex', 'is_selected', 'activity_id',
  234. 'usage_environment', 'is_reports')[(page - 1) * line:page * line]
  235. return response.json(0, {'list': list(user_qs), 'count': count})
  236. except Exception as e:
  237. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  238. @staticmethod
  239. def edit_activity_user(request_dict, response):
  240. activity_id = request_dict.get('activity_id', None)
  241. user_id = request_dict.get('user_id', None)
  242. is_selected = request_dict.get('is_selected', None)
  243. try:
  244. ActivityUser.objects.filter(activity_id=activity_id, id=user_id).update(is_selected=is_selected)
  245. return response.json(0)
  246. except Exception as e:
  247. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))