diff --git a/.env.develop b/.env.develop index 01dcad1..2267fae 100644 --- a/.env.develop +++ b/.env.develop @@ -19,4 +19,5 @@ SMTP_PASS=aigdvnrbueitklry SMTP_FROM='"Sistema Transporte" ' # PATH UPLOAD -GTFS_UPLOADS=/uploads \ No newline at end of file +GTFS_UPLOADS=/uploads/gtfs +PHOTOS_UPLOADS=/uploads/photos \ No newline at end of file diff --git a/.env.testing b/.env.testing index 6ec7c0f..93c9d7c 100644 --- a/.env.testing +++ b/.env.testing @@ -17,3 +17,7 @@ SMTP_PROTOCOL=tls SMTP_USER=francisco.sandoval@outlook.cl SMTP_PASS=aigdvnrbueitklry SMTP_FROM='"Sistema Transporte" ' + +# PATH UPLOAD +GTFS_UPLOADS=/uploads/gtfs +PHOTOS_UPLOADS=/uploads/photos \ No newline at end of file diff --git a/project/api/views/gtfs_archivo.py b/project/api/views/gtfs_archivo.py index 3c4ab33..c164528 100644 --- a/project/api/views/gtfs_archivo.py +++ b/project/api/views/gtfs_archivo.py @@ -20,7 +20,7 @@ class GtfsArchivoViewSet(viewsets.ModelViewSet): def create(self, request, *args, **kwargs): - fs = FileSystemStorage(location = request.data['ruta_archivo']) + fs = FileSystemStorage(location = config('GTFS_UPLOADS','/tmp')) fileUp = fs.save(request.data['archivo'], request.data['binario']) request.data['ruta_archivo'] = fileUp diff --git a/project/api/views/persona.py b/project/api/views/persona.py index 9586002..bb099bc 100755 --- a/project/api/views/persona.py +++ b/project/api/views/persona.py @@ -1,6 +1,12 @@ from rest_framework import viewsets -from .. import models, serializers -from django.http import HttpResponse +from rest_framework.decorators import action +from api import models, serializers +from django.http import HttpResponse, FileResponse +from django.core.files.storage import FileSystemStorage +from pathlib import Path +from decouple import config +import os +import mimetypes import logging class PersonaViewSet(viewsets.ModelViewSet): @@ -12,9 +18,67 @@ class PersonaViewSet(viewsets.ModelViewSet): def create(self, request): try: - super().create(request) + fs = FileSystemStorage(location = config('PHOTOS_UPLOADS','/tmp')) + fs.save(request.data['photo'], request.data['photo']) + + return super().create(request) except Exception as e: - # logging.warning(e.detail['rut'][0]) if e.detail['rut']: return HttpResponse(e.detail['rut'][0], status=400) - return HttpResponse(e, status=400) \ No newline at end of file + return HttpResponse(e, status=400) + + def partial_update(self, request, pk=None): + try: + # indicar el nombre con que se guardara el archivo + archivo = request.data['photo'] + extension = Path(archivo.name).suffix + nombre_archivo = f'{pk}{extension}' + + # guardar el archivo en la carpeta + fs = FileSystemStorage(location = config('PHOTOS_UPLOADS','/tmp')) + + # Sobrescribir el archivo si ya existe + if fs.exists(nombre_archivo): + fs.delete(nombre_archivo) + + fs.save(nombre_archivo, archivo) + + # proceder con guardar registro + return super().partial_update(request, pk) + except Exception as e: + return HttpResponse(e, status=400) + + + + @action(detail=False, methods=['get']) + def photo(self, request, *args, **kwargs): + file_location = None + try: + rut = request.GET['id'] + folder = config('PHOTOS_UPLOADS','/tmp') + + file_location = os.path.join(folder, 'desconocido.jpg') + file_query = os.path.join(folder, f'{rut}.png') + if os.path.isfile(file_query): + file_location = file_query + else: + file_query = os.path.join(folder, f'{rut}.jpg') + if os.path.isfile(file_query): + file_location = file_query + else: + file_query = os.path.join(folder, f'{rut}.jpeg') + if os.path.isfile(file_query): + file_location = file_query + + tipo_mime = mimetypes.guess_type(file_location)[0] + nombre_archivo = os.path.basename(file_location) + + archivo = open(file_location, 'rb') + response = FileResponse(archivo) + response['Access-Control-Expose-Headers'] = 'Content-Disposition' + response['Content-Type'] = tipo_mime + response['Content-Disposition'] = f'inline; filename="{nombre_archivo}"' + return response + except Exception as err: + logging.error({ 'error': err, 'file_location': file_location }) + return HttpResponse('Error al descargar archivo', status=500)