Merge branch 'feature/#33_serializar_usuarios' into 'develop'
Closed feature/#33_serializar_usuarios See merge request !23
Showing
3 changed files
with
50 additions
and
4 deletions
1 | -from django.contrib.auth.tokens import default_token_generator | ||
2 | -from rest_framework import mixins, viewsets, status | ||
3 | from rest_framework.decorators import action | 1 | from rest_framework.decorators import action |
2 | +from django_filters.rest_framework import DjangoFilterBackend | ||
3 | +from rest_framework import mixins, viewsets, status, filters | ||
4 | from rest_framework.exceptions import ValidationError | 4 | from rest_framework.exceptions import ValidationError |
5 | from rest_framework.parsers import JSONParser | 5 | from rest_framework.parsers import JSONParser |
6 | from rest_framework.permissions import IsAuthenticated, AllowAny | 6 | from rest_framework.permissions import IsAuthenticated, AllowAny |
7 | from rest_framework.response import Response | 7 | from rest_framework.response import Response |
8 | 8 | ||
9 | +from core.permissions import CustomModelPermissions | ||
9 | from usuario.models import Usuario | 10 | from usuario.models import Usuario |
10 | -from usuario.serializers import UsuarioSerializer, CambiarClaveSecretaSerializer | 11 | +from usuario.serializers import UsuarioSerializer, CambiarClaveSecretaSerializer, UsuarioListaSerializer |
12 | +from usuario.filters import UsuarioFilter | ||
11 | 13 | ||
12 | 14 | ||
13 | -class UsuarioViewSet(mixins.RetrieveModelMixin, mixins.UpdateModelMixin, viewsets.GenericViewSet): | 15 | +class UsuarioViewSet(mixins.RetrieveModelMixin, mixins.ListModelMixin, mixins.UpdateModelMixin, |
16 | + viewsets.GenericViewSet): | ||
14 | queryset = Usuario.objects.all() | 17 | queryset = Usuario.objects.all() |
15 | permission_classes = (IsAuthenticated,) | 18 | permission_classes = (IsAuthenticated,) |
16 | serializer_class = UsuarioSerializer | 19 | serializer_class = UsuarioSerializer |
20 | + filter_backends = (DjangoFilterBackend, filters.OrderingFilter) | ||
21 | + filterset_class = UsuarioFilter | ||
22 | + ordering_fields = ('last_name', 'first_name', 'cuil') | ||
23 | + ordering = ('last_name',) | ||
17 | 24 | ||
18 | def get_object(self, base_method=False): | 25 | def get_object(self, base_method=False): |
19 | user = self.request.user | 26 | user = self.request.user |
@@ -22,6 +29,22 @@ class UsuarioViewSet(mixins.RetrieveModelMixin, mixins.UpdateModelMixin, viewset | @@ -22,6 +29,22 @@ class UsuarioViewSet(mixins.RetrieveModelMixin, mixins.UpdateModelMixin, viewset | ||
22 | 29 | ||
23 | return user | 30 | return user |
24 | 31 | ||
32 | + def get_serializer_class(self): | ||
33 | + if self.action == 'list': | ||
34 | + return UsuarioListaSerializer | ||
35 | + | ||
36 | + return self.serializer_class | ||
37 | + | ||
38 | + def get_permissions(self): | ||
39 | + """ | ||
40 | + Instantiates and returns the list of permissions that this view requires. | ||
41 | + """ | ||
42 | + permission_classes = self.permission_classes | ||
43 | + if self.action == 'list': | ||
44 | + permission_classes = [CustomModelPermissions] | ||
45 | + | ||
46 | + return [permission() for permission in permission_classes] | ||
47 | + | ||
25 | @action( | 48 | @action( |
26 | methods=('patch',), | 49 | methods=('patch',), |
27 | detail=False, | 50 | detail=False, |
project/apps/usuario/filters.py
0 → 100644
1 | +from django_filters import rest_framework as filters | ||
2 | + | ||
3 | +from .models import Usuario | ||
4 | + | ||
5 | + | ||
6 | +class UsuarioFilter(filters.FilterSet): | ||
7 | + class Meta: | ||
8 | + model = Usuario | ||
9 | + fields = ('last_name',) | ||
10 | + | ||
11 | + last_name = filters.CharFilter(field_name='last_name', lookup_expr='icontains') | ||
12 | + |
@@ -28,6 +28,17 @@ class UsuarioSerializer(serializers.ModelSerializer): | @@ -28,6 +28,17 @@ class UsuarioSerializer(serializers.ModelSerializer): | ||
28 | } | 28 | } |
29 | 29 | ||
30 | 30 | ||
31 | +class UsuarioListaSerializer(serializers.ModelSerializer): | ||
32 | + class Meta: | ||
33 | + model = Usuario | ||
34 | + fields = ( | ||
35 | + 'username', | ||
36 | + 'first_name', | ||
37 | + 'last_name', | ||
38 | + 'cuil', | ||
39 | + ) | ||
40 | + | ||
41 | + | ||
31 | class CambiarClaveSecretaSerializer(DRFSerializer): | 42 | class CambiarClaveSecretaSerializer(DRFSerializer): |
32 | clave = serializers.CharField(max_length=128, write_only=True, required=True) | 43 | clave = serializers.CharField(max_length=128, write_only=True, required=True) |
33 | clave_nueva = serializers.CharField(max_length=128, write_only=True, required=True) | 44 | clave_nueva = serializers.CharField(max_length=128, write_only=True, required=True) |
-
Please register or login to post a comment