zmiany mariusza
This commit is contained in:
11
backend/formulas/views/__init__.py
Normal file
11
backend/formulas/views/__init__.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from .formula import FormulaViewSet
|
||||
from .symbol import SymbolViewSet
|
||||
from .category import FormulaCategoryViewSet
|
||||
from .favorite import FavoriteFormulaViewSet
|
||||
|
||||
__all__ = [
|
||||
"FormulaViewSet",
|
||||
"SymbolViewSet",
|
||||
"FormulaCategoryViewSet",
|
||||
"FavoriteFormulaViewSet",
|
||||
]
|
||||
BIN
backend/formulas/views/__pycache__/__init__.cpython-312.pyc
Normal file
BIN
backend/formulas/views/__pycache__/__init__.cpython-312.pyc
Normal file
Binary file not shown.
BIN
backend/formulas/views/__pycache__/category.cpython-312.pyc
Normal file
BIN
backend/formulas/views/__pycache__/category.cpython-312.pyc
Normal file
Binary file not shown.
BIN
backend/formulas/views/__pycache__/favorite.cpython-312.pyc
Normal file
BIN
backend/formulas/views/__pycache__/favorite.cpython-312.pyc
Normal file
Binary file not shown.
BIN
backend/formulas/views/__pycache__/formula.cpython-312.pyc
Normal file
BIN
backend/formulas/views/__pycache__/formula.cpython-312.pyc
Normal file
Binary file not shown.
BIN
backend/formulas/views/__pycache__/symbol.cpython-312.pyc
Normal file
BIN
backend/formulas/views/__pycache__/symbol.cpython-312.pyc
Normal file
Binary file not shown.
11
backend/formulas/views/category.py
Normal file
11
backend/formulas/views/category.py
Normal file
@@ -0,0 +1,11 @@
|
||||
# backend/formulas/views/category.py
|
||||
from rest_framework.viewsets import ModelViewSet
|
||||
from formulas.models.category import FormulaCategory
|
||||
from formulas.serializers.category import FormulaCategorySerializer
|
||||
from formulas.permissions import IsAdminOrEditor
|
||||
from rest_framework.permissions import IsAuthenticatedOrReadOnly
|
||||
|
||||
class FormulaCategoryViewSet(ModelViewSet):
|
||||
queryset = FormulaCategory.objects.all()
|
||||
serializer_class = FormulaCategorySerializer
|
||||
permission_classes = [IsAuthenticatedOrReadOnly]
|
||||
BIN
backend/formulas/views/category.zip
Normal file
BIN
backend/formulas/views/category.zip
Normal file
Binary file not shown.
40
backend/formulas/views/favorite.py
Normal file
40
backend/formulas/views/favorite.py
Normal file
@@ -0,0 +1,40 @@
|
||||
# backend/formulas/views/favorite.py
|
||||
from rest_framework import status, viewsets
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework.response import Response
|
||||
from formulas.models.formula import Formula
|
||||
from formulas.models.favorite import FavoriteFormula
|
||||
from formulas.serializers.formula import FormulaSerializer
|
||||
|
||||
class FavoriteFormulaViewSet(viewsets.ViewSet):
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
@action(detail=False, methods=['get'])
|
||||
def list(self, request):
|
||||
favorites = FavoriteFormula.objects.filter(user=request.user)
|
||||
formulas = [fav.formula for fav in favorites]
|
||||
serializer = FormulaSerializer(formulas, many=True)
|
||||
return Response(serializer.data)
|
||||
|
||||
@action(detail=True, methods=['post'])
|
||||
def add(self, request, pk=None):
|
||||
try:
|
||||
formula = Formula.objects.get(pk=pk)
|
||||
except Formula.DoesNotExist:
|
||||
return Response({"detail": "Formula not found."}, status=404)
|
||||
|
||||
fav, created = FavoriteFormula.objects.get_or_create(user=request.user, formula=formula)
|
||||
if not created:
|
||||
return Response({"detail": "Already in favorites."}, status=400)
|
||||
|
||||
return Response({"detail": "Added to favorites."}, status=200)
|
||||
|
||||
@action(detail=True, methods=['delete'])
|
||||
def remove(self, request, pk=None):
|
||||
try:
|
||||
fav = FavoriteFormula.objects.get(user=request.user, formula_id=pk)
|
||||
fav.delete()
|
||||
return Response({"detail": "Removed from favorites."}, status=204)
|
||||
except FavoriteFormula.DoesNotExist:
|
||||
return Response({"detail": "Not in favorites."}, status=404)
|
||||
21
backend/formulas/views/formula.py
Normal file
21
backend/formulas/views/formula.py
Normal file
@@ -0,0 +1,21 @@
|
||||
# backend/formulas/views/formula.py
|
||||
from rest_framework.viewsets import ModelViewSet
|
||||
from formulas.models.formula import Formula
|
||||
from formulas.serializers.formula import FormulaSerializer
|
||||
from formulas.permissions import IsAdminOrEditor
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.response import Response
|
||||
from formulas.serializers import SymbolSerializer
|
||||
|
||||
class FormulaViewSet(ModelViewSet):
|
||||
queryset = Formula.objects.all()
|
||||
serializer_class = FormulaSerializer
|
||||
permission_classes = [IsAdminOrEditor]
|
||||
lookup_field = "code"
|
||||
|
||||
@action(detail=True, methods=['get'], url_path='symbols')
|
||||
def get_symbols(self, request, code=None):
|
||||
formula = self.get_object()
|
||||
symbols = formula.symbols.all()
|
||||
serializer = SymbolSerializer(symbols, many=True)
|
||||
return Response(serializer.data)
|
||||
10
backend/formulas/views/symbol.py
Normal file
10
backend/formulas/views/symbol.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from rest_framework.viewsets import ModelViewSet
|
||||
from formulas.models.symbol import Symbol
|
||||
from formulas.serializers.symbol import SymbolSerializer
|
||||
from formulas.permissions import IsAdminOrEditor
|
||||
|
||||
class SymbolViewSet(ModelViewSet):
|
||||
queryset = Symbol.objects.all()
|
||||
serializer_class = SymbolSerializer
|
||||
permission_classes = [IsAdminOrEditor]
|
||||
lookup_field = "code"
|
||||
Reference in New Issue
Block a user