g3w-admin-processing 1.2.1b1.dev1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- g3w_admin_processing-1.2.1b1.dev1.dist-info/METADATA +258 -0
- g3w_admin_processing-1.2.1b1.dev1.dist-info/RECORD +80 -0
- g3w_admin_processing-1.2.1b1.dev1.dist-info/WHEEL +5 -0
- g3w_admin_processing-1.2.1b1.dev1.dist-info/licenses/LICENSE +373 -0
- g3w_admin_processing-1.2.1b1.dev1.dist-info/top_level.txt +1 -0
- qprocessing/__init__.py +7 -0
- qprocessing/admin.py +21 -0
- qprocessing/api/__init__.py +0 -0
- qprocessing/api/permissions.py +41 -0
- qprocessing/api/views/__init__.py +4 -0
- qprocessing/api/views/actions.py +84 -0
- qprocessing/api/views/admin.py +76 -0
- qprocessing/api/views/models.py +219 -0
- qprocessing/api/views/upload.py +177 -0
- qprocessing/apiurls.py +64 -0
- qprocessing/apps.py +38 -0
- qprocessing/configs.py +131 -0
- qprocessing/forms.py +133 -0
- qprocessing/migrations/0001_initial.py +24 -0
- qprocessing/migrations/0002_auto_20230615_1609.py +23 -0
- qprocessing/migrations/0003_qprocessinginputupload.py +32 -0
- qprocessing/migrations/0004_auto_20230809_0920.py +26 -0
- qprocessing/migrations/0005_alter_qprocessinginputupload_user.py +21 -0
- qprocessing/migrations/__init__.py +0 -0
- qprocessing/models.py +143 -0
- qprocessing/receivers.py +100 -0
- qprocessing/sitetrees.py +31 -0
- qprocessing/static/qprocessing/js/components/DrawInputVectorFeatures.js +180 -0
- qprocessing/static/qprocessing/js/components/InputFieldChooser.js +226 -0
- qprocessing/static/qprocessing/js/components/InputPrjRasterLayer.js +135 -0
- qprocessing/static/qprocessing/js/components/InputPrjVectorLayer.js +415 -0
- qprocessing/static/qprocessing/js/components/ModelPanel.js +496 -0
- qprocessing/static/qprocessing/js/components/ModelResults.js +73 -0
- qprocessing/static/qprocessing/js/components/OutputFile.js +63 -0
- qprocessing/static/qprocessing/js/components/OutputRasterLayer.js +63 -0
- qprocessing/static/qprocessing/js/components/OutputVectorLayer.js +115 -0
- qprocessing/static/qprocessing/js/components/UploadVectorFile.js +53 -0
- qprocessing/static/qprocessing/js/i18n/en.js +33 -0
- qprocessing/static/qprocessing/js/i18n/index.js +7 -0
- qprocessing/static/qprocessing/js/i18n/it.js +33 -0
- qprocessing/static/qprocessing/js/plugin.js +122 -0
- qprocessing/tasks.py +136 -0
- qprocessing/templates/qprocessing/project_form.html +70 -0
- qprocessing/templates/qprocessing/projects_list.html +72 -0
- qprocessing/tests/__init__.py +0 -0
- qprocessing/tests/base.py +104 -0
- qprocessing/tests/data/models/qbuffer.model3 +233 -0
- qprocessing/tests/data/models/qintersection.model3 +288 -0
- qprocessing/tests/data/models/qpointspolygons.model3 +222 -0
- qprocessing/tests/data/models/test.model3 +321 -0
- qprocessing/tests/data/models/test_buffer_all_inputs.model3 +269 -0
- qprocessing/tests/data/models/test_buffer_layer1.model3 +201 -0
- qprocessing/tests/data/project_data/buildings.geojson +15 -0
- qprocessing/tests/data/project_data/qprocessing.gpkg +0 -0
- qprocessing/tests/data/qprocessing_qprocessing.qgs +2813 -0
- qprocessing/tests/data/qprocessing_qprocessing_attachments.zip +0 -0
- qprocessing/tests/data/test_qgis_328.qgs +954 -0
- qprocessing/tests/data/test_qgis_328_attachments.zip +0 -0
- qprocessing/tests/data/upload/fail_shp_to_upload.zip +0 -0
- qprocessing/tests/data/upload/linestring_to_upload.geojson +9 -0
- qprocessing/tests/data/upload/point_to_upload.geojson +10 -0
- qprocessing/tests/data/upload/point_to_upload_espg4326.geojson +10 -0
- qprocessing/tests/data/upload/point_to_upload_espg4326.qmd +26 -0
- qprocessing/tests/data/upload/polygon_to_upload.geojson +9 -0
- qprocessing/tests/data/upload/shp_to_upload.cpg +1 -0
- qprocessing/tests/data/upload/shp_to_upload.dbf +0 -0
- qprocessing/tests/data/upload/shp_to_upload.prj +1 -0
- qprocessing/tests/data/upload/shp_to_upload.qix +0 -0
- qprocessing/tests/data/upload/shp_to_upload.shp +0 -0
- qprocessing/tests/data/upload/shp_to_upload.shx +0 -0
- qprocessing/tests/data/upload/shp_to_upload.zip +0 -0
- qprocessing/tests/test_algorithm.py +52 -0
- qprocessing/tests/test_api.py +173 -0
- qprocessing/tests/test_api_models.py +459 -0
- qprocessing/urls.py +33 -0
- qprocessing/utils/__init__.py +0 -0
- qprocessing/utils/data.py +283 -0
- qprocessing/utils/exceptions.py +20 -0
- qprocessing/utils/formtypes.py +521 -0
- qprocessing/views.py +96 -0
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
# coding=utf-8
|
|
2
|
+
"""" Tests for API REST models
|
|
3
|
+
.. note:: This program is free software; you can redistribute it and/or modify
|
|
4
|
+
it under the terms of the Mozilla Public License 2.0.
|
|
5
|
+
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
__author__ = 'lorenzetti@gis3w.it'
|
|
9
|
+
__date__ = '2023-07-07'
|
|
10
|
+
__copyright__ = 'Copyright 2015 - 2023, Gis3w'
|
|
11
|
+
__license__ = 'MPL 2.0'
|
|
12
|
+
|
|
13
|
+
from django.conf import settings
|
|
14
|
+
from django.test.utils import override_settings
|
|
15
|
+
from django.core.files import File
|
|
16
|
+
from django.urls import reverse
|
|
17
|
+
from django.core.files.uploadedfile import SimpleUploadedFile
|
|
18
|
+
from guardian.shortcuts import assign_perm
|
|
19
|
+
from qprocessing.models import QProcessingProject, QProcessingInputUpload
|
|
20
|
+
from .base import (
|
|
21
|
+
CURRENT_PATH,
|
|
22
|
+
TEST_BASE_PATH,
|
|
23
|
+
TestQProcessingBase,
|
|
24
|
+
MODEL_FILE_BUFFER,
|
|
25
|
+
MODEL_FILE_POINTSPOLYGONS,
|
|
26
|
+
MODEL_FILE_INTERSECTIONS,
|
|
27
|
+
UPLOAD_FILE_POINT,
|
|
28
|
+
UPLOAD_FILE_POLYGON,
|
|
29
|
+
UPLOAD_FILE_LINESTRING,
|
|
30
|
+
UPLOAD_FILE_SHP_POLYGON_ZIP,
|
|
31
|
+
UPLOAD_FILE_SHP_POLYGON_ZIP_FAIL
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
from qgis.core import QgsVectorLayer, QgsWkbTypes
|
|
36
|
+
from qgis.PyQt.QtCore import QTemporaryDir
|
|
37
|
+
|
|
38
|
+
import json
|
|
39
|
+
import zipfile
|
|
40
|
+
import time
|
|
41
|
+
from io import BytesIO
|
|
42
|
+
import os
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class TestQProcessingModelsAPIREST(TestQProcessingBase):
|
|
46
|
+
"""
|
|
47
|
+
Test for QProcessing API REST for running models
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
@classmethod
|
|
51
|
+
def setUp(cls):
|
|
52
|
+
|
|
53
|
+
super().setUp()
|
|
54
|
+
|
|
55
|
+
# Create QProcessingProject
|
|
56
|
+
# --------------------------------------------
|
|
57
|
+
|
|
58
|
+
# Create testing object and data
|
|
59
|
+
with open(cls.model_file_buffer, 'r') as f:
|
|
60
|
+
cls.qpp_buffer = QProcessingProject(model=File(f, name=MODEL_FILE_BUFFER.split('/')[1]))
|
|
61
|
+
cls.qpp_buffer.save()
|
|
62
|
+
|
|
63
|
+
cls.qpp_buffer.projects.add(cls.project.instance)
|
|
64
|
+
|
|
65
|
+
with open(cls.model_file_ponintspolygons, 'r') as f:
|
|
66
|
+
cls.qpp_pointspolygons = QProcessingProject(model=File(f, name=MODEL_FILE_POINTSPOLYGONS.split('/')[1]))
|
|
67
|
+
cls.qpp_pointspolygons.save()
|
|
68
|
+
|
|
69
|
+
cls.qpp_pointspolygons.projects.add(cls.project.instance)
|
|
70
|
+
|
|
71
|
+
with open(cls.model_file_intersections, 'r') as f:
|
|
72
|
+
cls.qpp_intesections = QProcessingProject(model=File(f, name=MODEL_FILE_INTERSECTIONS.split('/')[1]))
|
|
73
|
+
cls.qpp_intesections.save()
|
|
74
|
+
|
|
75
|
+
cls.qpp_intesections.projects.add(cls.project.instance)
|
|
76
|
+
|
|
77
|
+
# Create API REST post data
|
|
78
|
+
# --------------------------------------------
|
|
79
|
+
|
|
80
|
+
cls.buffer_post_data = {
|
|
81
|
+
"inputs": {
|
|
82
|
+
"buffer_layer2": "qprocessing_2f527e7f_4c58_485a_a8b3_6370801c37ae",
|
|
83
|
+
"dissolve": "False",
|
|
84
|
+
"distance": "100000"
|
|
85
|
+
},
|
|
86
|
+
"outputs": {
|
|
87
|
+
"native:buffer_1:Buffer results": "shp"
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
cls.pointspolygons_post_data = {
|
|
92
|
+
"inputs": {
|
|
93
|
+
"points_layer": "qprocessing_2f527e7f_4c58_485a_a8b3_6370801c37ae",
|
|
94
|
+
"polygon_layer": "poligono2_ca3f4152_353f_471b_a7dc_5fd1b1b0bffe",
|
|
95
|
+
"weight_fileds": "value"
|
|
96
|
+
},
|
|
97
|
+
"outputs": {
|
|
98
|
+
"native:countpointsinpolygon_1:Points in polygons":"geojson"
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
cls.pointspolygons_features_post_data = {
|
|
103
|
+
"inputs": {
|
|
104
|
+
"points_layer": "qprocessing_2f527e7f_4c58_485a_a8b3_6370801c37ae",
|
|
105
|
+
"polygon_layer": "poligono2_ca3f4152_353f_471b_a7dc_5fd1b1b0bffe:1",
|
|
106
|
+
"weight_fileds": "value"
|
|
107
|
+
},
|
|
108
|
+
"outputs": {
|
|
109
|
+
"native:countpointsinpolygon_1:Points in polygons": "geojson"
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
def _test_models_results(self):
|
|
114
|
+
|
|
115
|
+
url = reverse('qprocessing-run-model', args=[self.qpp_buffer.pk, self.project.instance.pk])
|
|
116
|
+
|
|
117
|
+
# Login as admin01
|
|
118
|
+
# ----------------------------------------------
|
|
119
|
+
self.client.login(username=self.test_admin1.username, password=self.test_admin1.username)
|
|
120
|
+
|
|
121
|
+
# Testing MODEL_FILE_BUFFER processing model
|
|
122
|
+
# _____________________________________________
|
|
123
|
+
res = self.client.post(url, data=self.buffer_post_data, content_type='application/json')
|
|
124
|
+
self.assertEqual(res.status_code, 200)
|
|
125
|
+
jres = json.loads(res.content)
|
|
126
|
+
self.assertTrue(res.status_code, 200)
|
|
127
|
+
|
|
128
|
+
if settings.QPROCESSING_ASYNC_RUN:
|
|
129
|
+
|
|
130
|
+
# Wait for processing result
|
|
131
|
+
time.sleep(1)
|
|
132
|
+
task_info_url = reverse('qprocessing-infotask', args=[jres['task_id']])
|
|
133
|
+
|
|
134
|
+
res = self.client.get(task_info_url)
|
|
135
|
+
self.assertEqual(res.status_code, 200)
|
|
136
|
+
|
|
137
|
+
jres = json.loads(res.content)
|
|
138
|
+
|
|
139
|
+
self.assertEqual(jres['status'], 'complete')
|
|
140
|
+
self.assertEqual(jres['exception'], '')
|
|
141
|
+
self.assertEqual(jres['progress'], 0)
|
|
142
|
+
# self.assertEqual(jres['task_result'], {'native:buffer_1:Buffer results': '/qprocessing/api/download/gAAAAABkq-KAS1ExRItPOBL1Bi8CTBcl0INK6VL9ipYiQjy-48Fmo4r-2ERNDMa0paQcG_2vzFVFY11Lgmcl_qCN1qsykovFOQFf67CtJhK3OLvPU7HjqygYSX0rdJrwv0NE_xADuBdZ02orxcH_IxU24-bib8YMsw==/'})
|
|
143
|
+
|
|
144
|
+
# Get file result file ZIP
|
|
145
|
+
res = self.client.get(jres['task_result']['native:buffer_1:Buffer results'])
|
|
146
|
+
|
|
147
|
+
else:
|
|
148
|
+
|
|
149
|
+
self.assertTrue(jres['result'])
|
|
150
|
+
|
|
151
|
+
# Download the result
|
|
152
|
+
res = self.client.get(jres['data']['native:buffer_1:Buffer results'])
|
|
153
|
+
self.assertEqual(res.status_code, 200)
|
|
154
|
+
self.assertEqual(res.headers['Content-Type'], 'application/x-zip-compressed')
|
|
155
|
+
self.assertTrue('buffer-results' in res.headers['Content-Disposition'])
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
# For return shp:
|
|
159
|
+
z = zipfile.ZipFile(BytesIO(res.content))
|
|
160
|
+
temp = QTemporaryDir()
|
|
161
|
+
z.extractall(temp.path())
|
|
162
|
+
vl = QgsVectorLayer(temp.path())
|
|
163
|
+
self.assertTrue(vl.isValid())
|
|
164
|
+
|
|
165
|
+
fields = [f for f in vl.fields()]
|
|
166
|
+
|
|
167
|
+
self.assertEqual(len(fields), 3)
|
|
168
|
+
self.assertEqual(fields[0].name(), 'fid')
|
|
169
|
+
self.assertEqual(fields[1].name(), 'name')
|
|
170
|
+
self.assertEqual(fields[2].name(), 'value')
|
|
171
|
+
|
|
172
|
+
self.assertEqual(QgsWkbTypes.geometryDisplayString(vl.geometryType()), 'Polygon')
|
|
173
|
+
|
|
174
|
+
# Testing MODEL_FILE_POINTSPOLYGONS processing model
|
|
175
|
+
# ______________________________________________________
|
|
176
|
+
url = reverse('qprocessing-run-model', args=[self.qpp_pointspolygons.pk, self.project.instance.pk])
|
|
177
|
+
res = self.client.post(url, data=self.pointspolygons_post_data, content_type='application/json')
|
|
178
|
+
self.assertEqual(res.status_code, 200)
|
|
179
|
+
jres = json.loads(res.content)
|
|
180
|
+
|
|
181
|
+
if settings.QPROCESSING_ASYNC_RUN:
|
|
182
|
+
|
|
183
|
+
# Wait for processing result
|
|
184
|
+
time.sleep(1)
|
|
185
|
+
task_info_url = reverse('qprocessing-infotask', args=[jres['task_id']])
|
|
186
|
+
|
|
187
|
+
res = self.client.get(task_info_url)
|
|
188
|
+
self.assertEqual(res.status_code, 200)
|
|
189
|
+
|
|
190
|
+
jres = json.loads(res.content)
|
|
191
|
+
|
|
192
|
+
self.assertEqual(jres['status'], 'complete')
|
|
193
|
+
self.assertEqual(jres['exception'], '')
|
|
194
|
+
self.assertEqual(jres['progress'], 0)
|
|
195
|
+
|
|
196
|
+
# Get file result file GeoJson
|
|
197
|
+
res = self.client.get(jres['task_result']['native:countpointsinpolygon_1:Points in polygons'])
|
|
198
|
+
self.assertTrue(res.status_code, 200)
|
|
199
|
+
|
|
200
|
+
else:
|
|
201
|
+
|
|
202
|
+
self.assertTrue(jres['result'])
|
|
203
|
+
|
|
204
|
+
# Download the result
|
|
205
|
+
res = self.client.get(jres['data']['native:countpointsinpolygon_1:Points in polygons'])
|
|
206
|
+
self.assertEqual(res.status_code, 200)
|
|
207
|
+
self.assertEqual(res.headers['Content-Type'], 'application/json')
|
|
208
|
+
self.assertTrue('points-in-polygons' in res.headers['Content-Disposition'])
|
|
209
|
+
|
|
210
|
+
temp = QTemporaryDir()
|
|
211
|
+
z.extractall(temp.path())
|
|
212
|
+
result_file = f'{temp.path()}/result.geojson'
|
|
213
|
+
with open(result_file, 'w') as f:
|
|
214
|
+
f.write(res.content.decode())
|
|
215
|
+
vl = QgsVectorLayer(result_file)
|
|
216
|
+
self.assertTrue(vl.isValid())
|
|
217
|
+
|
|
218
|
+
self.assertEqual(vl.featureCount(), 4)
|
|
219
|
+
self.assertEqual(QgsWkbTypes.geometryDisplayString(vl.geometryType()), 'Polygon')
|
|
220
|
+
|
|
221
|
+
res = self.client.post(url, data=self.pointspolygons_features_post_data, content_type='application/json')
|
|
222
|
+
self.assertEqual(res.status_code, 200)
|
|
223
|
+
jres = json.loads(res.content)
|
|
224
|
+
|
|
225
|
+
if settings.QPROCESSING_ASYNC_RUN:
|
|
226
|
+
# Wait for processing result
|
|
227
|
+
time.sleep(1)
|
|
228
|
+
task_info_url = reverse('qprocessing-infotask', args=[jres['task_id']])
|
|
229
|
+
|
|
230
|
+
res = self.client.get(task_info_url)
|
|
231
|
+
self.assertEqual(res.status_code, 200)
|
|
232
|
+
|
|
233
|
+
jres = json.loads(res.content)
|
|
234
|
+
|
|
235
|
+
self.assertEqual(jres['status'], 'complete')
|
|
236
|
+
self.assertEqual(jres['exception'], '')
|
|
237
|
+
self.assertEqual(jres['progress'], 0)
|
|
238
|
+
|
|
239
|
+
# Get file result file GeoJson
|
|
240
|
+
res = self.client.get(jres['task_result']['native:countpointsinpolygon_1:Points in polygons'])
|
|
241
|
+
self.assertTrue(res.status_code, 200)
|
|
242
|
+
|
|
243
|
+
else:
|
|
244
|
+
|
|
245
|
+
self.assertTrue(jres['result'])
|
|
246
|
+
|
|
247
|
+
# Download the result
|
|
248
|
+
res = self.client.get(jres['data']['native:countpointsinpolygon_1:Points in polygons'])
|
|
249
|
+
self.assertEqual(res.status_code, 200)
|
|
250
|
+
self.assertEqual(res.headers['Content-Type'], 'application/json')
|
|
251
|
+
self.assertTrue('points-in-polygons' in res.headers['Content-Disposition'])
|
|
252
|
+
|
|
253
|
+
temp = QTemporaryDir()
|
|
254
|
+
z.extractall(temp.path())
|
|
255
|
+
result_file = f'{temp.path()}/result.geojson'
|
|
256
|
+
with open(result_file, 'w') as f:
|
|
257
|
+
f.write(res.content.decode())
|
|
258
|
+
vl = QgsVectorLayer(result_file)
|
|
259
|
+
self.assertTrue(vl.isValid())
|
|
260
|
+
|
|
261
|
+
self.assertEqual(vl.featureCount(), 1)
|
|
262
|
+
self.assertEqual(QgsWkbTypes.geometryDisplayString(vl.geometryType()), 'Polygon')
|
|
263
|
+
|
|
264
|
+
self.client.logout()
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
@override_settings(
|
|
269
|
+
QPROCESSING_ASYNC_RUN=True,
|
|
270
|
+
HUEY={
|
|
271
|
+
# Huey implementation to use.
|
|
272
|
+
'huey_class': 'huey.RedisExpireHuey',
|
|
273
|
+
'name': 'g3w-suite',
|
|
274
|
+
'url': 'redis://localhost:6379/?db=0',
|
|
275
|
+
'immediate': True, #run synchronously.
|
|
276
|
+
'consumer': {
|
|
277
|
+
'workers': 1,
|
|
278
|
+
'worker_type': 'process',
|
|
279
|
+
},
|
|
280
|
+
}
|
|
281
|
+
)
|
|
282
|
+
def test_run_model_async(self):
|
|
283
|
+
"""
|
|
284
|
+
Testing run models in asynchronous mode
|
|
285
|
+
"""
|
|
286
|
+
|
|
287
|
+
self._test_models_results()
|
|
288
|
+
|
|
289
|
+
@override_settings(
|
|
290
|
+
QPROCESSING_ASYNC_RUN=False,
|
|
291
|
+
)
|
|
292
|
+
def test_run_model_sync(self):
|
|
293
|
+
"""
|
|
294
|
+
Testing run models in synchronous mode
|
|
295
|
+
"""
|
|
296
|
+
|
|
297
|
+
self._test_models_results()
|
|
298
|
+
|
|
299
|
+
def test_permissions(self):
|
|
300
|
+
"""
|
|
301
|
+
Test permissions
|
|
302
|
+
"""
|
|
303
|
+
|
|
304
|
+
url = reverse('qprocessing-run-model', args=[self.qpp_buffer.pk, self.project.instance.pk])
|
|
305
|
+
|
|
306
|
+
# Login as admin01
|
|
307
|
+
# ----------------------------------------------
|
|
308
|
+
self.client.login(username=self.test_admin1.username, password=self.test_admin1.username)
|
|
309
|
+
|
|
310
|
+
res = self.client.post(url, data=self.buffer_post_data, content_type='application/json')
|
|
311
|
+
self.assertEqual(res.status_code, 200)
|
|
312
|
+
jres = json.loads(res.content)
|
|
313
|
+
|
|
314
|
+
self.client.logout()
|
|
315
|
+
|
|
316
|
+
# Login as Viewer1
|
|
317
|
+
# ----------------------------------------------
|
|
318
|
+
self.client.login(username=self.test_viewer1.username, password=self.test_viewer1.username)
|
|
319
|
+
|
|
320
|
+
res = self.client.post(url, data=self.buffer_post_data, content_type='application/json')
|
|
321
|
+
self.assertEqual(res.status_code, 403)
|
|
322
|
+
|
|
323
|
+
# Assign permission
|
|
324
|
+
# Give permission to Viewer1
|
|
325
|
+
assign_perm('view_project', self.test_viewer1, self.project.instance)
|
|
326
|
+
assign_perm('run_model', self.test_viewer1, self.qpp_buffer)
|
|
327
|
+
|
|
328
|
+
res = self.client.post(url, data=self.buffer_post_data, content_type='application/json')
|
|
329
|
+
self.assertEqual(res.status_code, 200)
|
|
330
|
+
|
|
331
|
+
self.client.logout()
|
|
332
|
+
|
|
333
|
+
def __check_filename_uploaded(self, uploading_file:str, uploaded_file:str, user_pk:int) -> None:
|
|
334
|
+
"""
|
|
335
|
+
Private method for comparison of uploading file name and uploaded file name.
|
|
336
|
+
Is used Django FileSystemStorage class that replace file name if a file with same name exists:
|
|
337
|
+
- uploding_file = 'point_to_upload.geojson'
|
|
338
|
+
- uploaded_file = 'point_to_upload_zTF77Rn.geojson'
|
|
339
|
+
"""
|
|
340
|
+
|
|
341
|
+
if "/" in uploading_file:
|
|
342
|
+
uploading_file = uploading_file.split("/")[1]
|
|
343
|
+
|
|
344
|
+
foname, foext = uploaded_file.split(".")
|
|
345
|
+
foname = f'{"_".join(foname.split("_")[:-1])}.{foext}'
|
|
346
|
+
|
|
347
|
+
self.assertTrue(foname == uploading_file or uploaded_file == uploading_file)
|
|
348
|
+
|
|
349
|
+
# Check filesystem
|
|
350
|
+
self.assertTrue(os.path.exists(f"{settings.QPROCESSING_OUTPUT_PATH}{str(user_pk)}/uploads/{uploaded_file}"))
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
def __create_simplesploadedsile(self, file_to_upload:str, mime_type:str="application/json") -> list:
|
|
354
|
+
"""
|
|
355
|
+
Create a list of SimpleUploadedFile instance for self.client.post data
|
|
356
|
+
"""
|
|
357
|
+
|
|
358
|
+
with open(os.path.join(CURRENT_PATH, TEST_BASE_PATH, file_to_upload), 'rb') as f:
|
|
359
|
+
geofile = [
|
|
360
|
+
SimpleUploadedFile(file_to_upload.split('/')[1], f.read(), content_type=mime_type)
|
|
361
|
+
]
|
|
362
|
+
return geofile
|
|
363
|
+
|
|
364
|
+
def __deleted_upaded_files(self, uploaded_filed:list) -> None:
|
|
365
|
+
pass
|
|
366
|
+
|
|
367
|
+
def test_input_upload(self):
|
|
368
|
+
"""
|
|
369
|
+
Test input upload API REST
|
|
370
|
+
"""
|
|
371
|
+
|
|
372
|
+
url = reverse(
|
|
373
|
+
'qprocessing-action-input-upload',
|
|
374
|
+
args=[self.qpp_buffer.pk, self.project.instance.pk, 'buffer_layer2']
|
|
375
|
+
)
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
# Login as admin01
|
|
379
|
+
# ----------------------------------------------
|
|
380
|
+
self.client.login(
|
|
381
|
+
username=self.test_admin1.username, password=self.test_admin1.username
|
|
382
|
+
)
|
|
383
|
+
|
|
384
|
+
# Check for input buffer_layer2: point and polygon
|
|
385
|
+
# ------------------------------------------------
|
|
386
|
+
# Point
|
|
387
|
+
geofile = self.__create_simplesploadedsile(UPLOAD_FILE_POINT)
|
|
388
|
+
|
|
389
|
+
res = self.client.post(url, data={'file': geofile})
|
|
390
|
+
|
|
391
|
+
self.assertEqual(res.status_code, 200)
|
|
392
|
+
jres = json.loads(res.content)
|
|
393
|
+
|
|
394
|
+
self.assertTrue(jres['result'])
|
|
395
|
+
|
|
396
|
+
qpiu = QProcessingInputUpload.objects.get(uuid= jres['data']['file'])
|
|
397
|
+
self.__check_filename_uploaded(UPLOAD_FILE_POINT, qpiu.name, self.test_admin1.pk)
|
|
398
|
+
|
|
399
|
+
# Polygon
|
|
400
|
+
geofile = self.__create_simplesploadedsile(UPLOAD_FILE_POLYGON)
|
|
401
|
+
|
|
402
|
+
res = self.client.post(url, data={"file": geofile})
|
|
403
|
+
|
|
404
|
+
self.assertEqual(res.status_code, 200)
|
|
405
|
+
jres = json.loads(res.content)
|
|
406
|
+
|
|
407
|
+
self.assertTrue(jres["result"])
|
|
408
|
+
|
|
409
|
+
qpiu = QProcessingInputUpload.objects.get(uuid=jres["data"]["file"])
|
|
410
|
+
self.__check_filename_uploaded(UPLOAD_FILE_POLYGON, qpiu.name, self.test_admin1.pk)
|
|
411
|
+
|
|
412
|
+
# Line: not possible
|
|
413
|
+
geofile = self.__create_simplesploadedsile(UPLOAD_FILE_LINESTRING)
|
|
414
|
+
|
|
415
|
+
res = self.client.post(url, data={"file": geofile})
|
|
416
|
+
|
|
417
|
+
self.assertEqual(res.status_code, 400)
|
|
418
|
+
jres = json.loads(res.content)
|
|
419
|
+
|
|
420
|
+
self.assertFalse(jres["result"])
|
|
421
|
+
self.assertTrue("must have a geometry type of: point,polygon" in jres["error"])
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
# Check upload ZIP file for SHP
|
|
425
|
+
geofile = self.__create_simplesploadedsile(UPLOAD_FILE_SHP_POLYGON_ZIP)
|
|
426
|
+
|
|
427
|
+
res = self.client.post(url, data={"file": geofile})
|
|
428
|
+
|
|
429
|
+
self.assertEqual(res.status_code, 200)
|
|
430
|
+
jres = json.loads(res.content)
|
|
431
|
+
|
|
432
|
+
self.assertTrue(jres["result"])
|
|
433
|
+
|
|
434
|
+
qpiu = QProcessingInputUpload.objects.get(uuid=jres["data"]["file"])
|
|
435
|
+
self.assertEqual(f'{UPLOAD_FILE_SHP_POLYGON_ZIP.split("/")[-1].split(".")[0]}.shp', qpiu.name)
|
|
436
|
+
|
|
437
|
+
for ext in settings.QPROCESSING_INPUT_SHP_EXTS:
|
|
438
|
+
self.assertTrue(
|
|
439
|
+
os.path.exists(
|
|
440
|
+
f"{settings.QPROCESSING_OUTPUT_PATH}{str(self.test_admin1.pk)}/uploads/{qpiu.name.split('.')[0]}.{ext}"
|
|
441
|
+
)
|
|
442
|
+
)
|
|
443
|
+
|
|
444
|
+
# Upload not correct ZIP file for shp
|
|
445
|
+
geofile = self.__create_simplesploadedsile(UPLOAD_FILE_SHP_POLYGON_ZIP_FAIL)
|
|
446
|
+
|
|
447
|
+
res = self.client.post(url, data={"file": geofile})
|
|
448
|
+
|
|
449
|
+
self.assertEqual(res.status_code, 400)
|
|
450
|
+
jres = json.loads(res.content)
|
|
451
|
+
|
|
452
|
+
self.assertFalse(jres["result"])
|
|
453
|
+
self.assertEqual(jres["error"], "Zip file for shape files is not correct. "
|
|
454
|
+
"Missing the following files type: prj, shx")
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
# Logout
|
|
458
|
+
# ----------------------------------------------
|
|
459
|
+
self.client.logout()
|
qprocessing/urls.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# coding=utf-8
|
|
2
|
+
""" Qprocessing django urls module.
|
|
3
|
+
.. note:: This program is free software; you can redistribute it and/or modify
|
|
4
|
+
it under the terms of the Mozilla Public License 2.0.
|
|
5
|
+
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
__author__ = 'lorenzetti@gis3w.it'
|
|
9
|
+
__date__ = '2023-05-09'
|
|
10
|
+
__copyright__ = 'Copyright 2015 - 2023, Gis3w'
|
|
11
|
+
__license__ = 'MPL 2.0'
|
|
12
|
+
|
|
13
|
+
from django.urls import path
|
|
14
|
+
from django.contrib.auth.decorators import login_required
|
|
15
|
+
from base.urls import G3W_SITETREE_I18N_ALIAS
|
|
16
|
+
from .views import \
|
|
17
|
+
QProcessingProjectsListView, \
|
|
18
|
+
QProcessingProjectAddView, \
|
|
19
|
+
QProcessingProjectUpdateView, \
|
|
20
|
+
QProcessingProjectDeleteView
|
|
21
|
+
|
|
22
|
+
G3W_SITETREE_I18N_ALIAS.append('qprocessing')
|
|
23
|
+
|
|
24
|
+
urlpatterns = [
|
|
25
|
+
|
|
26
|
+
# For QProcessingProject CRUD
|
|
27
|
+
path('projects/', login_required(QProcessingProjectsListView.as_view()), name='qprocessing-project-list'),
|
|
28
|
+
path('projects/add/', login_required(QProcessingProjectAddView.as_view()), name='qprocessing-project-add'),
|
|
29
|
+
path('projects/update/<int:pk>/', login_required(QProcessingProjectUpdateView.as_view()),
|
|
30
|
+
name='qprocessing-project-update'),
|
|
31
|
+
path('projects/delete/<int:pk>/', login_required(QProcessingProjectDeleteView.as_view()),
|
|
32
|
+
name='qprocessing-project-delete'),
|
|
33
|
+
]
|
|
File without changes
|