Skip to content

Commit

Permalink
support uncompiled ESM projects (no NPM or r.js)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheppard committed Sep 29, 2020
1 parent 9907aa0 commit b153a25
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
3 changes: 2 additions & 1 deletion default_settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Django settings
context_processors = (
'django.template.context_processors.csrf',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'wq.db.rest.auth.context_processors.is_authenticated',
Expand Down Expand Up @@ -35,7 +36,7 @@
REST_FRAMEWORK = {

'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.TemplateHTMLRenderer',
'wq.db.rest.renderers.HTMLRenderer',
'wq.db.rest.renderers.JSONRenderer',
'wq.db.rest.renderers.GeoJSONRenderer',
),
Expand Down
4 changes: 3 additions & 1 deletion rest/management/commands/dump_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def handle(self, **options):
cls=encoders.JSONEncoder,
indent=4,
)
if options['format'] == "amd":
if options['format'] == "esm":
text = "export default %s;" % text
elif options['format'] == "amd":
text = "define(%s);" % text
self.stdout.write(text)
52 changes: 51 additions & 1 deletion rest/renderers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,56 @@
from rest_framework.renderers import JSONRenderer
from rest_framework.renderers import TemplateHTMLRenderer, JSONRenderer
from django.conf import settings
from wq.db.default_settings import SRID as DEFAULT_SRID
import re


APP_TEMPLATES = {}


def load_app_template(template_name):
with open(template_name) as f:
template = f.read()
template = re.sub(
'<title>(.+)</title>',
'<title>{{title}}</title>',
template
)
if '{{title}}' in template:
return template, True
else:
return template, False


def get_title(data, request):
title = None
if isinstance(data, dict):
title = data.get('label')

return title or settings.PROJECT_NAME


def render_app(template_name, data, request):
if template_name not in APP_TEMPLATES:
APP_TEMPLATES[template_name] = load_app_template(template_name)

template, has_title = APP_TEMPLATES[template_name]
if has_title:
return template.replace('{{title}}', get_title(data, request))
else:
return template


class HTMLRenderer(TemplateHTMLRenderer):
def render(self, data, accepted_media_type=None, renderer_context=None):
if getattr(settings, 'WQ_APP_TEMPLATE', None):
return render_app(
settings.WQ_APP_TEMPLATE,
data,
(renderer_context or {}).get('request'),
)
return super(HTMLRenderer, self).render(
data, accepted_media_type, renderer_context
)


class JSONRenderer(JSONRenderer):
Expand Down

0 comments on commit b153a25

Please sign in to comment.