aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps
diff options
context:
space:
mode:
authorGravatar Gareth Coles <[email protected]>2019-04-11 20:34:18 +0100
committerGravatar Gareth Coles <[email protected]>2019-04-11 20:34:18 +0100
commited8311057579de883fc010f9dd5e1bb331970418 (patch)
treee5b6d1ad7b2f9aa1758c8946b8b08356a2075453 /pydis_site/apps
parentSome work towards initial pages and forms (diff)
Lots of extra form input handling.
But now the root article creator form renders nicely!
Diffstat (limited to 'pydis_site/apps')
-rw-r--r--pydis_site/apps/home/templatetags/wiki_extra.py66
1 files changed, 66 insertions, 0 deletions
diff --git a/pydis_site/apps/home/templatetags/wiki_extra.py b/pydis_site/apps/home/templatetags/wiki_extra.py
new file mode 100644
index 00000000..9749a411
--- /dev/null
+++ b/pydis_site/apps/home/templatetags/wiki_extra.py
@@ -0,0 +1,66 @@
+from django import template
+from django.forms import (
+ BooleanField, CharField, ChoiceField, ComboField, DateField, DateTimeField, DecimalField, DurationField, EmailField,
+ Field, FileField, FilePathField, FloatField, GenericIPAddressField, ImageField, IntegerField, ModelChoiceField,
+ ModelMultipleChoiceField, MultiValueField, MultipleChoiceField, NullBooleanField, RegexField, SlugField,
+ SplitDateTimeField, TimeField, TypedChoiceField, TypedMultipleChoiceField, URLField, UUIDField, BoundField)
+from django.template import Template
+from django.template.loader import get_template
+from django.utils.safestring import mark_safe
+from wiki.editors.markitup import MarkItUpWidget
+
+TEMPLATE_PATH = "wiki/forms/fields/{0}.html"
+
+TEMPLATES = {
+ BooleanField: TEMPLATE_PATH.format("boolean"),
+ CharField: TEMPLATE_PATH.format("char"),
+ ChoiceField: TEMPLATE_PATH.format("choice"),
+ TypedChoiceField: TEMPLATE_PATH.format("typed_choice"),
+ DateField: TEMPLATE_PATH.format("date"),
+ DateTimeField: TEMPLATE_PATH.format("date_time"),
+ DecimalField: TEMPLATE_PATH.format("decimal"),
+ DurationField: TEMPLATE_PATH.format("duration"),
+ EmailField: TEMPLATE_PATH.format("email"),
+ FileField: TEMPLATE_PATH.format("file"),
+ FilePathField: TEMPLATE_PATH.format("file_path"),
+ FloatField: TEMPLATE_PATH.format("float"),
+ ImageField: TEMPLATE_PATH.format("image"),
+ IntegerField: TEMPLATE_PATH.format("integer"),
+ GenericIPAddressField: TEMPLATE_PATH.format("generic_ip_address"),
+ MultipleChoiceField: TEMPLATE_PATH.format("multiple_choice"),
+ TypedMultipleChoiceField: TEMPLATE_PATH.format("typed_multiple_choice"),
+ NullBooleanField: TEMPLATE_PATH.format("null_boolean"),
+ RegexField: TEMPLATE_PATH.format("regex"),
+ SlugField: TEMPLATE_PATH.format("slug"),
+ TimeField: TEMPLATE_PATH.format("time"),
+ URLField: TEMPLATE_PATH.format("url"),
+ UUIDField: TEMPLATE_PATH.format("uuid"),
+
+ ComboField: TEMPLATE_PATH.format("combo"),
+ MultiValueField: TEMPLATE_PATH.format("multi_value"),
+ SplitDateTimeField: TEMPLATE_PATH.format("split_date_time"),
+
+ ModelChoiceField: TEMPLATE_PATH.format("model_choice"),
+ ModelMultipleChoiceField: TEMPLATE_PATH.format("model_multiple_choice"),
+}
+
+
+register = template.Library()
+
+
+def render_field(field: Field):
+ if isinstance(field, BoundField):
+ template_path = TEMPLATES.get(field.field.__class__)
+ is_markitup = isinstance(field.field.widget, MarkItUpWidget)
+ else:
+ template_path = TEMPLATES.get(field.__class__)
+ is_markitup = isinstance(field.widget, MarkItUpWidget)
+
+ if not template_path:
+ raise NotImplementedError(f"Unknown field type: {field.__class__}")
+
+ template_obj: Template = get_template(template_path)
+ context = {"field": field, "is_markitup": is_markitup}
+
+ return mark_safe(template_obj.render(context))