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
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
def delete_one(cls, request, obj_id): count, _ = ArticleModel.objects.filter(id=obj_id).delete() if count == 0: raise NotFound(f"Article with id '{obj_id}' not found")
def _get_category(cls, obj_id): try: return CategoryModel.objects.get(id=obj_id) except CategoryModel.DoesNotExist: raise NotFound(f"Category with id '{obj_id}' not found")
def _raise_not_found(cls, obj_id): raise NotFound(f"User with id '{obj_id}' not found")
def _get_article(cls, obj_id, queryset=ArticleModel.objects): try: return queryset.get(id=obj_id) except ArticleModel.DoesNotExist: raise NotFound(f"Article with id '{obj_id}' not found")