Ajuste del enlace con minIO
This commit is contained in:
39
pdf/views.py
39
pdf/views.py
@@ -1,11 +1,10 @@
|
|||||||
# mi_proyecto/pdf/views.py
|
# mi_proyecto/pdf/views.py
|
||||||
|
# dpsk - 2026
|
||||||
import io
|
import io
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django.template.loader import render_to_string
|
from django.template.loader import render_to_string
|
||||||
from weasyprint import HTML
|
from weasyprint import HTML
|
||||||
from arches.app.models.models import GraphModel
|
|
||||||
from arches.app.models.resource import Resource
|
from arches.app.models.resource import Resource
|
||||||
from django.contrib.auth.models import Group
|
from django.contrib.auth.models import Group
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
@@ -13,6 +12,8 @@ from zoneinfo import ZoneInfo
|
|||||||
from functools import reduce
|
from functools import reduce
|
||||||
from operator import getitem
|
from operator import getitem
|
||||||
from pypdf import PdfReader, PdfWriter
|
from pypdf import PdfReader, PdfWriter
|
||||||
|
from django.conf import settings
|
||||||
|
import boto3
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
import requests
|
import requests
|
||||||
import base64
|
import base64
|
||||||
@@ -20,14 +21,9 @@ import json
|
|||||||
import ast
|
import ast
|
||||||
|
|
||||||
def static_map_view(geojson_colec, sat_map):
|
def static_map_view(geojson_colec, sat_map):
|
||||||
g_sys_cfg=GraphModel.objects.get(name='{"en": "Arches System Settings"}')
|
|
||||||
r_sys_cfg=Resource.objects.get(graph=g_sys_cfg)
|
|
||||||
r_sys_cfg_data=r_sys_cfg.to_json()
|
|
||||||
access_token = r_sys_cfg_data['Map Settings']['Mapbox']['MAPBOX_API_KEY']
|
|
||||||
|
|
||||||
geojson_dict = ast.literal_eval(geojson_colec)
|
geojson_dict = ast.literal_eval(geojson_colec)
|
||||||
if sat_map:
|
if sat_map:
|
||||||
style_id = "satellite-streets-v12" # streets-v12 | outdoors-v11 | satellite-streets-v12
|
|
||||||
for geo in geojson_dict['features'] :
|
for geo in geojson_dict['features'] :
|
||||||
if geo['geometry'].get('type') == 'Polygon':
|
if geo['geometry'].get('type') == 'Polygon':
|
||||||
geo['properties'] = {
|
geo['properties'] = {
|
||||||
@@ -48,15 +44,21 @@ def static_map_view(geojson_colec, sat_map):
|
|||||||
"stroke-width": 2,
|
"stroke-width": 2,
|
||||||
"stroke-opacity": 0.8
|
"stroke-opacity": 0.8
|
||||||
}
|
}
|
||||||
else:
|
|
||||||
style_id = "light-v11"
|
|
||||||
|
|
||||||
geojson_string = json.dumps(geojson_dict, separators=(',', ':'))
|
geojson_string = json.dumps(geojson_dict, separators=(',', ':'))
|
||||||
encoded_geojson = urllib.parse.quote(geojson_string)
|
encoded_geojson = urllib.parse.quote(geojson_string)
|
||||||
|
|
||||||
username = "mapbox"
|
username = "mapbox"
|
||||||
|
if sat_map:
|
||||||
|
style_id = "satellite-streets-v12" # streets-v12 | outdoors-v11 | satellite-streets-v12
|
||||||
|
else:
|
||||||
|
style_id = "light-v11"
|
||||||
|
|
||||||
width, height = 400, 400
|
width, height = 400, 400
|
||||||
padding = 100
|
padding = 100
|
||||||
|
|
||||||
|
# Procedimiento pendiente para extraer el token desde la configuración del sistema
|
||||||
|
access_token = 'pk.eyJ1IjoianNidXJnb3N2IiwiYSI6ImNsaWhoN3ZtMTBxeHUzZm1xaGM0cGpxOXIifQ.ki3vldVB5MwZMqDSpLSjLg'
|
||||||
|
|
||||||
mapbox_url = (
|
mapbox_url = (
|
||||||
f"https://api.mapbox.com/styles/v1/{username}/{style_id}/static/"
|
f"https://api.mapbox.com/styles/v1/{username}/{style_id}/static/"
|
||||||
@@ -216,9 +218,20 @@ def export_resource_pdf(request, resourceid, printubic, satmap, printimgs, print
|
|||||||
for medio in reduce(getitem, ruta_medios[:-1], resource_data)[ruta_medios[-1]] :
|
for medio in reduce(getitem, ruta_medios[:-1], resource_data)[ruta_medios[-1]] :
|
||||||
if medio['formato'] == 'application/pdf' :
|
if medio['formato'] == 'application/pdf' :
|
||||||
try :
|
try :
|
||||||
response = requests.get(f'{sprpcb_url}{medio["url"]}', timeout=10)
|
minio_bucket=settings.AWS_STORAGE_BUCKET_NAME
|
||||||
response.raise_for_status()
|
s3_client = boto3.client('s3',
|
||||||
remote_pdf_stream = io.BytesIO(response.content)
|
endpoint_url=settings.AWS_S3_ENDPOINT_URL,
|
||||||
|
aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
|
||||||
|
aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY
|
||||||
|
)
|
||||||
|
pdf_s3_url = s3_client.generate_presigned_url(
|
||||||
|
'get_object',
|
||||||
|
Params={'Bucket': minio_bucket, 'Key': f'uploadedfiles/{medio["file"]}'},
|
||||||
|
ExpiresIn=3600
|
||||||
|
)
|
||||||
|
response_s3 = requests.get(pdf_s3_url, timeout=10)
|
||||||
|
response_s3.raise_for_status()
|
||||||
|
remote_pdf_stream = io.BytesIO(response_s3.content)
|
||||||
merger.append(remote_pdf_stream)
|
merger.append(remote_pdf_stream)
|
||||||
remote_pdf_stream.close()
|
remote_pdf_stream.close()
|
||||||
except requests.RequestException as e:
|
except requests.RequestException as e:
|
||||||
@@ -231,4 +244,4 @@ def export_resource_pdf(request, resourceid, printubic, satmap, printimgs, print
|
|||||||
|
|
||||||
response = HttpResponse(output_buffer.read(), content_type='application/pdf')
|
response = HttpResponse(output_buffer.read(), content_type='application/pdf')
|
||||||
response['Content-Disposition'] = f'attachment; filename="{nombre_pdf}.pdf"'
|
response['Content-Disposition'] = f'attachment; filename="{nombre_pdf}.pdf"'
|
||||||
return response
|
return response
|
||||||
|
|||||||
Reference in New Issue
Block a user