Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Feedback #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions TechShop/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from pathlib import Path
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

Expand Down Expand Up @@ -40,19 +41,19 @@
"shop",
"products",
"feedback",
'orders',
'product_price',
'person'
"orders",
"product_price",
"person",
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"corsheaders.middleware.CorsMiddleware",
"django.middleware.common.CommonMiddleware",
]
Expand Down Expand Up @@ -128,15 +129,15 @@
# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

CORS_ORIGIN_ALLOW_ALL = False
CORS_ORIGIN_WHITELIST = [
'http://localhost:3000',
"http://localhost:8000",
]

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "static")

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = "/media/"
15 changes: 8 additions & 7 deletions TechShop/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""

from django.contrib import admin
from django.urls import path
from django.urls import include, path
from products.views import *
from feedback.views import *

urlpatterns = [
path('admin/', admin.site.urls),
path('products/list/', ProductListView.as_view()),
path('product/<int:product_id>/', ProductById.as_view()),
path('product/', ProductById.as_view()),
path('product/search/', ProductSearchList.as_view()),
path('feedback/<int:product_id>/product/', FeedBackByProduct.as_view())
path("admin/", admin.site.urls),
path("products/list/", ProductListView.as_view()),
path("product/<int:product_id>/", ProductById.as_view()),
path("product/", ProductById.as_view()),
path("product/search/", ProductSearchList.as_view()),
path("feedback/", include("feedback.urls")),
]
6 changes: 5 additions & 1 deletion feedback/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ class Feedback(models.Model):
auto_now=True, blank=True, null=True, verbose_name="Дата обновления"
)
user = models.ForeignKey(
User, blank=True, null=True, on_delete=models.PROTECT, verbose_name='Автор отзыва'
User,
blank=True,
null=True,
on_delete=models.PROTECT,
verbose_name="Автор отзыва",
)

class Meta:
Expand Down
2 changes: 1 addition & 1 deletion feedback/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
class FeedBackByProductSerializer(serializers.ModelSerializer):
class Meta:
model = Feedback
fields = ('__all__')
fields = "__all__"
1 change: 1 addition & 0 deletions feedback/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.test import TestCase

# Create your tests here.
# тестовый вариант по созванию / приложение выдавало API/ тесты пройтись по коду и выявления ошибок
11 changes: 11 additions & 0 deletions feedback/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.urls import path

from . import views

urlpatterns = [
path(
"product/<int:product_id>/",
views.FeedbackView.get,
),
path("product/<int:product_id>/add", views.FeedbackView.add),
]
39 changes: 33 additions & 6 deletions feedback/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
from django.shortcuts import render
import json
from django.http import HttpResponse, JsonResponse

from rest_framework.views import APIView, Response
from feedback.models import Feedback
from feedback.serializers import FeedBackByProductSerializer
from django.contrib.auth.models import User

from django.views.decorators.csrf import csrf_exempt

from products.models import Products

class FeedBackByProduct(APIView):
def get(self, request, product_id):

class FeedbackView(APIView):
def get(request, product_id):
products = Feedback.objects.filter(product_id=product_id)
return Response({
'feedbacks': FeedBackByProductSerializer(products, many=True).data
})
return JsonResponse(
{"feedbacks": FeedBackByProductSerializer(products, many=True).data}
)

@csrf_exempt
def add(request, product_id):
body = request.body

json_object = json.loads(body)

product = Products.objects.filter(pk=product_id).first()
user_id = json_object["user"]

user = User.objects.filter(pk=user_id).first()

feedback = Feedback()
feedback.product = product
feedback.rating = json_object["rating"]
feedback.description = json_object["description"]
feedback.title = json_object["title"]
feedback.user = user
feedback.save()
return HttpResponse("OK")
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
django
djangorestframework
Pillow
cors
cors
django-cors-headers