1313 FieldTypeEnum ,
1414 _FieldID ,
1515 BaseField ,
16+ BooleanField ,
1617)
1718from .joins import TagSubtag
1819from ...constants import TAG_FAVORITE , TAG_ARCHIVED
@@ -139,7 +140,7 @@ def fields(self) -> list[BaseField]:
139140 fields .extend (self .tag_box_fields )
140141 fields .extend (self .text_fields )
141142 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 )
143144 return fields
144145
145146 @property
@@ -171,18 +172,11 @@ def __init__(
171172 self ,
172173 path : Path ,
173174 folder : Folder ,
174- fields : list [BaseField ] | None = None ,
175+ fields : list [BaseField ],
175176 ) -> None :
176177 self .path = path
177178 self .folder = folder
178179
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-
186180 for field in fields :
187181 if isinstance (field , TextField ):
188182 self .text_fields .append (field )
@@ -210,22 +204,25 @@ def remove_tag(self, tag: Tag, field: TagBoxField | None = None) -> None:
210204 tag_box_field .tags .remove (tag )
211205
212206
213- class LibraryField (Base ):
207+ class ValueType (Base ):
214208 """Define Field Types in the Library.
215209
216210 Example:
217211 key: content_tags (this field is slugified `name`)
218212 name: Content Tags (this field is human readable name)
219213 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
220216
221217 """
222218
223- __tablename__ = "library_fields "
219+ __tablename__ = "value_type "
224220
225221 key : Mapped [str ] = mapped_column (primary_key = True )
226222 name : Mapped [str ] = mapped_column (nullable = False )
227223 type : Mapped [FieldTypeEnum ] = mapped_column (default = FieldTypeEnum .TEXT_LINE )
228- order : Mapped [int ]
224+ is_default : Mapped [bool ]
225+ position : Mapped [int ]
229226
230227 # add relations to other tables
231228 text_fields : Mapped [list [TextField ]] = relationship (
@@ -237,9 +234,27 @@ class LibraryField(Base):
237234 tag_box_fields : Mapped [list [TagBoxField ]] = relationship (
238235 "TagBoxField" , back_populates = "type"
239236 )
237+ boolean_fields : Mapped [list [BooleanField ]] = relationship (
238+ "BooleanField" , back_populates = "type"
239+ )
240240
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" )
243258def slugify_field_key (mapper , connection , target ):
244259 """Slugify the field key before inserting into the database."""
245260 if not target .key :
0 commit comments