Skip to content

Commit

Permalink
Add Layer Definition File and 3D Model to the resource API (#388)
Browse files Browse the repository at this point in the history
* Add Layer Definition File and 3D Model to the resource API

* Chage Wavefront to 3DModel in the resource API
  • Loading branch information
Xpirix authored Jun 19, 2024
1 parent 148aebe commit 207457e
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 25 deletions.
19 changes: 19 additions & 0 deletions qgis-app/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -38,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__


Expand All @@ -62,3 +66,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
98 changes: 74 additions & 24 deletions qgis-app/api/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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 3D model
Wavefront.objects.create(
creator=self.creator0,
name="flooded buildings extractor",
description="A 3D model for testing purpose",
thumbnail_image=self.thumbnail,
file=self.file,
approved=True,
)

def tearDown(self):
pass

Expand All @@ -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":
Expand All @@ -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"] == "3DModel":
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"], "3DModel")
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")
Expand All @@ -117,37 +156,24 @@ 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
elif d["resource_type"] == "Model":
m_index = i
elif d["resource_type"] == "Style":
s_index = i
elif d["resource_type"] == "LayerDefinition":
l_index = i
elif d["resource_type"] == "3DModel":
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"
Expand All @@ -159,58 +185,82 @@ 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
elif d["resource_type"] == "Model":
m_index = i
elif d["resource_type"] == "Style":
s_index = i
elif d["resource_type"] == "LayerDefinition":
l_index = i
elif d["resource_type"] == "3DModel":
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):
param = "creator=creator 0"
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
elif d["resource_type"] == "Model":
m_index = i
elif d["resource_type"] == "Style":
s_index = i
elif d["resource_type"] == "LayerDefinition":
l_index = i
elif d["resource_type"] == "3DModel":
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):
param = "keyword=testing"
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
elif d["resource_type"] == "Model":
m_index = i
elif d["resource_type"] == "Style":
s_index = i
elif d["resource_type"] == "LayerDefinition":
l_index = i
elif d["resource_type"] == "3DModel":
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})
Expand Down
20 changes: 19 additions & 1 deletion qgis-app/api/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,10 +16,14 @@
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):
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:
Expand Down Expand Up @@ -115,6 +119,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,
},
]


Expand All @@ -135,6 +149,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

Expand Down

0 comments on commit 207457e

Please sign in to comment.