Commit 681a307c authored by Luis García's avatar Luis García
Browse files

Update blog api

parent 434cb5d2
......@@ -4,4 +4,4 @@ from django.forms import ModelForm
class CommentForm(ModelForm):
class Meta:
model = Comment
fields = ['content', 'post', 'parent']
\ No newline at end of file
fields = ['content', 'post']
\ No newline at end of file
......@@ -25,10 +25,10 @@ class Post(models.Model):
content = RichTextUploadingField(verbose_name='Content')
image = models.ImageField(verbose_name="Image", upload_to='posts/%Y-%m/')
pin_up = models.BooleanField(verbose_name="Pin up", default=False)
comments = models.BooleanField(verbose_name="Comments", default=True)
enable_comments = models.BooleanField(verbose_name="Enable comments", default=True)
#order = models.IntegerField(verbose_name="Order", default=1)
owner = models.ForeignKey(User, verbose_name='Owner', on_delete=models.CASCADE, related_name='Posts')
category = models.ForeignKey(Category, verbose_name='Category', on_delete=models.CASCADE, related_name='Posts')
owner = models.ForeignKey(User, verbose_name='Owner', on_delete=models.CASCADE, related_name='posts')
category = models.ForeignKey(Category, verbose_name='Category', on_delete=models.CASCADE, related_name='posts')
created = models.DateTimeField(auto_now_add=True, verbose_name='Created date')
updated = models.DateTimeField(auto_now=True, verbose_name='Updated date')
visible = models.BooleanField(verbose_name='visible', default=True)
......@@ -46,8 +46,7 @@ class Comment(models.Model):
content = models.TextField(verbose_name="Content")
username = models.CharField(verbose_name='User name', max_length=50)
email = models.EmailField(verbose_name="Email")
post = models.ForeignKey(Post, verbose_name="Post", on_delete=models.PROTECT, related_name='Comments')
parent = models.ForeignKey('self', verbose_name="Parent", on_delete=models.CASCADE, related_name='Replies', blank=True, null=True)
post = models.ForeignKey(Post, verbose_name="Post", on_delete=models.PROTECT, related_name='comments')
created = models.DateTimeField(auto_now_add=True, verbose_name='Created date')
class Meta():
......@@ -57,6 +56,20 @@ class Comment(models.Model):
def __str__(self):
return self.content
class Reply(models.Model):
content = models.TextField(verbose_name="Content")
username = models.CharField(verbose_name='User name', max_length=50)
email = models.EmailField(verbose_name="Email")
comment = models.ForeignKey(Comment, verbose_name="Comment", on_delete=models.CASCADE, related_name='replies')
created = models.DateTimeField(auto_now_add=True, verbose_name='Created date')
class Meta():
verbose_name = 'Reply'
verbose_name_plural = 'Replies'
def __str__(self):
return self.content
# Model for Tag
class Tag(models.Model):
name = models.CharField(verbose_name='Name', max_length=50, unique=True)
......@@ -72,8 +85,8 @@ class Tag(models.Model):
# Model for Post Tag
class PostTag(models.Model):
post = models.ForeignKey(Post, verbose_name="Post", on_delete=models.PROTECT, related_name='Tags')
tag = models.ForeignKey(Tag, verbose_name="Tag", on_delete=models.CASCADE, related_name='Posts')
post = models.ForeignKey(Post, verbose_name="Post", on_delete=models.PROTECT, related_name='tags')
tag = models.ForeignKey(Tag, verbose_name="Tag", on_delete=models.CASCADE, related_name='posts')
class Meta():
unique_together = (('post', 'tag'),)
......
from rest_framework.serializers import ModelSerializer
from api.utils import AsymetricRelatedField
from core.serializers import UserSerializer
from .models import Post, Comment
from .models import Post, Comment, Reply, Category, Tag, PostTag
class CategorySerializer(ModelSerializer):
class Meta:
model = Category
fields = ('id', 'name', 'description')
class PostSerializer(ModelSerializer):
category = AsymetricRelatedField.from_serializer(CategorySerializer)()
owner = AsymetricRelatedField.from_serializer(UserSerializer)()
class Meta:
model = Post
fields = ('id', 'slug', 'title', 'first_paragraph', 'content', 'image', 'pin_up', 'order', 'owner', 'created')
fields = ('id', 'slug', 'title', 'first_paragraph', 'content', 'image', 'pin_up', 'enable_comments', 'owner', 'category', 'created')
class CommentSerializer(ModelSerializer):
class Meta:
model = Comment
fields = ('id', 'content', 'username', 'email', 'post', 'created')
\ No newline at end of file
fields = ('id', 'content', 'username', 'email', 'post', 'created')
class ReplySerializer(ModelSerializer):
class Meta:
model = Reply
fields = ('id', 'content', 'username', 'email', 'Comment', 'created')
class TagSerializer(ModelSerializer):
class Meta:
model = Tag
fields = ('id', 'name', 'description', 'created')
class PostTagSerializer(ModelSerializer):
class Meta:
model = PostTag
fields = ('id', 'post', 'tag')
\ No newline at end of file
from django.urls import path
from rest_framework_extensions.routers import ExtendedSimpleRouter
from .views import PostListView, PostCreateView, PostDetailView, PostUpdateView, PostViewSet, CommentViewSet
from rest_framework.routers import DefaultRouter
#Blog views
from .views import PostListView, PostCreateView, PostDetailView, PostUpdateView
#Api blog views
from .views import PostViewSet, CommentViewSet, ReplyViewSet, CategoryViewSet, TagViewSet, PostTagViewSet
blog_patterns = ([
#Views for blog
......@@ -11,10 +14,12 @@ blog_patterns = ([
], 'blog')
#views for blog api
router = ExtendedSimpleRouter()
router.register(r'posts', PostViewSet, basename='posts').register(r'comments',
CommentViewSet,
'comment',
parents_query_lookups=['post'])
router = DefaultRouter()
router.register(r'posts', PostViewSet, basename='posts')
router.register(r'comments', CommentViewSet, basename='comments')
router.register(r'replies', ReplyViewSet, basename='replies')
router.register(r'categories', CategoryViewSet, basename='categories')
router.register(r'tags', TagViewSet, basename='tags')
router.register(r'post-tags', PostTagViewSet, basename='ost-tags')
blog_api_patterns = router.urls
\ No newline at end of file
from .aplication import PostListView, PostCreateView, PostDetailView, PostUpdateView
from .api import PostViewSet, CommentViewSet
\ No newline at end of file
from .api import PostViewSet, CommentViewSet, ReplyViewSet, CategoryViewSet, TagViewSet, PostTagViewSet
\ No newline at end of file
......@@ -2,21 +2,21 @@ from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.viewsets import ModelViewSet
from rest_framework.permissions import IsAuthenticated
from rest_framework_extensions.mixins import NestedViewSetMixin
from api.permissions import IsSuperOrReadOnly, IsAuthOrReadOnly
from blog.models import Post, Comment
from blog.serializers import PostSerializer, CommentSerializer
from api.permissions import IsSuperOrReadOnly, IsAuthOrReadOnly, IsStaffOrReadOnly
from blog.models import Post, Comment, Reply, Category, Tag, PostTag
from blog.serializers import PostSerializer, CommentSerializer, ReplySerializer, CategorySerializer, TagSerializer, PostTagSerializer
############################################################
#REST API for Post
############################################################
class PostViewSet(NestedViewSetMixin, ModelViewSet):
#Everyone can read, just staff user can modificate
permission_classes = (IsSuperOrReadOnly,)
queryset = Post.objects.all()
permission_classes = (IsStaffOrReadOnly,)
queryset = Post.objects.filter(visible=True)
serializer_class = PostSerializer
#Enable filter by code
filter_backends = (DjangoFilterBackend,)
filterset_fields = ('slug',)
filterset_fields = ('slug','category','tags__tag')
############################################################
......@@ -26,16 +26,68 @@ class CommentViewSet(NestedViewSetMixin, ModelViewSet):
permission_classes = (IsAuthOrReadOnly, )
queryset = Comment.objects.all()
serializer_class = CommentSerializer
#Enable filter by code
#Enable filter by comment and username
filter_backends = (DjangoFilterBackend,)
filterset_fields = ('post', 'username')
def create(self, request, *args, **kwargs):
#Set the username and email by default if the user is authenticated
user = request.user
if user.is_authenticated:
request.data['username'] = user.username
request.data['email'] = user.email
return super(CommentViewSet, self).create(request, *args, **kwargs)
############################################################
#REST API for Reply
############################################################
class ReplyViewSet(NestedViewSetMixin, ModelViewSet):
permission_classes = (IsAuthOrReadOnly, )
queryset = Reply.objects.all()
serializer_class = ReplySerializer
#Enable filter by comment and username
filter_backends = (DjangoFilterBackend,)
filterset_fields = ('post',)
filterset_fields = ('comment', 'username')
def create(self, request, *args, **kwargs):
if 'parent_lookup_id' in kwargs:
request.data['post'] = kwargs['parent_lookup_id']
#Set the username and email by default if the user is authenticated
user = request.user
if user.is_authenticated:
request.data['username'] = user.username
request.data['email'] = user.email
return super(CommentViewSet, self).create(request, *args, **kwargs)
\ No newline at end of file
return super(ReplyViewSet, self).create(request, *args, **kwargs)
############################################################
#REST API for Category
############################################################
class CategoryViewSet(NestedViewSetMixin, ModelViewSet):
permission_classes = (IsStaffOrReadOnly, )
queryset = Category.objects.filter(visible=True)
serializer_class = CategorySerializer
#Enable filter by name
filter_backends = (DjangoFilterBackend,)
filterset_fields = ('name', )
############################################################
#REST API for Tag
############################################################
class TagViewSet(NestedViewSetMixin, ModelViewSet):
permission_classes = (IsStaffOrReadOnly, )
queryset = Tag.objects.all()
serializer_class = TagSerializer
#Enable filter by name
filter_backends = (DjangoFilterBackend,)
filterset_fields = ('name', )
############################################################
#REST API for PostTag
############################################################
class PostTagViewSet(NestedViewSetMixin, ModelViewSet):
permission_classes = (IsStaffOrReadOnly, )
queryset = PostTag.objects.all()
serializer_class = PostTagSerializer
#Enable filter by name
filter_backends = (DjangoFilterBackend,)
filterset_fields = ('post', 'post')
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment