From 0ea7cb754127c6bcce8598eedbf47afd99960b26 Mon Sep 17 00:00:00 2001 From: Lova ANDRIARIMALALA <43842786+Xpirix@users.noreply.github.com> Date: Tue, 23 Apr 2024 12:07:48 +0300 Subject: [PATCH 1/2] Add Layer Definition File and 3D Model to the resource API --- qgis-app/api/serializers.py | 17 ++++++ qgis-app/api/tests/test_views.py | 98 ++++++++++++++++++++++++-------- qgis-app/api/views.py | 18 +++++- 3 files changed, 108 insertions(+), 25 deletions(-) diff --git a/qgis-app/api/serializers.py b/qgis-app/api/serializers.py index 062392fa..76f64b27 100644 --- a/qgis-app/api/serializers.py +++ b/qgis-app/api/serializers.py @@ -4,6 +4,8 @@ from rest_framework import serializers from sorl_thumbnail_serializer.fields import HyperlinkedSorlImageField from styles.models import Style +from layerdefinitions.models import LayerDefinition +from wavefronts.models import Wavefront class ResourceBaseSerializer(serializers.ModelSerializer): @@ -62,3 +64,18 @@ class StyleSerializer(ResourceBaseSerializer): class Meta(ResourceBaseSerializer.Meta): model = Style + +class LayerDefinitionSerializer(ResourceBaseSerializer): + class Meta(ResourceBaseSerializer.Meta): + model = LayerDefinition + + def get_resource_subtype(self, obj): + return None + + +class WavefrontSerializer(ResourceBaseSerializer): + class Meta(ResourceBaseSerializer.Meta): + model = Wavefront + + def get_resource_subtype(self, obj): + return None \ No newline at end of file diff --git a/qgis-app/api/tests/test_views.py b/qgis-app/api/tests/test_views.py index 9f0ff30b..5ec477d0 100644 --- a/qgis-app/api/tests/test_views.py +++ b/qgis-app/api/tests/test_views.py @@ -12,6 +12,8 @@ from geopackages.models import Geopackage from models.models import Model from styles.models import Style, StyleType +from layerdefinitions.models import LayerDefinition +from wavefronts.models import Wavefront @override_settings(MEDIA_ROOT="api/tests") @@ -75,6 +77,26 @@ def setUp(self): approved=True, ) + # create LayerDefinition + LayerDefinition.objects.create( + creator=self.creator0, + name="flooded buildings extractor", + description="A LayerDefinition for testing purpose", + thumbnail_image=self.thumbnail, + file=self.file, + approved=True, + ) + + # create Wavefront + Wavefront.objects.create( + creator=self.creator0, + name="flooded buildings extractor", + description="A Wavefront for testing purpose", + thumbnail_image=self.thumbnail, + file=self.file, + approved=True, + ) + def tearDown(self): pass @@ -83,7 +105,7 @@ def test_get_list_resources(self): url = reverse("resource-list") response = self.client.get(url) json_parse = json.loads(response.content) - self.assertEqual(json_parse["total"], 3) + self.assertEqual(json_parse["total"], 5) result = json_parse["results"] for i, d in enumerate(result): if d["resource_type"] == "Geopackage": @@ -92,15 +114,32 @@ def test_get_list_resources(self): m_index = i elif d["resource_type"] == "Style": s_index = i + elif d["resource_type"] == "LayerDefinition": + l_index = i + elif d["resource_type"] == "Wavefront": + w_index = i self.assertIsNotNone(g_index) self.assertIsNotNone(m_index) self.assertIsNotNone(s_index) + self.assertIsNotNone(l_index) + self.assertIsNotNone(w_index) + self.assertEqual(result[g_index]["resource_type"], "Geopackage") self.assertEqual(result[g_index]["resource_subtype"], None) self.assertEqual(result[g_index]["creator"], "creator") + self.assertEqual(result[m_index]["resource_type"], "Model") self.assertEqual(result[m_index]["resource_subtype"], None) self.assertEqual(result[m_index]["creator"], "creator 0") + + self.assertEqual(result[l_index]["resource_type"], "LayerDefinition") + self.assertEqual(result[l_index]["resource_subtype"], None) + self.assertEqual(result[l_index]["creator"], "creator 0") + + self.assertEqual(result[w_index]["resource_type"], "Wavefront") + self.assertEqual(result[w_index]["resource_subtype"], None) + self.assertEqual(result[w_index]["creator"], "creator 0") + self.assertEqual(result[s_index]["resource_type"], "Style") self.assertEqual(result[s_index]["resource_subtype"], "Marker") self.assertEqual(result[s_index]["creator"], "creator") @@ -117,6 +156,8 @@ def test_get_list_resources_with_filter(self): g_index = None m_index = None s_index = None + l_index = None + w_index = None for i, d in enumerate(result): if d["resource_type"] == "Geopackage": g_index = i @@ -124,30 +165,15 @@ def test_get_list_resources_with_filter(self): m_index = i elif d["resource_type"] == "Style": s_index = i + elif d["resource_type"] == "LayerDefinition": + l_index = i + elif d["resource_type"] == "Wavefront": + w_index = i self.assertIsNotNone(g_index) self.assertIsNone(m_index) self.assertIsNone(s_index) - - def test_get_list_resources_with_filter_resource_type(self): - param = "resource_type=geopackage" - url = "%s?%s" % (reverse("resource-list"), param) - response = self.client.get(url) - json_parse = json.loads(response.content) - self.assertEqual(json_parse["total"], 1) - result = json_parse["results"] - g_index = None - m_index = None - s_index = None - for i, d in enumerate(result): - if d["resource_type"] == "Geopackage": - g_index = i - elif d["resource_type"] == "Model": - m_index = i - elif d["resource_type"] == "Style": - s_index = i - self.assertIsNotNone(g_index) - self.assertIsNone(m_index) - self.assertIsNone(s_index) + self.assertIsNone(l_index) + self.assertIsNone(w_index) def test_get_list_resources_with_filter_resource_subtype(self): param = "resource_subtype=Marker" @@ -159,6 +185,8 @@ def test_get_list_resources_with_filter_resource_subtype(self): g_index = None m_index = None s_index = None + l_index = None + w_index = None for i, d in enumerate(result): if d["resource_type"] == "Geopackage": g_index = i @@ -166,8 +194,14 @@ def test_get_list_resources_with_filter_resource_subtype(self): m_index = i elif d["resource_type"] == "Style": s_index = i + elif d["resource_type"] == "LayerDefinition": + l_index = i + elif d["resource_type"] == "Wavefront": + w_index = i self.assertIsNone(g_index) self.assertIsNone(m_index) + self.assertIsNone(l_index) + self.assertIsNone(w_index) self.assertIsNotNone(s_index) def test_get_list_resources_with_filter_creator(self): @@ -175,11 +209,13 @@ def test_get_list_resources_with_filter_creator(self): url = "%s?%s" % (reverse("resource-list"), param) response = self.client.get(url) json_parse = json.loads(response.content) - self.assertEqual(json_parse["total"], 1) + self.assertEqual(json_parse["total"], 3) result = json_parse["results"] g_index = None m_index = None s_index = None + l_index = None + w_index = None for i, d in enumerate(result): if d["resource_type"] == "Geopackage": g_index = i @@ -187,8 +223,14 @@ def test_get_list_resources_with_filter_creator(self): m_index = i elif d["resource_type"] == "Style": s_index = i + elif d["resource_type"] == "LayerDefinition": + l_index = i + elif d["resource_type"] == "Wavefront": + w_index = i self.assertIsNone(g_index) self.assertIsNotNone(m_index) + self.assertIsNotNone(l_index) + self.assertIsNotNone(w_index) self.assertIsNone(s_index) def test_get_list_resources_with_filter_keyword(self): @@ -196,11 +238,13 @@ def test_get_list_resources_with_filter_keyword(self): url = "%s?%s" % (reverse("resource-list"), param) response = self.client.get(url) json_parse = json.loads(response.content) - self.assertEqual(json_parse["total"], 3) + self.assertEqual(json_parse["total"], 5) result = json_parse["results"] g_index = None m_index = None s_index = None + l_index = None + w_index = None for i, d in enumerate(result): if d["resource_type"] == "Geopackage": g_index = i @@ -208,9 +252,15 @@ def test_get_list_resources_with_filter_keyword(self): m_index = i elif d["resource_type"] == "Style": s_index = i + elif d["resource_type"] == "LayerDefinition": + l_index = i + elif d["resource_type"] == "Wavefront": + w_index = i self.assertIsNotNone(g_index) self.assertIsNotNone(m_index) self.assertIsNotNone(s_index) + self.assertIsNotNone(l_index) + self.assertIsNotNone(w_index) def test_download_resource_should_be_a_file_in_a_zip(self): url = reverse("resource-download", kwargs={"uuid": self.style.uuid}) diff --git a/qgis-app/api/views.py b/qgis-app/api/views.py index 5d392d6f..84dc80ec 100644 --- a/qgis-app/api/views.py +++ b/qgis-app/api/views.py @@ -1,6 +1,6 @@ from collections import OrderedDict -from api.serializers import GeopackageSerializer, ModelSerializer, StyleSerializer +from api.serializers import GeopackageSerializer, ModelSerializer, StyleSerializer, LayerDefinitionSerializer, WavefrontSerializer from base.license import zip_a_file_if_not_zipfile from django.contrib.postgres.search import SearchVector from django.http import Http404, HttpResponse @@ -16,6 +16,8 @@ from rest_framework import filters, permissions from rest_framework.views import APIView from styles.models import Style +from layerdefinitions.models import LayerDefinition +from wavefronts.models import Wavefront def filter_resource_type(queryset, request, *args, **kwargs): @@ -115,6 +117,16 @@ class ResourceAPIList(FlatMultipleModelAPIView): "serializer_class": StyleSerializer, "filter_fn": filter_general, }, + { + "queryset": LayerDefinition.approved_objects.all(), + "serializer_class": LayerDefinitionSerializer, + "filter_fn": filter_general, + }, + { + "queryset": Wavefront.approved_objects.all(), + "serializer_class": WavefrontSerializer, + "filter_fn": filter_general, + }, ] @@ -135,6 +147,10 @@ def get(self, request, *args, **kwargs): object = Model.approved_objects.get(uuid=uuid) elif Style.approved_objects.filter(uuid=uuid).exists(): object = Style.approved_objects.get(uuid=uuid) + elif LayerDefinition.approved_objects.filter(uuid=uuid).exists(): + object = LayerDefinition.approved_objects.get(uuid=uuid) + elif Wavefront.approved_objects.filter(uuid=uuid).exists(): + object = Wavefront.approved_objects.get(uuid=uuid) else: raise Http404 From 7b22890f549b4337de3424849c0e516246703e96 Mon Sep 17 00:00:00 2001 From: Lova ANDRIARIMALALA <43842786+Xpirix@users.noreply.github.com> Date: Wed, 24 Apr 2024 11:27:17 +0300 Subject: [PATCH 2/2] Chage Wavefront to 3DModel in the resource API --- qgis-app/api/serializers.py | 2 ++ qgis-app/api/tests/test_views.py | 16 ++++++++-------- qgis-app/api/views.py | 2 ++ 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/qgis-app/api/serializers.py b/qgis-app/api/serializers.py index 76f64b27..fe6d4a5a 100644 --- a/qgis-app/api/serializers.py +++ b/qgis-app/api/serializers.py @@ -40,6 +40,8 @@ def validate(self, attrs): return attrs def get_resource_type(self, obj): + if self.Meta.model.__name__ == "Wavefront": + return "3DModel" return self.Meta.model.__name__ diff --git a/qgis-app/api/tests/test_views.py b/qgis-app/api/tests/test_views.py index 5ec477d0..ce1c23ff 100644 --- a/qgis-app/api/tests/test_views.py +++ b/qgis-app/api/tests/test_views.py @@ -87,11 +87,11 @@ def setUp(self): approved=True, ) - # create Wavefront + # create 3D model Wavefront.objects.create( creator=self.creator0, name="flooded buildings extractor", - description="A Wavefront for testing purpose", + description="A 3D model for testing purpose", thumbnail_image=self.thumbnail, file=self.file, approved=True, @@ -116,7 +116,7 @@ def test_get_list_resources(self): s_index = i elif d["resource_type"] == "LayerDefinition": l_index = i - elif d["resource_type"] == "Wavefront": + elif d["resource_type"] == "3DModel": w_index = i self.assertIsNotNone(g_index) self.assertIsNotNone(m_index) @@ -136,7 +136,7 @@ def test_get_list_resources(self): self.assertEqual(result[l_index]["resource_subtype"], None) self.assertEqual(result[l_index]["creator"], "creator 0") - self.assertEqual(result[w_index]["resource_type"], "Wavefront") + self.assertEqual(result[w_index]["resource_type"], "3DModel") self.assertEqual(result[w_index]["resource_subtype"], None) self.assertEqual(result[w_index]["creator"], "creator 0") @@ -167,7 +167,7 @@ def test_get_list_resources_with_filter(self): s_index = i elif d["resource_type"] == "LayerDefinition": l_index = i - elif d["resource_type"] == "Wavefront": + elif d["resource_type"] == "3DModel": w_index = i self.assertIsNotNone(g_index) self.assertIsNone(m_index) @@ -196,7 +196,7 @@ def test_get_list_resources_with_filter_resource_subtype(self): s_index = i elif d["resource_type"] == "LayerDefinition": l_index = i - elif d["resource_type"] == "Wavefront": + elif d["resource_type"] == "3DModel": w_index = i self.assertIsNone(g_index) self.assertIsNone(m_index) @@ -225,7 +225,7 @@ def test_get_list_resources_with_filter_creator(self): s_index = i elif d["resource_type"] == "LayerDefinition": l_index = i - elif d["resource_type"] == "Wavefront": + elif d["resource_type"] == "3DModel": w_index = i self.assertIsNone(g_index) self.assertIsNotNone(m_index) @@ -254,7 +254,7 @@ def test_get_list_resources_with_filter_keyword(self): s_index = i elif d["resource_type"] == "LayerDefinition": l_index = i - elif d["resource_type"] == "Wavefront": + elif d["resource_type"] == "3DModel": w_index = i self.assertIsNotNone(g_index) self.assertIsNotNone(m_index) diff --git a/qgis-app/api/views.py b/qgis-app/api/views.py index 84dc80ec..e53694b9 100644 --- a/qgis-app/api/views.py +++ b/qgis-app/api/views.py @@ -22,6 +22,8 @@ def filter_resource_type(queryset, request, *args, **kwargs): resource_type = request.query_params["resource_type"] + if resource_type.lower() == "3dmodel": + resource_type = "wavefront" if queryset.model.__name__.lower() == resource_type.lower(): return queryset else: