implementiran REST API dio

This commit is contained in:
emaric 2024-01-11 12:38:56 +01:00
parent 6614ae2e44
commit 3931fc57bf
10 changed files with 56 additions and 26 deletions

View File

@ -1,5 +1,7 @@
from django.db import models
from django.contrib.gis.db import models
from django.contrib.auth.models import User
from django.utils import timezone
class ObjektSigurnosti(models.Model):
naziv = models.CharField(max_length=255)
@ -13,4 +15,13 @@ class ObjektSigurnosti(models.Model):
simbol_oznaka = models.CharField(max_length=255, null=True, blank=True)
def __str__(self):
return self.naziv
return self.naziv
# class Log(models.Model):
# user = models.ForeignKey(User, on_delete=models.CASCADE)
# akcija = models.CharField(max_length=255)
# opis = models.TextField()
# vrijeme = models.DateTimeField(default=timezone.now)
# def __str__(self):
# return f"{self.user.username} - {self.akcija} - {self.opis}"

View File

@ -1,10 +0,0 @@
<!DOCTYPE html>
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>

View File

@ -1,10 +1,8 @@
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import ObjektSigurnostiViewSet
router = DefaultRouter()
router.register(r'objekti', ObjektSigurnostiViewSet, basename='objekt-sigurnosti')
from django.urls import path, include
from .views import ObjektSigurnostiList, ObjektSigurnostiDetail
urlpatterns = [
path('', include(router.urls)),
]
path('objekti/', ObjektSigurnostiList.as_view(), name='objektisigurnosti-list'),
path('objekti/<int:pk>/', ObjektSigurnostiDetail.as_view(), name='objektisigurnosti-detail' ),
]

View File

@ -1,13 +1,45 @@
from django.shortcuts import render
from rest_framework import viewsets
# views.py
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework import generics
from django.http import Http404
from .models import ObjektSigurnosti
from .serializers import ObjektSigurnostiSerializer
from django.shortcuts import get_object_or_404
def home(request):
return render(request, 'base.html')
class ObjektSigurnostiViewSet(viewsets.ModelViewSet):
class ObjektSigurnostiList(generics.ListCreateAPIView):
serializer_class = ObjektSigurnostiSerializer
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)

Binary file not shown.

View File

@ -16,10 +16,9 @@ Including another URLconf
"""
from django.contrib import admin
from django.urls import path, include
from plovidba_aplikacija.views import home
from plovidba_aplikacija import views
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('plovidba_aplikacija.urls')),
path('', home, name='base'),
]