2015-08-02 18:59:11 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from sqlalchemy import *
|
|
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
|
|
from sqlalchemy.orm import *
|
|
|
|
import os
|
|
|
|
from cps import config
|
|
|
|
import re
|
2016-04-20 16:56:03 +00:00
|
|
|
import ast
|
2015-08-02 18:59:11 +00:00
|
|
|
|
|
|
|
#calibre sort stuff
|
|
|
|
title_pat = re.compile(config.TITLE_REGEX, re.IGNORECASE)
|
|
|
|
def title_sort(title):
|
|
|
|
match = title_pat.search(title)
|
|
|
|
if match:
|
|
|
|
prep = match.group(1)
|
|
|
|
title = title.replace(prep, '') + ', ' + prep
|
|
|
|
return title.strip()
|
|
|
|
|
|
|
|
|
|
|
|
dbpath = os.path.join(config.DB_ROOT, "metadata.db")
|
2016-08-03 05:37:53 +00:00
|
|
|
engine = create_engine('sqlite:///{0}'.format(dbpath.encode('utf-8')), echo=False)
|
2015-08-02 18:59:11 +00:00
|
|
|
conn = engine.connect()
|
|
|
|
conn.connection.create_function('title_sort', 1, title_sort)
|
|
|
|
Base = declarative_base()
|
|
|
|
|
|
|
|
books_authors_link = Table('books_authors_link', Base.metadata,
|
2016-04-03 21:52:32 +00:00
|
|
|
Column('book', Integer, ForeignKey('books.id'), primary_key=True),
|
|
|
|
Column('author', Integer, ForeignKey('authors.id'), primary_key=True)
|
|
|
|
)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
|
|
|
books_tags_link = Table('books_tags_link', Base.metadata,
|
2016-04-03 21:52:32 +00:00
|
|
|
Column('book', Integer, ForeignKey('books.id'), primary_key=True),
|
|
|
|
Column('tag', Integer, ForeignKey('tags.id'), primary_key=True)
|
|
|
|
)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
|
|
|
books_series_link = Table('books_series_link', Base.metadata,
|
2016-04-03 21:52:32 +00:00
|
|
|
Column('book', Integer, ForeignKey('books.id'), primary_key=True),
|
|
|
|
Column('series', Integer, ForeignKey('series.id'), primary_key=True)
|
|
|
|
)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
|
|
|
books_ratings_link = Table('books_ratings_link', Base.metadata,
|
2016-04-03 21:52:32 +00:00
|
|
|
Column('book', Integer, ForeignKey('books.id'), primary_key=True),
|
|
|
|
Column('rating', Integer, ForeignKey('ratings.id'), primary_key=True)
|
|
|
|
)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2015-10-12 23:21:22 +00:00
|
|
|
books_languages_link = Table('books_languages_link', Base.metadata,
|
2016-04-03 21:52:32 +00:00
|
|
|
Column('book', Integer, ForeignKey('books.id'), primary_key=True),
|
|
|
|
Column('lang_code', Integer, ForeignKey('languages.id'), primary_key=True)
|
|
|
|
)
|
2015-10-12 23:21:22 +00:00
|
|
|
|
2016-04-20 16:56:03 +00:00
|
|
|
cc = conn.execute("SELECT id, datatype FROM custom_columns")
|
2016-04-19 22:20:02 +00:00
|
|
|
cc_ids = []
|
2016-04-20 16:56:03 +00:00
|
|
|
cc_exceptions = ['bool', 'datetime', 'int', 'comments', 'float', ]
|
2016-04-19 22:20:02 +00:00
|
|
|
books_custom_column_links = {}
|
|
|
|
for row in cc:
|
2016-04-20 16:56:03 +00:00
|
|
|
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)
|
2016-04-19 22:20:02 +00:00
|
|
|
|
|
|
|
cc_classes = {}
|
|
|
|
for id in cc_ids:
|
|
|
|
ccdict={'__tablename__':'custom_column_' + str(id),
|
|
|
|
'id':Column(Integer, primary_key=True),
|
|
|
|
'value':Column(String)}
|
|
|
|
cc_classes[id] = type('Custom_Column_' + str(id), (Base,), ccdict)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
|
|
|
class Comments(Base):
|
2016-04-03 21:52:32 +00:00
|
|
|
__tablename__ = 'comments'
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-04-03 21:52:32 +00:00
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
text = Column(String)
|
|
|
|
book = Column(Integer, ForeignKey('books.id'))
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-04-03 21:52:32 +00:00
|
|
|
def __init__(self, text, book):
|
|
|
|
self.text = text
|
|
|
|
self.book = book
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-04-03 21:52:32 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return u"<Comments({0})>".format(self.text)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Tags(Base):
|
2016-04-03 21:52:32 +00:00
|
|
|
__tablename__ = 'tags'
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-04-03 21:52:32 +00:00
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
|
|
name = Column(String)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-04-03 21:52:32 +00:00
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-04-03 21:52:32 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return u"<Tags('{0})>".format(self.name)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
|
|
|
class Authors(Base):
|
2016-04-03 21:52:32 +00:00
|
|
|
__tablename__ = 'authors'
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-04-03 21:52:32 +00:00
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
name = Column(String)
|
|
|
|
sort = Column(String)
|
|
|
|
link = Column(String)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-04-03 21:52:32 +00:00
|
|
|
def __init__(self, name, sort, link):
|
|
|
|
self.name = name
|
|
|
|
self.sort = sort
|
|
|
|
self.link = link
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-04-03 21:52:32 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return u"<Authors('{0},{1}{2}')>".format(self.name, self.sort, self.link)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
|
|
|
class Series(Base):
|
2016-04-03 21:52:32 +00:00
|
|
|
__tablename__ = 'series'
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-04-03 21:52:32 +00:00
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
name = Column(String)
|
|
|
|
sort = Column(String)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-04-03 21:52:32 +00:00
|
|
|
def __init__(self, name, sort):
|
|
|
|
self.name = name
|
|
|
|
self.sort = sort
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-04-03 21:52:32 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return u"<Series('{0},{1}')>".format(self.name, self.sort)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
|
|
|
class Ratings(Base):
|
2016-04-03 21:52:32 +00:00
|
|
|
__tablename__ = 'ratings'
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-04-03 21:52:32 +00:00
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
rating = Column(Integer)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-04-03 21:52:32 +00:00
|
|
|
def __init__(self,rating):
|
|
|
|
self.rating = rating
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-04-03 21:52:32 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return u"<Ratings('{0}')>".format(self.rating)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2015-10-12 23:21:22 +00:00
|
|
|
class Languages(Base):
|
|
|
|
__tablename__ = 'languages'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
lang_code = Column(String)
|
|
|
|
|
|
|
|
def __init__(self,lang_code):
|
|
|
|
self.lang_code = lang_code
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return u"<Languages('{0}')>".format(self.lang_code)
|
|
|
|
|
2015-08-02 18:59:11 +00:00
|
|
|
class Data(Base):
|
2016-04-03 21:52:32 +00:00
|
|
|
__tablename__ = 'data'
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-04-03 21:52:32 +00:00
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
book = Column(Integer, ForeignKey('books.id'))
|
|
|
|
format = Column(String)
|
|
|
|
uncompressed_size = Column(Integer)
|
|
|
|
name = Column(String)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-04-03 21:52:32 +00:00
|
|
|
def __init__(self, book, format, uncompressed_size, name):
|
|
|
|
self.book = book
|
|
|
|
self.format = format
|
|
|
|
self.uncompressed_size = uncompressed_size
|
|
|
|
self.name = name
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-04-03 21:52:32 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return u"<Data('{0},{1}{2}{3}')>".format(self.book, self.format, self.uncompressed_size, self.name)
|
2015-08-02 18:59:11 +00:00
|
|
|
|
|
|
|
class Books(Base):
|
2016-04-03 21:52:32 +00:00
|
|
|
__tablename__ = 'books'
|
|
|
|
|
2016-04-19 22:20:02 +00:00
|
|
|
id = Column(Integer, primary_key=True)
|
2016-04-03 21:52:32 +00:00
|
|
|
title = Column(String)
|
|
|
|
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)
|
|
|
|
|
|
|
|
authors = relationship('Authors', secondary=books_authors_link, backref='books')
|
|
|
|
tags = relationship('Tags', secondary=books_tags_link, backref='books')
|
|
|
|
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')
|
2016-04-17 16:03:47 +00:00
|
|
|
|
2016-04-03 21:52:32 +00:00
|
|
|
def __init__(self, title, sort, author_sort, timestamp, pubdate, series_index, last_modified, path, has_cover, authors, tags):
|
|
|
|
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):
|
|
|
|
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)
|
2016-04-19 22:20:02 +00:00
|
|
|
for id in cc_ids:
|
|
|
|
setattr(Books, 'custom_column_' + str(id), relationship(cc_classes[id], secondary=books_custom_column_links[id], backref='books'))
|
|
|
|
|
2016-04-17 16:03:47 +00:00
|
|
|
class Custom_Columns(Base):
|
|
|
|
__tablename__ = 'custom_columns'
|
|
|
|
|
|
|
|
id = Column(Integer,primary_key=True)
|
|
|
|
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 16:56:03 +00:00
|
|
|
normalized = Column(Boolean)
|
|
|
|
|
|
|
|
def get_display_dict(self):
|
|
|
|
display_dict = ast.literal_eval(self.display)
|
|
|
|
return display_dict
|
2015-08-02 18:59:11 +00:00
|
|
|
|
2016-04-19 22:20:02 +00:00
|
|
|
#Base.metadata.create_all(engine)
|
2015-08-02 18:59:11 +00:00
|
|
|
Session = sessionmaker()
|
|
|
|
Session.configure(bind=engine)
|
|
|
|
session = Session()
|
2016-04-17 16:03:47 +00:00
|
|
|
|
|
|
|
|