def get(self, request, *args, **kwargs): serializer = HasCameraGroupSerializer(data=request.query_params) if not serializer.is_valid(): retdata = BaseResponse(code=0, msg='error') if serializer.errors.get('camera_group_id'): if serializer.errors['camera_group_id'][0].code == 'required': retdata.error_msg = '摄像头分组ID是必须的' else: retdata.error_msg = '摄像头分组ID不合法,为6-20位字母、数字或下划线' return Response(retdata.result) camera_group_id = serializer.validated_data.get('camera_group_id') has_camera_group = CameraGroup.objects.filter( user=request.user, camera_group_id=camera_group_id).first() if has_camera_group: retdata = BaseResponse(code=1, msg='success', data=1, success_msg='组ID已存在') return Response(retdata.result) retdata = BaseResponse(code=1, msg='success', data=0, success_msg='组ID不存在,可新建') return Response(retdata.result)
def post(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) if serializer.is_valid(): retdata = BaseResponse( code=1, msg='success', data=serializer.validated_data, success_msg='登录成功' ) return Response(retdata.result) retdata = BaseResponse( code=0, msg='error' ) non_field_errors = serializer.errors.get('non_field_errors') if non_field_errors: retdata.error_msg = non_field_errors[0] return Response(retdata.result) elif serializer.errors.get('username'): retdata.error_msg = '用户名不合法,为6到16位字母、数字或下划线' elif serializer.errors.get('email'): retdata.error_msg = '邮箱格式错误或不支持的邮箱' elif serializer.errors.get('password'): if serializer.errors['password'][0].code == 'required': retdata.error_msg = '账号或密码是必传的' else: retdata.error_msg = '密码不合法,为6到16位字母、数字或合法符号' return Response(retdata.result)
def delete(self, request, *args, **kwargs): serializer = InterfaceGroupIDSerializer(data=request.data) if not serializer.is_valid(): retdata = BaseResponse(code=0, msg='error') if serializer.errors.get('ai_skill_group_id'): if serializer.errors['ai_skill_group_id'][ 0].code == 'required': retdata.error_msg = '技能分组ID是必须的' else: retdata.error_msg = '技能分组ID不合法,为6-20位字母、数字或下划线' return Response(retdata.result) ai_skill_group = AISkillGroup.objects.filter( user=request.user, ai_skill_group_id=serializer.validated_data['ai_skill_group_id'] ).first() if not ai_skill_group: retdata = BaseResponse(code=0, msg='error', error_msg='不存在的接口库') return Response(retdata.result) ai_skills = ai_skill_group.aiskill_set.all() if ai_skills: for ai_skill in ai_skills: if ai_skill.bound: retdata = BaseResponse(code=0, msg='error', error_msg='该技能接口库中存在绑定摄像头的接口,无法删除') return Response(retdata.result) ai_skill_group.delete() retdata = BaseResponse(code=1, msg='success', success_msg='接口库删除成功') return Response(retdata.result)
def get(self, requests, *args, **kwargs): serializer = CameraPreviewSerializer(data=requests.query_params) if not serializer.is_valid(): retdata = BaseResponse(code=0, msg='error') if serializer.errors.get('camera_id'): if serializer.errors['camera_id'][0].code == 'required': retdata.error_msg = '摄像图ID是必须的' else: retdata.error_msg = '摄像头ID不合法,为6-20位字母、数字或下划线' return Response(retdata.result) camera = Camera.objects.filter( user=requests.user, camera_id=serializer.validated_data['camera_id']).first() if not camera: retdata = BaseResponse(code=0, msg='error', error_msg='不存在该摄像头') return Response(retdata.result) preview = get_preview(camera.camera_url) if not preview: if not is_opened(camera.camera_url): camera.state = 10 camera.save() retdata = BaseResponse(code=0, msg='error', error_msg='摄像头连接或抽帧发生错误', data=RetrieveCameraSerializer(camera).data) return Response(retdata.result) retdata = BaseResponse(code=1, msg='success', success_msg='摄像头预览图片请求成功', data=preview) return Response(retdata.result)
def get(self, request, *args, **kwargs): serializer = InterfaceGroupIDSerializer(data=request.query_params) if not serializer.is_valid(): retdata = BaseResponse(code=0, msg='error') if serializer.errors.get('ai_skill_group_id'): if serializer.errors['ai_skill_group_id'][ 0].code == 'required': retdata.error_msg = '技能接口分组ID是必须的' else: retdata.error_msg = '技能接口分组ID不合法,为6-20位字母、数字或下划线' return Response(retdata.result) has_ai_skill_gourp = AISkillGroup.objects.filter( user=request.user, ai_skill_group_id=serializer.validated_data['ai_skill_group_id'] ).first() retdata = BaseResponse(code=1, msg='success') if not has_ai_skill_gourp: retdata.success_msg = '技能接口分组不存在,可创建' retdata.data = 0 else: retdata.success_msg = '技能接口分组已存在' retdata.data = 1 return Response(retdata.result)
def delete(self, request, *args, **kwargs): serializer = FaceGroupIDSerialzier(data=request.data) if not serializer.is_valid(): retdata = BaseResponse(code=0, msg='error') if serializer.errors.get('face_group_id'): if serializer.errors['face_group_id'][0].code == 'required': retdata.error_msg = '人脸库ID是必须的' else: retdata.error_msg = '人脸库ID不合法,为6-20位字母、数字或下划线' return Response(retdata.result) face_group = FaceGroup.objects.filter( user=request.user, face_group_id=serializer.validated_data['face_group_id']).first() if not face_group: retdata = BaseResponse(code=0, msg='error', error_msg='不存在该人脸库') return Response(retdata.result) if face_group.bound: retdata = BaseResponse(code=0, msg='error', error_msg='人脸库已绑定摄像头,请先解除绑定后删除') return Response(retdata.result) face_group.delete() retdata = BaseResponse(code=1, msg='success', success_msg='删除人脸库成功') return Response(retdata.result)
def delete(self, request, *args, **kwargs): serializer = InterfaceIDSerializer(data=request.data) if not serializer.is_valid(): retdata = BaseResponse(code=0, msg='error') if serializer.errors.get('ai_skill_id'): if serializer.errors['ai_skill_id'][0].code == 'required': retdata.error_msg = '技能接口ID是必须的' else: retdata.error_msg = '技能接口ID不合法,为6-20位字母、数字或下划线' return Response(retdata.result) ai_skill = AISkill.objects.filter( user=request.user, ai_skill_id=serializer.validated_data['ai_skill_id']).first() if not ai_skill: retdata = BaseResponse(code=0, msg='error', error_msg='AI技能接口不存在') return Response(retdata.result) if ai_skill.bound: retdata = BaseResponse(code=0, msg='error', error_msg='技能被摄像头绑定,请解绑后删除') return Response(retdata.result) ai_skill.delete() retdata = BaseResponse(code=1, msg='success', success_msg='AI技能接口删除成功') return Response(retdata.result)
def post(self, request, *args, **kwargs): serializer = FaceGroupSerializer(data=request.data) if not serializer.is_valid(): retdata = BaseResponse(code=0, msg='error') if serializer.errors.get('face_group_id'): if serializer.errors['face_group_id'][0].code == 'required': retdata.error_msg = '人脸库ID是必须的' else: retdata.error_msg = '人脸库ID不合法,为6-20位字母、数字或下划线' elif serializer.errors.get('name'): if serializer.errors['name'][0].code == 'required': retdata.error_msg = '人脸库名称是必须的' else: retdata.error_msg = '人脸库名称不得超过30个字符或为空' elif serializer.errors.get('description'): retdata.error_msg = '人脸库描述不得超过300个字符' return Response(retdata.result) has_face_group = FaceGroup.objects.filter( user=request.user, face_group_id=serializer.validated_data['face_group_id']).first() if has_face_group: retdata = BaseResponse(code=0, msg='error', error_msg='人脸库已存在') return Response(retdata.result) face_group_obj = serializer.save(user=request.user) retdata = BaseResponse(code=1, msg='success', data=FaceGroupSerializer(face_group_obj).data, success_msg='创建人脸库成功') return Response(retdata.result)
def delete(self, request, *args, **kwargs): serializer = FaceIDSerializer(data=request.data) if not serializer.is_valid(): retdata = BaseResponse(code=0, msg='error') if serializer.errors.get('face_id'): if serializer.errors['face_id'][0].code == 'required': retdata.error_msg = '人脸ID是必须的' else: retdata.error_msg = '人脸ID不合法,为6-20位字母、数字或下划线' return Response(retdata.result) face = Face.objects.filter( user=request.user, face_id=serializer.validated_data['face_id']).first() if not face: retdata = BaseResponse(code=0, msg='error', error_msg='不存在人脸') return Response(retdata.result) if face.group.bound: retdata = BaseResponse(code=0, msg='error', error_msg='人脸所属组绑定了技能,不能删除') return Response(retdata.result) face.face_image.delete() face.delete() retdata = BaseResponse(code=1, msg='success', success_msg='人脸信息删除成功') return Response(retdata.result)
def get(self, request, *args, **kwargs): serializer = InterfaceGroupIDSerializer(data=request.query_params) if not serializer.is_valid(): retdata = BaseResponse(code=0, msg='error') if serializer.errors.get('ai_skill_group_id'): if serializer.errors['ai_skill_group_id'][ 0].code == 'required': retdata.error_msg = '技能接口分组是必须的' else: retdata.error_msg = '技能接口分组ID不合法,为6-20位字母、数字或下划线' return Response(retdata.result) ai_skill_group = AISkillGroup.objects.filter( user=request.user, ai_skill_group_id=serializer.validated_data['ai_skill_group_id'] ).first() if not ai_skill_group: retdata = BaseResponse(code=0, msg='error', error_msg='不存在的接口库') return Response(retdata.result) ai_skills = ai_skill_group.aiskill_set.all() ai_skills_serializer = InterfaceSerializer(ai_skills, many=True) retdata = BaseResponse(code=1, msg='success', success_msg='AI技能接口请求成功', data=ai_skills_serializer.data) return Response(retdata.result)
def post(self, request, *args, **kwargs): serializer = InterfaceGroupSerializer(data=request.data) if not serializer.is_valid(): retdata = BaseResponse(code=0, msg='error') if serializer.errors.get('ai_skill_group_id'): if serializer.errors['ai_skill_group_id'][ 0].code == 'required': retdata.error_msg = '技能分组ID是必须的' else: retdata.error_msg = '技能分组ID不合法,为6-20位字母、数字或下划线' elif serializer.errors.get('name'): if serializer.errors['name'][0].code == 'required': retdata.error_msg = '技能名称是必须的' else: retdata.error_msg = '技能分组名称不能超过30个字符或为空' elif serializer.errors.get('description'): retdata.error_msg = '技能分组描述不能超过300个字符' return Response(retdata.result) has_ai_skill_group = AISkillGroup.objects.filter( user=request.user, ai_skill_group_id=serializer.validated_data['ai_skill_group_id'] ).first() if has_ai_skill_group: retdata = BaseResponse(code=0, msg='error', error_msg='已存在接口库') return Response(retdata.result) ai_skill_group_obj = serializer.save(user=request.user) retdata = BaseResponse( code=1, msg='success', data=InterfaceGroupSerializer(ai_skill_group_obj).data, success_msg='接口库创建成功') return Response(retdata.result)
def get(self, request, *args, **kwargs): serializer = RetrieveCameraCheckSerializer(data=request.query_params) if not serializer.is_valid(): retdata = BaseResponse(code=0, msg='error') if serializer.errors.get('camera_group_id'): if serializer.errors['camera_group_id'][0].code == 'required': retdata.error_msg = '摄像头分组ID是必须的' else: retdata.error_msg = '摄像头分组ID不合法,为6-20位字母、数字或下划线' return Response(retdata.result) has_camera_group = CameraGroup.objects.filter( camera_group_id=serializer.validated_data['camera_group_id'] ).first() if not has_camera_group: retdata = BaseResponse(code=0, msg='error', error_msg='没有该摄像头分组') return Response(retdata.result) cameras = Camera.objects.filter(group__camera_group_id=serializer. validated_data['camera_group_id']) cameras_serializer = RetrieveCameraSerializer(instance=cameras, many=True) retdata = BaseResponse(code=1, msg='success', success_msg='数据请求成功', data=cameras_serializer.data) return Response(retdata.result)
def get(self, request, *args, **kwargs): serializer = FaceGroupIDSerialzier(data=request.query_params) if not serializer.is_valid(): retdata = BaseResponse(code=0, msg='error') if serializer.errors.get('face_group_id'): if serializer.errors['face_group_id'][0].code == 'required': retdata.error_msg = '人脸库ID是必须的' else: retdata.error_msg = '人脸库ID不合法,为6-20位字母、数字或下划线' return Response(retdata.result) has_face_group = FaceGroup.objects.filter( user=request.user, face_group_id=serializer.validated_data['face_group_id']).first() if has_face_group: retdata = BaseResponse(code=1, msg='success', data=1, success_msg='人脸库已存在') return Response(retdata.result) else: retdata = BaseResponse(code=1, msg='success', data=0, success_msg='人脸库不存在,可创建') return Response(retdata.result)
def delete(self, request, *args, **kwargs): serializer = DeleteCameraGroupSerializer(data=request.data) if not serializer.is_valid(): retdata = BaseResponse(code=0, msg='error') if serializer.errors.get('camera_group_id'): if serializer.errors['camera_group_id'][0].code == 'required': retdata.error_msg = '摄像头分组ID是必须的' else: retdata.error_msg = '摄像头分组ID不合法,为6-20位字母、数字或下划线' elif serializer.errors.get('force'): retdata.error_msg = 'force字段是bool类型' return Response(retdata.result) camera_group = CameraGroup.objects.filter( user=request.user, camera_group_id=serializer.validated_data['camera_group_id'] ).first() if not camera_group: retdata = BaseResponse(code=0, msg='error', error_msg='摄像头分组不存在') return Response(retdata.result) cameras = camera_group.camera_set.all() for camera in cameras: if camera.state in (13, 20, 21, 22): if serializer.validated_data['force']: for ai_skill_setting in camera.ai_skill_settings.all(): ai_skill_setting.ai_skill.bound = False ai_skill_setting.ai_skill.save() face_relevance = ai_skill_setting.face_relevance if face_relevance: for face_group in face_relevance.face_group.all(): face_group.bound = False face_group.save() task_id = request.user.username + '##' + camera.camera_id periodic_task = PeriodicTask.objects.filter( name=task_id).first() if periodic_task: periodic_task.enabled = False periodic_task.save() periodic_task.delete() with RedisTaskState(task_id=task_id) as task_state: task_state.set_state('stopped') camera.ai_skill_settings.clear() else: retdata = BaseResponse( code=0, msg='error', error_msg='无法删除摄像头分组,存在摄像头已绑定技能或者正在运行') return Response(retdata.result) camera_group.delete() retdata = BaseResponse(code=1, msg='success', success_msg='摄像头组删除成功') return Response(retdata.result)
def patch(self, request, *args, **kwargs): serializer = UpdateCameraSerializer(data=request.data) if not serializer.is_valid(): retdata = BaseResponse(code=0, msg='error') if serializer.errors.get('camera_id'): if serializer.errors['camera_id'][0].code == 'required': retdata.error_msg = '摄像头ID是必须的' else: retdata.error_msg = '摄像头ID不合法,为6-20位字母、数字或下划线' elif serializer.errors.get('name'): retdata.error_msg = '摄像头名称不能超过30个字符或为空' elif serializer.errors.get('description'): retdata.error_msg = '摄像头描述不能超过300个字符' elif serializer.errors.get('camera_url'): retdata.error_msg = serializer.errors['camera_url'][0] return Response(retdata.result) name = serializer.validated_data.get('name') description = serializer.validated_data.get('description') camera_url = serializer.validated_data.get('camera_url') if not name and not description and not camera_url: retdata = BaseResponse(code=0, msg='error', error_msg='请传入须修改参数') return Response(retdata.result) camera_obj = Camera.objects.filter( user=request.user, camera_id=serializer.validated_data['camera_id']).first() if not camera_obj: retdata = BaseResponse(code=0, msg='error', error_msg='没有该摄像头,请新建') return Response(retdata.result) if camera_obj.state == 21: retdata = BaseResponse(code=0, msg='error', error_msg='无法修改摄像头,已绑定技能或者正在运行') return Response(retdata.result) camera_obj.name = serializer.validated_data.get( 'name', camera_obj.name) camera_obj.description = serializer.validated_data.get( 'description', camera_obj.description) camera_url = serializer.validated_data.get('camera_url') if camera_url: camera_obj.camera_url = camera_url camera_obj.state = 11 camera_obj.save() retdata = BaseResponse(code=1, msg='success', success_msg='摄像头信息修改成功', data=RetrieveCameraSerializer(camera_obj).data) return Response(retdata.result)
def patch(self, request, *args, **kwargs): faces = Face.objects.filter(user=request.user) serializer = UpdateFaceSerializer(instance=faces, data=request.data, partial=True) if not serializer.is_valid(): retdata = BaseResponse(code=0, msg='error') if serializer.errors.get('face_id'): retdata.error_msg = '人脸库ID,为6-20位字母、数字或下划线' elif serializer.errors.get('name'): retdata.error_msg = '人脸名称不能超过30个字符或为空' elif serializer.errors.get('face_image'): retdata.error_msg = '请上传一个有效的图片文件' return Response(retdata.result) face_id = serializer.validated_data.get('face_id') name = serializer.validated_data.get('name') face_image = serializer.validated_data.get('face_image') if not name and not face_image: retdata = BaseResponse(code=0, msg='error', error_msg='请传入须修改数据') return Response(retdata.result) if not face_id: retdata = BaseResponse(code=0, msg='error', error_msg='人脸ID是必须的') return Response(retdata.result) face = faces.filter(face_id=face_id).first() if not face: retdata = BaseResponse(code=0, msg='error', error_msg='不存在该人脸') return Response(retdata.result) if face.group.bound: retdata = BaseResponse(code=0, msg='error', error_msg='所属人脸库已绑定摄像头,请先解除绑定后修改人脸信息') return Response(retdata.result) face_image = request.data.get('face_image') if face_image: face_detector = FaceDetection(image=face_image) if not face_detector.detect(): retdata = BaseResponse(code=0, msg='error', error_msg='图片中没有人脸或非标准人脸照片') return Response(retdata.result) new_face_obj = serializer.save() retdata = BaseResponse(code=1, msg='success', data=FaceSerializer(new_face_obj).data, success_msg='人脸信息更改成功') return Response(retdata.result)
def patch(self, request, *args, **kwargs): ai_skill_groups = AISkillGroup.objects.filter(user=request.user) serializer = InterfaceGroupSerializer(instance=ai_skill_groups, data=request.data, partial=True) if not serializer.is_valid(): retdata = BaseResponse(code=0, msg='error') if serializer.errors.get('ai_skill_group_id'): retdata.error_msg = '接口分组ID不合法,为6-20位字母、数字或下划线' elif serializer.errors.get('name'): retdata.error_msg = '技能分组名称不能超过30个字符或为空' elif serializer.errors.get('description'): retdata.error_msg = '技能分组描述不能超过300个字符' return Response(retdata.result) ai_skill_group_id = serializer.validated_data.get('ai_skill_group_id') name = serializer.validated_data.get('name') description = serializer.validated_data.get('description') if not ai_skill_group_id: retdata = BaseResponse(code=0, msg='error', error_msg='接口库ID是必须的') return Response(retdata.result) if not name and not description: retdata = BaseResponse(code=0, msg='error', error_msg='请传入须修改的参数') return Response(retdata.result) ai_skill_group = ai_skill_groups.filter( ai_skill_group_id=ai_skill_group_id).first() if not ai_skill_group: retdata = BaseResponse(code=0, msg='error', error_msg='接口库不存在') return Response(retdata.result) ai_skills = ai_skill_group.aiskill_set.all() if ai_skills: for ai_skill in ai_skills: if ai_skill.bound: retdata = BaseResponse( code=0, msg='error', error_msg='该技能接口库中存在绑定摄像头的接口,无法修改信息') return Response(retdata.result) new_ai_skill_group_obj = serializer.save() retdata = BaseResponse( code=1, msg='success', success_msg='接口库信息更改成功', data=InterfaceGroupSerializer(new_ai_skill_group_obj).data) return Response(retdata.result)
def post(self, request, *args, **kwargs): serializer = CreateCameraSerializer(data=request.data) if not serializer.is_valid(): retdata = BaseResponse(code=0, msg='error') if serializer.errors.get('camera_id'): if serializer.errors['camera_id'][0].code == 'required': retdata.error_msg = '摄像头ID是必须的' else: retdata.error_msg = '摄像头ID不合法,为6-20位字母、数字或下划线' elif serializer.errors.get('camera_group_id'): if serializer.errors['camera_group_id'][0].code == 'required': retdata.error_msg = '摄像头分组ID是必须的' else: retdata.error_msg = '摄像头分组ID不合法,为6-20位字母、数字或下划线' elif serializer.errors.get('name'): if serializer.errors['name'][0].code == 'required': retdata.error_msg = '摄像头名称是必须的' else: retdata.error_msg = '摄像头名称不能超过30个字符或为空' elif serializer.errors.get('camera_url'): if serializer.errors['camera_url'][0].code == 'required': retdata.error_msg = '摄像头视频流地址是必须的' else: retdata.error_msg = serializer.errors['camera_url'][0] elif serializer.errors.get('desription'): retdata.error_msg = '摄像头描述不能超过300个字符' return Response(retdata.result) has_camera = Camera.objects.filter( user=request.user, camera_id=serializer.validated_data['camera_id']).first() has_camera_group = CameraGroup.objects.filter( user=request.user, camera_group_id=serializer.validated_data['camera_group_id'] ).first() if has_camera: retdata = BaseResponse(code=0, msg='error', error_msg='摄像头ID已存在,请重新设置') return Response(retdata.result) if not has_camera_group: retdata = BaseResponse(code=0, msg='error', error_msg='摄像头分组不存在,不可绑定到该分组') return Response(retdata.result) camera_obj = serializer.save(user=request.user) retdata = BaseResponse(code=1, msg='success', success_msg='摄像头创建成功', data=RetrieveCameraSerializer(camera_obj).data) return Response(retdata.result)
def patch(self, request, *args, **kwargs): ai_skills = AISkill.objects.filter(user=request.user) serializer = UpdateInterfaceSerializer(instance=ai_skills, data=request.data, partial=True) if not serializer.is_valid(): retdata = BaseResponse(code=0, msg='error') if serializer.errors.get('ai_skill_id'): retdata.error_msg = '技能接口ID不合法,为6-20位字母、数字或下划线' elif serializer.errors.get('name'): retdata.error_msg = '技能接口名称不能超过30个字符或为空' elif serializer.errors.get('description'): retdata.error_msg = '技能接口描述不能超过300个字符' elif serializer.errors.get('ai_skill_url'): retdata.error_msg = serializer.errors['ai_skill_url'][0] return Response(retdata.result) ai_skill_id = serializer.validated_data.get('ai_skill_id') name = serializer.validated_data.get('name') description = serializer.validated_data.get('description') ai_skill_url = serializer.validated_data.get('ai_skill_url') if not ai_skill_id: retdata = BaseResponse(code=0, msg='error', error_msg='AI技能接口ID是必须的') return Response(retdata.result) if not name and not description and not ai_skill_url: retdata = BaseResponse(code=0, msg='error', error_msg='请传入须修改的数据') return Response(retdata.result) ai_skill = ai_skills.filter(ai_skill_id=ai_skill_id).first() if not ai_skill: retdata = BaseResponse(code=0, msg='error', error_msg='AI技能接口不存在') return Response(retdata.result) if ai_skill.bound: retdata = BaseResponse(code=0, msg='error', error_msg='技能被摄像头绑定,请解绑后修改接口信息') return Response(retdata.result) new_ai_skill_obj = serializer.save() retdata = BaseResponse(code=1, msg='success', success_msg='AI技能接口信息更新成功', data=InterfaceSerializer(new_ai_skill_obj).data) return Response(retdata.result)
def post(self, request, *args, **kwargs): serializer = FaceSerializer(data=request.data) if not serializer.is_valid(): retdata = BaseResponse(code=0, msg='error') if serializer.errors.get('face_id'): if serializer.errors['face_id'][0].code == 'required': retdata.error_msg = '人脸ID是必须的' else: retdata.error_msg = '人脸ID不合法,为6-20位字母、数字或下划线' elif serializer.errors.get('face_group_id'): if serializer.errors['face_group_id'][0].code == 'required': retdata.error_msg = '人脸库ID是必须的' else: retdata.error_msg = '人脸库ID不合法,为6-20位字母、数字或下划线' elif serializer.errors.get('name'): if serializer.errors['name'][0].code == 'required': retdata.error_msg = '人脸名称是必须的' else: retdata.error_msg = '人脸名称不得超过30个字符或为空' elif serializer.errors.get('face_image'): if serializer.errors['face_image'][0].code == 'required': retdata.error_msg = '人脸图片base64编码是必须的' else: retdata.error_msg = '请上传一个有效的图片文件' return Response(retdata.result) has_face = Face.objects.filter( user=request.user, face_id=serializer.validated_data['face_id']).first() if has_face: retdata = BaseResponse(code=0, msg='error', error_msg='人脸已存在') return Response(retdata.result) has_face_group_id = FaceGroup.objects.filter( user=request.user, face_group_id=serializer.validated_data['face_group_id']).first() if not has_face_group_id: retdata = BaseResponse(code=0, msg='error', error_msg='人脸库不存在,无法绑定人脸') return Response(retdata.result) face_img_b64 = request.data.get('face_image') face_detector = FaceDetection(image=face_img_b64) if not face_detector.detect(): retdata = BaseResponse(code=0, msg='error', error_msg='图片中没有人脸或非标准人脸照片') return Response(retdata.result) face_obj = serializer.save(user=request.user) redata = BaseResponse(code=1, msg='success', data=FaceSerializer(face_obj).data, success_msg='人脸添加成功') return Response(redata.result)
def patch(self, request, *args, **kwargs): face_groups = FaceGroup.objects.filter(user=request.user) serializer = FaceGroupSerializer(instance=face_groups, data=request.data, partial=True) if not serializer.is_valid(): retdata = BaseResponse(code=0, msg='error') if serializer.errors.get('face_group_id'): retdata.error_msg = '人脸库ID不合法,为6-20位字母、数字或下划线' elif serializer.errors.get('name'): retdata.error_msg = '人脸库名称不得超过30个字符或为空' elif serializer.errors.get('description'): retdata.error_msg = '人脸库描述不得超过300个字符' return Response(retdata.result) face_group_id = serializer.validated_data.get('face_group_id') name = serializer.validated_data.get('name') description = serializer.validated_data.get('description') if not name and not description: retdata = BaseResponse(code=0, msg='error', error_msg='请传入须修改数据') return Response(retdata.result) if not face_group_id: retdata = BaseResponse(code=0, msg='error', error_msg='必须传入分组ID') return Response(retdata.result) face_group = FaceGroup.objects.filter( user=request.user, face_group_id=face_group_id).first() if not face_group: retdata = BaseResponse(code=0, msg='error', error_msg='无指定人脸库') return Response(retdata.result) if face_group.bound: retdata = BaseResponse(code=0, msg='error', error_msg='人脸库已绑定摄像头,请先解除绑定后修改信息') return Response(retdata.result) new_face_group_obj = serializer.save() retdata = BaseResponse( code=1, msg='success', data=FaceGroupSerializer(new_face_group_obj).data, success_msg='人脸库信息修改成功') return Response(retdata.result)
def patch(self, request, *args, **kargs): serializer = InterfaceIDSerializer(data=request.data) if not serializer.is_valid(): retdata = BaseResponse(code=0, msg='error') if serializer.errors.get('ai_skill_id'): if serializer.errors['ai_skill_id'][0].code == 'required': retdata.error_msg = '技能接口ID是必须的' else: retdata.error_msg = '技能接口ID不合法,为6-20位字母、数字或下划线' return Response(retdata.result) ai_skill = AISkill.objects.filter( user=request.user, ai_skill_id=serializer.validated_data.get('ai_skill_id')).first() try: ai_skill_test = requests.get(ai_skill.ai_skill_url, timeout=10).status_code if ai_skill_test != 200: ai_skill.state = 0 ai_skill.save() retdata = BaseResponse(code=1, msg='success', success_msg='接口异常', data=0) return Response(retdata.result) else: ai_skill.state = 1 ai_skill.save() retdata = BaseResponse(code=1, msg='success', success_msg='接口正常', data=1) return Response(retdata.result) except (requests.exceptions.ConnectionError, requests.exceptions.ConnectTimeout): ai_skill.state = 0 ai_skill.save() retdata = BaseResponse(code=1, msg='success', success_msg='接口异常', data=0) return Response(retdata.result)
def get(self, request, *args, **kwargs): email_serializer = SendEmailCaptchaSerializer(data=request.query_params) if not email_serializer.is_valid(): retdata = BaseResponse( code=0, msg='error' ) if email_serializer.errors.get('email'): if email_serializer.errors['email'][0].code == 'required': retdata.error_msg = '邮箱是必传的' else: retdata.error_msg = '邮箱格式错误或不支持的邮箱' elif email_serializer.errors.get('flag'): if email_serializer.errors['flag'][0].code == 'required': retdata.error_msg = '验证码类型是必传的' return Response(retdata.result) email = email_serializer.validated_data['email'] flag = email_serializer.validated_data['flag'] if flag == 'register': context = { 'text': '注册', 'flag': flag } elif flag == 'update': context = { 'text': '修改邮件', 'flag': flag } else: context = { 'text': '找回密码', 'flag': flag } send_email_captcha.delay(email, context) retdata = BaseResponse( code=1, msg='success', success_msg='验证码发送成功' ) return Response(retdata.result)
def post(self, request, *args, **kwargs): serializer = SetExtractFrameSettingsSerializer(data=request.data) if not serializer.is_valid(): retdata = BaseResponse(code=0, msg='error') if serializer.errors.get('camera_id'): if serializer.errors['camera_id'][0].code == 'required': retdata.error_msg = '摄像头ID是必须的' else: retdata.error_msg = '摄像头ID不合法,为6-20位字母、数字或下划线' elif serializer.errors.get('frequency'): if serializer.errors['frequency'][0].code == 'requried': retdata.error_msg = '抽帧频率是必须的' else: retdata.error_msg = '抽帧频率只能是1, 2, 3' elif serializer.errors.get('start_time'): if serializer.errors['start_time'][0].code == 'required': retdata.error_msg = '开始时间是必须的' else: retdata.error_msg = '时间格式错误' elif serializer.errors.get('end_time'): if serializer.errors['end_time'][0].code == 'required': retdata.error_msg = '结束时间是必须的' else: retdata.error_msg = '时间格式错误' elif serializer.errors.get('non_field_errors'): retdata.error_msg = serializer.errors['non_field_errors'][0] return Response(retdata.result) camera = Camera.objects.filter( user=request.user, camera_id=serializer.validated_data['camera_id']).first() if not camera: retdata = BaseResponse(code=0, msg='error', error_msg='不存在该摄像头') return Response(retdata.result) if camera.state == 21: retdata = BaseResponse(code=0, msg='error', error_msg='摄像头正在运行中,无法设置抽帧配置') return Response(retdata.result) camera_obj = serializer.save(user=request.user) retdata = BaseResponse(code=1, msg='success', data=RetrieveCameraSerializer(camera_obj).data, success_msg='抽帧配置成功, 请完成技能配置') return Response(retdata.result)
def patch(self, request, *args, **kwargs): serializer = UpdateCameraGroupSerializer(data=request.data) if not serializer.is_valid(): retdata = BaseResponse(code=0, msg='error') if serializer.errors.get('camera_group_id'): if serializer.errors['camera_group_id'][0].code == 'required': retdata.error_msg = '摄像头分组ID是必须的' else: retdata.error_msg = '摄像头分组ID不合法,为6-20位字母、数字或下划线' elif serializer.errors.get('name'): retdata.error_msg = '摄像头分组名称不能超过30个字符' elif serializer.errors.get('description'): retdata.error_msg = '摄像头分组描述不能超过300个字符' return Response(retdata.result) name = serializer.validated_data.get('name') description = serializer.validated_data.get('description') if not name and not description: retdata = BaseResponse(code=0, msg='error', error_msg='请传入须修改参数') return Response(retdata.result) camera_group = CameraGroup.objects.filter( user=request.user, camera_group_id=serializer.validated_data['camera_group_id'] ).first() if not camera_group: retdata = BaseResponse(code=0, msg='error', error_msg='摄像头分组不存在') return Response(retdata.result) camera_group.name = serializer.validated_data.get( 'name', camera_group.name) camera_group.description = serializer.validated_data.get( 'description', camera_group.description) camera_group.save() retdata = BaseResponse( code=1, msg='success', success_msg='摄像头组信息修改成功', data=RetrieveCameraGroupSerializer(camera_group).data) return Response(retdata.result)
def post(self, request, *args, **kwargs): serializer = StopSerializer(data=request.data) if not serializer.is_valid(): retdata = BaseResponse(code=0, msg='error') if serializer.errors.get('camera_id'): if serializer.errors['camera_id'][0].code == 'required': retdata.error_msg = '摄像头ID是必须的' else: retdata.error_msg = '摄像头ID不合法,为6-20位字母、数字或下划线' return Response(retdata.result) camera_obj = Camera.objects.filter( user=request.user, camera_id=serializer.validated_data['camera_id']).first() if not camera_obj: retdata = BaseResponse(code=0, msg='error', error_msg='不存在该摄像头') return Response(retdata.result) task_id = request.user.username + '##' + serializer.validated_data[ 'camera_id'] periodic_task = PeriodicTask.objects.filter(name=task_id).first() if not periodic_task: retdata = BaseResponse(code=0, msg='error', error_msg='摄像头没有运行任务') return Response(retdata.result) periodic_task.enabled = False periodic_task.save() periodic_task.delete() with RedisTaskState(task_id=task_id) as task_state: task_state.set_state('stopped') camera_obj.state = 22 camera_obj.save() retdata = BaseResponse(code=1, msg='success', data=RetrieveCameraSerializer(camera_obj).data, success_msg='任务已结束') return Response(retdata.result)
def delete(self, requests, *args, **kwargs): serializer = IncidentIDSerializer(data=requests.data) if not serializer.is_valid(): retdata = BaseResponse(code=0, msg='error') if serializer.errors.get('incident_id'): if serializer.errors['incident_id'][0].code == 'required': retdata.error_msg = '事件ID是必须的' else: retdata.error_msg = '事件ID不合法,为UUID格式' return Response(retdata.result) incident = Incident.objects.filter( user=requests.user, incident_id=serializer.validated_data['incident_id']).first() if not incident: retdata = BaseResponse(code=0, msg='error', error_msg='事件不存在') return Response(retdata.result) incident.delete() retdata = BaseResponse(code=1, msg='success', success_msg='事件删除成功') return Response(retdata.result)
def get(self, request, *args, **kwargs): serializer = FaceIDSerializer(data=request.query_params) if not serializer.is_valid(): retdata = BaseResponse(code=0, msg='error') if serializer.errors.get('face_id'): if serializer.errors['face_id'][0].code == 'required': retdata.error_msg = '人脸ID是必须的' else: retdata.error_msg = '人脸ID不合法,为6-20位字母、数字或下划线' return Response(retdata.result) has_face = Face.objects.filter( user=request.user, face_id=serializer.validated_data['face_id']) retdata = BaseResponse(code=1, msg='successs') if not has_face: retdata.success_msg = '人脸不存在,可创建' retdata.data = 0 else: retdata.success_msg = '人脸已存在' retdata.data = 1 return Response(retdata.result)
def patch(self, request, *args, **kwargs): data_serializer = ForgotPasswordSerializer(data=request.data) if not data_serializer.is_valid(): retdata = BaseResponse( code=0, msg='error' ) if data_serializer.errors.get('email'): if data_serializer.errors['email'][0].code == 'required': retdata.error_msg = '邮箱是必传的' else: retdata.error_msg = '邮箱格式不合法或不支持的邮箱' elif data_serializer.errors.get('new_password'): if data_serializer.errors['new_password'][0].code == 'required': retdata.error_msg = '新密码是必传的' else: retdata.error_msg = '密码不合法,为6到16位字母、数字或合法符号' elif data_serializer.errors.get('captcha'): if data_serializer.errors['captcha'][0].code == 'required': retdata.error_msg = '验证码是必传的' else: retdata.error_msg = '验证码格式错误,为5位数字' return Response(retdata.result) email = data_serializer.validated_data['email'] captcha = data_serializer.validated_data['captcha'] new_password = data_serializer.validated_data['new_password'] user_obj = User.objects.filter(email=email).first() if user_obj: with ValidateCaptcha(email=email, captcha=captcha) as captcha_validator: ret = captcha_validator.validate(settings.FORGOT_PASSWORD_CAPTCHA_KEY_NAME) if not ret['flag']: retdata = BaseResponse( code=0, msg='error', error_msg=ret['result'] ) return Response(retdata.result) user_obj.set_password(new_password) user_obj.save() retdata = BaseResponse( code=1, msg='success', success_msg='找回密码成功' ) return Response(retdata.result) retdata = BaseResponse( code=0, msg='error', error_msg='账户不存在' ) return Response(retdata.result)
def delete(self, request, *args, **kwargs): serializer = DeleteAllIncidentsSerilizer(data=request.data) if not serializer.is_valid(): retdata = BaseResponse(code=0, msg='error') if serializer.errors.get('camera_id'): retdata.error_msg = '摄像头ID不合法,为6-20位字母、数字或下划线' elif serializer.errors.get('ai_skill_id'): retdata.error_msg = 'AI技能接口ID不合法,为6-20位字母、数字或下划线' elif serializer.errors.get('time_range'): retdata.error_msg = '时间范围不合法,须传入start_time和end_time字段' elif serializer.errors.get('offset'): if serializer.errors['offset'][0].code == 'required': retdata.error_msg = '须传入数据偏移量' else: retdata.error_msg = '数据偏移量是整型' elif serializer.errors.get('limit'): if serializer.errors['limit'][0].code == 'required': retdata.error_msg = '须传入数据限制量' else: retdata.error_msg = '数据限制量为整型' return Response(retdata.result) camera_id = serializer.validated_data.get('camera_id') ai_skill_id = serializer.validated_data.get('ai_skill_id') time_range = serializer.validated_data.get('time_range') incidents = Incident.objects.filter(user=request.user) if not incidents: retdata = BaseResponse(code=1, msg='success', sucess_msg='暂无任何事件发生, 无需删除') return Response(retdata.result) if camera_id: incidents = incidents.filter(camera__camera_id=camera_id) if ai_skill_id: incidents = incidents.filter(ai_skill__ai_skill_id=ai_skill_id) if time_range: incidents = incidents.filter(occurrence_time__range=[ time_range['start_time'], time_range['end_time'] ]) incidents.delete() retdata = BaseResponse(code=1, msg='success', success_msg='事件删除成功') return Response(retdata.result)