2015-08-02 20:59:11 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-01-20 19:37:45 +01:00
|
|
|
# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
|
|
|
|
# Copyright (C) 2012-2019 mutschler, cervinko, ok11, jkrehm, nanu-c, Wineliva,
|
|
|
|
# pjeby, elelay, idalin, Ozzieisaacs
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2015-08-02 20:59:11 +02:00
|
|
|
from sqlalchemy import *
|
|
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
|
|
from sqlalchemy.orm import *
|
|
|
|
import os
|
|
|
|
import re
|
2016-04-20 18:56:03 +02:00
|
|
|
import ast
|
2017-01-28 20:16:40 +01:00
|
|
|
from ub import config
|
|
|
|
import ub
|
2019-01-14 20:27:53 +01:00
|
|
|
import sys
|
2017-01-28 20:16:40 +01:00
|
|
|
|
|
|
|
session = None
|
2017-05-22 22:54:53 +02:00
|
|
|
cc_exceptions = ['datetime', 'comments', 'float', 'composite', 'series']
|
2017-01-28 20:16:40 +01:00
|
|
|
cc_classes = None
|
|
|
|
engine = None
|
|
|
|
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2017-01-22 21:30:36 +01:00
|
|
|
# user defined sort function for calibre databases (Series, etc.)
|
2015-08-02 20:59:11 +02:00
|
|
|
def title_sort(title):
|
2017-01-22 21:30:36 +01:00
|
|
|
# calibre sort stuff
|
|
|
|
title_pat = re.compile(config.config_title_regex, re.IGNORECASE)
|
2015-08-02 20:59:11 +02:00
|
|
|
match = title_pat.search(title)
|
|
|
|
if match:
|
|
|
|
prep = match.group(1)
|
|
|
|
title = title.replace(prep, '') + ', ' + prep
|
|
|
|
return title.strip()
|
|
|
|
|
2017-11-30 16:49:46 +01:00
|
|
|
|
2017-10-21 21:50:47 +02:00
|
|
|
def lcase(s):
|
|
|
|
return s.lower()
|
|
|
|
|
2017-11-30 16:49:46 +01:00
|
|
|
|
2017-10-21 21:50:47 +02:00
|
|
|
def ucase(s):
|
|
|
|
return s.upper()
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2017-11-30 16:49:46 +01:00
|
|
|
|
2015-08-02 20:59:11 +02:00
|
|
|
Base = declarative_base()
|
|
|
|
|
|
|
|
books_authors_link = Table('books_authors_link', Base.metadata,
|
2017-10-21 21:50:47 +02:00
|
|
|
Column('book', Integer, ForeignKey('books.id'), primary_key=True),
|
|
|
|
Column('author', Integer, ForeignKey('authors.id'), primary_key=True)
|
2017-02-15 18:09:17 +01:00
|
|
|
)
|
2015-08-02 20:59:11 +02:00
|
|
|
|
|
|
|
books_tags_link = Table('books_tags_link', Base.metadata,
|
2017-10-21 21:50:47 +02:00
|
|
|
Column('book', Integer, ForeignKey('books.id'), primary_key=True),
|
|
|
|
Column('tag', Integer, ForeignKey('tags.id'), primary_key=True)
|
2017-02-15 18:09:17 +01:00
|
|
|
)
|
2015-08-02 20:59:11 +02:00
|
|
|
|
|
|
|
books_series_link = Table('books_series_link', Base.metadata,
|
2017-10-21 21:50:47 +02:00
|
|
|
Column('book', Integer, ForeignKey('books.id'), primary_key=True),
|
|
|
|
Column('series', Integer, ForeignKey('series.id'), primary_key=True)
|
2017-02-15 18:09:17 +01:00
|
|
|
)
|
2015-08-02 20:59:11 +02:00
|
|
|
|
|
|
|
books_ratings_link = Table('books_ratings_link', Base.metadata,
|
2017-10-21 21:50:47 +02:00
|
|
|
Column('book', Integer, ForeignKey('books.id'), primary_key=True),
|
|
|
|
Column('rating', Integer, ForeignKey('ratings.id'), primary_key=True)
|
2017-02-15 18:09:17 +01:00
|
|
|
)
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2015-10-13 01:21:22 +02:00
|
|
|
books_languages_link = Table('books_languages_link', Base.metadata,
|
2017-10-21 21:50:47 +02:00
|
|
|
Column('book', Integer, ForeignKey('books.id'), primary_key=True),
|
|
|
|
Column('lang_code', Integer, ForeignKey('languages.id'), primary_key=True)
|
2017-02-15 18:09:17 +01:00
|
|
|
)
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2017-02-22 20:59:48 +01:00
|
|
|
books_publishers_link = Table('books_publishers_link', Base.metadata,
|
2017-10-21 21:50:47 +02:00
|
|
|
Column('book', Integer, ForeignKey('books.id'), primary_key=True),
|
|
|
|
Column('publisher', Integer, ForeignKey('publishers.id'), primary_key=True)
|
2017-02-22 20:59:48 +01:00
|
|
|
)
|
2016-12-23 09:53:39 +01:00
|
|
|
|
2017-11-30 16:49:46 +01:00
|
|
|
|
2016-12-27 10:36:06 +01:00
|
|
|
class Identifiers(Base):
|
|
|
|
__tablename__ = 'identifiers'
|
|
|
|
|
2017-10-21 21:50:47 +02:00
|
|
|
id = Column(Integer, primary_key=True)
|
2016-12-27 10:36:06 +01:00
|
|
|
type = Column(String)
|
|
|
|
val = Column(String)
|
|
|
|
book = Column(Integer, ForeignKey('books.id'))
|
|
|
|
|
2017-04-02 10:42:33 +02:00
|
|
|
def __init__(self, val, id_type, book):
|
2016-12-27 10:36:06 +01:00
|
|
|
self.val = val
|
2017-04-02 10:42:33 +02:00
|
|
|
self.type = id_type
|
2016-12-27 10:36:06 +01:00
|
|
|
self.book = book
|
|
|
|
|
|
|
|
def formatType(self):
|
|
|
|
if self.type == "amazon":
|
|
|
|
return u"Amazon"
|
|
|
|
elif self.type == "isbn":
|
|
|
|
return u"ISBN"
|
|
|
|
elif self.type == "doi":
|
|
|
|
return u"DOI"
|
2017-01-05 20:11:02 +01:00
|
|
|
elif self.type == "goodreads":
|
|
|
|
return u"Goodreads"
|
2017-04-27 15:32:30 +02:00
|
|
|
elif self.type == "google":
|
|
|
|
return u"Google Books"
|
|
|
|
elif self.type == "kobo":
|
|
|
|
return u"Kobo"
|
2016-12-27 10:36:06 +01:00
|
|
|
else:
|
|
|
|
return self.type
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
if self.type == "amazon":
|
|
|
|
return u"https://amzn.com/{0}".format(self.val)
|
|
|
|
elif self.type == "isbn":
|
2017-01-18 19:07:45 +01:00
|
|
|
return u"http://www.worldcat.org/isbn/{0}".format(self.val)
|
2016-12-27 10:36:06 +01:00
|
|
|
elif self.type == "doi":
|
|
|
|
return u"http://dx.doi.org/{0}".format(self.val)
|
2017-01-05 20:11:02 +01:00
|
|
|
elif self.type == "goodreads":
|
|
|
|
return u"http://www.goodreads.com/book/show/{0}".format(self.val)
|
2017-01-16 04:14:49 +01:00
|
|
|
elif self.type == "douban":
|
|
|
|
return u"https://book.douban.com/subject/{0}".format(self.val)
|
2017-04-27 15:32:30 +02:00
|
|
|
elif self.type == "google":
|
|
|
|
return u"https://books.google.com/books?id={0}".format(self.val)
|
|
|
|
elif self.type == "kobo":
|
|
|
|
return u"https://www.kobo.com/ebook/{0}".format(self.val)
|
2018-07-18 21:36:51 +02:00
|
|
|
elif self.type == "url":
|
|
|
|
return u"{0}".format(self.val)
|
2016-12-27 10:36:06 +01:00
|
|
|
else:
|
|
|
|
return u""
|
|
|
|
|
|
|
|
|
2015-08-02 20:59:11 +02:00
|
|
|
class Comments(Base):
|
2016-04-03 23:52:32 +02:00
|
|
|
__tablename__ = 'comments'
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2017-10-21 21:50:47 +02:00
|
|
|
id = Column(Integer, primary_key=True)
|
2016-04-03 23:52:32 +02:00
|
|
|
text = Column(String)
|
|
|
|
book = Column(Integer, ForeignKey('books.id'))
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-04-03 23:52:32 +02:00
|
|
|
def __init__(self, text, book):
|
|
|
|
self.text = text
|
|
|
|
self.book = book
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-04-03 23:52:32 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return u"<Comments({0})>".format(self.text)
|
2015-08-02 20:59:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Tags(Base):
|
2016-04-03 23:52:32 +02:00
|
|
|
__tablename__ = 'tags'
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2017-10-21 21:50:47 +02:00
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
|
|
name = Column(String)
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-04-03 23:52:32 +02:00
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-04-03 23:52:32 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return u"<Tags('{0})>".format(self.name)
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-12-23 09:53:39 +01:00
|
|
|
|
2015-08-02 20:59:11 +02:00
|
|
|
class Authors(Base):
|
2016-04-03 23:52:32 +02:00
|
|
|
__tablename__ = 'authors'
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2017-10-21 21:50:47 +02:00
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
name = Column(String)
|
2016-04-03 23:52:32 +02:00
|
|
|
sort = Column(String)
|
|
|
|
link = Column(String)
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-04-03 23:52:32 +02:00
|
|
|
def __init__(self, name, sort, link):
|
|
|
|
self.name = name
|
|
|
|
self.sort = sort
|
|
|
|
self.link = link
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-04-03 23:52:32 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return u"<Authors('{0},{1}{2}')>".format(self.name, self.sort, self.link)
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-12-23 09:53:39 +01:00
|
|
|
|
2015-08-02 20:59:11 +02:00
|
|
|
class Series(Base):
|
2016-04-03 23:52:32 +02:00
|
|
|
__tablename__ = 'series'
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2017-10-21 21:50:47 +02:00
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
name = Column(String)
|
2016-04-03 23:52:32 +02:00
|
|
|
sort = Column(String)
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-04-03 23:52:32 +02:00
|
|
|
def __init__(self, name, sort):
|
|
|
|
self.name = name
|
|
|
|
self.sort = sort
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-04-03 23:52:32 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return u"<Series('{0},{1}')>".format(self.name, self.sort)
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-12-23 09:53:39 +01:00
|
|
|
|
2015-08-02 20:59:11 +02:00
|
|
|
class Ratings(Base):
|
2016-04-03 23:52:32 +02:00
|
|
|
__tablename__ = 'ratings'
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2017-10-21 21:50:47 +02:00
|
|
|
id = Column(Integer, primary_key=True)
|
2016-04-03 23:52:32 +02:00
|
|
|
rating = Column(Integer)
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-12-23 09:53:39 +01:00
|
|
|
def __init__(self, rating):
|
2016-04-03 23:52:32 +02:00
|
|
|
self.rating = rating
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-04-03 23:52:32 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return u"<Ratings('{0}')>".format(self.rating)
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-12-23 09:53:39 +01:00
|
|
|
|
2015-10-13 01:21:22 +02:00
|
|
|
class Languages(Base):
|
|
|
|
__tablename__ = 'languages'
|
|
|
|
|
2017-10-21 21:50:47 +02:00
|
|
|
id = Column(Integer, primary_key=True)
|
2015-10-13 01:21:22 +02:00
|
|
|
lang_code = Column(String)
|
|
|
|
|
2016-12-23 09:53:39 +01:00
|
|
|
def __init__(self, lang_code):
|
2015-10-13 01:21:22 +02:00
|
|
|
self.lang_code = lang_code
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return u"<Languages('{0}')>".format(self.lang_code)
|
|
|
|
|
2017-11-30 16:49:46 +01:00
|
|
|
|
2017-02-22 20:59:48 +01:00
|
|
|
class Publishers(Base):
|
|
|
|
__tablename__ = 'publishers'
|
|
|
|
|
2017-10-21 21:50:47 +02:00
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
name = Column(String)
|
2017-02-22 20:59:48 +01:00
|
|
|
sort = Column(String)
|
|
|
|
|
2017-11-30 16:49:46 +01:00
|
|
|
def __init__(self, name, sort):
|
2017-02-22 20:59:48 +01:00
|
|
|
self.name = name
|
|
|
|
self.sort = sort
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return u"<Publishers('{0},{1}')>".format(self.name, self.sort)
|
|
|
|
|
|
|
|
|
2015-08-02 20:59:11 +02:00
|
|
|
class Data(Base):
|
2016-04-03 23:52:32 +02:00
|
|
|
__tablename__ = 'data'
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2017-10-21 21:50:47 +02:00
|
|
|
id = Column(Integer, primary_key=True)
|
2016-04-03 23:52:32 +02:00
|
|
|
book = Column(Integer, ForeignKey('books.id'))
|
|
|
|
format = Column(String)
|
|
|
|
uncompressed_size = Column(Integer)
|
|
|
|
name = Column(String)
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2017-04-02 10:42:33 +02:00
|
|
|
def __init__(self, book, book_format, uncompressed_size, name):
|
2016-04-03 23:52:32 +02:00
|
|
|
self.book = book
|
2017-04-02 10:42:33 +02:00
|
|
|
self.format = book_format
|
2016-04-03 23:52:32 +02:00
|
|
|
self.uncompressed_size = uncompressed_size
|
|
|
|
self.name = name
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-04-03 23:52:32 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return u"<Data('{0},{1}{2}{3}')>".format(self.book, self.format, self.uncompressed_size, self.name)
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-12-23 09:53:39 +01:00
|
|
|
|
2015-08-02 20:59:11 +02:00
|
|
|
class Books(Base):
|
2016-04-03 23:52:32 +02:00
|
|
|
__tablename__ = 'books'
|
|
|
|
|
2017-07-10 18:20:46 +02:00
|
|
|
DEFAULT_PUBDATE = "0101-01-01 00:00:00+00:00"
|
2017-07-10 01:27:46 +02:00
|
|
|
|
2017-10-21 21:50:47 +02:00
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
title = Column(String)
|
2016-04-03 23:52:32 +02:00
|
|
|
sort = Column(String)
|
|
|
|
author_sort = Column(String)
|
|
|
|
timestamp = Column(String)
|
|
|
|
pubdate = Column(String)
|
|
|
|
series_index = Column(String)
|
|
|
|
last_modified = Column(String)
|
|
|
|
path = Column(String)
|
|
|
|
has_cover = Column(Integer)
|
2016-11-09 19:24:33 +01:00
|
|
|
uuid = Column(String)
|
2016-04-03 23:52:32 +02:00
|
|
|
|
|
|
|
authors = relationship('Authors', secondary=books_authors_link, backref='books')
|
2018-06-02 17:02:18 +02:00
|
|
|
tags = relationship('Tags', secondary=books_tags_link, backref='books',order_by="Tags.name")
|
2016-04-03 23:52:32 +02:00
|
|
|
comments = relationship('Comments', backref='books')
|
|
|
|
data = relationship('Data', backref='books')
|
|
|
|
series = relationship('Series', secondary=books_series_link, backref='books')
|
|
|
|
ratings = relationship('Ratings', secondary=books_ratings_link, backref='books')
|
|
|
|
languages = relationship('Languages', secondary=books_languages_link, backref='books')
|
2017-02-22 20:59:48 +01:00
|
|
|
publishers = relationship('Publishers', secondary=books_publishers_link, backref='books')
|
2017-01-28 20:16:40 +01:00
|
|
|
identifiers = relationship('Identifiers', backref='books')
|
2016-12-27 10:36:06 +01:00
|
|
|
|
2017-01-28 20:16:40 +01:00
|
|
|
def __init__(self, title, sort, author_sort, timestamp, pubdate, series_index, last_modified, path, has_cover,
|
2017-11-30 16:49:46 +01:00
|
|
|
authors, tags, languages=None):
|
2016-04-03 23:52:32 +02:00
|
|
|
self.title = title
|
|
|
|
self.sort = sort
|
|
|
|
self.author_sort = author_sort
|
|
|
|
self.timestamp = timestamp
|
|
|
|
self.pubdate = pubdate
|
|
|
|
self.series_index = series_index
|
|
|
|
self.last_modified = last_modified
|
|
|
|
self.path = path
|
|
|
|
self.has_cover = has_cover
|
|
|
|
|
|
|
|
def __repr__(self):
|
2016-12-23 09:53:39 +01:00
|
|
|
return u"<Books('{0},{1}{2}{3}{4}{5}{6}{7}{8}')>".format(self.title, self.sort, self.author_sort,
|
|
|
|
self.timestamp, self.pubdate, self.series_index,
|
|
|
|
self.last_modified, self.path, self.has_cover)
|
|
|
|
|
2018-05-26 17:21:20 +02:00
|
|
|
@property
|
|
|
|
def atom_timestamp(self):
|
|
|
|
return (self.timestamp or '').replace(' ', 'T')
|
2016-04-20 00:20:02 +02:00
|
|
|
|
2016-04-17 18:03:47 +02:00
|
|
|
class Custom_Columns(Base):
|
|
|
|
__tablename__ = 'custom_columns'
|
2017-03-30 21:17:18 +02:00
|
|
|
|
2017-10-21 21:50:47 +02:00
|
|
|
id = Column(Integer, primary_key=True)
|
2016-04-17 18:03:47 +02:00
|
|
|
label = Column(String)
|
|
|
|
name = Column(String)
|
|
|
|
datatype = Column(String)
|
|
|
|
mark_for_delete = Column(Boolean)
|
|
|
|
editable = Column(Boolean)
|
|
|
|
display = Column(String)
|
|
|
|
is_multiple = Column(Boolean)
|
2016-04-20 18:56:03 +02:00
|
|
|
normalized = Column(Boolean)
|
2017-04-02 10:42:33 +02:00
|
|
|
|
2016-04-20 18:56:03 +02:00
|
|
|
def get_display_dict(self):
|
|
|
|
display_dict = ast.literal_eval(self.display)
|
2019-01-14 20:27:53 +01:00
|
|
|
if sys.version_info < (3, 0):
|
|
|
|
display_dict['enum_values'] = [x.decode('unicode_escape') for x in display_dict['enum_values']]
|
2016-04-20 18:56:03 +02:00
|
|
|
return display_dict
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2017-01-22 21:30:36 +01:00
|
|
|
|
2017-01-28 20:16:40 +01:00
|
|
|
def setup_db():
|
2017-03-07 19:10:17 +01:00
|
|
|
global engine
|
2017-01-22 16:44:37 +01:00
|
|
|
global session
|
2017-01-22 21:30:36 +01:00
|
|
|
global cc_classes
|
2017-01-22 16:44:37 +01:00
|
|
|
|
2017-11-30 16:49:46 +01:00
|
|
|
if config.config_calibre_dir is None or config.config_calibre_dir == u'':
|
2017-04-26 20:21:40 +02:00
|
|
|
content = ub.session.query(ub.Settings).first()
|
|
|
|
content.config_calibre_dir = None
|
|
|
|
content.db_configured = False
|
|
|
|
ub.session.commit()
|
|
|
|
config.loadSettings()
|
2017-01-28 20:16:40 +01:00
|
|
|
return False
|
2017-01-22 21:30:36 +01:00
|
|
|
|
2017-01-22 16:44:37 +01:00
|
|
|
dbpath = os.path.join(config.config_calibre_dir, "metadata.db")
|
2017-01-28 20:16:40 +01:00
|
|
|
try:
|
2018-07-28 17:19:08 +02:00
|
|
|
if not os.path.exists(dbpath):
|
|
|
|
raise
|
2018-08-12 09:29:57 +02:00
|
|
|
engine = create_engine('sqlite:///' + dbpath, echo=False, isolation_level="SERIALIZABLE", connect_args={'check_same_thread': False})
|
2017-01-28 20:16:40 +01:00
|
|
|
conn = engine.connect()
|
2017-03-29 21:43:55 +02:00
|
|
|
except Exception:
|
2017-01-28 20:16:40 +01:00
|
|
|
content = ub.session.query(ub.Settings).first()
|
|
|
|
content.config_calibre_dir = None
|
|
|
|
content.db_configured = False
|
|
|
|
ub.session.commit()
|
|
|
|
config.loadSettings()
|
|
|
|
return False
|
|
|
|
content = ub.session.query(ub.Settings).first()
|
|
|
|
content.db_configured = True
|
|
|
|
ub.session.commit()
|
|
|
|
config.loadSettings()
|
2017-01-22 16:44:37 +01:00
|
|
|
conn.connection.create_function('title_sort', 1, title_sort)
|
2017-10-21 21:50:47 +02:00
|
|
|
conn.connection.create_function('lower', 1, lcase)
|
|
|
|
conn.connection.create_function('upper', 1, ucase)
|
2017-01-22 16:44:37 +01:00
|
|
|
|
2017-03-07 19:10:17 +01:00
|
|
|
if not cc_classes:
|
|
|
|
cc = conn.execute("SELECT id, datatype FROM custom_columns")
|
|
|
|
|
|
|
|
cc_ids = []
|
|
|
|
books_custom_column_links = {}
|
|
|
|
cc_classes = {}
|
|
|
|
for row in cc:
|
|
|
|
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)
|
|
|
|
)
|
|
|
|
cc_ids.append([row.id, 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)}
|
2017-05-22 22:54:53 +02:00
|
|
|
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)}
|
2017-03-07 19:10:17 +01:00
|
|
|
else:
|
|
|
|
ccdict = {'__tablename__': 'custom_column_' + str(row.id),
|
|
|
|
'id': Column(Integer, primary_key=True),
|
|
|
|
'value': Column(String)}
|
|
|
|
cc_classes[row.id] = type('Custom_Column_' + str(row.id), (Base,), ccdict)
|
|
|
|
|
2017-04-02 10:42:33 +02:00
|
|
|
for cc_id in cc_ids:
|
2017-05-22 22:54:53 +02:00
|
|
|
if (cc_id[1] == 'bool') or (cc_id[1] == 'int'):
|
2017-04-02 10:42:33 +02:00
|
|
|
setattr(Books, 'custom_column_' + str(cc_id[0]), relationship(cc_classes[cc_id[0]],
|
2017-03-07 19:10:17 +01:00
|
|
|
primaryjoin=(
|
2017-04-02 10:42:33 +02:00
|
|
|
Books.id == cc_classes[cc_id[0]].book),
|
2017-03-07 19:10:17 +01:00
|
|
|
backref='books'))
|
2017-01-22 16:44:37 +01:00
|
|
|
else:
|
2017-04-02 10:42:33 +02:00
|
|
|
setattr(Books, 'custom_column_' + str(cc_id[0]), relationship(cc_classes[cc_id[0]],
|
|
|
|
secondary=books_custom_column_links[cc_id[0]],
|
2017-03-07 19:10:17 +01:00
|
|
|
backref='books'))
|
2017-01-22 16:44:37 +01:00
|
|
|
|
2018-08-12 09:29:57 +02:00
|
|
|
|
|
|
|
Session = scoped_session(sessionmaker(autocommit=False,
|
|
|
|
autoflush=False,
|
|
|
|
bind=engine))
|
2017-01-22 16:44:37 +01:00
|
|
|
session = Session()
|
2017-04-27 15:32:30 +02:00
|
|
|
return True
|