From f78b968075a662ba9f8dab6099d922cce93f5e03 Mon Sep 17 00:00:00 2001 From: Sli Date: Sun, 20 Oct 2024 01:05:34 +0200 Subject: [PATCH] Move markdown input and select widgets to a widget folder --- com/views.py | 3 +- core/views/forms.py | 61 ---------------------------------- core/views/page.py | 3 +- core/views/widgets/markdown.py | 15 +++++++++ core/views/widgets/select.py | 46 +++++++++++++++++++++++++ election/views.py | 6 ++-- forum/views.py | 2 +- pedagogy/forms.py | 2 +- 8 files changed, 70 insertions(+), 68 deletions(-) create mode 100644 core/views/widgets/markdown.py create mode 100644 core/views/widgets/select.py diff --git a/com/views.py b/com/views.py index f9ea78eb..69ee7221 100644 --- a/com/views.py +++ b/com/views.py @@ -51,7 +51,8 @@ from core.views import ( QuickNotifMixin, TabedViewMixin, ) -from core.views.forms import MarkdownInput, SelectDateTime +from core.views.forms import SelectDateTime +from core.views.widgets.markdown import MarkdownInput # Sith object diff --git a/core/views/forms.py b/core/views/forms.py index 9c34a003..d1597c2c 100644 --- a/core/views/forms.py +++ b/core/views/forms.py @@ -29,18 +29,14 @@ from captcha.fields import CaptchaField from django import forms from django.conf import settings from django.contrib.auth.forms import AuthenticationForm, UserCreationForm -from django.contrib.staticfiles.storage import staticfiles_storage from django.core.exceptions import ValidationError from django.db import transaction from django.forms import ( CheckboxSelectMultiple, DateInput, DateTimeInput, - SelectMultiple, - Textarea, TextInput, ) -from django.forms.widgets import Select from django.utils.translation import gettext from django.utils.translation import gettext_lazy as _ from phonenumber_field.widgets import RegionalPhoneNumberWidget @@ -67,63 +63,6 @@ class SelectDate(DateInput): super().__init__(attrs=attrs, format=format or "%Y-%m-%d") -class MarkdownInput(Textarea): - template_name = "core/widgets/markdown_textarea.jinja" - - def get_context(self, name, value, attrs): - context = super().get_context(name, value, attrs) - - context["statics"] = { - "js": staticfiles_storage.url("webpack/core/components/easymde-index.ts"), - "css": staticfiles_storage.url("webpack/core/components/easymde-index.css"), - } - return context - - -class AutoCompleteSelectMixin: - component_name = "autocomplete-select" - template_name = "core/widgets/autocomplete_select.jinja" - is_ajax = False - - def optgroups(self, name, value, attrs=None): - """Don't create option groups when doing ajax""" - if self.is_ajax: - return [] - return super().optgroups(name, value, attrs=attrs) - - def get_context(self, name, value, attrs): - context = super().get_context(name, value, attrs) - context["component"] = self.component_name - context["statics"] = { - "js": staticfiles_storage.url( - "webpack/core/components/ajax-select-index.ts" - ), - "csss": [ - staticfiles_storage.url( - "webpack/core/components/ajax-select-index.css" - ), - staticfiles_storage.url("core/components/ajax-select.scss"), - ], - } - return context - - -class AutoCompleteSelect(AutoCompleteSelectMixin, Select): ... - - -class AutoCompleteSelectMultiple(AutoCompleteSelectMixin, SelectMultiple): ... - - -class AutoCompleteSelectUser(AutoCompleteSelectMixin, Select): - component_name = "user-ajax-select" - is_ajax = True - - -class AutoCompleteSelectMultipleUser(AutoCompleteSelectMixin, SelectMultiple): - component_name = "user-ajax-select" - is_ajax = True - - class NFCTextInput(TextInput): template_name = "core/widgets/nfc.jinja" diff --git a/core/views/page.py b/core/views/page.py index 01fd59f6..e33e84ba 100644 --- a/core/views/page.py +++ b/core/views/page.py @@ -23,7 +23,8 @@ from django.views.generic.edit import CreateView, DeleteView, UpdateView from core.models import LockError, Page, PageRev from core.views import CanCreateMixin, CanEditMixin, CanEditPropMixin, CanViewMixin -from core.views.forms import MarkdownInput, PageForm, PagePropForm +from core.views.forms import PageForm, PagePropForm +from core.views.widgets.markdown import MarkdownInput class CanEditPagePropMixin(CanEditPropMixin): diff --git a/core/views/widgets/markdown.py b/core/views/widgets/markdown.py new file mode 100644 index 00000000..bd442b20 --- /dev/null +++ b/core/views/widgets/markdown.py @@ -0,0 +1,15 @@ +from django.contrib.staticfiles.storage import staticfiles_storage +from django.forms import Textarea + + +class MarkdownInput(Textarea): + template_name = "core/widgets/markdown_textarea.jinja" + + def get_context(self, name, value, attrs): + context = super().get_context(name, value, attrs) + + context["statics"] = { + "js": staticfiles_storage.url("webpack/core/components/easymde-index.ts"), + "css": staticfiles_storage.url("webpack/core/components/easymde-index.css"), + } + return context diff --git a/core/views/widgets/select.py b/core/views/widgets/select.py new file mode 100644 index 00000000..8bc7f5ea --- /dev/null +++ b/core/views/widgets/select.py @@ -0,0 +1,46 @@ +from django.contrib.staticfiles.storage import staticfiles_storage +from django.forms import Select, SelectMultiple + + +class AutoCompleteSelectMixin: + component_name = "autocomplete-select" + template_name = "core/widgets/autocomplete_select.jinja" + is_ajax = False + + def optgroups(self, name, value, attrs=None): + """Don't create option groups when doing ajax""" + if self.is_ajax: + return [] + return super().optgroups(name, value, attrs=attrs) + + def get_context(self, name, value, attrs): + context = super().get_context(name, value, attrs) + context["component"] = self.component_name + context["statics"] = { + "js": staticfiles_storage.url( + "webpack/core/components/ajax-select-index.ts" + ), + "csss": [ + staticfiles_storage.url( + "webpack/core/components/ajax-select-index.css" + ), + staticfiles_storage.url("core/components/ajax-select.scss"), + ], + } + return context + + +class AutoCompleteSelect(AutoCompleteSelectMixin, Select): ... + + +class AutoCompleteSelectMultiple(AutoCompleteSelectMixin, SelectMultiple): ... + + +class AutoCompleteSelectUser(AutoCompleteSelectMixin, Select): + component_name = "user-ajax-select" + is_ajax = True + + +class AutoCompleteSelectMultipleUser(AutoCompleteSelectMixin, SelectMultiple): + component_name = "user-ajax-select" + is_ajax = True diff --git a/election/views.py b/election/views.py index 4280680a..1f189f41 100644 --- a/election/views.py +++ b/election/views.py @@ -11,12 +11,12 @@ from django.views.generic import DetailView, ListView from django.views.generic.edit import CreateView, DeleteView, FormView, UpdateView from core.views import CanCreateMixin, CanEditMixin, CanViewMixin -from core.views.forms import ( +from core.views.forms import SelectDateTime +from core.views.widgets.markdown import MarkdownInput +from core.views.widgets.select import ( AutoCompleteSelect, AutoCompleteSelectMultiple, AutoCompleteSelectUser, - MarkdownInput, - SelectDateTime, ) from election.models import Candidature, Election, ElectionList, Role, Vote diff --git a/forum/views.py b/forum/views.py index 052eb068..11f5ac59 100644 --- a/forum/views.py +++ b/forum/views.py @@ -50,7 +50,7 @@ from core.views import ( CanViewMixin, can_view, ) -from core.views.forms import MarkdownInput +from core.views.widgets.markdown import MarkdownInput from forum.models import Forum, ForumMessage, ForumMessageMeta, ForumTopic diff --git a/pedagogy/forms.py b/pedagogy/forms.py index 56a3dce7..9a182f92 100644 --- a/pedagogy/forms.py +++ b/pedagogy/forms.py @@ -25,7 +25,7 @@ from django import forms from django.utils.translation import gettext_lazy as _ from core.models import User -from core.views.forms import MarkdownInput +from core.views.widgets.markdown import MarkdownInput from pedagogy.models import UV, UVComment, UVCommentReport