forked from TDTP/admin_transporte_backend
se agrega descarga de archivo zip
parent
d748b08e0f
commit
73290773d5
|
@ -15,7 +15,7 @@ SMTP_HOST=smtp-mail.outlook.com
|
|||
SMTP_PORT=587
|
||||
SMTP_PROTOCOL=tls
|
||||
SMTP_USER=francisco.sandoval@outlook.cl
|
||||
SMTP_PASS=ppcsrdsvecdewnfl
|
||||
SMTP_PASS=aigdvnrbueitklry
|
||||
SMTP_FROM='"Sistema Transporte" <francisco.sandoval@outlook.cl>'
|
||||
|
||||
# PATH UPLOAD
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
# Conexion con base datos principal
|
||||
DBHOST=192.168.211.210
|
||||
DBPORT=5432
|
||||
DBNAME=transporte
|
||||
DBSCHEMA=public
|
||||
DBUSER=postgres
|
||||
DBPASS=password
|
||||
|
||||
# Conexion con base datos redis
|
||||
DB_REDIS_HOST=192.168.211.210
|
||||
DB_REDIS_PORT=6379
|
||||
|
||||
# Datos de emisor de correos
|
||||
SMTP_HOST=smtp-mail.outlook.com
|
||||
SMTP_PORT=587
|
||||
SMTP_PROTOCOL=tls
|
||||
SMTP_USER=francisco.sandoval@outlook.cl
|
||||
SMTP_PASS=aigdvnrbueitklry
|
||||
SMTP_FROM='"Sistema Transporte" <francisco.sandoval@outlook.cl>'
|
|
@ -1,7 +1,6 @@
|
|||
from django.urls import resolve
|
||||
from django.http import HttpResponse
|
||||
from .models import Usuario, Persona
|
||||
from decouple import config
|
||||
from project.settings import SECRET_KEY
|
||||
import jwt
|
||||
import logging
|
||||
|
|
|
@ -3,11 +3,14 @@ from rest_framework import viewsets
|
|||
from rest_framework.response import Response
|
||||
from rest_framework.decorators import action
|
||||
from django_filters.rest_framework import DjangoFilterBackend
|
||||
from rest_framework import status
|
||||
from .. import models , serializers
|
||||
from django.db import models as dj_models
|
||||
import os
|
||||
from django.core.files.storage import FileSystemStorage
|
||||
from django.core.files import File
|
||||
from django.http import FileResponse, HttpResponse
|
||||
from rest_framework import status
|
||||
from api import models , serializers
|
||||
from decouple import config
|
||||
import os
|
||||
import logging
|
||||
|
||||
class GtfsArchivoViewSet(viewsets.ModelViewSet):
|
||||
queryset = models.GtfsArchivo.objects.all()
|
||||
|
@ -17,13 +20,28 @@ class GtfsArchivoViewSet(viewsets.ModelViewSet):
|
|||
|
||||
|
||||
def create(self, request, *args, **kwargs):
|
||||
# se indica que si no se indico el id, entonces sea el maximo + 1
|
||||
#if not request.data.get('id_gtfs', None):
|
||||
# max_id = models.GtfsArchivo.objects.aggregate(dj_models.Max('id_gtfs'))['id_gtfs__max']
|
||||
# new_id = max_id + 1 if max_id is not None else 1
|
||||
# request.data['id_gtfs'] = new_id
|
||||
fs = FileSystemStorage(location = request.data['ruta_archivo'])
|
||||
|
||||
fileUp = fs.save(request.data['archivo'], request.data['binario'])
|
||||
uploaded_file_url = fs.url(fileUp)
|
||||
request.data['ruta_archivo'] = fileUp
|
||||
|
||||
return super().create(request, *args, **kwargs)
|
||||
|
||||
@action(detail=False, methods=['get'])
|
||||
def download(self, request, *args, **kwargs):
|
||||
try:
|
||||
id = request.GET['id']
|
||||
registro = models.GtfsArchivo.objects.filter(id_gtfs = id).first()
|
||||
|
||||
folder = config('GTFS_UPLOADS','/tmp')
|
||||
file_location = os.path.join(folder, registro.ruta_archivo)
|
||||
|
||||
archivo = open(file_location, 'rb')
|
||||
response = FileResponse(archivo)
|
||||
response['Access-Control-Expose-Headers'] = 'Content-Disposition'
|
||||
response['Content-Type'] = 'application/zip'
|
||||
response['Content-Disposition'] = f'attachment; filename="{registro.archivo}"'
|
||||
return response
|
||||
except Exception as err:
|
||||
logging.error({ 'error': err })
|
||||
return HttpResponse('Error al descargar archivo', status=500)
|
Loading…
Reference in New Issue