From 8c261f5f6f3ce7c190d95d479678a01c9287beb7 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Tue, 18 Aug 2020 15:28:34 -0400 Subject: [PATCH] Extra form.extra rendering into a macro --- CTFd/forms/users.py | 3 +-- CTFd/themes/admin/templates/macros/forms.html | 25 +++++++++++++++++++ .../admin/templates/modals/users/create.html | 16 ++---------- .../admin/templates/modals/users/edit.html | 16 ++---------- CTFd/themes/core/templates/macros/forms.html | 25 +++++++++++++++++++ CTFd/themes/core/templates/register.html | 16 ++---------- CTFd/themes/core/templates/settings.html | 9 ++----- 7 files changed, 59 insertions(+), 51 deletions(-) create mode 100644 CTFd/themes/admin/templates/macros/forms.html create mode 100644 CTFd/themes/core/templates/macros/forms.html diff --git a/CTFd/forms/users.py b/CTFd/forms/users.py index 1013b20e..13c8e0a1 100644 --- a/CTFd/forms/users.py +++ b/CTFd/forms/users.py @@ -40,8 +40,7 @@ def build_custom_user_fields( else: form_field.render_kw = {"data-initial": initial} - entry = (field.name, form_field) - fields.append(entry) + fields.append(form_field) return fields diff --git a/CTFd/themes/admin/templates/macros/forms.html b/CTFd/themes/admin/templates/macros/forms.html new file mode 100644 index 00000000..520f0059 --- /dev/null +++ b/CTFd/themes/admin/templates/macros/forms.html @@ -0,0 +1,25 @@ +{% macro render_extra_fields(fields, show_labels=True, show_optionals=True, show_descriptions=True) -%} + {% for field in fields %} +
+ {% if show_labels %} + {{ field.label }} + {% endif %} + + {% if show_optionals %} + {% if field.flags.required is false %} + Optional + {% endif %} + {% endif %} + + {{ field(class="form-control") }} + + {% if show_descriptions %} + {% if field.description %} + + {{ field.description }} + + {% endif %} + {% endif %} +
+ {% endfor %} +{%- endmacro %} \ No newline at end of file diff --git a/CTFd/themes/admin/templates/modals/users/create.html b/CTFd/themes/admin/templates/modals/users/create.html index 2c2fa3bf..f477c28f 100644 --- a/CTFd/themes/admin/templates/modals/users/create.html +++ b/CTFd/themes/admin/templates/modals/users/create.html @@ -1,4 +1,5 @@ {% with form = Forms.users.UserCreateForm() %} +{% from "admin/macros/forms.html" import render_extra_fields %}
{{ form.name.label }} @@ -28,20 +29,7 @@ {{ form.country(class="form-control custom-select") }}
- {% for k, v in form.extra %} -
- {{ v.label }} - {% if v.flags.required is false %} - Optional - {% endif %} - {{ v(class="form-control") }} - {% if v.description %} - - {{ v.description }} - - {% endif %} -
- {% endfor %} + {{ render_extra_fields(form.extra) }}
diff --git a/CTFd/themes/admin/templates/modals/users/edit.html b/CTFd/themes/admin/templates/modals/users/edit.html index d6512905..67915064 100644 --- a/CTFd/themes/admin/templates/modals/users/edit.html +++ b/CTFd/themes/admin/templates/modals/users/edit.html @@ -1,4 +1,5 @@ {% with form = Forms.users.UserEditForm(obj=user) %} +{% from "admin/macros/forms.html" import render_extra_fields %}
{{ form.name.label }} @@ -25,20 +26,7 @@ {{ form.country(class="form-control custom-select") }}
- {% for k, v in form.extra %} -
- {{ v.label }} - {% if v.flags.required is false %} - Optional - {% endif %} - {{ v(class="form-control") }} - {% if v.description %} - - {{ v.description }} - - {% endif %} -
- {% endfor %} + {{ render_extra_fields(form.extra) }}
diff --git a/CTFd/themes/core/templates/macros/forms.html b/CTFd/themes/core/templates/macros/forms.html new file mode 100644 index 00000000..520f0059 --- /dev/null +++ b/CTFd/themes/core/templates/macros/forms.html @@ -0,0 +1,25 @@ +{% macro render_extra_fields(fields, show_labels=True, show_optionals=True, show_descriptions=True) -%} + {% for field in fields %} +
+ {% if show_labels %} + {{ field.label }} + {% endif %} + + {% if show_optionals %} + {% if field.flags.required is false %} + Optional + {% endif %} + {% endif %} + + {{ field(class="form-control") }} + + {% if show_descriptions %} + {% if field.description %} + + {{ field.description }} + + {% endif %} + {% endif %} +
+ {% endfor %} +{%- endmacro %} \ No newline at end of file diff --git a/CTFd/themes/core/templates/register.html b/CTFd/themes/core/templates/register.html index c9c379c0..312b8c61 100644 --- a/CTFd/themes/core/templates/register.html +++ b/CTFd/themes/core/templates/register.html @@ -23,6 +23,7 @@ {% endif %} {% with form = Forms.auth.RegistrationForm() %} + {% from "macros/forms.html" import render_extra_fields %}
{{ form.name.label }} @@ -47,20 +48,7 @@
{{ form.nonce() }} - {% for k, v in form.extra %} -
- {{ v.label }} - {% if v.flags.required is false %} - Optional - {% endif %} - {{ v(class="form-control") }} - {% if v.description %} - - {{ v.description }} - - {% endif %} -
- {% endfor %} + {{ render_extra_fields(form.extra) }}
diff --git a/CTFd/themes/core/templates/settings.html b/CTFd/themes/core/templates/settings.html index d10a7b4a..2d3d0b77 100644 --- a/CTFd/themes/core/templates/settings.html +++ b/CTFd/themes/core/templates/settings.html @@ -23,6 +23,7 @@ {% include "components/errors.html" %} {% with form = Forms.self.SettingsForm(country=country) %} + {% from "macros/forms.html" import render_extra_fields %}
@@ -62,13 +63,7 @@
- {% for k, v in form.extra %} -
- {{ v.label }} - {{ v(class="form-control") }} -
- {% endfor %} - + {{ render_extra_fields(form.extra) }}