Enhancement #1886079: default value for custom columns

This commit is contained in:
Charles Haley 2020-07-04 13:35:08 +01:00
parent 8b7b02cc41
commit b106805700
2 changed files with 43 additions and 0 deletions

View File

@ -92,6 +92,29 @@ def _add_newbook_tag(mi):
mi.tags.append(tag)
def _add_default_custom_column_values(mi, fm):
cols = fm.custom_field_metadata(include_composites=False)
for cc,col in iteritems(cols):
dv = col['display'].get('default_value', '')
try:
if dv:
if not mi.get_user_metadata(cc, make_copy=False):
mi.set_user_metadata(cc, col)
dt = col['datatype']
if dt == 'bool':
dv = {_('yes'): 'true', _('no'): 'false'}.get(icu_lower(dv), '')
elif dt == 'datetime' and icu_lower(dv) == _('now'):
dv = nowf()
elif dt == 'rating':
try:
dv = int(dv) * 2
except:
dv = None
mi.set(cc, dv)
except:
pass
dynamic_category_preferences = frozenset({'grouped_search_make_user_categories', 'grouped_search_terms', 'user_categories'})
@ -1571,6 +1594,7 @@ class Cache(object):
mi.tags = list(mi.tags)
if apply_import_tags:
_add_newbook_tag(mi)
_add_default_custom_column_values(mi, self.field_metadata)
if not add_duplicates and self._has_book(mi):
return
series_index = (self._get_next_series_num_for(mi.series) if mi.series_index is None else mi.series_index)

View File

@ -177,6 +177,10 @@ class CreateCustomColumn(QDialog):
self.comments_type.setCurrentIndex(idx)
elif ct == 'rating':
self.allow_half_stars.setChecked(bool(c['display'].get('allow_half_stars', False)))
if ct not in ['composite', '*composite']:
self.default_value.setText(c['display'].get('default_value', ''))
self.datatype_changed()
if ct in ['text', 'composite', 'enumeration']:
self.use_decorations.setChecked(c['display'].get('use_decorations', False))
@ -390,6 +394,15 @@ class CreateCustomColumn(QDialog):
l.addWidget(cch)
add_row(None, l)
# Default value
self.default_value = dv = QLineEdit(self)
dv.setToolTip('<p>' + _('Default value when a new book is added to the '
'library. For Date columns enter the word "now" or the date as '
'yyyy-mm-dd. For Yes/No columns enter "Yes" or "No". For Text with '
'a fixed set of values enter one of the permitted values. For '
'Rating columns enter a number between 0 and 5.') + '</p>')
self.default_value_label = add_row(_('Default value'), dv)
self.resize(self.sizeHint())
# }}}
@ -456,6 +469,8 @@ class CreateCustomColumn(QDialog):
getattr(self, 'composite_'+x).setVisible(col_type in ['composite', '*composite'])
for x in ('box', 'default_label', 'label', 'colors', 'colors_label'):
getattr(self, 'enum_'+x).setVisible(col_type == 'enumeration')
for x in ('value_label', 'value'):
getattr(self, 'default_'+x).setVisible(col_type not in ['composite', '*composite'])
self.use_decorations.setVisible(col_type in ['text', 'composite', 'enumeration'])
self.is_names.setVisible(col_type == '*text')
is_comments = col_type == 'comments'
@ -566,6 +581,10 @@ class CreateCustomColumn(QDialog):
if col_type in ['text', 'composite', 'enumeration'] and not is_multiple:
display_dict['use_decorations'] = self.use_decorations.checkState()
if col_type != 'composite':
display_dict['default_value'] = unicode_type(self.default_value.text())
display_dict['description'] = self.description_box.text().strip()
if not self.editing_col: