From e04a179f34c60df515b444608ddd02b533345578 Mon Sep 17 00:00:00 2001 From: long2ice Date: Fri, 2 Sep 2022 22:38:22 +0800 Subject: [PATCH] feat: improve datetime and date input --- CHANGELOG.md | 1 + .../templates/widgets/inputs/date.html | 14 +++ .../templates/widgets/inputs/datetime.html | 27 ++++++ fastapi_admin/widgets/inputs.py | 92 +++++++++---------- 4 files changed, 88 insertions(+), 46 deletions(-) create mode 100644 fastapi_admin/templates/widgets/inputs/date.html create mode 100644 fastapi_admin/templates/widgets/inputs/datetime.html diff --git a/CHANGELOG.md b/CHANGELOG.md index f48ed57..6fe4b8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### 1.0.5 - Fix translations build. +- Improve datetime and date input ### 1.0.4 diff --git a/fastapi_admin/templates/widgets/inputs/date.html b/fastapi_admin/templates/widgets/inputs/date.html new file mode 100644 index 0000000..cc4118f --- /dev/null +++ b/fastapi_admin/templates/widgets/inputs/date.html @@ -0,0 +1,14 @@ +{% extends "widgets/inputs/datetime.html" %} +{% block script %} + +{% endblock %} diff --git a/fastapi_admin/templates/widgets/inputs/datetime.html b/fastapi_admin/templates/widgets/inputs/datetime.html new file mode 100644 index 0000000..76afada --- /dev/null +++ b/fastapi_admin/templates/widgets/inputs/datetime.html @@ -0,0 +1,27 @@ + + +
+ + + {% if help_text %} + + {{ help_text }} + + {% endif %} +
+ +{% block script %} + +{% endblock %} diff --git a/fastapi_admin/widgets/inputs.py b/fastapi_admin/widgets/inputs.py index 52c9bca..60f6b7d 100644 --- a/fastapi_admin/widgets/inputs.py +++ b/fastapi_admin/widgets/inputs.py @@ -15,7 +15,7 @@ class Input(Widget): template = "widgets/inputs/input.html" def __init__( - self, help_text: Optional[str] = None, default: Any = None, null: bool = False, **context + self, help_text: Optional[str] = None, default: Any = None, null: bool = False, **context ): super().__init__(null=null, help_text=help_text, **context) self.default = default @@ -44,12 +44,12 @@ class Text(Input): input_type: Optional[str] = "text" def __init__( - self, - help_text: Optional[str] = None, - default: Any = None, - null: bool = False, - placeholder: str = "", - disabled: bool = False, + self, + help_text: Optional[str] = None, + default: Any = None, + null: bool = False, + placeholder: str = "", + disabled: bool = False, ): super().__init__( null=null, @@ -65,11 +65,11 @@ class Select(Input): template = "widgets/inputs/select.html" def __init__( - self, - help_text: Optional[str] = None, - default: Any = None, - null: bool = False, - disabled: bool = False, + self, + help_text: Optional[str] = None, + default: Any = None, + null: bool = False, + disabled: bool = False, ): super().__init__(help_text=help_text, null=null, default=default, disabled=disabled) @@ -91,12 +91,12 @@ class Select(Input): class ForeignKey(Select): def __init__( - self, - model: Type[Model], - default: Any = None, - null: bool = False, - disabled: bool = False, - help_text: Optional[str] = None, + self, + model: Type[Model], + default: Any = None, + null: bool = False, + disabled: bool = False, + help_text: Optional[str] = None, ): super().__init__(help_text=help_text, default=default, null=null, disabled=disabled) self.model = model @@ -116,10 +116,10 @@ class ManyToMany(Select): template = "widgets/inputs/many_to_many.html" def __init__( - self, - model: Type[Model], - disabled: bool = False, - help_text: Optional[str] = None, + self, + model: Type[Model], + disabled: bool = False, + help_text: Optional[str] = None, ): super().__init__(help_text=help_text, disabled=disabled) self.model = model @@ -144,13 +144,13 @@ class ManyToMany(Select): class Enum(Select): def __init__( - self, - enum: Type[EnumCLS], - default: Any = None, - enum_type: Type = int, - null: bool = False, - disabled: bool = False, - help_text: Optional[str] = None, + self, + enum: Type[EnumCLS], + default: Any = None, + enum_type: Type = int, + null: bool = False, + disabled: bool = False, + help_text: Optional[str] = None, ): super().__init__(help_text=help_text, default=default, null=null, disabled=disabled) self.enum = enum @@ -174,10 +174,10 @@ class Json(Input): template = "widgets/inputs/json.html" def __init__( - self, - help_text: Optional[str] = None, - null: bool = False, - options: Optional[dict] = None, + self, + help_text: Optional[str] = None, + null: bool = False, + options: Optional[dict] = None, ): """ options config to jsoneditor, see https://github.com/josdejong/jsoneditor @@ -204,23 +204,23 @@ class Editor(Text): class DateTime(Text): - input_type = "datetime" + template = "widgets/inputs/datetime.html" class Date(Text): - input_type = "date" + template = "widgets/inputs/date.html" class File(Input): input_type = "file" def __init__( - self, - upload: FileUpload, - default: Any = None, - null: bool = False, - disabled: bool = False, - help_text: Optional[str] = None, + self, + upload: FileUpload, + default: Any = None, + null: bool = False, + disabled: bool = False, + help_text: Optional[str] = None, ): super().__init__( null=null, @@ -246,11 +246,11 @@ class Radio(Select): template = "widgets/inputs/radio.html" def __init__( - self, - options: List[Tuple[str, Any]], - help_text: Optional[str] = None, - default: Any = None, - disabled: bool = False, + self, + options: List[Tuple[str, Any]], + help_text: Optional[str] = None, + default: Any = None, + disabled: bool = False, ): super().__init__(default=default, disabled=disabled, help_text=help_text) self.options = options