Enable custom column datetime (adv. search not working yet) #1984

This commit is contained in:
Ozzie Isaacs 2021-05-13 10:39:36 +02:00
parent 9ef705650b
commit b0cc52e0aa
5 changed files with 29 additions and 10 deletions

View File

@ -59,7 +59,7 @@ except ImportError:
log = logger.create() log = logger.create()
cc_exceptions = ['datetime', 'comments', 'composite', 'series'] cc_exceptions = ['comments', 'composite', 'series']
cc_classes = {} cc_classes = {}
Base = declarative_base() Base = declarative_base()
@ -491,23 +491,25 @@ class CalibreDB():
ccdict['value'] = Column(Float) ccdict['value'] = Column(Float)
elif row.datatype == 'int': elif row.datatype == 'int':
ccdict['value'] = Column(Integer) ccdict['value'] = Column(Integer)
elif row.datatype == 'datetime':
ccdict['value'] = Column(TIMESTAMP)
elif row.datatype == 'bool': elif row.datatype == 'bool':
ccdict['value'] = Column(Boolean) ccdict['value'] = Column(Boolean)
else: else:
ccdict['value'] = Column(String) ccdict['value'] = Column(String)
if row.datatype in ['float', 'int', 'bool']: if row.datatype in ['float', 'int', 'bool', 'datetime']:
ccdict['book'] = Column(Integer, ForeignKey('books.id')) ccdict['book'] = Column(Integer, ForeignKey('books.id'))
cc_classes[row.id] = type(str('custom_column_' + str(row.id)), (Base,), ccdict) cc_classes[row.id] = type(str('custom_column_' + str(row.id)), (Base,), ccdict)
for cc_id in cc_ids: for cc_id in cc_ids:
if (cc_id[1] == 'bool') or (cc_id[1] == 'int') or (cc_id[1] == 'float'): if cc_id[1] in ['bool', 'int', 'float', 'datetime']:
setattr(Books, setattr(Books,
'custom_column_' + str(cc_id[0]), 'custom_column_' + str(cc_id[0]),
relationship(cc_classes[cc_id[0]], relationship(cc_classes[cc_id[0]],
primaryjoin=( primaryjoin=(
Books.id == cc_classes[cc_id[0]].book), Books.id == cc_classes[cc_id[0]].book),
backref='books')) backref='books'))
elif (cc_id[1] == 'series'): elif cc_id[1] == 'series':
setattr(Books, setattr(Books,
'custom_column_' + str(cc_id[0]), 'custom_column_' + str(cc_id[0]),
relationship(books_custom_column_links[cc_id[0]], relationship(books_custom_column_links[cc_id[0]],

View File

@ -501,6 +501,11 @@ def edit_cc_data_number(book_id, book, c, to_save, cc_db_value, cc_string):
to_save[cc_string] = None to_save[cc_string] = None
elif c.datatype == 'bool': elif c.datatype == 'bool':
to_save[cc_string] = 1 if to_save[cc_string] == 'True' else 0 to_save[cc_string] = 1 if to_save[cc_string] == 'True' else 0
elif c.datatype == 'datetime':
try:
to_save[cc_string] = datetime.strptime(to_save[cc_string], "%Y-%m-%d")
except ValueError:
to_save[cc_string] = db.Books.DEFAULT_PUBDATE
if to_save[cc_string] != cc_db_value: if to_save[cc_string] != cc_db_value:
if cc_db_value is not None: if cc_db_value is not None:
@ -559,7 +564,7 @@ def edit_cc_data(book_id, book, to_save):
else: else:
cc_db_value = None cc_db_value = None
if to_save[cc_string].strip(): if to_save[cc_string].strip():
if c.datatype == 'int' or c.datatype == 'bool' or c.datatype == 'float': if c.datatype in ['int', 'bool', 'float', "datetime"]:
changed, to_save = edit_cc_data_number(book_id, book, c, to_save, cc_db_value, cc_string) changed, to_save = edit_cc_data_number(book_id, book, c, to_save, cc_db_value, cc_string)
else: else:
changed, to_save = edit_cc_data_string(book, c, to_save, cc_db_value, cc_string) changed, to_save = edit_cc_data_string(book, c, to_save, cc_db_value, cc_string)

View File

@ -63,6 +63,7 @@ if (!Modernizr.inputtypes.date) {
}).trigger("change"); }).trigger("change");
} }
/* /*
Takes a prefix, query typeahead callback, Bloodhound typeahead adapter Takes a prefix, query typeahead callback, Bloodhound typeahead adapter
and returns the completions it gets from the bloodhound engine prefixed. and returns the completions it gets from the bloodhound engine prefixed.
@ -78,11 +79,6 @@ function prefixedSource(prefix, query, cb, bhAdapter) {
}); });
} }
/*function getPath() {
var jsFileLocation = $("script[src*=edit_books]").attr("src"); // the js file path
return jsFileLocation.substr(0, jsFileLocation.search("/static/js/edit_books.js")); // the js folder path
}*/
var authors = new Bloodhound({ var authors = new Bloodhound({
name: "authors", name: "authors",
datumTokenizer: function datumTokenizer(datum) { datumTokenizer: function datumTokenizer(datum) {

View File

@ -149,6 +149,20 @@
{% endif %}> {% endif %}>
{% endif %} {% endif %}
{% if c.datatype == 'datetime' %}
<div style="position: relative">
<input type="date" class="form-control" name="{{ 'custom_column_' ~ c.id }}" id="{{ 'custom_column_' ~ c.id }}"
{% if book['custom_column_' ~ c.id]|length > 0 %}
value="{% if book['custom_column_' ~ c.id][0].value %}{{ book['custom_column_' ~ c.id][0].value|formatdateinput}}{% endif %}"
{% endif %}>
<input type="text" class="fake_custom_column_{{ c.id }} form-control fake-input hidden "
{% if book['custom_column_' ~ c.id]|length > 0 %}
value="{% if book['custom_column_' ~ c.id][0].value %}{{book['custom_column_' ~ c.id][0].value|formatdate}}{% endif %}"
{% endif %}>
</div>
{% endif %}
{% if c.datatype == 'enumeration' %} {% if c.datatype == 'enumeration' %}
<select class="form-control" name="{{ 'custom_column_' ~ c.id }}" id="{{ 'custom_column_' ~ c.id }}"> <select class="form-control" name="{{ 'custom_column_' ~ c.id }}" id="{{ 'custom_column_' ~ c.id }}">

View File

@ -193,6 +193,8 @@
{% else %} {% else %}
{% if c.datatype == 'float' %} {% if c.datatype == 'float' %}
{{ column.value|formatfloat(2) }} {{ column.value|formatfloat(2) }}
{% elif c.datatype == 'datetime' %}
{{ column.value|formatdate }}
{% else %} {% else %}
{% if c.datatype == 'series' %} {% if c.datatype == 'series' %}
{{ '%s [%s]' % (column.value, column.extra|formatfloat(2)) }} {{ '%s [%s]' % (column.value, column.extra|formatfloat(2)) }}