示例#1
0
    def patch(self, request, *args, **kwargs):  # pylint: disable=unused-argument
        '''
        调整子部门 [PATCH]
        目前应只需要排序和移入
        '''
        parent_dept = self.get_object()
        data = json.loads(request.body.decode('utf-8'))
        subject = data.get('subject', '')
        if subject not in ['sort', 'add']:
            raise ValidationError(
                {'subject': ['this field must be `sort` or `add`']})

        filters = {}
        if subject == 'sort':
            filters = {'parent': parent_dept}

        dept_uids = get_patch_scope(request)
        try:
            depts = Dept.get_from_pks(pks=dept_uids,
                                      pk_name='uid',
                                      raise_exception=True,
                                      **filters)
        except ObjectDoesNotExist as error:
            bad_uid = error.args[0]
            raise ValidationError(
                {'dept_uids': ['dept:{} invalid'.format(bad_uid)]})

        cli = CLI()
        if subject == 'sort':
            cli.sort_depts_in_dept(depts, parent_dept)
        elif subject == 'add':
            for dept in depts:
                cli.move_dept_to_dept(dept, parent_dept)

        return Response(DeptListSerializer(parent_dept).data)
示例#2
0
 def update(self, request, *args, **kwargs):    # pylint: disable=unused-argument
     '''
     update user depts
     '''
     user = self.get_object()
     data = request.data
     uids = data.get('dept_uids', [])
     subject = data.get('subject', '')
     update_user_nodes(user, nodes=[], uids=uids, node_type='dept', action_subject=subject)
     return Response(DeptListSerializer(self.get_object()).data)