Move _urls_for_js to utils.py (circular imports)

This commit is contained in:
David Larlet 2023-12-19 20:55:19 -05:00
parent 7ff543e1d5
commit 8d11815625
No known key found for this signature in database
GPG key ID: 3E2953A359E7E7BD
3 changed files with 18 additions and 19 deletions

View file

@ -1,4 +1,3 @@
import json
import os import os
import time import time
@ -12,6 +11,7 @@ from django.urls import reverse
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from .managers import PublicManager from .managers import PublicManager
from .utils import _urls_for_js
# Did not find a clean way to do this in Django # Did not find a clean way to do this in Django
@ -196,8 +196,6 @@ class Map(NamedModel):
@property @property
def preview_settings(self): def preview_settings(self):
from .views import _urls_for_js
layers = self.datalayer_set.all() layers = self.datalayer_set.all()
datalayer_data = [c.metadata() for c in layers] datalayer_data = [c.metadata() for c in layers]
map_settings = self.settings map_settings = self.settings

View file

@ -1,9 +1,26 @@
import gzip import gzip
import os import os
from django.conf import settings
from django.urls import URLPattern, URLResolver, get_resolver from django.urls import URLPattern, URLResolver, get_resolver
def _urls_for_js(urls=None):
"""
Return templated URLs prepared for javascript.
"""
if urls is None:
# prevent circular import
from .urls import i18n_urls, urlpatterns
urls = [
url.name for url in urlpatterns + i18n_urls if getattr(url, "name", None)
]
urls = dict(zip(urls, [get_uri_template(url) for url in urls]))
urls.update(getattr(settings, "UMAP_EXTRA_URLS", {}))
return urls
def get_uri_template(urlname, args=None, prefix=""): def get_uri_template(urlname, args=None, prefix=""):
""" """
Utility function to return an URI Template from a named URL in django Utility function to return an URI Template from a named URL in django

View file

@ -390,22 +390,6 @@ ajax_proxy = AjaxProxy.as_view()
# ############## # # ############## #
def _urls_for_js(urls=None):
"""
Return templated URLs prepared for javascript.
"""
if urls is None:
# prevent circular import
from .urls import i18n_urls, urlpatterns
urls = [
url.name for url in urlpatterns + i18n_urls if getattr(url, "name", None)
]
urls = dict(zip(urls, [get_uri_template(url) for url in urls]))
urls.update(getattr(settings, "UMAP_EXTRA_URLS", {}))
return urls
def simple_json_response(**kwargs): def simple_json_response(**kwargs):
return HttpResponse(json.dumps(kwargs), content_type="application/json") return HttpResponse(json.dumps(kwargs), content_type="application/json")