Merge remote-tracking branch 'origin/main'
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,10 +16,9 @@ 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: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'),
|
||||||
]
|
]
|
||||||
|
|
||||||
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
||||||
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
|
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 121 KiB |
@@ -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
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class ProductConfig(AppConfig):
|
||||||
|
default_auto_field = 'django.db.models.BigAutoField'
|
||||||
|
name = 'product'
|
||||||
@@ -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='زمان ثبت کامنت'),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -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,
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -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]}"
|
||||||
@@ -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')
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
# Create your tests here.
|
||||||
@@ -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)
|
||||||
|
|
||||||
|
|
||||||
@@ -42,7 +42,7 @@ multidict==6.1.0
|
|||||||
oauthlib==3.2.2
|
oauthlib==3.2.2
|
||||||
pillow==10.4.0
|
pillow==10.4.0
|
||||||
psutil==6.0.0
|
psutil==6.0.0
|
||||||
psycopg2-binary==2.9.9
|
psycopg2-binary==2.9.10
|
||||||
pycparser==2.22
|
pycparser==2.22
|
||||||
PyJWT==2.9.0
|
PyJWT==2.9.0
|
||||||
pyTelegramBotAPI==4.23.0
|
pyTelegramBotAPI==4.23.0
|
||||||
|
|||||||
+7
-7
@@ -1,11 +1,11 @@
|
|||||||
services:
|
services:
|
||||||
frontend:
|
# frontend:
|
||||||
build:
|
# build:
|
||||||
context: ./frontend
|
# context: ./frontend
|
||||||
ports:
|
# ports:
|
||||||
- "80:3000"
|
# - "80:3000"
|
||||||
depends_on:
|
# depends_on:
|
||||||
- django
|
# - django
|
||||||
|
|
||||||
django:
|
django:
|
||||||
build:
|
build:
|
||||||
|
|||||||
Reference in New Issue
Block a user