-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathapi_crud.py
127 lines (108 loc) · 4.39 KB
/
api_crud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from typing import Generic, List, Optional, Type, TypeVar
from unicodedata import category
from core.base_crud import SLUGTYPE, BaseCRUD
from core.utils import unique_slug_generator
from django.core.exceptions import ObjectDoesNotExist
from django.db.models import Model, Prefetch, query
from fastapi import Depends, HTTPException
from fastapi.encoders import jsonable_encoder
from blog.models import Category, Post
from blog.schemas import CreateCategory, CreatePost, UpdateCategory, UpdatePost
class PostCRUD(BaseCRUD[Post, CreatePost, UpdatePost, SLUGTYPE]):
"""
CRUD Operation for blog posts
"""
def get(self, slug: SLUGTYPE) -> Optional[Post]:
"""
Get single blog post.
"""
try:
query = Post.objects.select_related("user", "category").get(slug=slug)
return query
except ObjectDoesNotExist:
raise HTTPException(status_code=404, detail="This post does not exists.")
def get_multiple(self, limit:int = 100, offset: int = 0) -> List[Post]:
"""
Get multiple posts using a query limit and offset flag.
"""
query = Post.objects.select_related("user", "category").all()[offset:offset+limit]
if not query:
raise HTTPException(status_code=404, detail="There are no posts.")
return list(query)
def get_posts_by_category(self, slug: SLUGTYPE) -> List[Post]:
"""
Get all posts belonging to a particular category.
"""
query_category = Category.objects.filter(slug=slug)
if not query_category:
raise HTTPException(status_code=404, detail="This category does not exist.")
query = Post.objects.filter(category__slug=slug).select_related("user").all()
return list(query)
def create(self, obj_in: CreatePost) -> Post:
"""
Create an post.
"""
slug = unique_slug_generator(obj_in.title)
post = Post.objects.filter(slug=slug)
if not post:
slug = unique_slug_generator(obj_in.title, new_slug=True)
obj_in = jsonable_encoder(obj_in)
query = Post.objects.create(**obj_in)
return query
def update(self, obj_in: UpdatePost, slug: SLUGTYPE) -> Post:
"""
Update an item.
"""
self.get(slug=slug)
if not isinstance(obj_in, list):
obj_in = jsonable_encoder(obj_in)
return Post.objects.filter(slug=slug).update(**obj_in)
def delete(self, slug: SLUGTYPE) -> Post:
"""Delete an item."""
self.model.objects.filter(slug=slug).delete()
return {"detail": "Successfully deleted!"}
class CategoryCRUD(BaseCRUD[Category, CreateCategory, UpdateCategory, SLUGTYPE]):
"""
CRUD Operation for blog categories.
"""
def get(self, slug: SLUGTYPE) -> Optional[Category]:
"""
Get a single category.
"""
try:
query = Category.objects.get(slug=slug)
return query
except ObjectDoesNotExist:
raise HTTPException(status_code=404, detail="This post does not exists.")
def get_multiple(self, limit:int = 100, offset: int = 0) -> List[Category]:
"""
Get multiple categories using a query limiting flag.
"""
query = Category.objects.all()[offset:offset+limit]
if not query:
raise HTTPException(status_code=404, detail="There are no posts.")
return list(query)
def create(self, obj_in: CreateCategory) -> Category:
"""
Create a category.
"""
slug = unique_slug_generator(obj_in.title)
category = Category.objects.filter(slug=slug)
if category:
raise HTTPException(status_code=404, detail="Category exists already.")
obj_in = jsonable_encoder(obj_in)
query = Category.objects.create(**obj_in)
return query
def update(self, obj_in: UpdateCategory, slug: SLUGTYPE) -> Category:
"""
Update a category.
"""
if not isinstance(obj_in, list):
obj_in = jsonable_encoder(obj_in)
return self.model.objects.filter(slug=slug).update(**obj_in)
def delete(self, slug: SLUGTYPE) -> Post:
"""Delete a category."""
Post.objects.filter(slug=slug).delete()
return {"detail": "Successfully deleted!"}
post = PostCRUD(Post)
category = CategoryCRUD(Category)