def post(self, request, format=None): username_post = request.data.get('username', '') password_post = request.data.get('password', '') if username_post != '': user = None if HotShotUser.objects.filter(username__exact=username_post): user = HotShotUser.objects.get(username__exact=username_post) elif HotShotUser.objects.filter(phone__exact=username_post): user = HotShotUser.objects.get(phone__exact=username_post) # return CustomResponse(code=100, msg='用户不存在', data=None, status=status.HTTP_204_NO_CONTENT) if user is not None: if user.check_password(password_post): serializer = serializers.HotShotUserSerializer(user) return CustomResponse(code=1, msg='login success', data=serializer.data, status=status.HTTP_200_OK) return CustomResponse(code=101, msg='密码错误', status=status.HTTP_400_BAD_REQUEST) return CustomResponse(code=100, msg='用户不存在', data=None, status=status.HTTP_404_NOT_FOUND) return CustomResponse(code=100, msg='用户不存在', data=None, status=status.HTTP_404_NOT_FOUND)
def get(self, request, format=None): ac = self.request.GET.get('ac') if ac == 'list': model = UserFavoriteLSPModel.objects.filter(uid=request.user.id) serializer = UserFavoriteLSPListSerializer(model, many=True) return CustomResponse(data=serializer.data, code=1, msg='success', status=status.HTTP_200_OK) elif ac == "retrieve": video_id = self.request.GET.get('video_id') model = UserFavoriteLSPModel.objects.filter(uid=request.user.id, video_id=video_id) if model.exists(): serializer = UserFavoriteLSPListSerializer(model, many=True) return CustomResponse(data=serializer.data, code=1, msg='exist', status=status.HTTP_200_OK) else: return CustomResponse(data=None, code=0, msg='don\'t exist', status=status.HTTP_404_NOT_FOUND) return CustomResponse(data=None, code=0, msg='parameter error', status=status.HTTP_400_BAD_REQUEST)
def post(self, request, format=None): ac = self.request.GET.get('ac') if ac == 'add': data = {'uid': request.user.id, 'video': request.data['videoId']} serializer = UserFavoriteLSPSerializer(data=data) if serializer.is_valid(): serializer.save() return CustomResponse(data=serializer.data, code=1, msg='success', status=status.HTTP_200_OK) return CustomResponse(data=serializer.errors, code=0, msg='fail', status=status.HTTP_400_BAD_REQUEST) elif ac == 'del': model = UserFavoriteLSPModel.objects.filter( uid=request.user.id, video_id=self.request.data.get('videoId')) if model.exists(): model.delete() return CustomResponse(data=None, code=1, msg='success', status=status.HTTP_200_OK) return CustomResponse(data=None, code=0, msg='未收藏相关视频', status=status.HTTP_204_NO_CONTENT)
def post(self, request, format='json'): serializers = UploadAvatarSerializer(data=request.data) if serializers.is_valid(): suffix = request.data.get('suffix').split('.')[1] user = HotShotUser.objects.get( username__exact=request.user.username) avatar = Image.open(request.data['avatar']) user_avatar_path = os.path.join(settings.MEDIA_ROOT, 'avatar/' + user.uid).replace( '\\', '/') if os.path.exists(user_avatar_path): print('path exist') shutil.rmtree(user_avatar_path) os.mkdir(user_avatar_path) else: print('path not exist') os.mkdir(user_avatar_path) avatar_file_path = os.path.join( user_avatar_path, str(int(time.time())) + '.' + suffix).replace('\\', '/') print('avatar path=' + avatar_file_path) avatar.save(avatar_file_path.replace('/', '\\')) HotShotUser.objects.filter(id=request.user.id).update( avatar=avatar_file_path) user_serializer = HotShotUserSerializer( HotShotUser.objects.get(id=request.user.id)) return CustomResponse(data=user_serializer.data, code=1, msg='success', status=status.HTTP_200_OK) return CustomResponse(data=serializers.errors, code=0, msg='fail', status=status.HTTP_400_BAD_REQUEST)
def get(self, request, format=None): phone = self.request.GET.get('phone') if verify_phone(phone=phone): yunxin = Yunxin() sms_response = yunxin.get_sms(phone=phone) if sms_response is not None and sms_response['code'] == 200: sms_code = sms_response['obj'] SMSModel.objects.update_or_create(phone=phone, defaults={ 'code': sms_code, 'timestamp': str(int(time.time())) }) return CustomResponse(code=1, msg="require msm code success", data='', status=status.HTTP_200_OK) return CustomResponse(code=107, msg='can not get msm code from yunxin', status=status.HTTP_500_INTERNAL_SERVER_ERROR) return CustomResponse(code=108, msg='phone error', data='', status=status.HTTP_400_BAD_REQUEST)
def get(self, request, format=None): object = models.OECommentModel.objects.filter( video_id=request.GET.get('videoId')) serializer = serializers.OECommentListSerializer(object, many=True) return CustomResponse(data=serializer.data, code=1, msg='success', status=status.HTTP_200_OK)
def post(self, request, format=None): data = { 'user': request.user.id, 'video': request.data.get('videoId'), 'content': request.data.get('content') } serializer = OECommentSerializer(data=data) if serializer.is_valid(): serializer.save() return CustomResponse(data='', code=1, msg='success', status=status.HTTP_200_OK) return CustomResponse(data=None, code=0, msg=serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, format='json'): serializers = UploadPublicVideoSerializer(data=request.data) if serializers.is_valid(): suffix = request.data.get('suffix').split('.')[1] video = request.data['video'] random = str(uuid.uuid1().time) video_name = random + '.' + suffix video_path = os.path.join(settings.MEDIA_ROOT + '/public/video/', video_name) video_stream = open(video_path, 'wb+') for chunk in video.chunks(): video_stream.write(chunk) video_stream.close() # 获取第一帧 cap = cv2.VideoCapture(video_path) cap.set(cv2.CAP_PROP_POS_MSEC, 1) success, image = cap.read() if success: image_path = os.path.join( settings.MEDIA_ROOT + '/public/cover/', random + '.jpg') cv2.imwrite(image_path, image) author = HotShotUser.objects.get(id=request.user.id) public_video = PublicVideoModel() public_video.playUrl = 'public/video/' + video_name public_video.content = request.data.get('content', '') public_video.cover = 'public/cover/' + random + '.jpg' public_video.author = author public_video.save() return CustomResponse(data=None, code=1, msg='success', status=status.HTTP_200_OK) return CustomResponse(data=None, code=0, msg=serializers.errors, status=status.HTTP_400_BAD_REQUEST)
def get_paginated_response(self, data): code = 1 msg = 'success' if not data: code = 0 msg = 'Data Not Found' response_data = data else: response_data = { 'videoList': data, 'nextLink': self.get_next_link() } return CustomResponse(data=response_data, code=code, msg=msg, status=status.HTTP_200_OK)
def post(self, request, format=None): verify = request.data.get('verify', '') username = request.data.get('username', '') phone = request.data.get('phone', '') if verify == '': return CustomResponse(data=None, code=103, msg="verify is empty", status=status.HTTP_400_BAD_REQUEST) if HotShotUser.objects.filter(phone__exact=phone): return CustomResponse(code=104, msg='phone exist', data='', status=status.HTTP_400_BAD_REQUEST) if HotShotUser.objects.filter(username__exact=username): return CustomResponse(code=105, msg='username exist', data='', status=status.HTTP_400_BAD_REQUEST) serializers = HotShotUserSerializer(data=request.data) smsModel = SMSModel.objects.filter(phone=phone, code=verify) if smsModel.exists(): dt_now = int(time.time()) dt_old = int(smsModel[0].timestamp) if (dt_now - dt_old) <= 10 * 60: # 验证码10分钟有效 if serializers.is_valid(): user = serializers.save() # 加密密码 user.set_password(request.data['password']) uid = uuid.uuid1().int >> 108 HotShotUser.objects.filter(id=user.id).update( password=user.password, is_active=True, uid=uid) return CustomResponse(data=None, code=1, msg="register success", status=status.HTTP_200_OK) return CustomResponse(data=None, code=201, msg=serializers.errors, status=status.HTTP_400_BAD_REQUEST) return CustomResponse(data=None, code=106, msg='verify invalid', status=status.HTTP_400_BAD_REQUEST) return CustomResponse(data=None, code=201, msg="param error", status=status.HTTP_400_BAD_REQUEST)