Skip to content

Commit

Permalink
Update: class with drf api
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderMungan committed Dec 10, 2023
1 parent 7b16d38 commit e2c7f7e
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 0 deletions.
Binary file modified backend/backend/__pycache__/settings.cpython-312.pyc
Binary file not shown.
1 change: 1 addition & 0 deletions backend/backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
'rest_framework',
"corsheaders",
'drf_yasg',
'secondApp'
]

MIDDLEWARE = [
Expand Down
13 changes: 13 additions & 0 deletions backend/backend/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,19 @@
from django.contrib import admin
from django.urls import path, include, re_path
from app.views import *
from secondApp.views import *

from rest_framework.routers import DefaultRouter

from drf_yasg.views import get_schema_view
from drf_yasg import openapi

router = DefaultRouter()
router.register(r'/hayvan', HayvanOlustur)




schema_view = get_schema_view(
openapi.Info(
title="CoderMungan Portfolio Api",
Expand All @@ -49,4 +57,9 @@
path('api/', include('app.urls')),
path('docs/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
path('animal',include(router.urls)),
path('hayvanpost/<id>', HayvanSinglePost.as_view()),
path('hayvanguncelle/<id>', HayvanSadeceGuncelle.as_view()),
path('hayvangetistegi/<id>',HayvanSadeceGet.as_view()),
path('hayvanfiltre', HayvanFiltre.as_view()),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Empty file added backend/secondApp/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions backend/secondApp/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions backend/secondApp/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class SecondappConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'secondApp'
27 changes: 27 additions & 0 deletions backend/secondApp/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 4.2.7 on 2023-12-09 23:30

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Animal',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=50, verbose_name='Name Animal')),
('width', models.IntegerField(verbose_name='Animal Width')),
('height', models.IntegerField(verbose_name='Animal Height')),
('tur', models.CharField(max_length=50)),
('yas', models.IntegerField()),
('renk', models.CharField(blank=True, max_length=50, null=True)),
('olusturulma_tarihi', models.DateTimeField(auto_now_add=True)),
],
),
]
Empty file.
18 changes: 18 additions & 0 deletions backend/secondApp/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.db import models

# Create your models here.



class Animal(models.Model):
name = models.CharField(("Name Animal"), max_length=50)
width = models.IntegerField(("Animal Width"))
height = models.IntegerField(("Animal Height"))
tur = models.CharField(max_length=50)
yas = models.IntegerField()
renk = models.CharField(max_length=50, blank=True, null=True)
olusturulma_tarihi = models.DateTimeField(auto_now_add=True)


def __str__(self) -> str:
return self.name
14 changes: 14 additions & 0 deletions backend/secondApp/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from rest_framework.serializers import ModelSerializer
from rest_framework import serializers

from .models import *



class AnimalSerializer(serializers.ModelSerializer):

class Meta:
model = Animal
fields = "__all__"


3 changes: 3 additions & 0 deletions backend/secondApp/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
60 changes: 60 additions & 0 deletions backend/secondApp/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from django.shortcuts import render

from rest_framework import status
from rest_framework.response import Response

# ModelViewSet Import
from rest_framework import viewsets
from rest_framework import generics
from rest_framework.decorators import APIView
from rest_framework.permissions import IsAuthenticatedOrReadOnly
# Modeli Çek
from .models import *

# Serializer Çek
from .serializers import *
# Create your views here.

# Model Geniş Model
class HayvanOlustur(viewsets.ModelViewSet):
queryset = Animal.objects.all()
serializer_class = AnimalSerializer


# Retrive olduğundan dolayı tekli veriyi güncelleme
class HayvanSadeceGuncelle(generics.RetrieveUpdateDestroyAPIView):
lookup_field = 'id'
queryset=Animal.objects.all()
serializer_class = AnimalSerializer


# Retrive olduğundan dolayı tekli veriyi gösterme
class HayvanSadeceGet(generics.RetrieveAPIView):
lookup_field = 'id'
queryset = Animal.objects.all()
serializer_class = AnimalSerializer


# Retrive olduğundan dolayı tekli veriyi silme
class HayvanSinglePost(generics.RetrieveDestroyAPIView):
lookup_field = 'id'
queryset = Animal.objects.all()
serializer_class = AnimalSerializer

def destroy(self, request, *args, **kwargs):
instance = self.get_object()
self.perform_destroy(instance)


data = {'message': 'İt silindi'}

return Response(data, status=status.HTTP_201_CREATED)


# Filtreleme işlemleri
class HayvanFiltre(generics.ListAPIView):
queryset = Animal.objects.all()
serializer_class = AnimalSerializer

def get_queryset(self):
return Animal.objects.filter(yas__lte = 50)

0 comments on commit e2c7f7e

Please sign in to comment.