comment create and get view
This commit is contained in:
@@ -93,6 +93,8 @@ INSTALLED_APPS = [
|
|||||||
'rest_framework_simplejwt.token_blacklist',
|
'rest_framework_simplejwt.token_blacklist',
|
||||||
'rest_framework.authtoken',
|
'rest_framework.authtoken',
|
||||||
'djoser',
|
'djoser',
|
||||||
|
# custom apps
|
||||||
|
'product'
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
@@ -248,6 +250,55 @@ UNFOLD = {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
"SIDEBAR": {
|
||||||
|
"show_search": True,
|
||||||
|
"show_all_applications": False,
|
||||||
|
"navigation": [
|
||||||
|
{
|
||||||
|
|
||||||
|
"separator": False, # Top border
|
||||||
|
"collapsible": False, # Collapsible group of links
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"title": _("داشبرد ادمین"),
|
||||||
|
"icon": "dashboard",
|
||||||
|
"link": reverse_lazy("admin:index"),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
{
|
||||||
|
"title": _("پنل فروش محصولات وبسایت"),
|
||||||
|
"separator": True,
|
||||||
|
"collapsible": True,
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"title": _("محصولات"),
|
||||||
|
"icon": "redeem",
|
||||||
|
"link": reverse_lazy("admin:product_product_changelist"),
|
||||||
|
},
|
||||||
|
|
||||||
|
# esm category model ro lower case bezar inja amir
|
||||||
|
|
||||||
|
# {
|
||||||
|
# "title": _("دسته بندی"),
|
||||||
|
# "icon": "category",
|
||||||
|
# "link": reverse_lazy("admin:product_ _changelist"),
|
||||||
|
# },
|
||||||
|
{
|
||||||
|
"title": _("نظرات"),
|
||||||
|
"icon": "chat",
|
||||||
|
"link": reverse_lazy("admin:product_commentmodel_changelist"),
|
||||||
|
},
|
||||||
|
|
||||||
|
],
|
||||||
|
},
|
||||||
|
|
||||||
|
],
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ from django.urls import path, include
|
|||||||
from drf_spectacular.views import SpectacularSwaggerView, SpectacularAPIView
|
from drf_spectacular.views import SpectacularSwaggerView, SpectacularAPIView
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from rest_framework_simplejwt.views import TokenObtainPairView,TokenRefreshView
|
from rest_framework_simplejwt.views import TokenObtainPairView,TokenRefreshView
|
||||||
|
from product import views
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
|
|
||||||
# djoser
|
# djoser
|
||||||
@@ -16,6 +16,7 @@ urlpatterns = [
|
|||||||
path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
|
path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
|
||||||
path('admin/', admin.site.urls),
|
path('admin/', admin.site.urls),
|
||||||
path('schema/', SpectacularAPIView.as_view(), name='schema'),
|
path('schema/', SpectacularAPIView.as_view(), name='schema'),
|
||||||
|
path('comment/<int:product_pk>', views.CommentView.as_view(), name='comment-list'),
|
||||||
path('', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
|
path('', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 121 KiB |
@@ -1,3 +1,14 @@
|
|||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
from .models import *
|
||||||
|
from unfold.admin import ModelAdmin
|
||||||
|
|
||||||
# Register your models here.
|
|
||||||
|
@admin.register(Product)
|
||||||
|
class ProductAdmin(ModelAdmin):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@admin.register(CommentModel)
|
||||||
|
class CommentAdmin(ModelAdmin):
|
||||||
|
pass
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
# Generated by Django 5.1.2 on 2024-12-12 13:57
|
||||||
|
|
||||||
|
import django.db.models.deletion
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
initial = True
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='Product',
|
||||||
|
fields=[
|
||||||
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('name', models.CharField(max_length=255)),
|
||||||
|
('description', models.TextField()),
|
||||||
|
('price', models.PositiveIntegerField(default=0)),
|
||||||
|
('image', models.ImageField(upload_to='product_images/')),
|
||||||
|
('rating', models.PositiveIntegerField(default=0)),
|
||||||
|
('view', models.IntegerField(default=0, verbose_name='بازدید')),
|
||||||
|
('sell', models.IntegerField(default=0, verbose_name='فروش')),
|
||||||
|
('in_stock', models.IntegerField(default=0, verbose_name='تعداد موجود')),
|
||||||
|
('discount', models.SmallIntegerField(default=0, verbose_name='تخفیف')),
|
||||||
|
('slug', models.SlugField(allow_unicode=True, blank=True, help_text='این فیلد را خالی بگذارید', null=True, unique=True, verbose_name='نام یکتا')),
|
||||||
|
('link_of_metas', models.CharField(blank=True, max_length=400, null=True, verbose_name='لینک استخراج متا')),
|
||||||
|
('meta_description', models.CharField(blank=True, help_text='این فیلد را حتما پر کنید', max_length=300, null=True)),
|
||||||
|
('meta_keywords', models.CharField(blank=True, help_text='این فیلد را حتما پر کنید', max_length=300, null=True)),
|
||||||
|
('meta_rating', models.FloatField(default=5, help_text='امتیاز محصول')),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='CommentModel',
|
||||||
|
fields=[
|
||||||
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('comment', models.TextField()),
|
||||||
|
('timestamp', models.DateTimeField(auto_now_add=True)),
|
||||||
|
('show', models.BooleanField(default=False)),
|
||||||
|
('product', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='product.product')),
|
||||||
|
],
|
||||||
|
options={
|
||||||
|
'verbose_name': 'نظر',
|
||||||
|
'verbose_name_plural': 'نظرات',
|
||||||
|
},
|
||||||
|
),
|
||||||
|
]
|
||||||
+39
@@ -0,0 +1,39 @@
|
|||||||
|
# Generated by Django 5.1.2 on 2024-12-12 14:34
|
||||||
|
|
||||||
|
import django.db.models.deletion
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('product', '0001_initial'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RemoveField(
|
||||||
|
model_name='commentmodel',
|
||||||
|
name='comment',
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='commentmodel',
|
||||||
|
name='content',
|
||||||
|
field=models.TextField(default='', verbose_name='محتوای نظر'),
|
||||||
|
preserve_default=False,
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='commentmodel',
|
||||||
|
name='product',
|
||||||
|
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='product.product', verbose_name='محصول'),
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='commentmodel',
|
||||||
|
name='show',
|
||||||
|
field=models.BooleanField(default=True, verbose_name='نشان دادن کامنت'),
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='commentmodel',
|
||||||
|
name='timestamp',
|
||||||
|
field=models.DateTimeField(auto_now_add=True, verbose_name='زمان ثبت کامنت'),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -39,3 +39,19 @@ class Product(models.Model):
|
|||||||
if not self.slug:
|
if not self.slug:
|
||||||
self.slug = slugify(self.name, allow_unicode=True)
|
self.slug = slugify(self.name, allow_unicode=True)
|
||||||
super().save(*args, **kwargs)
|
super().save(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
class CommentModel(models.Model):
|
||||||
|
product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='comments', verbose_name='محصول')
|
||||||
|
content = models.TextField(verbose_name='محتوای نظر')
|
||||||
|
# user = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||||
|
timestamp = models.DateTimeField(auto_now_add=True, verbose_name='زمان ثبت کامنت')
|
||||||
|
show = models.BooleanField(default=True, verbose_name='نشان دادن کامنت')
|
||||||
|
class Meta:
|
||||||
|
verbose_name = 'نظر'
|
||||||
|
verbose_name_plural = 'نظرات'
|
||||||
|
# def __str__(self):
|
||||||
|
# return f"{self.user}-{self.comment[:30]}"
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"{self.content[:30]}"
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
from .models import *
|
||||||
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
|
||||||
|
class CommentSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = CommentModel
|
||||||
|
fields = "__all__"
|
||||||
|
read_only_fields = ('show', 'product')
|
||||||
@@ -1,3 +1,29 @@
|
|||||||
from django.shortcuts import render
|
from django.core.paginator import Paginator
|
||||||
|
from rest_framework.views import APIView
|
||||||
|
from .models import *
|
||||||
|
from .serializers import *
|
||||||
|
from rest_framework import status
|
||||||
|
from rest_framework.response import Response
|
||||||
|
from django.db.models import Q
|
||||||
|
from django.shortcuts import get_object_or_404
|
||||||
|
from rest_framework.permissions import IsAuthenticatedOrReadOnly
|
||||||
|
|
||||||
|
|
||||||
|
class CommentView(APIView):
|
||||||
|
serializer_class = CommentSerializer
|
||||||
|
permission_classes = [IsAuthenticatedOrReadOnly]
|
||||||
|
def get(self, request, product_pk):
|
||||||
|
product = get_object_or_404(Product, id=product_pk)
|
||||||
|
comments = product.comments.filter(show=True)
|
||||||
|
comments_ser = self.serializer_class(instance=comments, many=True)
|
||||||
|
return Response({'comments': comments_ser.data}, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
def post(self, request, product_pk):
|
||||||
|
comment_ser = CommentSerializer(data=request.data)
|
||||||
|
product = get_object_or_404(Product, id=product_pk)
|
||||||
|
if comment_ser.is_valid():
|
||||||
|
comment_ser.save(product=product)
|
||||||
|
#TODO comment_ser.save(product=product, user=request.user)
|
||||||
|
return Response(comment_ser.data, status=status.HTTP_201_CREATED)
|
||||||
|
return Response(comment_ser.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
# Create your views here.
|
|
||||||
|
|||||||
Reference in New Issue
Block a user