From c3e38597e22365edd59362618d388da13c6d2460 Mon Sep 17 00:00:00 2001 From: Hartmut Holzgraefe Date: Sun, 23 Feb 2020 21:11:25 +0000 Subject: [PATCH 1/2] allow empty map title on the model level --- .../migrations/0018_allow_empty_title.py | 20 +++++++++++++++++++ www/maposmatic/models.py | 4 ++-- 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 www/maposmatic/migrations/0018_allow_empty_title.py diff --git a/www/maposmatic/migrations/0018_allow_empty_title.py b/www/maposmatic/migrations/0018_allow_empty_title.py new file mode 100644 index 00000000..cd71f255 --- /dev/null +++ b/www/maposmatic/migrations/0018_allow_empty_title.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.20 on 2020-02-23 11:28 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('maposmatic', '0017_uploadfile'), + ] + + operations = [ + migrations.AlterField( + model_name='maprenderingjob', + name='maptitle', + field=models.CharField(blank=True, max_length=256, null=True), + ), + ] diff --git a/www/maposmatic/models.py b/www/maposmatic/models.py index 3715ac1d..6186c37e 100644 --- a/www/maposmatic/models.py +++ b/www/maposmatic/models.py @@ -94,7 +94,7 @@ class MapRenderingJob(models.Model): NONCE_SIZE = 16 - maptitle = models.CharField(max_length=256) + maptitle = models.CharField(max_length=256, null=True, blank=True) stylesheet = models.CharField(max_length=256) overlay = models.CharField(max_length=256, null=True, blank=True) layout = models.CharField(max_length=256) @@ -362,7 +362,7 @@ class UploadFile(models.Model): FILE_TYPES = ( ('gpx', 'GPX Track'), ('umap', 'UMAP Export File'), - ('poi', 'POI File') + ('poi', 'POI File'), ); uploaded_file = models.FileField(upload_to='upload/general/%Y/%m/%d/', null=True, blank=True) From 29110d3457c91f2b74a7f6520ccd2e5a6a9bf75c Mon Sep 17 00:00:00 2001 From: Hartmut Holzgraefe Date: Sun, 23 Feb 2020 21:15:42 +0000 Subject: [PATCH 2/2] add API support for multiple upload files --- www/maposmatic/apis.py | 274 ++++++++++++++++++++++++++--------------- 1 file changed, 175 insertions(+), 99 deletions(-) diff --git a/www/maposmatic/apis.py b/www/maposmatic/apis.py index 67531f78..26349b04 100644 --- a/www/maposmatic/apis.py +++ b/www/maposmatic/apis.py @@ -242,82 +242,90 @@ def _jobs_post(request): except LookupError as e: result['error']['paper_size'] = str(e) + import_files = [] + import_urls = [] + + if 'import_urls' in input: + for import_url in input['import_urls']: + import_urls.append(import_url) + if 'track_url' in input: - try: - request.FILES['track'] = _get_remote_file(input['track_url']) - except Exception as e: - result['error']['track_url'] = "Can't fetch track_url: %s" % e - - if 'track' in request.FILES: - try: - gpxxml = request.FILES['track'].read().decode('utf-8-sig') - gpx = gpxpy.parse(gpxxml) - - if _no_geometry(job): - (min_lat, max_lat, min_lon, max_lon) = gpx.get_bounds() - d_lat = (max_lat - min_lat) * 0.05 - d_lon = (max_lon - min_lon) * 0.05 - job.lat_bottom_right = min_lat - d_lat - job.lat_upper_left = max_lat + d_lat - job.lon_bottom_right = min_lon - d_lon - job.lon_upper_left = max_lon + d_lon - - if not job.maptitle and gpx.name: - job.maptitle = gpx.name - - if 'track' in request.FILES: - job.track.save(request.FILES['track'].name, request.FILES['track'], save=False) - - except Exception as e: - result['error']['track'] = 'Cannot parse GPX track: %s' % e - - # TODO: move file type specific code to separate sub-functions - # TODO: error handling probably needs to become a bit better than just - # one catch-all exception handler at the very end + import_urls.append(input['track_url']) if 'umap_url' in input: - # TODO: if not a /geojson url -> try to parse the map id number - # and construct actual /geojson url from that - try: - request.FILES['umap'] = _get_remote_file(input['umap_url']) - except Exception as e: - result['error']['umap_url'] = "Can't fetch umap_url: %s" % e + import_urls.append(input['umap_url']) - if 'umap' in request.FILES: - _process_umap_file(job, input, request.FILES['umap']) + for import_url in import_urls: + try: + import_file = _get_remote_file(import_url) + import_files.append(import_file) + except Exception as e: + result['error']['import_url'] = "Can't fetch import_url: %s" % e - if 'poi_file' in request.FILES: - try: - poijson = request.FILES['poi_file'].read().decode('utf-8-sig') + for import_file in request.FILES: + import_files.append(request.FILES[import_file]) - poi = json.loads(poijson) + processed_files = [] - if not job.maptitle and poi['title']: - job.maptitle = poi['title'] + if len(import_files) > 0: + import_data = { + 'lat_bottom_right' : 90, + 'lat_upper_left' : -90, + 'lon_bottom_right' : 180, + 'lon_upper_left' : -180, + 'maptitles' : [], + 'layout' : False, + } - bounds = [180, -180, 90, -90] - for cat in poi['nodes']: - for node in cat['nodes']: - bounds[0] = min(float(node['lon']), bounds[0]) - bounds[1] = max(float(node['lon']), bounds[1]) - bounds[2] = min(float(node['lat']), bounds[2]) - bounds[3] = max(float(node['lat']), bounds[3]) + for import_file in import_files: + try: + import_file.open() + first_line = import_file.readline(100).decode('utf-8-sig') + if first_line.startswith('