Cecilia

Crear APi de persona

  1 +from rest_framework import routers
  2 +
  3 +from persona.api import PersonaViewSet
  4 +
  5 +router = routers.DefaultRouter()
  6 +
  7 +router.register('persona', PersonaViewSet)
@@ -39,6 +39,8 @@ INSTALLED_APPS = [ @@ -39,6 +39,8 @@ INSTALLED_APPS = [
39 'django.contrib.staticfiles', 39 'django.contrib.staticfiles',
40 # Aplicaciones de tercero 40 # Aplicaciones de tercero
41 'django_extensions', 41 'django_extensions',
  42 + 'rest_framework',
  43 + 'django_filters',
42 44
43 # Aplicaciones propias 45 # Aplicaciones propias
44 'persona', 46 'persona',
@@ -77,6 +79,18 @@ TEMPLATES = [ @@ -77,6 +79,18 @@ TEMPLATES = [
77 79
78 WSGI_APPLICATION = 'capacitacion.wsgi.application' 80 WSGI_APPLICATION = 'capacitacion.wsgi.application'
79 81
  82 +# CONFIGURACION DE REST FRAMEWORK
  83 +REST_FRAMEWORK = {
  84 + # Use Django's standard `django.contrib.auth` permissions,
  85 + # or allow read-only access for unauthenticated users.
  86 + 'DEFAULT_PERMISSION_CLASSES': [
  87 + 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
  88 + ],
  89 + 'DEFAULT_RENDERER_CLASSES': [
  90 + 'rest_framework.renderers.JSONRenderer',
  91 + 'rest_framework.renderers.BrowsableAPIRenderer',
  92 + ]
  93 +}
80 94
81 # Database 95 # Database
82 # https://docs.djangoproject.com/en/3.2/ref/settings/#databases 96 # https://docs.djangoproject.com/en/3.2/ref/settings/#databases
@@ -15,9 +15,14 @@ Including another URLconf @@ -15,9 +15,14 @@ Including another URLconf
15 """ 15 """
16 from django.contrib import admin 16 from django.contrib import admin
17 from django.urls import path, include 17 from django.urls import path, include
  18 +from django.views.generic import RedirectView
  19 +
  20 +from capacitacion.routers import router
18 21
19 urlpatterns = [ 22 urlpatterns = [
  23 + path('', RedirectView.as_view(url='/admin/')),
20 path('admin/', admin.site.urls), 24 path('admin/', admin.site.urls),
21 path('persona/', include('persona.urls'), name='persona'), 25 path('persona/', include('persona.urls'), name='persona'),
  26 + path('api/v1/', include((router.urls, 'capacitacion'), namespace='capacitacion')),
22 # path('persona/', include('organismo.urls'), name='persona') 27 # path('persona/', include('organismo.urls'), name='persona')
23 ] 28 ]
  1 +from django_filters.rest_framework import DjangoFilterBackend
  2 +from rest_framework import viewsets, filters
  3 +from rest_framework.permissions import IsAuthenticated
  4 +
  5 +from persona.filters import PersonaFilter
  6 +from persona.models import Persona
  7 +from persona.serializers import PersonaSerializer
  8 +
  9 +
  10 +class PersonaViewSet(viewsets.ModelViewSet):
  11 + queryset = Persona.objects.all()
  12 + serializer_class = PersonaSerializer
  13 + filterset_class = PersonaFilter
  14 + filter_backends = [DjangoFilterBackend]
  15 + permission_classes = (IsAuthenticated,)
  16 +
  1 +from django_filters import rest_framework as filters
  2 +
  3 +from persona.models import Persona
  4 +
  5 +
  6 +class PersonaFilter(filters.FilterSet):
  7 + class Meta:
  8 + model = Persona
  9 + fields = ('nombre', 'fecha_nacimiento')
  10 +
  11 + nombre = filters.CharFilter(field_name='nombre', lookup_expr='icontains')
  1 +import datetime
  2 +
  3 +from rest_framework import serializers
  4 +
  5 +from persona.models import Persona
  6 +
  7 +
  8 +class PersonaSerializer(serializers.ModelSerializer):
  9 + class Meta:
  10 + model = Persona
  11 + fields = ('nombre', 'apellido', 'documento_identidad', 'fecha_nacimiento', 'organismo', 'edad')
  12 +
  13 + edad = serializers.SerializerMethodField(method_name='calcular_edad')
  14 +
  15 + def calcular_edad(self, persona):
  16 + today = datetime.date.today()
  17 + diferencia = today - persona.fecha_nacimiento
  18 + edad = diferencia.days
  19 + edad = edad / 365
  20 + return int(edad)
1 django==3.2.7 1 django==3.2.7
  2 +pytest==6.2.2
  3 +pytest-django==4.1.0
  4 +pytest-mock==3.5.1
  5 +djangorestframework==3.12.4
  6 +django-filter==21.1