From 097778e426757906ef089afaf6ecc52af967e7fc Mon Sep 17 00:00:00 2001 From: xepaerz Date: Wed, 3 May 2023 19:56:13 +1000 Subject: [PATCH] update backend API, update structure dir react-app --- pairent_backend/pairent_app/urls.py | 12 +++---- pairent_backend/pairent_app/views.py | 31 ++++++++----------- pairent_backend/pairent_backend/settings.py | 10 ++++-- pairent_backend/pairent_backend/urls.py | 2 +- pairent_frontend_react/public/index.html | 5 ++- .../src/{main.css => App.css} | 0 pairent_frontend_react/src/App.js | 6 ++-- 7 files changed, 35 insertions(+), 31 deletions(-) rename pairent_frontend_react/src/{main.css => App.css} (100%) diff --git a/pairent_backend/pairent_app/urls.py b/pairent_backend/pairent_app/urls.py index 768363a..11ed44e 100644 --- a/pairent_backend/pairent_app/urls.py +++ b/pairent_backend/pairent_app/urls.py @@ -1,9 +1,9 @@ from django.urls import path +from rest_framework.urlpatterns import format_suffix_patterns + from . import views -app_name="pairent_app" - -urlpatterns = [ - path('', views.ApartamentListView.as_view(), name="ApartamentList"), - path('apartament/', views.ApartamentDetailView.as_view(), name='ApartamentDetail'), -] \ No newline at end of file +urlpatterns = format_suffix_patterns([ + path("apartaments/", views.ApartamentViewSet.as_view({'get': 'list'})), + path("apartament//", views.ApartamentViewSet.as_view({'get': 'retrieve'})), +]) diff --git a/pairent_backend/pairent_app/views.py b/pairent_backend/pairent_app/views.py index 4555645..b3b4a21 100644 --- a/pairent_backend/pairent_app/views.py +++ b/pairent_backend/pairent_app/views.py @@ -1,24 +1,19 @@ -from rest_framework.views import APIView -from rest_framework.response import Response +from rest_framework import viewsets from .models import Apartament -from .serializer import ApartamentListSerializer, ApartamentDetailSerializer +from .serializer import (ApartamentListSerializer, + ApartamentDetailSerializer) -class ApartamentListView(APIView): - """Вывод списка квартир""" +class ApartamentViewSet(viewsets.ReadOnlyModelViewSet): + """Вывод списка квартир или отдельной квартиры""" - def get(self, request): + def get_queryset(self): apartaments = Apartament.objects.all() - headers = {'total-count': len(apartaments)} - serializer = ApartamentListSerializer(apartaments, many=True) - return Response(serializer.data, headers=headers) - - -class ApartamentDetailView(APIView): - """Вывод квартиры""" - - def get(self, request, id): - apartament = Apartament.objects.get(id=id) - serializer = ApartamentDetailSerializer(apartament) - return Response(serializer.data) \ No newline at end of file + return apartaments + + def get_serializer_class(self): + if self.action == 'list': + return ApartamentListSerializer + elif self.action == "retrieve": + return ApartamentDetailSerializer \ No newline at end of file diff --git a/pairent_backend/pairent_backend/settings.py b/pairent_backend/pairent_backend/settings.py index 5231d42..12146ac 100644 --- a/pairent_backend/pairent_backend/settings.py +++ b/pairent_backend/pairent_backend/settings.py @@ -37,9 +37,11 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', - 'pairent_app', + 'rest_framework', 'corsheaders', + + 'pairent_app', ] MIDDLEWARE = [ @@ -53,12 +55,16 @@ MIDDLEWARE = [ 'corsheaders.middleware.CorsMiddleware', ] +# Настройка REST FRAMEWORK REST_FRAMEWORK = { 'DEFAULT_PERMISSION_CLASSES' : [ 'rest_framework.permissions.AllowAny' - ] + ], + 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination', + 'PAGE_SIZE': 1 } +# Настройка отвечающая, что все могут отправлять запрос на бекенд. УБРАТЬ ПРИ ПРОДАКШЕНЕ! CORS_ORIGIN_ALLOW_ALL = True ROOT_URLCONF = 'pairent_backend.urls' diff --git a/pairent_backend/pairent_backend/urls.py b/pairent_backend/pairent_backend/urls.py index 57e33a4..3cabe48 100644 --- a/pairent_backend/pairent_backend/urls.py +++ b/pairent_backend/pairent_backend/urls.py @@ -18,5 +18,5 @@ from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), - path('', include('pairent_app.urls', namespace='pairent_app')), + path('api/', include('pairent_app.urls')), ] diff --git a/pairent_frontend_react/public/index.html b/pairent_frontend_react/public/index.html index f663895..47737f5 100644 --- a/pairent_frontend_react/public/index.html +++ b/pairent_frontend_react/public/index.html @@ -7,7 +7,10 @@ - + +
+ +
diff --git a/pairent_frontend_react/src/main.css b/pairent_frontend_react/src/App.css similarity index 100% rename from pairent_frontend_react/src/main.css rename to pairent_frontend_react/src/App.css diff --git a/pairent_frontend_react/src/App.js b/pairent_frontend_react/src/App.js index 69e5711..1339ff6 100644 --- a/pairent_frontend_react/src/App.js +++ b/pairent_frontend_react/src/App.js @@ -1,7 +1,7 @@ import React from 'react'; -import Header from './components/Header/Header'; -import Footer from './components/Footer/Footer'; -import './main.css'; +import Header from './components/header/Header'; +import Footer from './components/footer/Footer'; +import './App.css'; function App() { return (