initiate drf app
This commit is contained in:
0
backend/api/__init__.py
Normal file
0
backend/api/__init__.py
Normal file
3
backend/api/admin.py
Normal file
3
backend/api/admin.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
6
backend/api/apps.py
Normal file
6
backend/api/apps.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ApiConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'api'
|
||||
0
backend/api/main/__init__.py
Normal file
0
backend/api/main/__init__.py
Normal file
9
backend/api/main/serializers.py
Normal file
9
backend/api/main/serializers.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from mainpage.models import FAQ
|
||||
|
||||
class FAQMainSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = FAQ
|
||||
fields = "__all__"
|
||||
|
||||
19
backend/api/main/views.py
Normal file
19
backend/api/main/views.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from rest_framework import status
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from api.utils.decorators import handle_exceptions
|
||||
|
||||
from api.main.serializers import FAQMainSerializer
|
||||
from mainpage.models import FAQ
|
||||
|
||||
class FAQView(APIView):
|
||||
@handle_exceptions
|
||||
def get(self, request):
|
||||
|
||||
faqs = FAQ.objects.all()
|
||||
|
||||
data = {
|
||||
'faqs': FAQMainSerializer(faqs, many=True).data
|
||||
}
|
||||
|
||||
return Response(data, status=status.HTTP_200_OK)
|
||||
0
backend/api/migrations/__init__.py
Normal file
0
backend/api/migrations/__init__.py
Normal file
3
backend/api/models.py
Normal file
3
backend/api/models.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
3
backend/api/tests.py
Normal file
3
backend/api/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
7
backend/api/urls.py
Normal file
7
backend/api/urls.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from django.urls import path
|
||||
|
||||
from api.main.views import FAQView
|
||||
|
||||
urlpatterns = [
|
||||
path("v1/faq/", FAQView.as_view(), name='faqMain'),
|
||||
]
|
||||
56
backend/api/utils/decorators.py
Normal file
56
backend/api/utils/decorators.py
Normal file
@@ -0,0 +1,56 @@
|
||||
from functools import wraps
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import status
|
||||
from django.core.exceptions import ValidationError, PermissionDenied
|
||||
from django.http import Http404
|
||||
from rest_framework.exceptions import APIException
|
||||
|
||||
def handle_exceptions(func):
|
||||
"""
|
||||
Обработчик ошибок для API endpoints
|
||||
Обрабатывает различные типы исключений и возвращает соответствующие HTTP статусы
|
||||
Текущие обрабоки:
|
||||
- Ошибки валидации - HTTP400
|
||||
- Объект не найден - HTTP404
|
||||
- Ошибки доступа - HTTP403
|
||||
- Обработки DRF исключений - вернется статус код от DRF
|
||||
- Необработанные исключения - HTTP500
|
||||
"""
|
||||
@wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
try:
|
||||
return func(*args, **kwargs)
|
||||
except ValidationError as e:
|
||||
# ошибки валидации
|
||||
return Response(
|
||||
{"error": e.messages if hasattr(e, 'messages') else str(e)},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
except Http404 as e:
|
||||
# объект не найден
|
||||
return Response(
|
||||
{"error": str(e) or "Запрашиваемый ресурс не найден"},
|
||||
status=status.HTTP_404_NOT_FOUND
|
||||
)
|
||||
except PermissionDenied as e:
|
||||
# ошибки доступа
|
||||
return Response(
|
||||
{"error": str(e) or "У вас нет прав для выполнения этого действия"},
|
||||
status=status.HTTP_403_FORBIDDEN
|
||||
)
|
||||
except APIException as e:
|
||||
# обработка DRF исключений
|
||||
return Response(
|
||||
{"error": str(e)},
|
||||
status=e.status_code
|
||||
)
|
||||
except Exception as e:
|
||||
# необработанные исключения
|
||||
return Response(
|
||||
{
|
||||
"error": "Произошла внутренняя ошибка сервера",
|
||||
"detail": str(e)
|
||||
},
|
||||
status=status.HTTP_500_INTERNAL_SERVER_ERROR
|
||||
)
|
||||
return wrapper
|
||||
Reference in New Issue
Block a user