From 26f314d3710dcf6c625101c76d9c4689447fc0f6 Mon Sep 17 00:00:00 2001 From: nanu-c Date: Mon, 22 May 2017 22:54:53 +0200 Subject: [PATCH] support int custom fields --- cps/db.py | 11 +++++++++-- cps/templates/book_edit.html | 19 ++++++++++++------- cps/web.py | 16 ++++++++++++++++ 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/cps/db.py b/cps/db.py index 704789b6..1e418593 100755 --- a/cps/db.py +++ b/cps/db.py @@ -11,7 +11,7 @@ from ub import config import ub session = None -cc_exceptions = ['datetime', 'int', 'comments', 'float', 'composite', 'series'] +cc_exceptions = ['datetime', 'comments', 'float', 'composite', 'series'] cc_classes = None engine = None @@ -335,11 +335,18 @@ def setup_db(): primary_key=True) ) cc_ids.append([row.id, row.datatype]) + import sys + print >>sys.stderr,row.datatype if row.datatype == 'bool': ccdict = {'__tablename__': 'custom_column_' + str(row.id), 'id': Column(Integer, primary_key=True), 'book': Column(Integer, ForeignKey('books.id')), 'value': Column(Boolean)} + elif row.datatype == 'int': + ccdict = {'__tablename__': 'custom_column_' + str(row.id), + 'id': Column(Integer, primary_key=True), + 'book': Column(Integer, ForeignKey('books.id')), + 'value': Column(Integer)} else: ccdict = {'__tablename__': 'custom_column_' + str(row.id), 'id': Column(Integer, primary_key=True), @@ -347,7 +354,7 @@ def setup_db(): cc_classes[row.id] = type('Custom_Column_' + str(row.id), (Base,), ccdict) for cc_id in cc_ids: - if cc_id[1] == 'bool': + if (cc_id[1] == 'bool') or (cc_id[1] == 'int'): setattr(Books, 'custom_column_' + str(cc_id[0]), relationship(cc_classes[cc_id[0]], primaryjoin=( Books.id == cc_classes[cc_id[0]].book), diff --git a/cps/templates/book_edit.html b/cps/templates/book_edit.html index 5b44ac07..a24f74b5 100644 --- a/cps/templates/book_edit.html +++ b/cps/templates/book_edit.html @@ -51,12 +51,12 @@ - +
- + {% if cc|length > 0 %} {% for c in cc %}
@@ -68,6 +68,11 @@ {% endif %} + + {% if c.datatype == 'int' %} + + {% endif %} + {% if c.datatype in ['text', 'series'] and not c.is_multiple %} 0 %} @@ -80,12 +85,12 @@ {% if book['custom_column_' ~ c.id]|length > 0 %} value="{% for column in book['custom_column_' ~ c.id] %}{{ column.value.strip() }}{% if not loop.last %}, {% endif %}{% endfor %}"{% endif %}> {% endif %} - + {% if c.datatype == 'enumeration' %} {{_('view book after edit')}} diff --git a/cps/web.py b/cps/web.py index 2f4b1525..e13ecca0 100755 --- a/cps/web.py +++ b/cps/web.py @@ -2589,6 +2589,22 @@ def edit_book(book_id): cc_class = db.cc_classes[c.id] new_cc = cc_class(value=to_save[cc_string], book=book_id) db.session.add(new_cc) + elif c.datatype == 'int': + if to_save[cc_string] == 'None': + to_save[cc_string] = None + if to_save[cc_string] != cc_db_value: + if cc_db_value is not None: + if to_save[cc_string] is not None: + setattr(getattr(book, cc_string)[0], 'value', to_save[cc_string]) + else: + del_cc = getattr(book, cc_string)[0] + getattr(book, cc_string).remove(del_cc) + db.session.delete(del_cc) + else: + cc_class = db.cc_classes[c.id] + new_cc = cc_class(value=to_save[cc_string], book=book_id) + db.session.add(new_cc) + else: if c.datatype == 'rating': to_save[cc_string] = str(int(float(to_save[cc_string]) * 2))