From cb46c8b3afacaf42dd0711e1703fca1dbaa1b1ae Mon Sep 17 00:00:00 2001 From: Riad Elimemmedov Date: Wed, 27 Mar 2024 01:10:47 +0400 Subject: [PATCH] Created category,posts,comment table --- backend/apps/posts/__init__.py | 0 backend/apps/posts/admin.py | 26 +++ backend/apps/posts/apps.py | 6 + backend/apps/posts/migrations/0001_initial.py | 196 ++++++++++++++++++ backend/apps/posts/migrations/__init__.py | 0 backend/apps/posts/models.py | 88 ++++++++ backend/apps/posts/tests.py | 3 + backend/apps/posts/views.py | 3 + backend/config/base.py | 2 +- 9 files changed, 323 insertions(+), 1 deletion(-) create mode 100644 backend/apps/posts/__init__.py create mode 100644 backend/apps/posts/admin.py create mode 100644 backend/apps/posts/apps.py create mode 100644 backend/apps/posts/migrations/0001_initial.py create mode 100644 backend/apps/posts/migrations/__init__.py create mode 100644 backend/apps/posts/models.py create mode 100644 backend/apps/posts/tests.py create mode 100644 backend/apps/posts/views.py diff --git a/backend/apps/posts/__init__.py b/backend/apps/posts/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/apps/posts/admin.py b/backend/apps/posts/admin.py new file mode 100644 index 0000000..d302da3 --- /dev/null +++ b/backend/apps/posts/admin.py @@ -0,0 +1,26 @@ +from django.contrib import admin + +from .models import Category, Comment, Post + +# Register your models here. + + +# !CategoryAdmin +@admin.register(Category) +class CategoryAdmin(admin.ModelAdmin): + list_display = ["id", "name", "slug", "created", "modified"] + list_display_links = ["id", "name"] + + +# !PostAdmin +@admin.register(Post) +class PostAdmin(admin.ModelAdmin): + list_display = ["id", "title", "slug", "created", "modified"] + list_display_links = ["id", "title"] + + +# !CommentAdmin +@admin.register(Comment) +class CommentAdmin(admin.ModelAdmin): + list_display = ["id", "created", "modified"] + list_display_links = ["id"] diff --git a/backend/apps/posts/apps.py b/backend/apps/posts/apps.py new file mode 100644 index 0000000..d7c011b --- /dev/null +++ b/backend/apps/posts/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class PostsConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "apps.posts" diff --git a/backend/apps/posts/migrations/0001_initial.py b/backend/apps/posts/migrations/0001_initial.py new file mode 100644 index 0000000..5adcd02 --- /dev/null +++ b/backend/apps/posts/migrations/0001_initial.py @@ -0,0 +1,196 @@ +# Generated by Django 5.0.1 on 2024-03-26 21:04 + +import django.core.validators +import django.db.models.deletion +import django_extensions.db.fields +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name="Category", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "created", + django_extensions.db.fields.CreationDateTimeField( + auto_now_add=True, verbose_name="created" + ), + ), + ( + "modified", + django_extensions.db.fields.ModificationDateTimeField( + auto_now=True, verbose_name="modified" + ), + ), + ( + "name", + models.CharField(max_length=100, verbose_name="Category name"), + ), + ( + "slug", + django_extensions.db.fields.AutoSlugField( + blank=True, + editable=False, + max_length=100, + populate_from="name", + unique=True, + ), + ), + ], + options={ + "verbose_name": "Category", + "verbose_name_plural": "Categories", + }, + ), + migrations.CreateModel( + name="Post", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "created", + django_extensions.db.fields.CreationDateTimeField( + auto_now_add=True, verbose_name="created" + ), + ), + ( + "modified", + django_extensions.db.fields.ModificationDateTimeField( + auto_now=True, verbose_name="modified" + ), + ), + ("title", models.CharField(max_length=250, verbose_name="Post title")), + ( + "slug", + django_extensions.db.fields.AutoSlugField( + blank=True, + editable=False, + max_length=100, + populate_from="title", + unique=True, + ), + ), + ( + "post_photo_url", + models.FileField( + blank=True, + upload_to="", + validators=[ + django.core.validators.FileExtensionValidator( + allowed_extensions=["png", "jpg", "jpeg"] + ) + ], + verbose_name="Post photo", + ), + ), + ("body", models.TextField(verbose_name="Post body")), + ( + "author", + models.ForeignKey( + null=True, + on_delete=django.db.models.deletion.SET_NULL, + related_name="posts_author", + to=settings.AUTH_USER_MODEL, + verbose_name="Author post", + ), + ), + ( + "categories", + models.ManyToManyField( + blank=True, + related_name="posts_categories", + to="posts.category", + verbose_name="Categories", + ), + ), + ( + "likes", + models.ManyToManyField( + blank=True, + related_name="post_likes", + to=settings.AUTH_USER_MODEL, + verbose_name="Likes", + ), + ), + ], + options={ + "verbose_name": "Post", + "verbose_name_plural": "Posts", + }, + ), + migrations.CreateModel( + name="Comment", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "created", + django_extensions.db.fields.CreationDateTimeField( + auto_now_add=True, verbose_name="created" + ), + ), + ( + "modified", + django_extensions.db.fields.ModificationDateTimeField( + auto_now=True, verbose_name="modified" + ), + ), + ("body", models.TextField(verbose_name="Comment body")), + ( + "author", + models.ForeignKey( + null=True, + on_delete=django.db.models.deletion.SET_NULL, + related_name="comment_author", + to=settings.AUTH_USER_MODEL, + verbose_name="Author comment", + ), + ), + ( + "post", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="comment_post", + to="posts.post", + verbose_name="Post", + ), + ), + ], + options={ + "verbose_name": "Comment", + "verbose_name_plural": "Comments", + }, + ), + ] diff --git a/backend/apps/posts/migrations/__init__.py b/backend/apps/posts/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/apps/posts/models.py b/backend/apps/posts/models.py new file mode 100644 index 0000000..68ad9e0 --- /dev/null +++ b/backend/apps/posts/models.py @@ -0,0 +1,88 @@ +from django.conf import settings +from django.core.validators import FileExtensionValidator +from django.db import models +from django.utils.translation import gettext_lazy as _ +from django_extensions.db.fields import AutoSlugField, SlugField +from django_extensions.db.models import TimeStampedModel + +# Create your models here. + + +# !Category +class Category(TimeStampedModel): + name = models.CharField(_("Category name"), max_length=100) + slug = AutoSlugField( + max_length=100, unique=True, db_index=True, populate_from="name" + ) + + class Meta: + verbose_name = _("Category") + verbose_name_plural = _("Categories") + + def __str__(self): + return self.name + + +# !Post +class Post(TimeStampedModel): + title = models.CharField(_("Post title"), max_length=250) + author = models.ForeignKey( + settings.AUTH_USER_MODEL, + verbose_name=_("Author post"), + related_name="posts_author", + null=True, + on_delete=models.SET_NULL, + ) + slug = AutoSlugField( + max_length=100, unique=True, db_index=True, populate_from="title" + ) + post_photo_url = models.FileField( + _("Post photo"), + blank=True, + validators=[FileExtensionValidator(allowed_extensions=["png", "jpg", "jpeg"])], + ) + categories = models.ManyToManyField( + Category, + verbose_name=_("Categories"), + related_name="posts_categories", + blank=True, + ) + body = models.TextField(_("Post body")) + likes = models.ManyToManyField( + settings.AUTH_USER_MODEL, + verbose_name=_("Likes"), + related_name="post_likes", + blank=True, + ) + + class Meta: + verbose_name = _("Post") + verbose_name_plural = _("Posts") + + def __str__(self): + return f"{self.title} by {self.author.username}" + + +# !Comment +class Comment(TimeStampedModel): + post = models.ForeignKey( + Post, + verbose_name=_("Post"), + related_name="comment_post", + on_delete=models.CASCADE, + ) + author = models.ForeignKey( + settings.AUTH_USER_MODEL, + verbose_name=_("Author comment"), + related_name="comment_author", + null=True, + on_delete=models.SET_NULL, + ) + body = models.TextField(_("Comment body")) + + class Meta: + verbose_name = _("Comment") + verbose_name_plural = _("Comments") + + def __str__(self): + return f"{self.body[:20]} by {self.author.username}" diff --git a/backend/apps/posts/tests.py b/backend/apps/posts/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/backend/apps/posts/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/backend/apps/posts/views.py b/backend/apps/posts/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/backend/apps/posts/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/backend/config/base.py b/backend/config/base.py index 7735442..ccf1a83 100644 --- a/backend/config/base.py +++ b/backend/config/base.py @@ -87,7 +87,7 @@ "apps.upload", "apps.order", "apps.transaction", - # "apps.authentication", + "apps.posts", ] # !Installed Apps