Translate english

This commit is contained in:
Andros Fenollosa
2021-07-14 16:19:03 +02:00
parent 997ad1f3e8
commit d04d21ae5b
24 changed files with 149 additions and 205 deletions

0
app/library/__init__.py Normal file
View File

3
app/library/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
app/library/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class LibrosConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "app.library"

View File

@ -0,0 +1,31 @@
# Generated by Django 3.2.5 on 2021-07-14 14:16
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Book',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=255)),
('genre', models.CharField(max_length=255)),
('year', models.CharField(max_length=4)),
('author', models.CharField(max_length=255)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
],
options={
'verbose_name': 'Book',
'verbose_name_plural': 'Books',
'ordering': ('created_at',),
},
),
]

View File

20
app/library/models.py Normal file
View File

@ -0,0 +1,20 @@
# app/Library/models.py
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=255)
genre = models.CharField(max_length=255)
year = models.CharField(max_length=4)
author = models.CharField(max_length=255)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ("created_at",)
verbose_name = "Book"
verbose_name_plural = "Books"
def __str__(self):
return self.title

View File

@ -0,0 +1,15 @@
# app/Library/serializers.py
from rest_framework import serializers
from .models import Book
class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = "__all__"
read_only_fields = (
"id",
"created_at",
"updated_at",
)

11
app/library/urls.py Normal file
View File

@ -0,0 +1,11 @@
# app/Library/urls.py
from django.urls import path
from app.library.views import ping, BookList, BookDetails
urlpatterns = [
path("ping/", ping, name="ping"),
path("api/book/", BookList.as_view(), name="book_list"),
path("api/book/<int:pk>/", BookDetails.as_view(), name="book_details"),
]

54
app/library/views.py Normal file
View File

@ -0,0 +1,54 @@
# app/Library/views.py
from django.http import JsonResponse
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .serializers import BookSerializer
from .models import Book
def ping(request):
data = {"ping": "pong!"}
return JsonResponse(data)
class BookList(APIView):
def get(self, request):
books = Book.objects.all()
serializer = BookSerializer(books, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
def post(self, request):
serializer = BookSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class BookDetails(APIView):
def get(self, request, pk):
book = Book.objects.filter(pk=pk).first()
if book:
serializer = BookSerializer(book)
return Response(serializer.data, status=status.HTTP_200_OK)
return Response(status=status.HTTP_404_NOT_FOUND)
def put(self, request, pk):
book = Book.objects.filter(pk=pk).first()
serializer = BookSerializer(book, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
elif not book:
return Response(serializer.data, status=status.HTTP_404_NOT_FOUND)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def delete(self, request, pk):
book = Book.objects.filter(pk=pk).first()
if book:
serializer = BookSerializer(book)
book.delete()
return Response(serializer.data, status=status.HTTP_200_OK)
return Response(status=status.HTTP_404_NOT_FOUND)