From 49c07cdbb246a70ff2738598c9d850daef2baedc Mon Sep 17 00:00:00 2001 From: LucasBraunX <78307547+LucasBraunX@users.noreply.github.com> Date: Tue, 13 Apr 2021 19:32:06 -0300 Subject: [PATCH 1/4] =?UTF-8?q?#157=20Adicionando=20rota=20para=20retornar?= =?UTF-8?q?=20dados=20de=20um=20produtor=20e=20fun=C3=A7=C3=B5es=20de=20co?= =?UTF-8?q?difica=C3=A7=C3=A3o=20base64?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: matheuscvp --- src/hortum/encode.py | 29 +++++++++++++++++++++++++++++ src/hortum/productor/serializer.py | 11 ++++++++++- src/hortum/productor/urls.py | 5 ++++- src/hortum/productor/viewsets.py | 16 ++++++++++++++-- 4 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 src/hortum/encode.py diff --git a/src/hortum/encode.py b/src/hortum/encode.py new file mode 100644 index 00000000..1b251905 --- /dev/null +++ b/src/hortum/encode.py @@ -0,0 +1,29 @@ +from base64 import b64decode, b64encode + +def encode_string(*decoded_strings): + '''Codifica uma ou mais strings pra base64''' + encoded_strings = [] + + for string in decoded_strings: + b_str = string.encode('utf-8') + b_str = b64encode(b_str) + encoded_strings.append(b_str.decode('utf-8')) + + if len(encoded_strings) > 1: + return encoded_strings + else: + return encoded_strings[0] + +def decode_string(*encoded_strings): + '''Decodifica uma ou mais strings em base64''' + decoded_strings = [] + + for string in encoded_strings: + b_str = string.encode('utf-8') + b_str = b64decode(b_str) + decoded_strings.append(b_str.decode('utf-8')) + + if len(decoded_strings) > 1: + return decoded_strings + else: + return decoded_strings[0] diff --git a/src/hortum/productor/serializer.py b/src/hortum/productor/serializer.py index e56b5bfc..b465c38e 100644 --- a/src/hortum/productor/serializer.py +++ b/src/hortum/productor/serializer.py @@ -4,7 +4,7 @@ from ..users.models import User from ..users.serializer import UserSerializer -from ..announcement.serializer import AnnouncementCreateSerializer +from ..announcement.serializer import AnnouncementCreateSerializer, AnnouncementListSerializer from ..picture.serializer import PictureSerializer class ProductorSerializer(serializers.ModelSerializer): @@ -21,6 +21,15 @@ def create(self, validated_data): user = User.objects.create_user(**user_data, is_productor=True) return Productor.objects.create(user=user, **validated_data) +class ProductorRetrieveSerializer(serializers.ModelSerializer): + username = serializers.CharField(source='user.username') + email = serializers.EmailField(source='user.email') + announcements = AnnouncementListSerializer(many=True) + + class Meta: + model = Productor + fields = ['username', 'email', 'idPicture', 'announcements'] + class LocalizationSerializer(serializers.ModelSerializer): class Meta: model = Localization diff --git a/src/hortum/productor/urls.py b/src/hortum/productor/urls.py index 5a5e19f9..97263910 100644 --- a/src/hortum/productor/urls.py +++ b/src/hortum/productor/urls.py @@ -5,5 +5,8 @@ routerRegister = routers.SimpleRouter() routerRegister.register(r'', viewsets.ProductorRegistrationAPIView, basename='productor') +router = routers.SimpleRouter() +router.register(r'retrieve', viewsets.ProductorRetrieveAPIView, basename='retrieveProductor') + urlpatterns = [ -] +] + router.urls diff --git a/src/hortum/productor/viewsets.py b/src/hortum/productor/viewsets.py index c1504125..be52f65e 100644 --- a/src/hortum/productor/viewsets.py +++ b/src/hortum/productor/viewsets.py @@ -1,4 +1,4 @@ -from .serializer import ProductorSerializer +from .serializer import ProductorSerializer, ProductorRetrieveSerializer from .models import Productor @@ -6,10 +6,22 @@ from rest_framework import mixins from rest_framework import permissions +from ..encode import decode_string + class ProductorRegistrationAPIView(GenericViewSet, mixins.CreateModelMixin): ''' EndPoint para registro de produtor ''' permission_classes = (permissions.AllowAny,) serializer_class = ProductorSerializer - queryset = Productor.objects.all() \ No newline at end of file + queryset = Productor.objects.all() + +class ProductorRetrieveAPIView(GenericViewSet, mixins.RetrieveModelMixin): + permission_classes = (permissions.IsAuthenticated,) + serializer_class = ProductorRetrieveSerializer + queryset = Productor.objects.all() + lookup_field = 'encoded_email' + + def get_object(self): + email = decode_string(self.kwargs['encoded_email']) + return Productor.objects.get(user__email=email) From c3b29d9c24a7efbf2ebf5196136c866a72457239 Mon Sep 17 00:00:00 2001 From: LucasBraunX <78307547+LucasBraunX@users.noreply.github.com> Date: Wed, 14 Apr 2021 19:33:41 -0300 Subject: [PATCH 2/4] =?UTF-8?q?#157=20refatorando=20fun=C3=A7=C3=B5es=20de?= =?UTF-8?q?=20codifica=C3=A7=C3=A3o=20b64?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hortum/encode.py | 40 +++++++++++++--------------------------- 1 file changed, 13 insertions(+), 27 deletions(-) diff --git a/src/hortum/encode.py b/src/hortum/encode.py index 1b251905..d759b94f 100644 --- a/src/hortum/encode.py +++ b/src/hortum/encode.py @@ -1,29 +1,15 @@ from base64 import b64decode, b64encode -def encode_string(*decoded_strings): - '''Codifica uma ou mais strings pra base64''' - encoded_strings = [] - - for string in decoded_strings: - b_str = string.encode('utf-8') - b_str = b64encode(b_str) - encoded_strings.append(b_str.decode('utf-8')) - - if len(encoded_strings) > 1: - return encoded_strings - else: - return encoded_strings[0] - -def decode_string(*encoded_strings): - '''Decodifica uma ou mais strings em base64''' - decoded_strings = [] - - for string in encoded_strings: - b_str = string.encode('utf-8') - b_str = b64decode(b_str) - decoded_strings.append(b_str.decode('utf-8')) - - if len(decoded_strings) > 1: - return decoded_strings - else: - return decoded_strings[0] +def encode_string(decoded_string): + '''Codifica uma string pra base64''' + b_str = decoded_string.encode('utf-8') + b_str = b64encode(b_str) + encoded_strings = b_str.decode('utf-8') + return encoded_strings + +def decode_string(encoded_string): + '''Decodifica uma string em base64''' + b_str = encoded_string.encode('utf-8') + b_str = b64decode(b_str) + decoded_strings = b_str.decode('utf-8') + return decoded_strings From 9468c5651ac061dc5e00fff682909324d924b07b Mon Sep 17 00:00:00 2001 From: LucasBraunX <78307547+LucasBraunX@users.noreply.github.com> Date: Wed, 14 Apr 2021 20:40:16 -0300 Subject: [PATCH 3/4] #157 adicionando testes de ProductorRetrieveAPIView --- src/hortum/productor/tests.py | 92 ++++++++++++++++++++++++++++++++ src/hortum/productor/urls.py | 2 +- src/hortum/productor/viewsets.py | 4 +- 3 files changed, 96 insertions(+), 2 deletions(-) diff --git a/src/hortum/productor/tests.py b/src/hortum/productor/tests.py index e576705a..9af8474f 100644 --- a/src/hortum/productor/tests.py +++ b/src/hortum/productor/tests.py @@ -1,6 +1,8 @@ from rest_framework.test import APITestCase from .models import Productor +from ..customer.models import Customer +from ..encode import encode_string class ProductorRegisterAPIViewTestCase(APITestCase): def setUp(self): @@ -71,3 +73,93 @@ def test_empty_field_register(self): 400, msg='Registro com campos vazios' ) + +class ProductorRetrieveAPIViewTestCase(APITestCase): + def create_user(self): + self.user_data = { + "username": "Marcelo", + "email": "marcelo@teste.com", + "password": "teste" + } + + url_signup = '/signup/customer/' + + response = self.client.post( + url_signup, + {'user': self.user_data}, + format='json' + ) + + self.assertEqual(response.status_code, 201, msg='Falha na criação de usuário') + + def create_productors(self): + productors_data = [ + { + 'username': 'João', + 'email': 'joao@productor.com', + 'password': 'teste' + }, + { + 'username': 'Mario', + 'email': 'mario@productor.com', + 'password': 'teste' + } + ] + + url_signup = '/signup/productor/' + + for prod_data in productors_data: + response = self.client.post( + url_signup, + {'user': prod_data}, + format='json' + ) + self.assertEqual(response.status_code, 201, msg='Falha na criação de outros produtores') + + def create_tokens(self): + user_cred = {'email': self.user_data['email'], 'password': self.user_data['password']} + + url_token = '/login/' + + response = self.client.post( + url_token, + user_cred, + format='json' + ) + + self.assertEqual(response.status_code, 200, msg='Credenciais inválidas') + + self.creds = {'HTTP_AUTHORIZATION': 'Bearer ' + response.data['access']} + + def setUp(self): + self.create_user() + self.create_productors() + self.create_tokens() + self.url_retrieve = '/productor/retrieve/' + + def tearDown(self): + Productor.objects.all().delete() + Customer.objects.all().delete() + + def test_retrieve_productor(self): + email_query = 'joao@productor.com' + url_retrieve_productor = self.url_retrieve + encode_string(email_query) + '/' + + response = self.client.get( + url_retrieve_productor, + **self.creds + ) + + self.assertEqual(response.status_code, 200, msg='Falha na obtenção de produtor específico') + self.assertEqual(response.data['username'], 'João', msg='Dados de produtor estão incoerentes') + + def test_invalid_email(self): + email_query = 'luigi@teste.com' + url_retrieve_productor = self.url_retrieve + encode_string(email_query) + '/' + + response = self.client.get( + url_retrieve_productor, + **self.creds + ) + + self.assertEqual(response.status_code, 404, msg='Retornando produtor com email inválido') diff --git a/src/hortum/productor/urls.py b/src/hortum/productor/urls.py index 97263910..81114878 100644 --- a/src/hortum/productor/urls.py +++ b/src/hortum/productor/urls.py @@ -5,7 +5,7 @@ routerRegister = routers.SimpleRouter() routerRegister.register(r'', viewsets.ProductorRegistrationAPIView, basename='productor') -router = routers.SimpleRouter() +router = routers.SimpleRouter(trailing_slash=True) router.register(r'retrieve', viewsets.ProductorRetrieveAPIView, basename='retrieveProductor') urlpatterns = [ diff --git a/src/hortum/productor/viewsets.py b/src/hortum/productor/viewsets.py index be52f65e..cc703c96 100644 --- a/src/hortum/productor/viewsets.py +++ b/src/hortum/productor/viewsets.py @@ -5,6 +5,7 @@ from rest_framework.viewsets import GenericViewSet from rest_framework import mixins from rest_framework import permissions +from django.shortcuts import get_object_or_404 from ..encode import decode_string @@ -24,4 +25,5 @@ class ProductorRetrieveAPIView(GenericViewSet, mixins.RetrieveModelMixin): def get_object(self): email = decode_string(self.kwargs['encoded_email']) - return Productor.objects.get(user__email=email) + prod = get_object_or_404(self.queryset.filter(user__email=email)) + return prod From c7ebff765182a5b00740eee9a5547d90e63d90ac Mon Sep 17 00:00:00 2001 From: LucasBraunX <78307547+LucasBraunX@users.noreply.github.com> Date: Wed, 14 Apr 2021 21:14:31 -0300 Subject: [PATCH 4/4] #157 ajustando serializador --- src/hortum/productor/serializer.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/hortum/productor/serializer.py b/src/hortum/productor/serializer.py index b465c38e..48940955 100644 --- a/src/hortum/productor/serializer.py +++ b/src/hortum/productor/serializer.py @@ -25,6 +25,7 @@ class ProductorRetrieveSerializer(serializers.ModelSerializer): username = serializers.CharField(source='user.username') email = serializers.EmailField(source='user.email') announcements = AnnouncementListSerializer(many=True) + idPicture = idPicture = PictureSerializer(many=False, read_only=True) class Meta: model = Productor