51 lines
2.0 KiB
Python
51 lines
2.0 KiB
Python
# views.py
|
|
from rest_framework.views import APIView
|
|
from rest_framework.generics import ListAPIView
|
|
from rest_framework.response import Response
|
|
from rest_framework import status
|
|
from rest_framework import generics
|
|
from .models import ObjektSigurnosti
|
|
from .serializers import ObjektSigurnostiSerializer
|
|
from django.shortcuts import get_object_or_404
|
|
from rest_framework.pagination import LimitOffsetPagination
|
|
|
|
|
|
class CustomObjektSigurnostiPagination(LimitOffsetPagination):
|
|
default_limit = 20
|
|
|
|
|
|
class ObjektSigurnostiList(generics.ListCreateAPIView):
|
|
serializer_class = ObjektSigurnostiSerializer
|
|
pagination_class = CustomObjektSigurnostiPagination
|
|
|
|
def get_queryset(self): #queryset je data iz database, listing and creating objects
|
|
queryset = ObjektSigurnosti.objects.all()
|
|
location = self.request.query_params.get('lokacija')
|
|
if location is not None:
|
|
queryset = queryset.filter(lokacija__icontains=location)
|
|
return queryset
|
|
|
|
# def get_serializer_class(self):
|
|
# if self.request.method == "GET":
|
|
# return ObjektSigurnostiSerializer
|
|
# return self.serializer_class()
|
|
|
|
|
|
class ObjektSigurnostiDetail(generics.RetrieveUpdateDestroyAPIView): #retrieving, updating, and deleting a specific object
|
|
queryset = ObjektSigurnosti.objects.all()
|
|
serializer_class = ObjektSigurnostiSerializer
|
|
|
|
# def perform_update(self, serializer):
|
|
# instance = serializer.save()
|
|
# opis = "Korisnik je uredio objekt sigurnosti {} (ID: {})".format(
|
|
# instance.vrsta.naziv, instance.id
|
|
# )
|
|
# Log.objects.create(user=self.request.user, akcija="Uređivanje", opis=opis)
|
|
|
|
# def perform_destroy(self, instance):
|
|
# super().perform_destroy(instance)
|
|
# opis = "Korisnik je obrisao objekt sigurnosti {} (ID: {})".format(
|
|
# instance.vrsta.naziv, instance.id
|
|
# )
|
|
# Log.objects.create(user=self.request.user, akcija="Brisanje", opis=opis)
|