Skip to content

Commit

Permalink
Move blog app into apps (#35)
Browse files Browse the repository at this point in the history
* Add blog app and document API endpoints

* blog app added

* blog app added

* moved blog into apps

* fixes

* .venv

* refactoring blog app

* blog urls and ins

* adding missing routes

* models inheriting from BaseModel

* added fileds to serializers

* renamed file correctly

* migrations

---------

Co-authored-by: saahndongransom <saahndongransom@gmail.com>
  • Loading branch information
JohananOppongAmoateng and saahndongransom authored Oct 5, 2024
1 parent 2a539e6 commit 7f68f89
Show file tree
Hide file tree
Showing 45 changed files with 659 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ DB_HOST='localhost'
DB_NAME='django_cameroon'
DB_USER=''
DB_PASSWORD=''
DB_PORT='3306'
DB_PORT='5432'

# Email
EMAIL_HOST='smtp.gmail.com'
Expand Down
1 change: 1 addition & 0 deletions .github/PULL_REQUEST_TEMPLATE/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ Closes #23
## Screenshots

(Insert image, only for frontend)
Bearer d855f89533088ce049754df15432f411
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/
.venv

# Ignore the Django secret key
secret_key.txt
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Assuming postgresql is installed in your computer, follow what's next:
sudo -u postgres psql
```
- ```bash
-- Create a database
CREATE DATABASE django_website_db;
Expand Down
Empty file added apps/blog/__init__.py
Empty file.
12 changes: 12 additions & 0 deletions apps/blog/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.contrib import admin
from apps.blog.models.tag import Tag
from apps.blog.models.category import Category
from apps.blog.models.author import Author
from apps.blog.models.blog import Blog
from apps.blog.models.image import Image

admin.site.register(Tag)
admin.site.register(Category)
admin.site.register(Author)
admin.site.register(Blog)
admin.site.register(Image)
16 changes: 16 additions & 0 deletions apps/blog/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.apps import AppConfig


# class BlogConfig(AppConfig):
# default_auto_field = 'django.db.models.BigAutoField'
# name = 'blog'



class BlogConfig(AppConfig):
name = 'apps.blog'
default_auto_field = 'django.db.models.BigAutoField'

def ready(self):
import apps.blog.signals

59 changes: 59 additions & 0 deletions apps/blog/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Generated by Django 5.0.1 on 2024-06-05 23:50

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Author',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('bio', models.TextField()),
],
),
migrations.CreateModel(
name='Category',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
],
),
migrations.CreateModel(
name='Tag',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
],
),
migrations.CreateModel(
name='Blog',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=200)),
('content', models.TextField()),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='blog.author')),
('categories', models.ManyToManyField(to='blog.category')),
('tags', models.ManyToManyField(to='blog.tag')),
],
),
migrations.CreateModel(
name='Image',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('image_file', models.ImageField(upload_to='images/')),
('uploaded_at', models.DateTimeField(auto_now_add=True)),
('blog_post', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='images', to='blog.blog')),
],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# Generated by Django 5.0.1 on 2024-10-04 08:17

import django.db.models.deletion
import django.utils.timezone
import utils.main
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('blog', '0001_initial'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.AddField(
model_name='author',
name='active',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='author',
name='created_at',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='author',
name='created_by',
field=models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='created_%(class)s_set', to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='author',
name='updated_at',
field=models.DateTimeField(auto_now=True),
),
migrations.AddField(
model_name='author',
name='updated_by',
field=models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='updated_%(class)s_set', to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='blog',
name='active',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='blog',
name='created_by',
field=models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='created_%(class)s_set', to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='blog',
name='updated_by',
field=models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='updated_%(class)s_set', to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='category',
name='active',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='category',
name='created_at',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='category',
name='created_by',
field=models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='created_%(class)s_set', to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='category',
name='updated_at',
field=models.DateTimeField(auto_now=True),
),
migrations.AddField(
model_name='category',
name='updated_by',
field=models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='updated_%(class)s_set', to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='image',
name='active',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='image',
name='created_at',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='image',
name='created_by',
field=models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='created_%(class)s_set', to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='image',
name='updated_at',
field=models.DateTimeField(auto_now=True),
),
migrations.AddField(
model_name='image',
name='updated_by',
field=models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='updated_%(class)s_set', to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='tag',
name='active',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='tag',
name='created_at',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='tag',
name='created_by',
field=models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='created_%(class)s_set', to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='tag',
name='updated_at',
field=models.DateTimeField(auto_now=True),
),
migrations.AddField(
model_name='tag',
name='updated_by',
field=models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='updated_%(class)s_set', to=settings.AUTH_USER_MODEL),
),
migrations.AlterField(
model_name='author',
name='id',
field=models.UUIDField(default=utils.main.generate_uuid, editable=False, help_text='Unique identifier for this object', primary_key=True, serialize=False),
),
migrations.AlterField(
model_name='blog',
name='id',
field=models.UUIDField(default=utils.main.generate_uuid, editable=False, help_text='Unique identifier for this object', primary_key=True, serialize=False),
),
migrations.AlterField(
model_name='category',
name='id',
field=models.UUIDField(default=utils.main.generate_uuid, editable=False, help_text='Unique identifier for this object', primary_key=True, serialize=False),
),
migrations.AlterField(
model_name='image',
name='id',
field=models.UUIDField(default=utils.main.generate_uuid, editable=False, help_text='Unique identifier for this object', primary_key=True, serialize=False),
),
migrations.AlterField(
model_name='tag',
name='id',
field=models.UUIDField(default=utils.main.generate_uuid, editable=False, help_text='Unique identifier for this object', primary_key=True, serialize=False),
),
]
Empty file.
Empty file added apps/blog/models/__init__.py
Empty file.
13 changes: 13 additions & 0 deletions apps/blog/models/author.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.db import models
from apps.users.models.base_model import BaseModel



class Author(BaseModel):
name = models.CharField(max_length=100)
bio = models.TextField()

def __str__(self):
return self.name


20 changes: 20 additions & 0 deletions apps/blog/models/blog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django.db import models
from apps.users.models.base_model import BaseModel
from apps.blog.models.author import Author
from apps.blog.models.tag import Tag
from apps.blog.models.category import Category

class Blog(BaseModel):
title = models.CharField(max_length=200)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
categories = models.ManyToManyField(Category)
tags = models.ManyToManyField(Tag)

def __str__(self):
return self.title



10 changes: 10 additions & 0 deletions apps/blog/models/category.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.db import models
from apps.users.models.base_model import BaseModel

class Category(BaseModel):
name = models.CharField(max_length=100)

def __str__(self):
return self.name


12 changes: 12 additions & 0 deletions apps/blog/models/image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.db import models
from apps.users.models.base_model import BaseModel
from apps.blog.models.blog import Blog

class Image(BaseModel):
image_file = models.ImageField(upload_to='images/')
uploaded_at = models.DateTimeField(auto_now_add=True)
blog_post = models.ForeignKey(Blog, related_name='images', on_delete=models.CASCADE)

def __str__(self):
return f"Image for {self.blog_post.title}"

9 changes: 9 additions & 0 deletions apps/blog/models/tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.db import models
from apps.users.models.base_model import BaseModel

class Tag(BaseModel):
name = models.CharField(max_length=100)

def __str__(self):
return self.name

8 changes: 8 additions & 0 deletions apps/blog/permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

from rest_framework.permissions import BasePermission, SAFE_METHODS

class IsAuthorOrReadOnly(BasePermission):
def has_object_permission(self, request, view, obj):
if request.method in SAFE_METHODS:
return True
return obj.author == request.user
Empty file added apps/blog/routes/__init__.py
Empty file.
21 changes: 21 additions & 0 deletions apps/blog/routes/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from django.urls import path
from apps.blog.views.post import PostDetail, PostList
from apps.blog.views.blog import BlogListCreateView
from apps.blog.views.author import AuthorListView
from apps.blog.views.category import CategoryListView
from apps.blog.views.image import ImageListView, ImageCreateView
from apps.blog.views.tag import TagListView
from apps.blog.views.index import index


urlpatterns = [
path('posts/', PostList.as_view(), name='post-list'),
path('posts/<int:pk>/', PostDetail.as_view(), name='post-detail'),
path('posts/create/', BlogListCreateView.as_view(), name='post-create'),
path('authors/', AuthorListView.as_view(), name='author-list'),
path('categories/', CategoryListView.as_view(), name='category-list'),
path('images/', ImageListView.as_view(), name='image-list'),
path('images/create/', ImageCreateView.as_view(), name='image-create'),
path('tags/', TagListView.as_view(), name='tag-list'),
path('', index, name='index'),
]
Empty file.
8 changes: 8 additions & 0 deletions apps/blog/serializers/author_serializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from rest_framework import serializers
from apps.blog.models.author import Author


class AuthorSerializer(serializers.ModelSerializer):
class Meta:
model = Author
fields = ('id', 'name', 'bio')
Loading

0 comments on commit 7f68f89

Please sign in to comment.