|
43 | 43 | ] |
44 | 44 | import decimal |
45 | 45 | import warnings |
46 | | -from typing import Callable, List, Optional, Union |
| 46 | +from typing import Callable, Dict, List, Optional, Type, Union |
47 | 47 |
|
48 | 48 | from bson import ObjectId |
49 | 49 | from mongoengine import fields |
@@ -82,17 +82,24 @@ def __init__( |
82 | 82 | *, |
83 | 83 | validators: Optional[Union[List, Callable]] = None, |
84 | 84 | filters: Optional[Union[List, Callable]] = None, |
| 85 | + wtf_field_class: Optional[Type] = None, |
85 | 86 | wtf_filters: Optional[Union[List, Callable]] = None, |
86 | 87 | wtf_validators: Optional[Union[List, Callable]] = None, |
| 88 | + wtf_options: Optional[Dict] = None, |
87 | 89 | **kwargs, |
88 | 90 | ): |
89 | 91 | """ |
90 | 92 | Extended :func:`__init__` method for mongoengine db field with WTForms options. |
91 | 93 |
|
92 | 94 | :param filters: DEPRECATED: wtf form field filters. |
93 | 95 | :param validators: DEPRECATED: wtf form field validators. |
| 96 | + :param wtf_field_class: Any subclass of :class:`wtforms.forms.core.Field` that |
| 97 | + can be used for form field generation. Takes precedence over |
| 98 | + :attr:`DEFAULT_WTF_FIELD` and :attr:`DEFAULT_WTF_CHOICES_FIELD` |
94 | 99 | :param wtf_filters: wtf form field filters. |
95 | 100 | :param wtf_validators: wtf form field validators. |
| 101 | + :param wtf_options: Dictionary with WTForm Field settings. |
| 102 | + Applied last, takes precedence over any generated field options. |
96 | 103 | :param kwargs: keyword arguments silently bypassed to normal mongoengine fields |
97 | 104 | """ |
98 | 105 | if validators is not None: |
@@ -123,14 +130,29 @@ def __init__( |
123 | 130 | self.wtf_filters = self._ensure_callable_or_list( |
124 | 131 | wtf_filters or filters, "wtf_filters" |
125 | 132 | ) |
| 133 | + self.wtf_options = wtf_options |
126 | 134 |
|
127 | 135 | # Some attributes that will be updated by parent methods |
128 | 136 | self.required = False |
129 | 137 | self.default = None |
130 | 138 | self.name = "" |
| 139 | + self.choices = None |
| 140 | + |
| 141 | + # Internals |
| 142 | + self._wtf_field_class = wtf_field_class |
| 143 | + self._wtf_options = {} |
131 | 144 |
|
132 | 145 | super().__init__(**kwargs) |
133 | 146 |
|
| 147 | + @property |
| 148 | + def wtf_field_class(self): |
| 149 | + """Final WTForm Field class, that will be used for field generation.""" |
| 150 | + if self._wtf_field_class: |
| 151 | + return self._wtf_field_class |
| 152 | + if self.choices and self.DEFAULT_WTF_CHOICES_FIELD: |
| 153 | + return self.DEFAULT_WTF_CHOICES_FIELD |
| 154 | + return self.DEFAULT_WTF_FIELD |
| 155 | + |
134 | 156 | @staticmethod |
135 | 157 | def _ensure_callable_or_list(argument, msg_flag: str) -> Optional[List]: |
136 | 158 | """ |
|
0 commit comments