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..96a51a9 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,10 +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/', 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) - - 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/__init__.py b/backend/product/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/product/admin.py b/backend/product/admin.py new file mode 100644 index 0000000..ff6fa92 --- /dev/null +++ b/backend/product/admin.py @@ -0,0 +1,14 @@ +from django.contrib import admin +from .models import * +from unfold.admin import ModelAdmin + + +@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/apps.py b/backend/product/apps.py new file mode 100644 index 0000000..235a333 --- /dev/null +++ b/backend/product/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class ProductConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'product' 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/migrations/0003_commentmodel_user.py b/backend/product/migrations/0003_commentmodel_user.py new file mode 100644 index 0000000..971b774 --- /dev/null +++ b/backend/product/migrations/0003_commentmodel_user.py @@ -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, + ), + ] diff --git a/backend/product/migrations/__init__.py b/backend/product/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/product/models.py b/backend/product/models.py new file mode 100644 index 0000000..460d3d0 --- /dev/null +++ b/backend/product/models.py @@ -0,0 +1,54 @@ +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() + 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(max_length=50, unique=True, blank=True, null=True, allow_unicode=True, + verbose_name='نام یکتا', help_text="این فیلد را خالی بگذارید") + link_of_metas = models.CharField(max_length=400, verbose_name='لینک استخراج متا', null=True, blank=True) + meta_description = models.CharField(max_length=300, blank=True, null=True, help_text='این فیلد را حتما پر کنید') + meta_keywords = models.CharField(max_length=300, blank=True, null=True, help_text='این فیلد را حتما پر کنید') + meta_rating = models.FloatField(default=5, help_text='امتیاز محصول') + + def format_discount_price(self): + discount_price = int(self.price * (100 - self.discount) / 100) + formatted_num = "{:,.0f}".format(discount_price) + return formatted_num + + def discount_price(self): + discount_price = int(self.price * (100 - self.discount) / 100) + return discount_price + + def format_price(self): + price = self.price + formatted_num = "{:,.0f}".format(price) + return formatted_num + + def __str__(self): + return self.name + + def save(self, *args, **kwargs): + if not self.slug: + self.slug = slugify(self.name, allow_unicode=True) + 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.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/tests.py b/backend/product/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/backend/product/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/backend/product/views.py b/backend/product/views.py new file mode 100644 index 0000000..570edec --- /dev/null +++ b/backend/product/views.py @@ -0,0 +1,37 @@ +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, 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, pk): + comment_ser = CommentSerializer(data=request.data) + 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) + + diff --git a/backend/requirements.txt b/backend/requirements.txt index 68b9e78..53c4c54 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -42,7 +42,7 @@ multidict==6.1.0 oauthlib==3.2.2 pillow==10.4.0 psutil==6.0.0 -psycopg2-binary==2.9.9 +psycopg2-binary==2.9.10 pycparser==2.22 PyJWT==2.9.0 pyTelegramBotAPI==4.23.0 diff --git a/docker-compose.yml b/docker-compose.yml index 8d8b898..f528766 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,11 +1,11 @@ services: - frontend: - build: - context: ./frontend - ports: - - "80:3000" - depends_on: - - django + # frontend: + # build: + # context: ./frontend + # ports: + # - "80:3000" + # depends_on: + # - django django: build: