From 704198655b13c55e0e2a3eb033fc7543ec695e4f Mon Sep 17 00:00:00 2001 From: Cervinko Cera Date: Wed, 20 Apr 2016 18:56:03 +0200 Subject: [PATCH] custom columns unfinished --- cps/__init__.py | 1 - cps/db.py | 24 ++++++++++------ cps/templates/edit_book.html | 43 +++++++++++++++++++++------- cps/web.py | 54 ++++++++++++++++++++++++++++++------ 4 files changed, 93 insertions(+), 29 deletions(-) diff --git a/cps/__init__.py b/cps/__init__.py index 3a1213c8..faa18be5 100755 --- a/cps/__init__.py +++ b/cps/__init__.py @@ -1,3 +1,2 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- - diff --git a/cps/db.py b/cps/db.py index 52c350da..2cebe7bf 100755 --- a/cps/db.py +++ b/cps/db.py @@ -7,6 +7,7 @@ from sqlalchemy.orm import * import os from cps import config import re +import ast #calibre sort stuff title_pat = re.compile(config.TITLE_REGEX, re.IGNORECASE) @@ -49,17 +50,18 @@ books_languages_link = Table('books_languages_link', Base.metadata, Column('lang_code', Integer, ForeignKey('languages.id'), primary_key=True) ) -cc = conn.execute("SELECT id FROM custom_columns") +cc = conn.execute("SELECT id, datatype FROM custom_columns") cc_ids = [] - +cc_exceptions = ['bool', 'datetime', 'int', 'comments', 'float', ] books_custom_column_links = {} for row in cc: - books_custom_column_links[row.id] = Table('books_custom_column_' + str(row.id) + '_link', Base.metadata, - Column('book', Integer, ForeignKey('books.id'), primary_key=True), - Column('value', Integer, ForeignKey('custom_column_' + str(row.id) + '.id'), primary_key=True) - ) - #books_custom_column_links[row.id]= - cc_ids.append(row.id) + if row.datatype not in cc_exceptions: + books_custom_column_links[row.id] = Table('books_custom_column_' + str(row.id) + '_link', Base.metadata, + Column('book', Integer, ForeignKey('books.id'), primary_key=True), + Column('value', Integer, ForeignKey('custom_column_' + str(row.id) + '.id'), primary_key=True) + ) + #books_custom_column_links[row.id]= + cc_ids.append(row.id) cc_classes = {} for id in cc_ids: @@ -216,7 +218,11 @@ class Custom_Columns(Base): editable = Column(Boolean) display = Column(String) is_multiple = Column(Boolean) - normalized = Column(Boolean) + normalized = Column(Boolean) + + def get_display_dict(self): + display_dict = ast.literal_eval(self.display) + return display_dict #Base.metadata.create_all(engine) Session = sessionmaker() diff --git a/cps/templates/edit_book.html b/cps/templates/edit_book.html index a3746e4e..846175ee 100644 --- a/cps/templates/edit_book.html +++ b/cps/templates/edit_book.html @@ -47,18 +47,41 @@ {% for c in cc %}
- 0 %} - {% for column in book['custom_column_' ~ c.id] %} - value="{{ column.value }} {% if not loop.last %}, {% endif %} - {% endfor %} - " + {% if c.datatype in ['text', 'series'] %} + 0 %} + {% for column in book['custom_column_' ~ c.id] %} + value="{{ column.value }}{% if not loop.last %}, {% endif %}{% endfor %}"{% endif %}> {% endif %} - > -
+ + {% if c.datatype == 'enumeration' %} + + {% endif %} + + {% if c.datatype == ('rating' or 'float' or 'int') %} + 0 %} + {% for column in book['custom_column_' ~ c.id] %} + value="{{ column.value }}{% if not loop.last %}, {% endif %}{% endfor %}"{% endif %}> + {% endif %} +
{% endfor %} - -

{% endif %} diff --git a/cps/web.py b/cps/web.py index 08c4636f..eb855ae4 100755 --- a/cps/web.py +++ b/cps/web.py @@ -316,7 +316,9 @@ def discover(page): @app.route("/book/") def show_book(id): entries = db.session.query(db.Books).filter(db.Books.id == id).first() - cc = db.session.query(db.Custom_Columns).all() + cc = db.session.query(db.Custom_Columns).filter(db.Custom_Columns.datatype.notin_(db.cc_exceptions)).all() + for c in cc: + print c.name #print entries.custom_column_1 #helper.get_custom_columns(entries.id) book_in_shelfs = [] @@ -697,7 +699,9 @@ def edit_user(user_id): def edit_book(book_id): ## create the function for sorting... db.session.connection().connection.connection.create_function("title_sort",1,db.title_sort) - cc = db.session.query(db.Custom_Columns).all() + cc = db.session.query(db.Custom_Columns).filter(db.Custom_Columns.datatype.notin_(db.cc_exceptions)).all() + for c in cc: + print c.name book = db.session.query(db.Books).filter(db.Books.id == book_id).first() author_names = [] for author in book.authors: @@ -744,12 +748,12 @@ def edit_book(book_id): if len(add_authors) > 0: for add_author in add_authors: # check if an author with that name exists - t_author = db.session.query(db.Authors).filter(db.Authors.name == add_author).first(); + t_author = db.session.query(db.Authors).filter(db.Authors.name == add_author).first() # if no author is found add it if t_author == None: new_author = db.Authors(add_author, add_author, "") db.session.add(new_author) - t_author = db.session.query(db.Authors).filter(db.Authors.name == add_author).first(); + t_author = db.session.query(db.Authors).filter(db.Authors.name == add_author).first() # add author to book book.authors.append(t_author) if author0_before_edit != book.authors[0].name: @@ -803,14 +807,14 @@ def edit_book(book_id): if len(add_tags) > 0: for add_tag in add_tags: # check if a tag with that name exists - new_tag = db.session.query(db.Tags).filter(db.Tags.name == add_tag).first(); + new_tag = db.session.query(db.Tags).filter(db.Tags.name == add_tag).first() # if no tag is found add it if new_tag == None: new_tag = db.Tags(add_tag) db.session.add(new_tag) - new_tag = db.session.query(db.Tags).filter(db.Tags.name == add_tag).first(); + new_tag = db.session.query(db.Tags).filter(db.Tags.name == add_tag).first() # add tag to book - book.tags.append(new_tag) + book.tags.append(new_tag) if to_save["series"].strip(): is_series = db.session.query(db.Series).filter(db.Series.name.like('%' + to_save["series"].strip() + '%')).first() @@ -826,6 +830,38 @@ def edit_book(book_id): else: new_rating = db.Ratings(rating=int(to_save["rating"].strip())) book.ratings[0] = new_rating + + for c in cc: + cc_string = "custom_column_" + str(c.id) + if len(getattr(book, cc_string)) > 0: + cc_db_value = getattr(book, cc_string)[0].value + else: + cc_db_value = None + if to_save[cc_string].strip(): + if to_save[cc_string].strip() != cc_db_value: + if cc_db_value != None: + #remove old cc_val + del_cc = getattr(book, cc_string)[0] + getattr(book, cc_string).remove(del_cc) + if len(del_cc.books) == 0: + db.session.delete(del_cc) + cc_class = db.cc_classes[c.id] + new_cc = db.session.query(cc_class).filter(cc_class.value == to_save[cc_string].strip()).first() + # if no cc val is found add it + if new_cc == None: + new_cc = cc_class(value=to_save[cc_string].strip()) + db.session.add(new_cc) + new_cc = db.session.query(cc_class).filter(cc_class.value == to_save[cc_string].strip()).first() + # add cc value to book + getattr(book, cc_string).append(new_cc) + else: + if cc_db_value != None: + #remove old cc_val + del_cc = getattr(book, cc_string)[0] + getattr(book, cc_string).remove(del_cc) + if len(del_cc.books) == 0: + db.session.delete(del_cc) + db.session.commit() author_names = [] for author in book.authors: @@ -905,5 +941,5 @@ def upload(): author_names = [] for author in db_book.authors: author_names.append(author.name) - - return render_template('edit_book.html', book=db_book, authors=author_names) + cc = db.session.query(db.Custom_Columns).filter(db.Custom_Columns.datatype.notin_(db.cc_exceptions)).all() + return render_template('edit_book.html', book=db_book, authors=author_names, cc=cc)