From ed8311057579de883fc010f9dd5e1bb331970418 Mon Sep 17 00:00:00 2001 From: Gareth Coles Date: Thu, 11 Apr 2019 20:34:18 +0100 Subject: Lots of extra form input handling. But now the root article creator form renders nicely! --- pydis_site/apps/home/templatetags/wiki_extra.py | 66 +++++++++++++++++++++++ pydis_site/templates/wiki/base.html | 7 +++ pydis_site/templates/wiki/create_root.html | 15 ++++++ pydis_site/templates/wiki/forms/fields/char.html | 21 ++++++++ pydis_site/templates/wiki/includes/formfield.html | 27 ++-------- 5 files changed, 114 insertions(+), 22 deletions(-) create mode 100644 pydis_site/apps/home/templatetags/wiki_extra.py create mode 100644 pydis_site/templates/wiki/forms/fields/char.html 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() + + +@register.simple_tag +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)) diff --git a/pydis_site/templates/wiki/base.html b/pydis_site/templates/wiki/base.html index 95b0186d..44ad8353 100644 --- a/pydis_site/templates/wiki/base.html +++ b/pydis_site/templates/wiki/base.html @@ -5,6 +5,13 @@ Wiki | {% block wiki_pagetitle %}{% endblock %}{% block wiki_site_title %}{% endblock %} {% endblock %} +{% block head %} + {{ block.super }} + + + +{% endblock %} + {% block content %} {% include "base/navbar.html" with active_item=True dropdowns=True icon_weight="far" use_logo=True %} diff --git a/pydis_site/templates/wiki/create_root.html b/pydis_site/templates/wiki/create_root.html index 7b677d47..0d7662ec 100644 --- a/pydis_site/templates/wiki/create_root.html +++ b/pydis_site/templates/wiki/create_root.html @@ -3,6 +3,21 @@ {% block wiki_pagetitle %}{% trans "Create root article" %}{% endblock %} +{% block head %} + {{ block.super }} + + {% for js in editor.Media.js %} + + {% endfor %} + + {% for media, srcs in editor.Media.css.items %} + {% for src in srcs %} + + {% endfor %} + {% endfor %} +{% endblock %} + + {% block wiki_contents %}
diff --git a/pydis_site/templates/wiki/forms/fields/char.html b/pydis_site/templates/wiki/forms/fields/char.html new file mode 100644 index 00000000..c94f5771 --- /dev/null +++ b/pydis_site/templates/wiki/forms/fields/char.html @@ -0,0 +1,21 @@ +{% if is_markitup %} + +{% else %} + +{% endif %} diff --git a/pydis_site/templates/wiki/includes/formfield.html b/pydis_site/templates/wiki/includes/formfield.html index 9ca88d4a..0176f9d4 100644 --- a/pydis_site/templates/wiki/includes/formfield.html +++ b/pydis_site/templates/wiki/includes/formfield.html @@ -1,3 +1,5 @@ +{% load wiki_extra %} + {% if field.is_hidden %} {{ field }} {% else %} @@ -8,16 +10,18 @@ {{ field.label | safe }} {% if field.field.required %}*{% endif %} {% endif %} + {% if field.help_text %}

{{ field.help_text|safe }}

{% endif %}
+
- {{ field }} + {% render_field field %}
@@ -30,25 +34,4 @@ {% endif %}
- -{#
#} -{#
#} -{# {% if field.label %}#} -{# #} -{# {% endif %}#} -{#
#} -{#
#} -{# {{ field }} {% if field.field.required %}*{% endif %}#} -{# {% if field.errors %}#} -{# {% for error in field.errors %}#} -{#
{{ error }}
#} -{# {% endfor %}#} -{# {% endif %}#} -{# {% if field.help_text %}#} -{#

{{ field.help_text|safe }}

#} -{# {% endif %}#} -{#
#} -{#
#} {% endif %} -- cgit v1.2.3