示例#1
0
文件: views.py 项目: kbairak/djsonapi
    def edit_one(cls, request, obj_id):
        body = get_body(request)
        schema = Object({'data': Object({
            'type': String(cls.TYPE),
            'id': String(),
            'attributes': Object({'slug': String(),
                                  'name': String()},
                                 minProperties=1),
        })})
        raise_for_body(body, schema)

        category = cls._get_category(obj_id)
        attributes = dict(body['data']['attributes'])

        if ('slug' in attributes and
                attributes['slug'] != category.slug and
                CategoryModel.objects.
                filter(slug=attributes['slug']).exists()):
            raise Conflict(f"Category with slug '{attributes['slug']}' "
                           f"already exists")

        for key, value in attributes.items():
            setattr(category, key, value)
        category.save()

        return category
示例#2
0
文件: views.py 项目: kbairak/djsonapi
    def create_one(cls, request):
        body = get_body(request)
        schema = Object({'data': Object({
            'type': String(cls.TYPE),
            'attributes': Object(
                {'username': String(),
                 'password': String(),
                 'first_name': String(),
                 'last_name': String()},
                required=['username', 'password'],
            )
        })})
        raise_for_body(body, schema)

        attributes = dict(body['data']['attributes'])
        if UserModel.objects.filter(username=attributes['username']).exists():
            raise Conflict(f"User with username '{attributes['username']}' "
                           f"already exists")

        password = attributes.pop('password')
        user = UserModel(**attributes)
        user.set_password(password)
        user.save()

        return user
示例#3
0
文件: views.py 项目: kbairak/djsonapi
    def change_author(cls, request, obj_id):
        body = get_body(request)
        schema = Object({'data': Object({'type': String(User.TYPE),
                                         'id': String()})})
        raise_for_body(body, schema)

        article = cls._get_article(obj_id)
        user = User._get_user(body['data']['id'])

        if user.id != article.author_id:
            article.author = user
            article.save()
示例#4
0
文件: views.py 项目: kbairak/djsonapi
    def create_one(cls, request):
        body = get_body(request)
        schema = Object({'data': Object({
            'type': String(cls.TYPE),
            'attributes': Object({'slug': String(), 'name': String()}),
        })})
        raise_for_body(body, schema)

        attributes = body['data']['attributes']
        slug = attributes['slug']
        if CategoryModel.objects.filter(slug=slug).exists():
            raise Conflict(f"Category with slug '{slug}' already exists")

        return CategoryModel.objects.create(**attributes)
示例#5
0
文件: views.py 项目: kbairak/djsonapi
    def _get_articles(cls, request, obj_id):
        body = get_body(request)
        schema = Object({'data': {
            'type': "array",
            'items': Object({'type': String(Article.TYPE), 'id': String()}),
        }})
        raise_for_body(body, schema)

        category = cls._get_category(obj_id)

        requested_ids = set((article['id'] for article in body['data']))
        articles = ArticleModel.objects.filter(id__in=requested_ids)
        found_ids = set((str(article.id) for article in articles))
        errors = [NotFound(f"Article with id '{article_id}' not found")
                  for article_id in requested_ids - found_ids]
        if errors:
            raise DjsonApiExceptionMulti(*errors)

        return category, articles
示例#6
0
文件: views.py 项目: kbairak/djsonapi
    def _get_categories(cls, request, obj_id):
        body = get_body(request)
        schema = Object({'data': {
            'type': "array",
            'items': Object({'type': String(Category.TYPE), 'id': String()}),
        }})
        raise_for_body(body, schema)

        article = cls._get_article(obj_id)

        requested_ids = set((category['id'] for category in body['data']))
        categories = CategoryModel.objects.filter(id__in=requested_ids)
        found_ids = set((str(category.id) for category in categories))
        errors = [NotFound(f"Category with id '{category_id}' not found")
                  for category_id in requested_ids - found_ids]
        if errors:
            raise DjsonApiExceptionMulti(*errors)

        return article, categories
示例#7
0
文件: views.py 项目: kbairak/djsonapi
    def create_one(cls, request):
        body = get_body(request)
        schema = Object({'data': Object({
            'type': String(cls.TYPE),
            'attributes': Object({'slug': String(),
                                  'title': String(),
                                  'content': String()},
                                 required=['title', 'content']),
            'relationships': Object({'author': Object({'data': Object({
                'type': String(User.TYPE),
                'id': String(),
            })})}),
        })})
        raise_for_body(body, schema)

        user_id = body['data']['relationships']['author']['data']['id']
        author = User._get_user(user_id)

        attributes = body['data']['attributes']
        if 'slug' in attributes:
            slug = attributes.pop('slug')
            if ArticleModel.objects.filter(slug=slug).exists():
                raise Conflict(f"Article with slug '{slug}' already exists")
        else:
            prefix = slugify(attributes['title'])
            existing_slugs = set(ArticleModel.objects.
                                 filter(slug__startswith=prefix).
                                 values_list('slug', flat=True))
            if prefix not in existing_slugs:
                slug = prefix
            else:
                for i in itertools.count(1):
                    slug = f"{prefix}-{i}"
                    if slug not in existing_slugs:
                        break
        try:
            article = ArticleModel.objects.\
                create(author=author, slug=slug, **attributes)
        except Exception as exc:
            raise Conflict(str(exc))

        return {'data': article, 'included': [User(author)]}
示例#8
0
文件: views.py 项目: kbairak/djsonapi
    def edit_one(cls, request, obj_id):
        body = get_body(request)

        schema = Object({'data': Object({
            'type': String(cls.TYPE),
            'id': String(pattern=r'^\d+$'),
            'attributes': Object({'username': String(),
                                  'first_name': String(),
                                  'last_name': String()},
                                 required=[],
                                 minProperties=1),
        })})
        raise_for_body(body, schema)

        user = cls._get_user(obj_id)

        for key, value in body['data']['attributes'].items():
            setattr(user, key, value)
        user.save()

        return user
示例#9
0
文件: views.py 项目: kbairak/djsonapi
    def edit_one(cls, request, obj_id):
        body = get_body(request)
        schema = Object({'data': Object(
            {
                'type': String(cls.TYPE),
                'id': String(),
                'attributes': Object({'slug': String(),
                                      'title': String(),
                                      'content': String()},
                                     required=[],
                                     minProperties=1),
                'relationships': Object({
                    'author': Object({
                        'data': Object({'type': String(User.TYPE),
                                        'id': String()}),
                    })
                }),
            },
            required=['type', 'id'],
            minProperties=3,
        )})
        raise_for_body(body, schema)

        article = cls._get_article(obj_id,
                                   ArticleModel.objects.
                                   select_related('author'))

        kwargs = dict(body['data'].get('attributes', {}))
        if 'relationships' in body['data']:
            user_id = body['data']['relationships']['author']['data']['id']
            if int(user_id) != article.author_id:
                kwargs['author'] = User._get_user(user_id)

        for key, value in kwargs.items():
            setattr(article, key, value)
        article.save()

        return {'data': article, 'included': [User(article.author)]}