Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix upload .qgz project file #544

Merged
merged 5 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion g3w-admin/core/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ def __init__(self, *args, **kwargs):
'suite_logo',
'form_id',
'upload_url',
'delete_url',
HTML(
"""{% if form.suite_logo.value %}<img class="img-responsive img-thumbnail" src="{{ MEDIA_URL }}{{ form.suite_logo.value }}">{% endif %}""", ),
PrependedText('url_suite_logo', '<i class="fa fa-link"></i>'),
Expand Down
10 changes: 9 additions & 1 deletion g3w-admin/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def get_success_url(self):
def form_valid(self, form):
res = super(GroupCreateView, self).form_valid(form)

# delete tempory file form files
# delete temporary file form files
form.delete_temporary_files()
return res

Expand Down Expand Up @@ -352,6 +352,14 @@ def get_object(self, queryset=None):
def get_success_url(self):
return reverse('home')

def form_valid(self, form):
res = super().form_valid(form)

# Delete django-file-form temporary files
form.delete_temporary_files()

return res


# for MACROGROUPS
# ---------------------------------------------
Expand Down
18 changes: 15 additions & 3 deletions g3w-admin/qdjango/forms/projects.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from qgis.core import QgsProject
import re
import zipfile
Expand Down Expand Up @@ -28,6 +30,8 @@
from qdjango.utils.data import QgisProject
from qdjango.utils.validators import ProjectExists

import shutil


class QdjangoProjectFormMixin(object):
"""
Expand Down Expand Up @@ -59,8 +63,12 @@ def clean_qgis_file(self):
qgis_file = ContentFile(zfile.open(
qzfile, 'r').read(), name=qzfile)

# Update path property for QGIS api
qgis_file.path = self.cleaned_data['qgis_file'].file.path
# For QGIS Python API to recognize a .qgz file type, the file must have the extension `.qgz`
# So with current django-file-form version (3.5.0) is necessary make a copy of temporary file uploaded
# adding the .qgz extension
qgis_file.path = f"{self.cleaned_data['qgis_file'].file.path}.qgz"
shutil.copy(self.cleaned_data['qgis_file'].file.path, qgis_file.path)


if self.instance.pk:
kwargs['instance'] = self.instance
Expand All @@ -75,6 +83,11 @@ def clean_qgis_file(self):

self.qgisProject = QgisProject(qgis_file, **kwargs)
self.qgisProject.clean()

# Delete the .qgz copy
if file_extension.lower() == '.qgz':
os.remove(qgis_file.path)

except Exception as e:
raise ValidationError(str(e))
return qgis_file
Expand Down Expand Up @@ -194,7 +207,6 @@ def __init__(self, *args, **kwargs):
'qgis_file-uploads',
'form_id',
'upload_url',
'delete_url',
css_class='box-body',

),
Expand Down
4 changes: 4 additions & 0 deletions g3w-admin/qdjango/mixins/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def form_valid(self, form):
form.qgisProject.save(**form.cleaned_data)
if not form.instance.pk:
form.instance = form.qgisProject.instance

# Delete django-file-form temporary files
form.delete_temporary_files()

form.save()
return HttpResponseRedirect(self.get_success_url())

Expand Down
Binary file not shown.
27 changes: 26 additions & 1 deletion g3w-admin/qdjango/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from core.models import MapControl, ProjectMapUrlAlias
from core.forms import GroupForm
from .base import QdjangoTestBase, G3W_VIEWER1
from .utils import create_dff_project
from .utils import create_dff_project, create_dff_project_qgz
from editing.models import EDITING_ATOMIC_PERMISSIONS
from copy import copy

Expand Down Expand Up @@ -254,3 +254,28 @@ def test_qdjango_project_form(self):
self.assertTrue(new_viewer.has_perm('qdjango.view_project', p))


# TEST UPLOAD QGZ PROJECT FILE
# =================================================

# upload qgis_file
uf = create_dff_project_qgz('qgis_file')

# Test Create
# =========================================

form_data = {
'form_id': uf.form_id,
'feature_count_wms': 10,
'multilayer_query': 'single',
'multilayer_querybybbox': 'single',
'multilayer_querybypolygon': 'single',
'toc_tab_default': 'layers',
'toc_layers_init_status': 'not_collapsed',
'toc_themes_init_status': 'collapsed',
'legend_position': 'tab',
'url_alias': 'test_url_alias_name_qgz'
}

form = QdjangoProjectForm(request=self.request, group=self.project_group, data=form_data)
self.assertTrue(form.is_valid())

24 changes: 24 additions & 0 deletions g3w-admin/qdjango/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,30 @@ def create_dff_project(field_name, original_filename=None):

return uf

def create_dff_project_qgz(field_name, original_filename=None):
"""
Create a fake record django_file_form qgis project (.qgz) for form using it
:param field_name:
:param original_filename:
:return: UploadedFile model object
"""

project = open('{}test_qgz_project_328.qgz'.format(CURRENT_PATH + TEST_BASE_PATH), 'rb')

form_id = uuid.uuid4()
file_id=uuid.uuid4()

uf = UploadedFile(
form_id=form_id,
file_id=file_id,
field_name=field_name,
original_filename=original_filename if original_filename else 'test_qgz_project_328.qgz',
uploaded_file=File(project, name='test_qgz_project_328.qgz')
)
uf.save()

return uf


def clear_dff_project():
"""
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ django-formtools==2.2
django-crispy-forms==1.8.1
django-extensions==3.1.5
#---------------------------------------------------------------------------
# django-file-form: to replace with PyPi version where thi patch is present:
# django-file-form: to replace with PyPi version where this patch is present:
# https://github.com/mbraak/django-file-form/pull/638
git+https://github.com/gis3w/django-file-form.git@fix_filefield_not_required
#---------------------------------------------------------------------------
Expand Down