CloudStorageController.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/python3.6
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright (C) 2022 #
  5. # @Time : 2022/3/24 20:09
  6. # @Author : ming
  7. # @Email : zhangdongming@asj6.wecom.work
  8. # @File : CloudStorageController.py
  9. # @Software: PyCharm
  10. import logging
  11. import time
  12. from django.utils.decorators import method_decorator
  13. from django.views.decorators.csrf import csrf_exempt
  14. from django.views.generic.base import View
  15. from Model.models import Surveys, SurveysTitle, Order_Model, CloudVodSurveysAnswer, SurveysUserLog
  16. from Object.ResponseObject import ResponseObject
  17. from Object.TokenObject import TokenObject
  18. from Service.CommonService import CommonService
  19. class CloudStorageView(View):
  20. @method_decorator(csrf_exempt)
  21. def dispatch(self, *args, **kwargs):
  22. return super(CloudStorageView, self).dispatch(*args, **kwargs)
  23. def get(self, request, *args, **kwargs):
  24. request.encoding = 'utf-8'
  25. operation = kwargs.get('operation')
  26. return self.validation(request.GET, request, operation)
  27. def post(self, request, *args, **kwargs):
  28. request.encoding = 'utf-8'
  29. operation = kwargs.get('operation')
  30. return self.validation(request.POST, request, operation)
  31. def validation(self, request_dict, request, operation):
  32. logger = logging.getLogger('info')
  33. token = TokenObject(request.META.get('HTTP_AUTHORIZATION'))
  34. response = ResponseObject()
  35. if token.code != 0:
  36. return response.json(token.code)
  37. userID = token.userID
  38. ''' API '''
  39. logger.info('---- this user id:{},dict{}'.format(userID, request_dict))
  40. if operation == 'get/info':
  41. return self.check_stock_user(userID, response)
  42. if operation == 'cloud/answer/save':
  43. ip = CommonService.get_ip_address(request)
  44. return self.answer_save(userID, ip, request_dict, response)
  45. def check_stock_user(self, user_id, response):
  46. order = Order_Model.objects.filter(userID=user_id, status=1, order_type=0)
  47. if order.count() == 0:
  48. return response.json(10030)
  49. try:
  50. no = '01'
  51. surveys = Surveys.objects.filter(no=no, user_type=1)
  52. if not all(surveys):
  53. return response.json(173)
  54. result = surveys[0]
  55. surveys_title = SurveysTitle.objects.filter(surveys_id=result.id).order_by('-created_time')
  56. submit = 0
  57. if surveys_title.exists():
  58. surveys_title = surveys_title[0]
  59. cloud_vod = CloudVodSurveysAnswer.objects.filter(title_id=surveys_title.id, user_id=user_id)
  60. if cloud_vod.exists():
  61. submit = 1
  62. data = {
  63. 'no': result.no,
  64. 'title': 'Zosi Cloud Storage',
  65. 'imageUrl': 'https://d2cjxvw3tr9apc.cloudfront.net/app/images/ansjer-cloud-surveys.png',
  66. 'userType': result.user_type,
  67. 'startTime': result.start_time,
  68. 'endTime': result.end_time,
  69. 'isShow': result.is_show,
  70. 'isSubmit': submit,
  71. 'page': '/surveys?token=' if result.is_show == 1 else ''
  72. }
  73. status = True if submit == 1 else False
  74. self.surveys_user_log_save(status, user_id)
  75. return response.json(0, data)
  76. except Exception as e:
  77. print(e)
  78. return response.json(500, repr(e))
  79. @classmethod
  80. def surveys_user_log_save(cls, status=False, userId='', survey_type=1):
  81. if userId:
  82. user_log = SurveysUserLog.objects.filter(user_id=userId, type=survey_type)
  83. if user_log.exists():
  84. if status and user_log[0].is_filled == 0:
  85. user_log.update(is_filled=1)
  86. else:
  87. createdTime = int(time.time())
  88. user_log = SurveysUserLog(user_id=userId, type=1, created_time=createdTime)
  89. user_log.save()
  90. @classmethod
  91. def answer_save(cls, userId, ip, request_dict, response):
  92. try:
  93. ipInfo = CommonService.getIpIpInfo(ip, "CN")
  94. country_name = ipInfo['country_name']
  95. no = request_dict.get('no', None)
  96. if not no:
  97. return response.json(10, 'no is null')
  98. survey = Surveys.objects.filter(no=no)
  99. if survey.exists():
  100. survey = survey[0]
  101. survey_title = SurveysTitle.objects.filter(surveys=survey.id)
  102. if survey_title.exists():
  103. survey_title = survey_title[0]
  104. cloud_vod = CloudVodSurveysAnswer.objects.filter(title_id=survey_title.id, user_id=userId)
  105. if cloud_vod.exists():
  106. return response.json(10, "Do not submit twice")
  107. score = request_dict.get('score', None)
  108. topicA = request_dict.get('topicA', None)
  109. topicB = request_dict.get('topicB', None)
  110. topicC = request_dict.get('topicC', None)
  111. topicD = request_dict.get('topicD', None)
  112. topicF = request_dict.get('topicF', None)
  113. createdTime = int(time.time())
  114. answer = CloudVodSurveysAnswer(title_id=survey_title.id, user_id=userId, ip=ip, answer1=int(score),
  115. answer2=topicA,
  116. answer3=topicB, answer4=topicC, answer5=topicD, answer6=topicF,
  117. created_time=createdTime, country_name=country_name)
  118. answer.save()
  119. SurveysUserLog.objects.filter(user_id=userId, type=1).update(is_filled=1)
  120. return response.json(0)
  121. except Exception as e:
  122. print(e)
  123. return response.json(500, repr(e))