示例#1
0
文件: base.py 项目: lowfang/dev_api
 def patch_view(self, request, *args, **kwargs):
     '''
     patch请求用与更新部分字段
     :param request:
     :param args:
     :param kwargs:
     :return: 更新后的字段
     '''
     self.process_update_request(request, *args, **kwargs)
     with transaction.atomic():
         self.patch_view_before(request, *args, **kwargs)
         assert self.form_data.get('id') == self.id and self.id, {
             'message': '传入参数不正确!',
             'status': 412
         }
         query_dict = model_to_dict_plus(self.queryset, None, None, None,
                                         True)
         query_dict.update(self.form_data)
         try:
             new_obj = self.model(**query_dict)
             new_obj.save()
         except ValueError as e:
             raise Exception({'message': '保存资源出错', 'status': 500})
         except IntegrityError as e:
             error = e.args[0] if e.args else ''
             raise Exception({
                 'message': '违反唯一性约束,原始错误信息为:{}'.format(error),
                 'status': 409
             })
         result = model_to_dict_plus(new_obj)
         return result
示例#2
0
 def get_view(self, request, *args, **kwargs):
     '''
     get请求用于获取资源详细信息
     :param request:
     :param args:
     :param kwargs:
     :return: 资源详细信息,对外键引用进行了处理
     '''
     self.process_get_request(request, *args, **kwargs)
     try:
         queryset = self.model.objects.get(id=self.id)
     except ObjectDoesNotExist:
         raise Exception({
             'message': '没有查询到ID为{}的记录'.format(self.id),
             'status': 400
         })
     result = model_to_dict_plus(queryset, self.fields, self.exclude,
                                 self.timezone)
     flow_list = []
     flow_obj = TaskFlow.objects.filter(
         task=queryset).order_by('create_date').select_related('user')
     for item in flow_obj:
         flow = model_to_dict_plus(item, None, None, self.timezone)
         flow_list.append(flow)
     return {'task': result, 'flow': flow_list}
示例#3
0
    def patch_view_before(self, request, *args, **kwargs):
        params = request.data
        action = params.get('action')
        assert action in ['success', 'back', 'close'], {
            'message': '不支持的action参数',
            'status': 412
        }
        note = params.get('notes')

        error_msg = {'message': '不正确的任务状态', 'status': 412}
        if action == 'success':
            assert self.queryset.status in [2, 4], error_msg
            status = 5
        elif action == 'back':
            assert self.queryset.status in [2, 4], error_msg
            status = 3
        elif action == 'close':
            assert self.queryset.status in [5], error_msg
            status = 6
        else:
            raise Exception({'message': '不支持的action参数', 'status': 412})

        with transaction.atomic():
            self.form_data = {'id': self.queryset.id, 'status': status}
            create_task_flow(self.user.id, self.queryset, note, action)
            result = model_to_dict_plus(self.queryset, None, None,
                                        self.timezone)
            return result
示例#4
0
 def post_view(self, request, *args, **kwargs):
     post_params = request.data
     form_data = post_params.get("form_data")
     with transaction.atomic():
         task = self.model.objects.create(**form_data)
         create_task_flow(self.user.id, task, None, 'created')
         result = model_to_dict_plus(task)
         return result
示例#5
0
    def patch_view_before(self, request, *args, **kwargs):
        params = request.data
        action = params.get("action")

        action_msg = {
            'message': '不支持的请求参数,match请求action必须为"submit"或者"cancel"或者"update"',
            'status': 412
        }
        assert action in ['submit', 'cancel', 'update', 'transfer'], action_msg

        status = self.queryset.status
        form_data = {'id': kwargs.get('id')}
        if action == 'submit':
            assert status in [1, 3], {"message": '任务状态不正确', 'status': 412}
            assert self.queryset.executor_id == self.user.id, {
                "message": "只能把自己的任务提交到审核",
                'status': 403
            }
            form_data.update({'status': self.queryset.status + 1})
        elif action == 'cancel':
            assert status in [2, 4], {
                'message': '只有待审批状态才可以撤销审核',
                'status': 412
            }
            assert self.queryset.executor_id == self.user.id, {
                'message': '只有任务执行本人才可以撤销审核',
                'status': 403
            }
            form_data.update({'status': status - 1})
        elif action == 'update':
            assert self.queryset.leader == self.user, {
                'message': '只有组长才能更改预期结束时间',
                'status': 403
            }
            try:
                end_date = parse(params.get('form_data', {}).get('end_date'))
            except:
                raise Exception({'message': '请求中时间格式不正确', 'status': 412})
            form_data.update({'end_date': end_date})
        elif action == 'transfer':
            assert self.queryset.leader == self.user, {
                'message': '只有组长才能更改预期结束时间',
                'status': 403
            }
            form_data.update({
                'executor_id':
                params.get('form_data', {}).get('executor_id')
            })
        # 把要更改的值写入到form_data中
        self.form_data = form_data
        create_task_flow(self.user.id, self.queryset, None, action)
        result = model_to_dict_plus(self.queryset, None, None, self.timezone)
        return result
示例#6
0
文件: base.py 项目: lowfang/dev_api
 def get_view(self, request, *args, **kwargs):
     '''
     get请求用于获取资源详细信息
     :param request:
     :param args:
     :param kwargs:
     :return: 资源详细信息,对外键引用进行了处理
     '''
     self.process_get_request(request, *args, **kwargs)
     try:
         self.queryset = self.model.objects.get(id=self.id)
     except ObjectDoesNotExist:
         raise Exception({
             'message': '没有查询到ID为{}的记录'.format(self.id),
             'status': 400
         })
     result = model_to_dict_plus(self.queryset, self.fields, self.exclude,
                                 self.timezone)
     return result
示例#7
0
文件: base.py 项目: lowfang/dev_api
 def get_view(self, request, *args, **kwargs):
     '''
     获取单表的数据
     :param request:默认
     :param args: 无
     :param kwargs: 无
     :return:{'count':数据数量,'data':[{数据详细信息},{},{}...]}
     '''
     self.process_request(request, *args, **kwargs)
     self.get_view_before(request, *args, **kwargs)
     contact_list = self.get_queryset()
     paginator = Paginator(contact_list, self.page_size)
     contacts = paginator.get_page(self.page)
     data = []
     for item in contacts.object_list:
         item_dict = model_to_dict_plus(item, self.fields, self.exclude,
                                        self.timezone)
         data.append(item_dict)
     result = {'data': data, 'count': contacts.paginator.count}
     return result
示例#8
0
文件: base.py 项目: lowfang/dev_api
 def post_view(self, request, *args, **kwargs):
     '''
     post方法,默认创建资源
     :param request:
     :param args:
     :param kwargs:
     :return: 返回创建后的资源对象
     '''
     post_params = request.data
     form_data = post_params.get("form_data")
     if not isinstance(form_data, dict):
         raise Exception({
             'message': '该post请求参数必须包含form_data而且值必须为json对象!',
             'status': 412
         })
     form_data['create_user_id'] = self.user.id
     form_data['write_user_id'] = self.user.id
     with transaction.atomic():
         task = self.model.objects.create(**form_data)
         result = model_to_dict_plus(task)
         return result
示例#9
0
文件: base.py 项目: lowfang/dev_api
    def put_view(self, request, *args, **kwargs):
        '''
        PUT请求用具更新所有字段
        :param request:
        :param args:
        :param kwargs:
        :return:更新后的字段
        '''

        self.process_update_request(request, *args, **kwargs)
        with transaction.atomic():
            self.put_view_before(request, *args, **kwargs)
            assert self.form_data.get('id') == self.id and self.id, {
                'message': '传入参数不正确!',
                'status': 412
            }
            try:
                new_obj = self.model(**self.form_data)
                new_obj.save()
            except ValueError as e:
                raise Exception({'message': '保存资源出错', 'status': 500})

            result = model_to_dict_plus(new_obj)
            return result