comment delete
This commit is contained in:
@@ -16,11 +16,9 @@ urlpatterns = [
|
||||
path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
|
||||
path('admin/', admin.site.urls),
|
||||
path('schema/', SpectacularAPIView.as_view(), name='schema'),
|
||||
path('comment/<int:product_pk>', views.CommentView.as_view(), name='comment-list'),
|
||||
path('comment/<int:pk>', views.CommentView.as_view(), name='comment-list'),
|
||||
path('', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
|
||||
]
|
||||
|
||||
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
||||
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
# Generated by Django 5.1.2 on 2024-12-12 15:03
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('product', '0002_remove_commentmodel_comment_commentmodel_content_and_more'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='commentmodel',
|
||||
name='user',
|
||||
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
|
||||
preserve_default=False,
|
||||
),
|
||||
]
|
||||
@@ -1,6 +1,6 @@
|
||||
from django.db import models
|
||||
from django.utils.text import slugify
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
class Product(models.Model):
|
||||
name = models.CharField(max_length=255)
|
||||
description = models.TextField()
|
||||
@@ -44,14 +44,11 @@ class Product(models.Model):
|
||||
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)
|
||||
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]}"
|
||||
return f"{self.user}-{self.content[:30]}"
|
||||
@@ -12,18 +12,26 @@ 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)
|
||||
def get(self, request, pk):
|
||||
product = get_object_or_404(Product, id=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):
|
||||
def post(self, request, pk):
|
||||
comment_ser = CommentSerializer(data=request.data)
|
||||
product = get_object_or_404(Product, id=product_pk)
|
||||
product = get_object_or_404(Product, id=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)
|
||||
def delete(self, request, pk):
|
||||
comment = get_object_or_404(CommentModel, pk=pk)
|
||||
if comment.user == request.user:
|
||||
comment.delete()
|
||||
return Response(status=status.HTTP_204_NO_CONTENT)
|
||||
else:
|
||||
return Response({"detail": "شما اجازه ی پاک کردن این کامنت را ندارید"}, status=status.HTTP_403_FORBIDDEN)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user