From a1918566a64bf8eab3889ecbda2a477f433d7977 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ses=C3=B3stris=20Vieira?= Date: Thu, 21 Sep 2023 09:35:37 -0300 Subject: [PATCH] Converte banner para base64 na api rest de eventos --- requirements/requirements.txt | 1 + sigi/apps/eventos/serializers.py | 32 ++++++++++++++++++++++++++++++++ sigi/apps/eventos/views.py | 7 +++++-- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/requirements/requirements.txt b/requirements/requirements.txt index daed0a0..3775bf1 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -9,6 +9,7 @@ Pillow==9.5.0 psycopg2-binary==2.9.6 PyYAML==6.0.1 python-docx==0.8.11 +python-magic==0.4.27 requests==2.31.0 uritemplate==4.1.1 weasyprint==58.0 diff --git a/sigi/apps/eventos/serializers.py b/sigi/apps/eventos/serializers.py index 5557562..822598a 100644 --- a/sigi/apps/eventos/serializers.py +++ b/sigi/apps/eventos/serializers.py @@ -1,3 +1,5 @@ +import base64 +import magic from rest_framework import serializers from sigi.apps.eventos.models import Evento @@ -9,6 +11,7 @@ class EventoSerializer(serializers.ModelSerializer): casa_municipio = serializers.SerializerMethodField("get_casa_municipio") casa_uf = serializers.SerializerMethodField("get_casa_uf") casa_cep = serializers.SerializerMethodField("get_casa_cep") + banner_base64 = serializers.SerializerMethodField("get_banner_base64") class Meta: model = Evento @@ -34,6 +37,7 @@ class EventoSerializer(serializers.ModelSerializer): "contato_inscricao", "telefone_inscricao", "banner", + "banner_base64", ] def get_casa_nome(self, obj): @@ -65,3 +69,31 @@ class EventoSerializer(serializers.ModelSerializer): if obj.casa_anfitria: return obj.casa_anfitria.cep return "" + + def get_banner_base64(self, obj): + if obj.banner: + mime_type = magic.from_file(obj.banner.path, mime=True) + obj.banner.file.seek(0) # Garante que está no início do arquivo + b64str = (base64.b64encode(obj.banner.file.read())).decode("ascii") + return f"data:{mime_type};base64, {b64str}" + return None + + +class EventoListSerializer(EventoSerializer): + class Meta: + model = Evento + fields = [ + "id", + "nome", + "turma", + "data_inicio", + "data_termino", + "carga_horaria", + "local", + "casa_nome", + "casa_logradouro", + "casa_bairro", + "casa_municipio", + "casa_uf", + "casa_cep", + ] diff --git a/sigi/apps/eventos/views.py b/sigi/apps/eventos/views.py index 35e2889..01bf9af 100644 --- a/sigi/apps/eventos/views.py +++ b/sigi/apps/eventos/views.py @@ -36,7 +36,10 @@ from sigi.apps.eventos.forms import ( FuncionarioForm, ParlamentarForm, ) -from sigi.apps.eventos.serializers import EventoSerializer +from sigi.apps.eventos.serializers import ( + EventoSerializer, + EventoListSerializer, +) from sigi.apps.parlamentares.models import Parlamentar from sigi.apps.servidores.models import Servidor @@ -379,7 +382,7 @@ class ApiEventoList(ApiEventoAbstract, generics.ListAPIView): Lista de eventos, oficinas e cursos realizados pelo ILB / Interlegis """ - pass + serializer_class = EventoListSerializer class ApiEventoRetrieve(ApiEventoAbstract, generics.RetrieveAPIView):