From 3a02bf5e2d3bc5cb8ba0ff68aebdd2575fd5c7d9 Mon Sep 17 00:00:00 2001 From: Luis <luis.garcia@capsus.mx> Date: Thu, 7 Nov 2019 17:26:21 -0600 Subject: [PATCH] Load mbtiles in consultation view --- .../settings/consultation_detail.html | 25 +++++++++++++++- settings/views.py | 30 +++++++++++++++++-- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/settings/templates/settings/consultation_detail.html b/settings/templates/settings/consultation_detail.html index c984c4d..549dc90 100644 --- a/settings/templates/settings/consultation_detail.html +++ b/settings/templates/settings/consultation_detail.html @@ -56,11 +56,34 @@ <div class="col-md-9 mx-auto"> <p>{% blocktrans %}Consultation{% endblocktrans %}</p> <h2>{{consultation.name}}</h2> + {% if request.GET.save %} + <div class="row"> + {% if request.GET.save == 'true' %} + <div class="alert alert-success w-100" role="alert"> + {% blocktrans %}Your file was loading successfuly{% endblocktrans %} + {% elif request.GET.save == 'no-mbtile' %} + <div class="alert alert-warning w-100" role="alert"> + {% blocktrans %}The file should be a mbtile file{% endblocktrans %} + {% else %} + <div class="alert alert-danger w-100" role="alert"> + {% blocktrans %}There was an error loading your file{% endblocktrans %} + {% endif %} + </div> + </div> + {% endif %} <div class="row"> <div class="col-md-8"> <p><b>{% blocktrans %}Code{% endblocktrans %}: </b>{{consultation.code}}</p> <p><b>{% blocktrans %}Status{% endblocktrans %}: </b>{{consultation.status}}</p> - </div> + <form class="form-inline" method = "post" enctype="multipart/form-data"> + {% csrf_token %} + <div class="form-group"> + <label for="inputFile">Map tiles</label> + <input type="file" class="form-control-file" id="inputFile" name="mbtiles"> + </div> + <button type="submit" class="btn btn-primary mb-2">Submit</button> + </form> + </div> <div class="col-md-4"> {% for status in next_status %} {% if status.name == "ready" and consultation.study_area %} diff --git a/settings/views.py b/settings/views.py index fcaaaf7..0525eb8 100644 --- a/settings/views.py +++ b/settings/views.py @@ -6,13 +6,15 @@ from django.contrib.auth.models import User from django.contrib.auth.hashers import make_password from django.contrib.auth.decorators import login_required from django.core.exceptions import PermissionDenied -from django.urls import reverse_lazy +from django.urls import reverse, reverse_lazy from django.utils.decorators import method_decorator from django.db import transaction +from django.http import HttpResponseRedirect from .forms import UserExtendFormSet from core.models import UserExtend from consultation.models import Group, Category, StudyArea, Consultation, Table, CategoryConsultation from status.methods import StatusMethod +import os class GroupDetailView(DetailView): model = Group @@ -122,4 +124,28 @@ class ConsultationDetailView(DetailView): context['categories'] = Category.objects.all() context['categories_consultation'] = CategoryConsultation.objects.filter(consultation = context['consultation'].id) context['next_status'] = status.next(context['consultation'].status.name) - return context \ No newline at end of file + return context + + def post(self, request, *args, **kwargs): + consultation_id = kwargs['pk'] + response = 'save=false' + #Get file and extencion + if 'mbtiles' in request.FILES: + file = request.FILES['mbtiles'] + extension = file.name.split('.')[-1] + #Base dir + BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + TILES_DIR = '/media/mbtiles/' + FILE_NAME = Consultation.objects.get(pk=consultation_id).code + FILE_EXTENSION = 'mbtiles' + #Save the file if is a mbtile + if extension.lower() == FILE_EXTENSION: + with open(BASE_DIR + TILES_DIR + FILE_NAME + '.' + FILE_EXTENSION, 'w+b') as f: + for chunk in file.chunks(): + f.write(chunk) + response = 'save=true' + else: + response = 'save=no-mbtile' + else: + response = '' + return HttpResponseRedirect(reverse('settings:consultation', kwargs={'pk':consultation_id}) + '?' + response) \ No newline at end of file -- GitLab