Use built-in django form assets instead of custom logic.

This commit is contained in:
Emil Stenström 2019-11-24 10:30:37 +01:00
parent 379c35d0b1
commit 096057b33d
5 changed files with 24 additions and 25 deletions

View file

@ -88,8 +88,10 @@ class Calendar(component.Component):
"date": date,
}
def template(self, date):
return "[your app]/components/calendar/calendar.html"
class Media:
template = "[your app]/components/calendar/calendar.html"
css = {'all': ['[your app]/components/calendar/calendar.css']}
js = ['[your app]/components/calendar/calendar.js']
@ -123,7 +125,7 @@ The output from the above template will be:
<html>
<head>
<title>My example calendar</title>
<link href="style.css" type="text/css" media="all" rel="stylesheet" />
<link href="style.css" type="text/css" media="all" rel="stylesheet">
<script type="text/javascript" src="script.js"></script>
</head>
<body>

View file

@ -1,30 +1,22 @@
from django.template.loader import render_to_string
from .component_registry import ComponentRegistry, AlreadyRegistered, NotRegistered # NOQA
from django.forms.widgets import MediaDefiningClass
class Component(object):
CSS_TEMPLATE = '<link href="{}" type="text/css" media="{}" rel="stylesheet" />'
JS_TEMPLATE = '<script type="text/javascript" src="{}"></script>'
class Component(metaclass=MediaDefiningClass):
def context(self):
return {}
@classmethod
def render_dependencies(cls):
out = []
def template(self, context):
return ""
for css_media, css_path in cls.Media.css.items():
out.append(cls.CSS_TEMPLATE.format(css_path, css_media))
for js_path in cls.Media.js:
out.append(cls.JS_TEMPLATE.format(js_path))
return "\n".join(out)
def render_dependencies(self):
return self.media.render()
def render(self, *args, **kwargs):
return render_to_string(self.Media.template, self.context(*args, **kwargs))
context = self.context(*args, **kwargs)
return render_to_string(self.template(context), context)
class Media:
template = None
css = {}
js = []

View file

@ -10,7 +10,8 @@ def component_dependencies_tag():
out = []
for component_class in unique_component_classes:
out.append(component_class.render_dependencies())
component = component_class()
out.append(component.render_dependencies())
return mark_safe("\n".join(out))

View file

@ -9,9 +9,11 @@ class SimpleComponent(component.Component):
"variable": variable,
}
def template(self, context):
return "simple_template.html"
class Media:
template = "simple_template.html"
css = {"all": "style.css"}
css = {"all": ["style.css"]}
js = ["script.js"]
class ComponentRegistryTest(unittest.TestCase):

View file

@ -10,9 +10,11 @@ class SimpleComponent(component.Component):
"variable": variable,
}
def template(self, context):
return "simple_template.html"
class Media:
template = "simple_template.html"
css = {"all": "style.css"}
css = {"all": ["style.css"]}
js = ["script.js"]
class ComponentTemplateTagTest(unittest.TestCase):
@ -26,7 +28,7 @@ class ComponentTemplateTagTest(unittest.TestCase):
template = Template('{% load component_tags %}{% component_dependencies %}')
rendered = template.render(Context())
self.assertEqual(rendered, dedent("""
<link href="style.css" type="text/css" media="all" rel="stylesheet" />
<link href="style.css" type="text/css" media="all" rel="stylesheet">
<script type="text/javascript" src="script.js"></script>
""").strip())
@ -44,6 +46,6 @@ class ComponentTemplateTagTest(unittest.TestCase):
template = Template('{% load component_tags %}{% component_dependencies %}')
rendered = template.render(Context())
self.assertEqual(rendered, dedent("""
<link href="style.css" type="text/css" media="all" rel="stylesheet" />
<link href="style.css" type="text/css" media="all" rel="stylesheet">
<script type="text/javascript" src="script.js"></script>
""").strip())