def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) name = request.data.get('name', '') if NotificationRule.objects.filter(name=name).first(): result = { 'success': False, 'messages': '消息规则:{} 已经存在, 不能重复创建'.format(name) } return Response(result, status=status.HTTP_400_BAD_REQUEST) serializer.is_valid(raise_exception=True) self.perform_create(serializer) headers = self.get_success_headers(serializer.data) instance = NotificationRule.objects.get(pk=int(serializer.data['id'])) # 操作日志记录 action_log(request=request, user=request.user, action_type=CREATE_ACTION_TYPE, old_instance=None, instance=instance, action_info=f'新增消息规则:{instance.__str__()}') result = { 'success': True, 'messages': f'新增消息规则:{instance.__str__()}!', 'results': serializer.data } return Response(result, status=status.HTTP_200_OK, headers=headers)
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) name = request.data.get('name', '') module_id = request.data.get('module', '') try: module = AnalysisModule.objects.get(pk=int(module_id)) except: module = None if AnalysisParameter.objects.filter(name=name, module=module).first(): result = {'success': False, 'messages': f'分析参数:{name}已经存在, 不能重复创建'} return Response(result, status=status.HTTP_400_BAD_REQUEST) serializer.is_valid(raise_exception=True) self.perform_create(serializer) headers = self.get_success_headers(serializer.data) instance = AnalysisParameter.objects.get(pk=int(serializer.data['id'])) action_log(request=request, user=request.user, action_type=CREATE_ACTION_TYPE, old_instance=None, instance=instance, action_info=f'新增分析参数:{instance.__str__()}') result = { 'success': True, 'messages': f'新增分析参数:{instance.__str__()}', 'results': serializer.data } return Response(result, status=status.HTTP_200_OK, headers=headers)
def post(self, request, *args, **kwargs): username = request.data.get('username') password = request.data.get('password') if not username: result = {'success': False, 'messages': '用户帐号名必须存在, 请发送用户名!'} return Response(result, status=status.HTTP_400_BAD_REQUEST) if not password: result = {'success': False, 'messages': '密码必须存在, 请发送密码!'} return Response(result, status=status.HTTP_400_BAD_REQUEST) user = User.objects.filter(username=username).first() if user: if not user.is_active or user.is_deleted: result = {'success': False, 'messages': '当前用户已禁用或删除, 请使用其他用户!'} return Response(result, status=status.HTTP_403_FORBIDDEN) login_user = authenticate(username=username.strip(), password=password) if login_user is None: result = {'success': False, 'messages': '用户名:{}密码错误, 请输入正确密码!'.format(username)} return Response(result, status=status.HTTP_403_FORBIDDEN) else: result = {'success': False, 'messages': '用户名:{}不存在或无效, 请输入正确用户!'.format(username)} return Response(result, status=status.HTTP_403_FORBIDDEN) # 认证成功 token = get_jwt_token(login_user) response_data = jwt_response_payload_handler(token, login_user, request) response = Response(response_data, status=status.HTTP_200_OK) # 操作日志记录 api_logger.debug(f'{login_user}登录系统') action_log(request=request, user=login_user, action_type=AUTH_ACTION_TYPE, old_instance=None, instance=login_user, action_info=f'使用账号密码登录系统') return response
def create(self, request, *args, **kwargs): """新增项目""" serializer = self.get_serializer(data=request.data) serial_number = request.data.get('serial_number', '') if Project.objects.filter(serial_number=serial_number).first(): result = { 'success': False, 'messages': f'项目:{serial_number} 已经存在, 不能重复创建' } return Response(result, status=status.HTTP_400_BAD_REQUEST) serializer.is_valid(raise_exception=True) self.perform_create(serializer) headers = self.get_success_headers(serializer.data) instance = Project.objects.get(pk=int(serializer.data['id'])) action_log(request=request, user=request.user, action_type=CREATE_ACTION_TYPE, old_instance=None, instance=instance, action_info=f'新增项目:{instance.__str__()}') result = { 'success': True, 'messages': f'新增项目:{instance.__str__()}!', 'results': serializer.data } return Response(result, status=status.HTTP_200_OK, headers=headers)
def bulk_delete(self, request, *args, **kwargs): """批量删除""" deleted_objects_ids = request.data.get('deleted_objects', []) queryset = self.get_queryset() deleted_objects = queryset.filter(id__in=deleted_objects_ids).all() active_count = NotificationRule.objects.get_active().count() for deleted_object in deleted_objects: if deleted_object.is_active and active_count <= 1: result = { 'success': False, 'messages': f'必须存在一个可用消息规则, 无法删除:{deleted_object.__str__()}' } return Response(result, status=status.HTTP_400_BAD_REQUEST) messages = ','.join( [deleted_object.__str__() for deleted_object in deleted_objects]) deleted_objects.delete() # 操作日志记录 action_log(request=request, user=request.user, action_type=BULK_DELETE_ACTION_TYPE, old_instance=None, instance=None, action_info=f'批量删除消息规则:{messages}') result = {'success': True, 'messages': f'批量删除消息规则: {messages}!'} return Response(result, status=status.HTTP_200_OK)
def activation(self, request, *args, **kwargs): """禁用/启用接口""" old = self.get_object() instance = self.get_object() active = request.data.get('active', True) if active: instance.is_active = True instance.save() # 操作日志记录 action_log(request, request.user, ACTIVE_ACTION_TYPE, old_instance=old, instance=instance, action_info=f'启动分析任务接口:{instance.__str__()}') result = {'success': True, 'messages': f'启动消分析任务接口:{instance.__str__()}'} return Response(result, status=status.HTTP_200_OK) else: active_count = AnalysisTaskInterface.objects.get_active().count() if active_count <= 1: result = {'success': False, 'messages': f'必须存在一个可用分析任务接口, 无法禁用:{instance.__str__()}'} return Response(result, status=status.HTTP_400_BAD_REQUEST) instance.is_active = False instance.save() # 操作日志记录 action_log(request, request.user, DISABLE_ACTION_TYPE, old_instance=old, instance=instance, action_info=f'禁用分析任务接口:{instance.__str__()}') result = {'success': True, 'messages': f'禁用分析任务接口:{instance.__str__()}'} return Response(result, status=status.HTTP_200_OK)
def password(self, request, *args, **kwargs): """修改个人密码""" instance = self.request.user old_instance = self.request.user old_password = request.data.get('old_password') new_password = request.data.get('new_password') if instance.check_password(old_password): instance.set_password(new_password) instance.save() action_log(request=request, user=request.user, action_type=UPDATE_ACTION_TYPE, old_instance=old_instance, instance=instance, action_info=f'修改个人密码:{instance.__str__()}') return Response({ 'messages': f'修改个人密码:{instance.__str__()}', 'success': True }) else: return Response({ 'messages': '原密码不正确!', 'success': False }, status=status.HTTP_400_BAD_REQUEST)
def bulk_delete(self, request, *args, **kwargs): # 批量删除 deleted_objects_ids = request.data.get('deleted_objects', []) queryset = self.get_queryset() deleted_objects_names = [] for deleted_object_id in deleted_objects_ids: instance = User.objects.get(pk=int(deleted_object_id)) if instance == request.user: result = { 'success': False, 'messages': f'用户:{instance.__str__()}为当前账号,不能删除自己!若要删除,请使用其他管理员账号登录删除此用户' } return Response(result, status=status.HTTP_400_BAD_REQUEST) deleted_objects_names.append(instance.__str__()) deleted_objects = queryset.filter(id__in=deleted_objects_ids).all() deleted_objects.delete() action_log(request=request, user=request.user, action_type=BULK_DELETE_ACTION_TYPE, old_instance=None, instance=None, action_info=f'批量删除用户:{deleted_objects_names}') result = { 'success': True, 'messages': f'批量删除用户:{deleted_objects_names}' } return Response(result, status=status.HTTP_200_OK)
def update(self, request, *args, **kwargs): """编辑项目""" partial = kwargs.pop('partial', False) instance = self.get_object() old_instance = self.get_object() serial_number = request.data.get('serial_number', '') if serial_number != instance.serial_number and Project.objects.filter( serial_number=serial_number).first(): result = { 'success': False, 'messages': f'项目:{instance.__str__()} 已经存在, 不能修改成此编号' } return Response(result, status=status.HTTP_400_BAD_REQUEST) serializer = self.get_serializer(instance, data=request.data, partial=partial) serializer.is_valid(raise_exception=True) self.perform_update(serializer) if getattr(instance, '_prefetched_objects_cache', None): instance._prefetched_objects_cache = {} action_log(request=request, user=request.user, action_type=UPDATE_ACTION_TYPE, old_instance=old_instance, instance=instance, action_info=f'修改项目:{instance.__str__()}') result = { 'success': True, 'messages': f'修改项目:{instance.__str__()}', 'results': serializer.data } return Response(result, status=status.HTTP_200_OK)
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) name = request.data.get('name', '') work_zone_id = request.data.get('work_zone', '') try: work_zone = WorkZone.objects.get(pk=int(work_zone_id)) except: work_zone = None if ProjectSerialNumberSetting.objects.filter( name=name, work_zone=work_zone).first(): result = { 'success': False, 'messages': '项目编号规则:{} 已经存在当前工作区, 不能重复创建'.format(name) } return Response(result, status=status.HTTP_400_BAD_REQUEST) serializer.is_valid(raise_exception=True) self.perform_create(serializer) headers = self.get_success_headers(serializer.data) instance = ProjectSerialNumberSetting.objects.get( pk=int(serializer.data['id'])) # 操作日志记录 action_log(request=request, user=request.user, action_type=CREATE_ACTION_TYPE, old_instance=None, instance=instance, action_info=f'新增项目编号规则:{instance.__str__()}') result = { 'success': True, 'messages': f'新增项目编号规则:{instance.__str__()}!', 'results': serializer.data } return Response(result, status=status.HTTP_200_OK, headers=headers)
def continue_run(self, request, *args, **kwargs): # 继续运行分析任务 instance = self.get_object() is_success, code, messages, resp_results = continue_run_analysis( instance) if is_success: instance.status = ANALYSIS_RUNNING instance.finished_time = None instance.save() result = {'success': True, 'messages': f'继续运行分析任务成功'} action_log(request=request, user=request.user, action_type=CONTINUE_ACTION_TYPE, old_instance=instance, instance=instance, action_info=f'继续运行分析任务:{instance.__str__()}') return Response(result, status=status.HTTP_200_OK) else: result = { 'success': False, 'messages': f'继续运行分析任务发生错误, 返回码:{code},' f' 错误信息:{messages}, 返回结果:{resp_results}' } return Response(result, status=status.HTTP_400_BAD_REQUEST)
def stop(self, request, *args, **kwargs): # 停止分析任务 instance = self.get_object() is_success, code, messages, resp_results = stop_analysis(instance) if is_success: instance.status = ANALYSIS_STOP instance.finished_time = datetime.now() instance.save() result = {'success': True, 'messages': f'停止分析任务成功'} action_log(request=request, user=request.user, action_type=STOP_ACTION_TYPE, old_instance=instance, instance=instance, action_info=f'停止分析任务:{instance.__str__()}') return Response(result, status=status.HTTP_200_OK) else: result = { 'success': False, 'messages': f'停止分析任务发生错误, 返回码:{code},' f' 错误信息:{messages}, 返回结果:{resp_results}' } return Response(result, status=status.HTTP_400_BAD_REQUEST)
def update(self, request, *args, **kwargs): partial = kwargs.pop('partial', False) instance = self.get_object() old_instance = self.get_object() name = request.data.get('name', '') version = request.data.get('version', '') if name != instance.name or version != instance.version: if AnalysisModule.objects.filter(name=name, version=version).first(): result = { 'success': False, 'messages': f'分析模块:{instance.name}@{instance.version} 已经存在, 不能修改' } return Response(result, status=status.HTTP_400_BAD_REQUEST) serializer = self.get_serializer(instance, data=request.data, partial=partial) serializer.is_valid(raise_exception=True) self.perform_update(serializer) if getattr(instance, '_prefetched_objects_cache', None): instance._prefetched_objects_cache = {} action_log(request=request, user=request.user, action_type=UPDATE_ACTION_TYPE, old_instance=instance, instance=old_instance, action_info=f'修改分析模块:{instance.name}@{instance.version}!') result = { 'success': True, 'messages': f'修改分析模块:{instance.name}@{instance.version}!', 'results': serializer.data } return Response(result, status=status.HTTP_200_OK)
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) name = request.data.get('name', '') version = request.data.get('version', '') if AnalysisModule.objects.filter(name=name, version=version).first(): result = { 'success': False, 'messages': '分析模块:{}@{} 已经存在, 不能重复创建'.format(name, version) } return Response(result, status=status.HTTP_400_BAD_REQUEST) serializer.is_valid(raise_exception=True) self.perform_create(serializer) headers = self.get_success_headers(serializer.data) instance = AnalysisModule.objects.get(pk=int(serializer.data['id'])) action_log(request=request, user=request.user, action_type=CREATE_ACTION_TYPE, old_instance=None, instance=instance, action_info=f'创建分析模块:{instance.name}@{instance.version}!') result = { 'success': True, 'messages': f'创建分析模块:{instance.name}@{instance.version}!', 'results': serializer.data } return Response(result, status=status.HTTP_200_OK, headers=headers)
def destroy(self, request, *args, **kwargs): instance = self.get_object() work_zone_id = request.data.get('work_zone', '') try: work_zone = WorkZone.objects.get(pk=int(work_zone_id)) except: work_zone = None active_count = ProjectSerialNumberSetting.objects.get_active( work_zone=work_zone).count() if instance.is_active and active_count <= 1: result = { 'success': False, 'messages': f'必须存在一个可用项目编号规则, 无法删除:{instance.__str__()}' } return Response(result, status=status.HTTP_400_BAD_REQUEST) self.perform_destroy(instance) # 操作日志记录 action_log(request=request, user=request.user, action_type=DELETE_ACTION_TYPE, old_instance=None, instance=None, action_info=f'刪除分析任务接口:{instance.__str__()}') result = { 'success': True, 'messages': f'刪除分析任务接口:{instance.__str__()}' } return Response(result, status=status.HTTP_200_OK)
def destroy(self, request, *args, **kwargs): instance = self.get_object() self.perform_destroy(instance) action_log(request=request, user=request.user, action_type=DELETE_ACTION_TYPE, old_instance=None, instance=None, action_info=f'删除样本:{instance.__str__()}') result = {'success': True, 'messages': f'删除样本:{instance.__str__()}'} return Response(result, status=status.HTTP_200_OK)
def bulk_mark(self, request, *args, **kwargs): # 批量标记多个 notification_ids = request.data.get('notifications', []) mark = request.data.get('mark', None) queryset = self.get_queryset() queryset = queryset.filter(id__in=[ int(notification_id) for notification_id in notification_ids ]) # 标记动作,当前可选值为['unread', 'read', 'delete', 'recover', 'clear'] if mark == 'unread': queryset.unread() action_flag = UNREAD_NOTIFY_ACTION_TYPE mark_desc = '未读' elif mark == 'read': queryset.read() action_flag = READ_NOTIFY_ACTION_TYPE mark_desc = '已读' elif mark == 'delete': queryset.deleted() action_flag = DELETE_NOTIFY_ACTION_TYPE mark_desc = '删除' elif mark == 'recover': queryset.active() action_flag = RECOVER_NOTIFY_ACTION_TYPE mark_desc = '恢复' elif mark == 'clear': queryset.delete() action_flag = CLEAR_NOTIFY_ACTION_TYPE mark_desc = '清空' else: action_flag = None mark_desc = None titles = [instance.title for instance in queryset] if action_flag: action_log(request=request, user=request.user, action_type=action_flag, old_instance=None, instance=None, action_info=f'批量{mark_desc}站内消息:{titles}') result = { 'success': True, 'messages': f'批量{mark_desc}站内消息:{titles}' } return Response(result, status=status.HTTP_200_OK) else: result = { 'success': False, 'messages': f'参数mark错误!请标记为以下值:[unread, read, delete, recover, clear]', 'results': [] } return Response(result, status=status.HTTP_400_BAD_REQUEST)
def destroy(self, request, *args, **kwargs): instance = self.get_object() active_count = AnalysisTaskInterface.objects.get_active().count() if instance.is_active and active_count <= 1: result = {'success': False, 'messages': f'必须存在一个可用分析任务接口, 无法删除:{instance.__str__()}'} return Response(result, status=status.HTTP_400_BAD_REQUEST) self.perform_destroy(instance) # 操作日志记录 action_log(request=request, user=request.user, action_type=DELETE_ACTION_TYPE, old_instance=None, instance=None, action_info=f'刪除分析任务接口:{instance.__str__()}') result = {'success': True, 'messages': f'刪除分析任务接口:{instance.__str__()}'} return Response(result, status=status.HTTP_200_OK)
def mark(self, request, *args, **kwargs): # 单个标记 mark = request.data.get('mark', None) old_instance = self.get_object() instance = self.get_object() # 标记动作,当前可选值为['unread', 'read', 'delete', 'recover', 'clear'] if mark == 'unread': instance.mark_as_unread() action_flag = UNREAD_NOTIFY_ACTION_TYPE mark_desc = '未读' elif mark == 'read': instance.mark_as_read() action_flag = READ_NOTIFY_ACTION_TYPE mark_desc = '已读' elif mark == 'delete': instance.mark_as_deleted() action_flag = DELETE_NOTIFY_ACTION_TYPE mark_desc = '删除' elif mark == 'recover': instance.mark_as_active() action_flag = RECOVER_NOTIFY_ACTION_TYPE mark_desc = '恢复' elif mark == 'clear': old_instance = None instance.delete() action_flag = CLEAR_NOTIFY_ACTION_TYPE mark_desc = '清空' else: action_flag = None mark_desc = None if action_flag: action_log(request=request, user=request.user, action_type=action_flag, old_instance=old_instance, instance=instance, action_info=f'{mark_desc}站内消息:{instance.title}') result = { 'success': True, 'messages': f'{mark_desc}站内消息:{instance.title}' } return Response(result, status=status.HTTP_200_OK) else: result = { 'success': False, 'messages': f'参数mark错误!请标记为以下值:[unread, read, delete, recover, clear]', 'results': [] } return Response(result, status=status.HTTP_400_BAD_REQUEST)
def activation(self, request, *args, **kwargs): """禁用/启用""" old = self.get_object() instance = self.get_object() active = request.data.get('active', True) work_zone_id = request.data.get('work_zone', '') try: work_zone = WorkZone.objects.get(pk=int(work_zone_id)) except: work_zone = None if active: instance.is_active = True instance.save() # 操作日志记录 action_log(request, request.user, ACTIVE_ACTION_TYPE, old_instance=old, instance=instance, action_info=f'启用项目编号规则:{instance.__str__()}') result = { 'success': True, 'messages': f'启用项目编号规则:{instance.__str__()}' } return Response(result, status=status.HTTP_200_OK) else: active_count = ProjectSerialNumberSetting.objects.get_active( work_zone=work_zone).count() if active_count <= 1: result = { 'success': False, 'messages': f'必须存在一个可用项目编号规则, 无法禁用:{instance.__str__()}' } return Response(result, status=status.HTTP_400_BAD_REQUEST) instance.is_active = False instance.save() # 操作日志记录 action_log(request, request.user, DISABLE_ACTION_TYPE, old_instance=old, instance=instance, action_info=f'禁用项目编号规则:{instance.__str__()}') result = { 'success': True, 'messages': f'禁用项目编号规则:{instance.__str__()}' } return Response(result, status=status.HTTP_200_OK)
def bulk_delete(self, request, *args, **kwargs): """批量删除""" deleted_objects_ids = request.data.get('deleted_objects', []) queryset = self.get_queryset() deleted_objects = queryset.filter(id__in=deleted_objects_ids).all() messages = ','.join( [deleted_object.__str__() for deleted_object in deleted_objects]) deleted_objects.delete() # 操作日志记录 action_log(request=request, user=request.user, action_type=BULK_DELETE_ACTION_TYPE, old_instance=None, instance=None, action_info=f'批量删除项目编号规则:{messages}') result = {'success': True, 'messages': f'批量删除项目编号规则: {messages}!'} return Response(result, status=status.HTTP_200_OK)
def bulk_delete(self, request, *args, **kwargs): # 批量删除 deleted_objects_ids = request.data.get('deleted_objects', []) queryset = self.get_queryset() deleted_objects = queryset.filter(id__in=deleted_objects_ids).all() deleted_objects_names = [ f'{deleted_object.__str__()}' for deleted_object in deleted_objects ] deleted_objects.delete() action_log(request=request, user=request.user, action_type=BULK_DELETE_ACTION_TYPE, old_instance=None, instance=None, action_info=f'批量删除样本:{deleted_objects_names}') result = {'success': True, 'messages': f'批量删除样本!'} return Response(result, status=status.HTTP_200_OK)
def update(self, request, *args, **kwargs): partial = kwargs.pop('partial', False) instance = self.get_object() old_instance = self.get_object() name = request.data.get('name', '') work_zone_id = request.data.get('work_zone', '') is_active = request.data.get('is_active', True) try: work_zone = WorkZone.objects.get(pk=int(work_zone_id)) except: work_zone = None if name != instance.name and ProjectSerialNumberSetting.objects.filter( name=name, work_zone=work_zone).first(): result = { 'success': False, 'messages': '项目编号规则:{} 已经存在当前工作区, 不能修改成此名称'.format(name) } return Response(result, status=status.HTTP_400_BAD_REQUEST) active_count = ProjectSerialNumberSetting.objects.get_active( work_zone=work_zone).count() if not is_active and active_count <= 1: result = { 'success': False, 'messages': f'必须存在一个可用项目编号规则, 无法禁用:{instance.__str__()}' } return Response(result, status=status.HTTP_400_BAD_REQUEST) serializer = self.get_serializer(instance, data=request.data, partial=partial) serializer.is_valid(raise_exception=True) self.perform_update(serializer) if getattr(instance, '_prefetched_objects_cache', None): instance._prefetched_objects_cache = {} # 操作日志记录 action_log(request=request, user=request.user, action_type=UPDATE_ACTION_TYPE, old_instance=old_instance, instance=instance, action_info=f'修改项目编号规则:{instance.__str__()}') result = { 'success': True, 'messages': f'修改项目编号规则:{instance.__str__()}!', 'results': serializer.data } return Response(result, status=status.HTTP_200_OK)
def all_mark(self, request, *args, **kwargs): # 全部标记 mark = request.data.get('mark', None) queryset = self.get_queryset() # 标记动作,当前可选值为['unread', 'read', 'delete', 'recover', 'clear'] if mark == 'unread': queryset.unread_all(recipient=request.user) action_flag = UNREAD_NOTIFY_ACTION_TYPE mark_desc = '未读' elif mark == 'read': queryset.read_all(recipient=request.user) action_flag = READ_NOTIFY_ACTION_TYPE mark_desc = '已读' elif mark == 'delete': queryset.delete_all(recipient=request.user) action_flag = DELETE_NOTIFY_ACTION_TYPE mark_desc = '删除' elif mark == 'recover': queryset.active_all(recipient=request.user) action_flag = RECOVER_NOTIFY_ACTION_TYPE mark_desc = '恢复' elif mark == 'clear': queryset.filter(recipient=request.user).delete() action_flag = CLEAR_NOTIFY_ACTION_TYPE mark_desc = '清空' else: action_flag = None mark_desc = None if action_flag: action_log(request=request, user=request.user, action_type=action_flag, old_instance=None, instance=None, action_info=f'{mark_desc}所有站内消息') result = {'success': True, 'messages': f'{mark_desc}所有站内消息'} return Response(result, status=status.HTTP_200_OK) else: result = { 'success': False, 'messages': f'参数mark错误!请标记为以下值:[unread, read, delete, recover, clear]', 'results': [] } return Response(result, status=status.HTTP_400_BAD_REQUEST)
def destroy(self, request, *args, **kwargs): instance = self.get_object() if instance == request.user: result = { 'success': False, 'messages': f'用户:{instance.__str__()}为当前账号,不能删除自己!若要删除,请使用其他管理员账号登录删除此用户' } return Response(result, status=status.HTTP_400_BAD_REQUEST) self.perform_destroy(instance) action_log(request=request, user=request.user, action_type=DELETE_ACTION_TYPE, old_instance=None, instance=None, action_info=f'删除用户:{instance.__str__()}') result = {'success': True, 'messages': f'删除用户:{instance.__str__()}'} return Response(result, status=status.HTTP_200_OK)
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) self.perform_create(serializer) headers = self.get_success_headers(serializer.data) instance = Announcement.objects.get(pk=int(serializer.data['id'])) # 操作日志记录 action_log(request=request, user=request.user, action_type=CREATE_ACTION_TYPE, old_instance=None, instance=instance, action_info=f'新增公告:{instance.__str__()}') result = { 'success': True, 'messages': f'新增公告:{instance.__str__()}!', 'results': serializer.data } return Response(result, status=status.HTTP_200_OK, headers=headers)
def publish(self, request, *args, **kwargs): """发布/下架""" old = self.get_object() instance = self.get_object() is_publish = request.data.get('publish', True) msg = '发布' action_type = ACTIVE_ACTION_TYPE if not is_publish: msg = '下架' action_type = DISABLE_ACTION_TYPE instance.is_publish = is_publish instance.save() # 操作日志记录 action_log(request, request.user, action_type, old_instance=old, instance=instance, action_info=f'{msg}公告:{instance.__str__()}') result = {'success': True, 'messages': f'{msg}公告:{instance.__str__()}'} return Response(result, status=status.HTTP_200_OK)
def update(self, request, *args, **kwargs): partial = kwargs.pop('partial', False) instance = self.get_object() old_instance = self.get_object() name = request.data.get('name', '') is_active = request.data.get('is_active', False) if name != instance.name and NotificationRule.objects.filter( name=name).first(): result = { 'success': False, 'messages': f'消息规则:{name} 已经存在, 不能修改成此名称' } return Response(result, status=status.HTTP_400_BAD_REQUEST) active_count = NotificationRule.objects.get_active().count() if not is_active and active_count <= 1: result = { 'success': False, 'messages': f'必须存在一个可用消息规则, 无法删除:{instance.__str__()}' } return Response(result, status=status.HTTP_400_BAD_REQUEST) serializer = self.get_serializer(instance, data=request.data, partial=partial) serializer.is_valid(raise_exception=True) self.perform_update(serializer) if getattr(instance, '_prefetched_objects_cache', None): instance._prefetched_objects_cache = {} # 操作日志记录 action_log(request=request, user=request.user, action_type=UPDATE_ACTION_TYPE, old_instance=old_instance, instance=instance, action_info=f'修改消息规则:{instance.__str__()}') result = { 'success': True, 'messages': f'修改消息规则:{instance.__str__()}!', 'results': serializer.data } return Response(result, status=status.HTTP_200_OK)
def activation(self, request, *args, **kwargs): """禁用/启用用户""" old_instance = self.get_object() instance = self.get_object() active = request.data.get('active', True) if active: instance.is_active = True instance.save() action_log(request=request, user=request.user, action_type=ACTIVE_ACTION_TYPE, old_instance=old_instance, instance=instance, action_info=f'启用用户:{instance.__str__()}') result = { 'success': True, 'messages': f'启用用户:{instance.__str__()}' } return Response(result, status=status.HTTP_200_OK) else: if instance == request.user: result = { 'success': False, 'messages': f'用户:{instance.__str__()}为当前账号,不能禁用自己!' } return Response(result, status=status.HTTP_400_BAD_REQUEST) instance.is_active = False instance.save() action_log(request=request, user=request.user, action_type=DISABLE_ACTION_TYPE, old_instance=old_instance, instance=instance, action_info=f'禁用用户:{instance.__str__()}') result = { 'success': True, 'messages': f'禁用用户:{instance.__str__()}' } return Response(result, status=status.HTTP_200_OK)
def update(self, request, *args, **kwargs): partial = kwargs.pop('partial', False) instance = self.get_object() old_instance = self.get_object() serializer = self.get_serializer(instance, data=request.data, partial=partial) serializer.is_valid(raise_exception=True) self.perform_update(serializer) if getattr(instance, '_prefetched_objects_cache', None): instance._prefetched_objects_cache = {} action_log(request=request, user=request.user, action_type=UPDATE_ACTION_TYPE, old_instance=old_instance, instance=instance, action_info=f'修改用户:{instance.__str__()}') result = { 'success': True, 'messages': f'修改用户:{instance.__str__()}!', 'results': serializer.data } return Response(result, status=status.HTTP_200_OK)