feature/implementacion de gestor de informacion y archivos minIO
This commit is contained in:
@@ -1,12 +1,86 @@
|
||||
from api.utils.storage_service import storage_service
|
||||
from rest_framework import serializers
|
||||
|
||||
from .models import DataStage
|
||||
from api.organization.models import Organizacion
|
||||
|
||||
class DataStageSerializer(serializers.ModelSerializer):
|
||||
archivo = serializers.FileField(write_only=True, required=False, allow_null=True)
|
||||
download_url = serializers.SerializerMethodField(read_only=True)
|
||||
|
||||
organizacion = serializers.PrimaryKeyRelatedField(required=False, allow_null=True, queryset=Organizacion.objects.all())
|
||||
|
||||
class Meta:
|
||||
model = DataStage
|
||||
fields = '__all__'
|
||||
read_only_fields = ('id', 'created_at', 'updated_at')
|
||||
read_only_fields = ('id', 'created_at', 'updated_at')
|
||||
# extra_kwargs = {'archivo': {'read_only': True},}
|
||||
|
||||
def get_download_url(self, obj):
|
||||
"""Retorna URL de descarga según dónde esté el archivo"""
|
||||
if not obj.archivo:
|
||||
return None
|
||||
|
||||
if storage_service.is_minio_path(obj.archivo):
|
||||
return storage_service.get_file_url(obj.archivo)
|
||||
else:
|
||||
request = self.context.get('request')
|
||||
if request:
|
||||
return request.build_absolute_uri(
|
||||
f"/api/v1/datastage/datastages/{obj.id}/download-datastage/"
|
||||
)
|
||||
return f"/api/v1/datastage/datastages/{obj.id}/download-datastage/"
|
||||
|
||||
def create(self, validated_data):
|
||||
"""Override para manejar la subida del archivo a MinIO"""
|
||||
archivo_file = validated_data.pop('archivo', None)
|
||||
organizacion = validated_data.get('organizacion')
|
||||
datastage = super().create(validated_data)
|
||||
print(f"ENDPOINT DE CREATE >>>>")
|
||||
# guardarlo en MinIO
|
||||
if archivo_file:
|
||||
ruta = storage_service.save_datastage(
|
||||
file=archivo_file,
|
||||
organizacion_id=organizacion.id if organizacion else datastage.organizacion.id,
|
||||
metadata={
|
||||
'datastage_id': str(datastage.id),
|
||||
'nombre': datastage.nombre if hasattr(datastage, 'nombre') else ''
|
||||
}
|
||||
)
|
||||
|
||||
if ruta:
|
||||
datastage.archivo = ruta
|
||||
datastage.save()
|
||||
else:
|
||||
# eliminar el registro creado
|
||||
datastage.delete()
|
||||
raise serializers.ValidationError({"archivo": "Error al guardar el archivo en el almacenamiento"})
|
||||
|
||||
return datastage
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
"""Override para manejar actualización de archivo"""
|
||||
archivo_file = validated_data.pop('archivo', None)
|
||||
organizacion = validated_data.get('organizacion', instance.organizacion)
|
||||
instance = super().update(instance, validated_data)
|
||||
|
||||
# Si hay nuevo archivo, reemplazarlo
|
||||
if archivo_file:
|
||||
if instance.archivo:
|
||||
storage_service.delete_file(instance.archivo)
|
||||
|
||||
ruta = storage_service.save_datastage(
|
||||
file=archivo_file,
|
||||
organizacion_id=organizacion.id,
|
||||
metadata={
|
||||
'datastage_id': str(instance.id),
|
||||
'updated': 'true'
|
||||
}
|
||||
)
|
||||
|
||||
if ruta:
|
||||
instance.archivo = ruta
|
||||
instance.save()
|
||||
else:
|
||||
raise serializers.ValidationError({"archivo": "Error al guardar el nuevo archivo"})
|
||||
return instance
|
||||
Reference in New Issue
Block a user