13
13
FieldTypeEnum ,
14
14
_FieldID ,
15
15
BaseField ,
16
+ BooleanField ,
16
17
)
17
18
from .joins import TagSubtag
18
19
from ...constants import TAG_FAVORITE , TAG_ARCHIVED
@@ -139,7 +140,7 @@ def fields(self) -> list[BaseField]:
139
140
fields .extend (self .tag_box_fields )
140
141
fields .extend (self .text_fields )
141
142
fields .extend (self .datetime_fields )
142
- fields = sorted (fields , key = lambda field : field .type .order )
143
+ fields = sorted (fields , key = lambda field : field .type .position )
143
144
return fields
144
145
145
146
@property
@@ -171,18 +172,11 @@ def __init__(
171
172
self ,
172
173
path : Path ,
173
174
folder : Folder ,
174
- fields : list [BaseField ] | None = None ,
175
+ fields : list [BaseField ],
175
176
) -> None :
176
177
self .path = path
177
178
self .folder = folder
178
179
179
- if fields is None :
180
- fields = [
181
- TagBoxField (type_key = _FieldID .TAGS_META .name , position = 0 ),
182
- TagBoxField (type_key = _FieldID .TAGS_CONTENT .name , position = 0 ),
183
- TextField (type_key = _FieldID .TITLE .name , position = 0 ),
184
- ]
185
-
186
180
for field in fields :
187
181
if isinstance (field , TextField ):
188
182
self .text_fields .append (field )
@@ -210,22 +204,25 @@ def remove_tag(self, tag: Tag, field: TagBoxField | None = None) -> None:
210
204
tag_box_field .tags .remove (tag )
211
205
212
206
213
- class LibraryField (Base ):
207
+ class ValueType (Base ):
214
208
"""Define Field Types in the Library.
215
209
216
210
Example:
217
211
key: content_tags (this field is slugified `name`)
218
212
name: Content Tags (this field is human readable name)
219
213
kind: type of content (Text Line, Text Box, Tags, Datetime, Checkbox)
214
+ is_default: Should the field be present in new Entry?
215
+ order: position of the field widget in the Entry form
220
216
221
217
"""
222
218
223
- __tablename__ = "library_fields "
219
+ __tablename__ = "value_type "
224
220
225
221
key : Mapped [str ] = mapped_column (primary_key = True )
226
222
name : Mapped [str ] = mapped_column (nullable = False )
227
223
type : Mapped [FieldTypeEnum ] = mapped_column (default = FieldTypeEnum .TEXT_LINE )
228
- order : Mapped [int ]
224
+ is_default : Mapped [bool ]
225
+ position : Mapped [int ]
229
226
230
227
# add relations to other tables
231
228
text_fields : Mapped [list [TextField ]] = relationship (
@@ -237,9 +234,27 @@ class LibraryField(Base):
237
234
tag_box_fields : Mapped [list [TagBoxField ]] = relationship (
238
235
"TagBoxField" , back_populates = "type"
239
236
)
237
+ boolean_fields : Mapped [list [BooleanField ]] = relationship (
238
+ "BooleanField" , back_populates = "type"
239
+ )
240
240
241
-
242
- @event .listens_for (LibraryField , "before_insert" )
241
+ @property
242
+ def as_field (self ) -> BaseField :
243
+ FieldClass = {
244
+ FieldTypeEnum .TEXT_LINE : TextField ,
245
+ FieldTypeEnum .TEXT_BOX : TextField ,
246
+ FieldTypeEnum .TAGS : TagBoxField ,
247
+ FieldTypeEnum .DATETIME : DatetimeField ,
248
+ FieldTypeEnum .BOOLEAN : BooleanField ,
249
+ }
250
+
251
+ return FieldClass [self .type ](
252
+ type_key = self .key ,
253
+ position = self .position ,
254
+ )
255
+
256
+
257
+ @event .listens_for (ValueType , "before_insert" )
243
258
def slugify_field_key (mapper , connection , target ):
244
259
"""Slugify the field key before inserting into the database."""
245
260
if not target .key :
0 commit comments