Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finished setting up blog app #37

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

# Ignore the virtual environment
venv/
myenv/

# Ignore the Django secret key
secret_key.txt
Expand Down
Empty file added apps/blog/__init__.py
Empty file.
9 changes: 9 additions & 0 deletions apps/blog/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.contrib import admin
from apps.blog.models.post import Category, Tag, Post

# Register your models here.
admin.site.register(Category)
admin.site.register(Tag)
admin.site.register(Post)


6 changes: 6 additions & 0 deletions apps/blog/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class BlogConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'apps.blog'
Empty file.
37 changes: 37 additions & 0 deletions apps/blog/models/post.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from django.db import models
from django.contrib.auth.models import User
import uuid

from apps.users.models.user import User



class Tag(models.Model):
name = models.CharField(max_length=50, unique=True)

def __str__(self):
return self.name

class Category(models.Model):
name = models.CharField(max_length=100, unique=True)

def __str__(self):
return self.name

class Post(models.Model):
image = models.ImageField(upload_to='blog_images/')
title = models.CharField(max_length=200)
content = models.TextField()
author = models.ForeignKey(User, on_delete=models.CASCADE)
category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True)
tags = models.ManyToManyField(Tag, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
status = models.BooleanField(default=True)
post_id = models.UUIDField(primary_key=True, unique=True, editable=False)


def __str__(self):
return self.title
class Meta:
ordering = ['-created_at']
12 changes: 12 additions & 0 deletions apps/blog/routes/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.urls import path
from apps.blog.views import post
from apps.blog.views.post import (PostCreateView, PostDetailView, PostListView, UserProfileListView)

urlpatterns = [
path('creat-posts/', PostCreateView.as_view(), name = 'create-post'),
path('post/<int:pk>/', PostDetailView.as_view(), name = 'post-detail'),
path('posts', PostListView.as_view, name = 'posts'),
path('user/posts/', UserProfileListView.as_view(), name = 'user-posts'),


]
41 changes: 41 additions & 0 deletions apps/blog/serializers/post_serializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from django.utils.translation import gettext_lazy as _
from drf_spectacular.utils import extend_schema_field, OpenApiTypes
from rest_framework import serializers

from apps.blog.models.post import Post, Tag, Category


class TagSerializer(serializers.ModelSerializer):
class Meta:
model = Tag
fields = ("id", "name")

class CategorySerializer(serializers.ModelSerializer):
class Meta:
model = Category
fields = ("id", "name")


class PostSerializer(serializers.ModelSerializer):
Category = CategorySerializer()
Tag = TagSerializer(many =True)
author = serializers.StringRelatedField

class Meta:
model = Post
exclude = ("post_id", "image", "title", "content", "author", "category", "Tag")

def create (self, validated_data):
category_data = validated_data.pop('category')
tags_data = validated_data.pop('tag')
author = self.context['request'].user

category, _ = Category.objects.get_or_create(name = category_data['name'])
post = Post.objects.create(category = category, author=author, **validated_data)

for tag_data in tags_data:
tag,_ = Tag.objects.get_or_create(name = tag_data['name'])
post.tags.add(tag)

return post

3 changes: 3 additions & 0 deletions apps/blog/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
37 changes: 37 additions & 0 deletions apps/blog/views/post.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework import generics

from rest_framework import permissions

from apps.blog.models.post import Category, Tag, Post
from apps.blog.serializers.post_serializer import PostSerializer


class PostCreateView(generics.CreateAPIView):
queryset = Post.objects.all()
serializer_class = PostSerializer
permission_classes = [permissions.IsAuthenticated]

def perform_create(self, serializer):
serializer.save(user=self.request.user)

class PostListView(generics.ListCreateAPIView):
queryset = Post.objects.all()
serializer_class = PostSerializer
permission_classes = [permissions.IsAuthenticated]

class PostDetailView(generics.RetrieveUpdateDestroyAPIView):
queryset = Post.objects.all()
serializer_class = PostSerializer
permission_classes = [permissions.IsAuthenticated]


class UserProfileListView(generics.ListAPIView): #Lists all posts by a specific user
serializer_class = PostSerializer
pagination_class = [permissions.IsAuthenticated]

def get_queryset(self):
return Post.objects.filter(user=self.request.user)

2 changes: 2 additions & 0 deletions website_api/routes/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
path('__debug__/', include(debug_toolbar.urls)),
path(f"{BASE_API_URL}/", include("apps.users.routes.api")),
path(f"{BASE_API_URL}/", include("apps.events.routes.api")),
path(f"{BASE_API_URL}/", include("apps.blog.routes.api")),

path(f"{BASE_API_URL}/", include("apps.events.routes.extra")),
] + swagger_urlpatterns
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Expand Down
1 change: 1 addition & 0 deletions website_api/settings/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
CUSTOM_APPS = [
"apps.users",
"apps.events",
"apps.blog",
]