diff --git a/backend/core/settings.py b/backend/core/settings.py index 22bda16..c518f31 100644 --- a/backend/core/settings.py +++ b/backend/core/settings.py @@ -93,6 +93,8 @@ INSTALLED_APPS = [ 'rest_framework_simplejwt.token_blacklist', 'rest_framework.authtoken', 'djoser', + # custom apps + 'product' ] 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"), + }, + + ], + }, + + ], + }, } diff --git a/backend/core/urls.py b/backend/core/urls.py index 21028ea..5ebc8f5 100644 --- a/backend/core/urls.py +++ b/backend/core/urls.py @@ -4,7 +4,7 @@ from django.urls import path, include from drf_spectacular.views import SpectacularSwaggerView, SpectacularAPIView from django.conf import settings from rest_framework_simplejwt.views import TokenObtainPairView,TokenRefreshView - +from product import views urlpatterns = [ # djoser @@ -16,6 +16,7 @@ urlpatterns = [ path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'), path('admin/', admin.site.urls), path('schema/', SpectacularAPIView.as_view(), name='schema'), + path('comment/', views.CommentView.as_view(), name='comment-list'), path('', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'), ] diff --git a/backend/media/product_images/9d010a8048e3773c5eb40231158346bf.jpg b/backend/media/product_images/9d010a8048e3773c5eb40231158346bf.jpg new file mode 100644 index 0000000..61b3411 Binary files /dev/null and b/backend/media/product_images/9d010a8048e3773c5eb40231158346bf.jpg differ diff --git a/backend/product/admin.py b/backend/product/admin.py index 8c38f3f..ff6fa92 100644 --- a/backend/product/admin.py +++ b/backend/product/admin.py @@ -1,3 +1,14 @@ 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 \ No newline at end of file diff --git a/backend/product/migrations/0001_initial.py b/backend/product/migrations/0001_initial.py new file mode 100644 index 0000000..77c9438 --- /dev/null +++ b/backend/product/migrations/0001_initial.py @@ -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': 'نظرات', + }, + ), + ] diff --git a/backend/product/migrations/0002_remove_commentmodel_comment_commentmodel_content_and_more.py b/backend/product/migrations/0002_remove_commentmodel_comment_commentmodel_content_and_more.py new file mode 100644 index 0000000..7467e0e --- /dev/null +++ b/backend/product/migrations/0002_remove_commentmodel_comment_commentmodel_content_and_more.py @@ -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='زمان ثبت کامنت'), + ), + ] diff --git a/backend/product/models.py b/backend/product/models.py index bd28fd2..db2baeb 100644 --- a/backend/product/models.py +++ b/backend/product/models.py @@ -38,4 +38,20 @@ class Product(models.Model): def save(self, *args, **kwargs): if not self.slug: self.slug = slugify(self.name, allow_unicode=True) - super().save(*args, **kwargs) \ No newline at end of file + 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]}" \ No newline at end of file diff --git a/backend/product/serializers.py b/backend/product/serializers.py new file mode 100644 index 0000000..3852d8e --- /dev/null +++ b/backend/product/serializers.py @@ -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') \ No newline at end of file diff --git a/backend/product/views.py b/backend/product/views.py index 91ea44a..1b22588 100644 --- a/backend/product/views.py +++ b/backend/product/views.py @@ -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.