Skip to content

Commit

Permalink
Created category,posts,comment table
Browse files Browse the repository at this point in the history
  • Loading branch information
riadelimemmedov committed Mar 26, 2024
1 parent 0b96cc0 commit cb46c8b
Show file tree
Hide file tree
Showing 9 changed files with 323 additions and 1 deletion.
Empty file added backend/apps/posts/__init__.py
Empty file.
26 changes: 26 additions & 0 deletions backend/apps/posts/admin.py
Original file line number Diff line number Diff line change
@@ -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"]
6 changes: 6 additions & 0 deletions backend/apps/posts/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class PostsConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "apps.posts"
196 changes: 196 additions & 0 deletions backend/apps/posts/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -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",
},
),
]
Empty file.
88 changes: 88 additions & 0 deletions backend/apps/posts/models.py
Original file line number Diff line number Diff line change
@@ -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}"
3 changes: 3 additions & 0 deletions backend/apps/posts/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.
3 changes: 3 additions & 0 deletions backend/apps/posts/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
2 changes: 1 addition & 1 deletion backend/config/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"apps.upload",
"apps.order",
"apps.transaction",
# "apps.authentication",
"apps.posts",
]

# !Installed Apps
Expand Down

0 comments on commit cb46c8b

Please sign in to comment.