123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- #!/usr/bin/python3.6
- # -*- coding: utf-8 -*-
- #
- # Copyright (C) 2022 #
- # @Time : 2022/3/24 20:09
- # @Author : ming
- # @Email : zhangdongming@asj6.wecom.work
- # @File : CloudStorageController.py
- # @Software: PyCharm
- import logging
- import time
- from django.utils.decorators import method_decorator
- from django.views.decorators.csrf import csrf_exempt
- from django.views.generic.base import View
- from Model.models import Surveys, SurveysTitle, Order_Model, CloudVodSurveysAnswer, SurveysUserLog
- from Object.ResponseObject import ResponseObject
- from Object.TokenObject import TokenObject
- from Service.CommonService import CommonService
- class CloudStorageView(View):
- @method_decorator(csrf_exempt)
- def dispatch(self, *args, **kwargs):
- return super(CloudStorageView, self).dispatch(*args, **kwargs)
- def get(self, request, *args, **kwargs):
- request.encoding = 'utf-8'
- operation = kwargs.get('operation')
- return self.validation(request.GET, request, operation)
- def post(self, request, *args, **kwargs):
- request.encoding = 'utf-8'
- operation = kwargs.get('operation')
- return self.validation(request.POST, request, operation)
- def validation(self, request_dict, request, operation):
- logger = logging.getLogger('info')
- token = TokenObject(request.META.get('HTTP_AUTHORIZATION'))
- response = ResponseObject()
- if token.code != 0:
- return response.json(token.code)
- userID = token.userID
- ''' API '''
- logger.info('---- this user id:{},dict{}'.format(userID, request_dict))
- if operation == 'get/info':
- return self.check_stock_user(userID, response)
- if operation == 'cloud/answer/save':
- ip = CommonService.get_ip_address(request)
- return self.answer_save(userID, ip, request_dict, response)
- def check_stock_user(self, user_id, response):
- order = Order_Model.objects.filter(userID=user_id, status=1, order_type=0)
- if order.count() == 0:
- return response.json(10030)
- try:
- no = '01'
- surveys = Surveys.objects.filter(no=no, user_type=1)
- if not all(surveys):
- return response.json(173)
- result = surveys[0]
- surveys_title = SurveysTitle.objects.filter(surveys_id=result.id).order_by('-created_time')
- submit = 0
- if surveys_title.exists():
- surveys_title = surveys_title[0]
- cloud_vod = CloudVodSurveysAnswer.objects.filter(title_id=surveys_title.id, user_id=user_id)
- if cloud_vod.exists():
- submit = 1
- data = {
- 'no': result.no,
- 'title': 'Zosi Cloud Storage',
- 'imageUrl': 'https://d2cjxvw3tr9apc.cloudfront.net/app/images/ansjer-cloud-surveys.png',
- 'userType': result.user_type,
- 'startTime': result.start_time,
- 'endTime': result.end_time,
- 'isShow': result.is_show,
- 'isSubmit': submit,
- 'page': '/surveys?token=' if result.is_show == 1 else ''
- }
- status = True if submit == 1 else False
- self.surveys_user_log_save(status, user_id)
- return response.json(0, data)
- except Exception as e:
- print(e)
- return response.json(500, repr(e))
- @classmethod
- def surveys_user_log_save(cls, status=False, userId='', survey_type=1):
- if userId:
- user_log = SurveysUserLog.objects.filter(user_id=userId, type=survey_type)
- if user_log.exists():
- if status and user_log[0].is_filled == 0:
- user_log.update(is_filled=1)
- else:
- createdTime = int(time.time())
- user_log = SurveysUserLog(user_id=userId, type=1, created_time=createdTime)
- user_log.save()
- @classmethod
- def answer_save(cls, userId, ip, request_dict, response):
- try:
- ipInfo = CommonService.getIpIpInfo(ip, "CN")
- country_name = ipInfo['country_name']
- no = request_dict.get('no', None)
- if not no:
- return response.json(10, 'no is null')
- survey = Surveys.objects.filter(no=no)
- if survey.exists():
- survey = survey[0]
- survey_title = SurveysTitle.objects.filter(surveys=survey.id)
- if survey_title.exists():
- survey_title = survey_title[0]
- cloud_vod = CloudVodSurveysAnswer.objects.filter(title_id=survey_title.id, user_id=userId)
- if cloud_vod.exists():
- return response.json(10, "Do not submit twice")
- score = request_dict.get('score', None)
- topicA = request_dict.get('topicA', None)
- topicB = request_dict.get('topicB', None)
- topicC = request_dict.get('topicC', None)
- topicD = request_dict.get('topicD', None)
- topicF = request_dict.get('topicF', None)
- createdTime = int(time.time())
- answer = CloudVodSurveysAnswer(title_id=survey_title.id, user_id=userId, ip=ip, answer1=int(score),
- answer2=topicA,
- answer3=topicB, answer4=topicC, answer5=topicD, answer6=topicF,
- created_time=createdTime, country_name=country_name)
- answer.save()
- SurveysUserLog.objects.filter(user_id=userId, type=1).update(is_filled=1)
- return response.json(0)
- except Exception as e:
- print(e)
- return response.json(500, repr(e))
|