Skip to content

Commit

Permalink
Merge pull request #56 from submarcos/fix_get_url
Browse files Browse the repository at this point in the history
Fix view get_url usage
  • Loading branch information
submarcos authored Nov 4, 2024
2 parents 58671bf + 98fcd70 commit c200324
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
8 changes: 8 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
Changelog
=========

1.0.0 (XXXX-XX-XX)
---------------------

**Bugfixes**

- Fix `get_url` view method. Use class name in lower format as default prefix.


1.0.0 (2024-09-04)
---------------------

Expand Down
6 changes: 3 additions & 3 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Simple layer tile view
urlpatterns = [
...
views.CityTileView.get_url(), # serve tiles at default /tiles/<int:z>/<int:x>/<int:y>
views.CityTileView.get_url(), # serve tiles at default /citytileview/<int:z>/<int:x>/<int:y>
...
]
Expand Down Expand Up @@ -92,7 +92,7 @@ Let's create a second layer.
# in your urls file
urlpatterns = [
...
views.CityAndStateTileView.get_url(), # serve tiles at default /tiles/<int:z>/<int:x>/<int:y>
views.CityAndStateTileView.get_url(), # serve tiles at default /cityandstatetileview/<int:z>/<int:x>/<int:y>
...
]
Expand Down Expand Up @@ -125,7 +125,7 @@ TileJSON and tile views share some data, as vector layers definitions. So we nee
urlpatterns = [
...
views.CityAndStateTileView.get_url(), # serve tiles at /city-and-states/<int:z>/<int:x>/<int:y>
views.CityAndStateTileJSON.get_url(), # serve tilejson at /city-and-states/tiles.json
views.CityAndStateTileJSON.get_urls(tiles_urls=views.CityAndStateTileView.get_url()), # serve tilejson at /city-and-states/tiles.json
...
]
Expand Down
21 changes: 14 additions & 7 deletions vectortiles/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
class BaseVectorView:
layer_classes = None
layers = None
prefix_tiles_url = "tiles"
prefix_url = None

@classmethod
def get_default_prefix_tiles_url(cls):
return cls.prefix_url if cls.prefix_url else cls.__name__.lower()

def get_layer_classes(self):
return self.layer_classes or []
Expand Down Expand Up @@ -166,17 +170,20 @@ def get_content_status(self, z, x, y):
def get_base_url(self):
pass

def get_default_url_pattern(self):
@classmethod
def get_default_url_pattern(cls):
return "{z}/{x}/{y}"

def get_default_url_matrix(self):
pattern = self.get_default_url_pattern()
@classmethod
def get_default_url_matrix(cls):
pattern = cls.get_default_url_pattern()
return f"{pattern.replace('{z}', '<int:z>').replace('{x}', '<int:x>').replace('{y}', '<int:y>')}"

def get_url(self, prefix=None, url_name=None):
@classmethod
def get_url(cls, prefix=None, url_name=None):
"""Generate URL to serve vector tiles with required parameters"""
return path(
f"{prefix or self.prefix_tiles_url}/{self.get_default_url_matrix()}",
self.as_view(),
f"{prefix or cls.get_default_prefix_tiles_url()}/{cls.get_default_url_matrix()}",
cls.as_view(),
name=url_name,
)
27 changes: 26 additions & 1 deletion vectortiles/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.urls import reverse

from test_vectortiles.test_app.models import Feature, Layer
from vectortiles.views import TileJSONView
from vectortiles.views import MVTView, TileJSONView


class VectorTileBaseTest(TestCase):
Expand Down Expand Up @@ -82,6 +82,31 @@ def test_features_with_filtered_date(self):
},
)

def test_get_url_defined_prefix_in_attribute(self):
class TestView(MVTView):
prefix_url = "test"

self.assertEqual(
str(TestView.get_url()), "<URLPattern 'test/<int:z>/<int:x>/<int:y>'>"
)

def test_get_url_defined_prefix_in_param(self):
class TestView(MVTView):
pass

self.assertEqual(
str(TestView.get_url(prefix="toto")),
"<URLPattern 'toto/<int:z>/<int:x>/<int:y>'>",
)

def test_get_url_default_prefix(self):
class TestView(MVTView):
pass

self.assertEqual(
str(TestView.get_url()), "<URLPattern 'testview/<int:z>/<int:x>/<int:y>'>"
)


class VectorTileTileJSONTestCase(VectorTileBaseTest):
def test_features(self):
Expand Down

0 comments on commit c200324

Please sign in to comment.