custom columns unfinished

This commit is contained in:
Cervinko Cera 2016-04-20 18:56:03 +02:00
parent 202cbc26a7
commit 704198655b
4 changed files with 93 additions and 29 deletions

View File

@ -1,3 +1,2 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

View File

@ -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()

View File

@ -47,18 +47,41 @@
{% for c in cc %}
<div class="form-group">
<label for="{{ 'custom_column_' ~ c.id }}">{{ c.name }}</label>
<input type="text" class="form-control" name="{{ 'custom_column_' ~ c.id}} " id="{{ 'custom_column_' ~ c.id }}"
{% if book['custom_column_' ~ c.id]|length > 0 %}
{% for column in book['custom_column_' ~ c.id] %}
value="{{ column.value }} {% if not loop.last %}, {% endif %}
{% endfor %}
"
{% if c.datatype in ['text', 'series'] %}
<input type="text" class="form-control" name="{{ 'custom_column_' ~ c.id }}" id="{{ 'custom_column_' ~ c.id }}"
{% if book['custom_column_' ~ c.id]|length > 0 %}
{% for column in book['custom_column_' ~ c.id] %}
value="{{ column.value }}{% if not loop.last %}, {% endif %}{% endfor %}"{% endif %}>
{% endif %}
>
<br />
{% if c.datatype == 'enumeration' %}
<select class="form-control" name="{{ 'custom_column_' ~ c.id }}" id="{{ 'custom_column_' ~ c.id }}">
<option></option>
{% for opt in c.get_display_dict().enum_values %}
<option
{% if book['custom_column_' ~ c.id]|length > 0 %}
{% if book['custom_column_' ~ c.id][0].value == opt %}selected="selected"{% endif %}
{% endif %}
>{{ opt }}</option>
{% endfor %}
</select>
{% endif %}
{% if c.datatype == ('rating' or 'float' or 'int') %}
<input type="number"
{% if c.datatype == 'rating'%}
min="1" max="5"
{% endif %}
{% if c.datatype != 'float' %}
step="1"
{% endif %}
class="form-control" name="{{ 'custom_column_' ~ c.id }}" id="{{ 'custom_column_' ~ c.id }}"
{% if book['custom_column_' ~ c.id]|length > 0 %}
{% for column in book['custom_column_' ~ c.id] %}
value="{{ column.value }}{% if not loop.last %}, {% endif %}{% endfor %}"{% endif %}>
{% endif %}
</div>
{% endfor %}
</div>
</p>
{% endif %}

View File

@ -316,7 +316,9 @@ def discover(page):
@app.route("/book/<int:id>")
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)