Browse Source

Converte banner para base64 na api rest de eventos

pull/166/head
Sesóstris Vieira 1 year ago
parent
commit
a1918566a6
  1. 1
      requirements/requirements.txt
  2. 32
      sigi/apps/eventos/serializers.py
  3. 7
      sigi/apps/eventos/views.py

1
requirements/requirements.txt

@ -9,6 +9,7 @@ Pillow==9.5.0
psycopg2-binary==2.9.6 psycopg2-binary==2.9.6
PyYAML==6.0.1 PyYAML==6.0.1
python-docx==0.8.11 python-docx==0.8.11
python-magic==0.4.27
requests==2.31.0 requests==2.31.0
uritemplate==4.1.1 uritemplate==4.1.1
weasyprint==58.0 weasyprint==58.0

32
sigi/apps/eventos/serializers.py

@ -1,3 +1,5 @@
import base64
import magic
from rest_framework import serializers from rest_framework import serializers
from sigi.apps.eventos.models import Evento from sigi.apps.eventos.models import Evento
@ -9,6 +11,7 @@ class EventoSerializer(serializers.ModelSerializer):
casa_municipio = serializers.SerializerMethodField("get_casa_municipio") casa_municipio = serializers.SerializerMethodField("get_casa_municipio")
casa_uf = serializers.SerializerMethodField("get_casa_uf") casa_uf = serializers.SerializerMethodField("get_casa_uf")
casa_cep = serializers.SerializerMethodField("get_casa_cep") casa_cep = serializers.SerializerMethodField("get_casa_cep")
banner_base64 = serializers.SerializerMethodField("get_banner_base64")
class Meta: class Meta:
model = Evento model = Evento
@ -34,6 +37,7 @@ class EventoSerializer(serializers.ModelSerializer):
"contato_inscricao", "contato_inscricao",
"telefone_inscricao", "telefone_inscricao",
"banner", "banner",
"banner_base64",
] ]
def get_casa_nome(self, obj): def get_casa_nome(self, obj):
@ -65,3 +69,31 @@ class EventoSerializer(serializers.ModelSerializer):
if obj.casa_anfitria: if obj.casa_anfitria:
return obj.casa_anfitria.cep return obj.casa_anfitria.cep
return "" 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",
]

7
sigi/apps/eventos/views.py

@ -36,7 +36,10 @@ from sigi.apps.eventos.forms import (
FuncionarioForm, FuncionarioForm,
ParlamentarForm, 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.parlamentares.models import Parlamentar
from sigi.apps.servidores.models import Servidor 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 Lista de eventos, oficinas e cursos realizados pelo ILB / Interlegis
""" """
pass serializer_class = EventoListSerializer
class ApiEventoRetrieve(ApiEventoAbstract, generics.RetrieveAPIView): class ApiEventoRetrieve(ApiEventoAbstract, generics.RetrieveAPIView):

Loading…
Cancel
Save