2015-08-02 20:59:11 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from sqlalchemy import *
|
2016-11-09 19:24:33 +01:00
|
|
|
from sqlalchemy import exc
|
2015-08-02 20:59:11 +02:00
|
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
|
|
from sqlalchemy.orm import *
|
2017-01-12 20:43:36 +01:00
|
|
|
from flask_login import AnonymousUserMixin
|
2015-08-02 20:59:11 +02:00
|
|
|
import os
|
2016-08-07 10:32:45 +02:00
|
|
|
import config
|
2017-01-12 20:43:36 +01:00
|
|
|
import traceback
|
2015-08-02 21:23:24 +02:00
|
|
|
from werkzeug.security import generate_password_hash
|
2017-01-12 20:43:36 +01:00
|
|
|
from flask_babel import gettext as _
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2015-08-04 01:44:05 +02:00
|
|
|
dbpath = os.path.join(config.APP_DB_ROOT, "app.db")
|
2015-08-02 20:59:11 +02:00
|
|
|
engine = create_engine('sqlite:///{0}'.format(dbpath), echo=False)
|
|
|
|
Base = declarative_base()
|
|
|
|
|
|
|
|
ROLE_USER = 0
|
|
|
|
ROLE_ADMIN = 1
|
2016-04-27 10:35:23 +02:00
|
|
|
ROLE_DOWNLOAD = 2
|
|
|
|
ROLE_UPLOAD = 4
|
|
|
|
ROLE_EDIT = 8
|
2016-04-27 16:00:58 +02:00
|
|
|
ROLE_PASSWD = 16
|
2017-01-12 20:43:36 +01:00
|
|
|
ROLE_ANONYMOUS = 32
|
2015-08-02 21:23:24 +02:00
|
|
|
DEFAULT_PASS = "admin123"
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-12-23 09:53:39 +01:00
|
|
|
|
2017-01-12 20:43:36 +01:00
|
|
|
class UserBase():
|
2016-04-27 10:35:23 +02:00
|
|
|
def is_authenticated(self):
|
|
|
|
return True
|
2016-12-23 09:53:39 +01:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
def role_admin(self):
|
|
|
|
if self.role is not None:
|
|
|
|
return True if self.role & ROLE_ADMIN == ROLE_ADMIN else False
|
|
|
|
else:
|
|
|
|
return False
|
2016-12-23 09:53:39 +01:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
def role_download(self):
|
|
|
|
if self.role is not None:
|
|
|
|
return True if self.role & ROLE_DOWNLOAD == ROLE_DOWNLOAD else False
|
|
|
|
else:
|
|
|
|
return False
|
2016-12-23 09:53:39 +01:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
def role_upload(self):
|
|
|
|
if self.role is not None:
|
|
|
|
return True if self.role & ROLE_UPLOAD == ROLE_UPLOAD else False
|
|
|
|
else:
|
|
|
|
return False
|
2016-12-23 09:53:39 +01:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
def role_edit(self):
|
|
|
|
if self.role is not None:
|
|
|
|
return True if self.role & ROLE_EDIT == ROLE_EDIT else False
|
|
|
|
else:
|
|
|
|
return False
|
2016-12-23 09:53:39 +01:00
|
|
|
|
2016-04-27 16:00:58 +02:00
|
|
|
def role_passwd(self):
|
|
|
|
if self.role is not None:
|
|
|
|
return True if self.role & ROLE_PASSWD == ROLE_PASSWD else False
|
|
|
|
else:
|
|
|
|
return False
|
2016-04-27 10:35:23 +02:00
|
|
|
|
2017-01-12 20:43:36 +01:00
|
|
|
def role_anonymous(self):
|
|
|
|
if self.role is not None:
|
|
|
|
return True if self.role & ROLE_ANONYMOUS == ROLE_ANONYMOUS else False
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
def is_active(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def is_anonymous(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_id(self):
|
|
|
|
return unicode(self.id)
|
|
|
|
|
2016-11-09 19:24:33 +01:00
|
|
|
def filter_language(self):
|
|
|
|
return self.default_language
|
|
|
|
|
|
|
|
def show_random_books(self):
|
|
|
|
return self.random_books
|
|
|
|
|
|
|
|
def show_language(self):
|
|
|
|
return self.language_books
|
|
|
|
|
|
|
|
def show_hot_books(self):
|
|
|
|
return self.hot_books
|
|
|
|
|
|
|
|
def show_series(self):
|
|
|
|
return self.series_books
|
|
|
|
|
|
|
|
def show_category(self):
|
|
|
|
return self.category_books
|
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
def __repr__(self):
|
2016-12-23 09:53:39 +01:00
|
|
|
return '<User %r>' % self.nickname
|
|
|
|
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2017-01-12 20:43:36 +01:00
|
|
|
class User(UserBase,Base):
|
|
|
|
__tablename__ = 'user'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
nickname = Column(String(64), unique=True)
|
|
|
|
email = Column(String(120), unique=True, default="")
|
|
|
|
role = Column(SmallInteger, default=ROLE_USER)
|
|
|
|
password = Column(String)
|
|
|
|
kindle_mail = Column(String(120), default="")
|
|
|
|
shelf = relationship('Shelf', backref='user', lazy='dynamic')
|
|
|
|
downloads = relationship('Downloads', backref='user', lazy='dynamic')
|
|
|
|
locale = Column(String(2), default="en")
|
|
|
|
random_books = Column(Integer, default=1)
|
|
|
|
language_books = Column(Integer, default=1)
|
|
|
|
series_books = Column(Integer, default=1)
|
|
|
|
category_books = Column(Integer, default=1)
|
|
|
|
hot_books = Column(Integer, default=1)
|
|
|
|
default_language = Column(String(3), default="all")
|
|
|
|
|
|
|
|
|
|
|
|
class Anonymous(AnonymousUserMixin,UserBase):
|
|
|
|
def __init__(self):
|
|
|
|
self.loadSettings()
|
|
|
|
|
|
|
|
def loadSettings(self):
|
|
|
|
data=session.query(User).filter(User.role.op('&')(ROLE_ANONYMOUS) == ROLE_ANONYMOUS).first()
|
|
|
|
self.nickname = data.nickname
|
|
|
|
self.role = data.role
|
|
|
|
self.random_books = data.random_books
|
|
|
|
self.default_language = data.default_language
|
|
|
|
self.language_books = data.language_books
|
|
|
|
self.series_books = data.series_books
|
|
|
|
self.category_books = data.category_books
|
|
|
|
self.hot_books = data.hot_books
|
|
|
|
self.default_language = data.default_language
|
2017-01-14 15:05:49 +01:00
|
|
|
self.locale = data.locale
|
2017-01-12 20:43:36 +01:00
|
|
|
|
|
|
|
def role_admin(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def is_active(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def is_anonymous(self):
|
|
|
|
return config.ANON_BROWSE
|
|
|
|
|
|
|
|
|
2015-08-02 20:59:11 +02:00
|
|
|
class Shelf(Base):
|
2016-04-27 10:35:23 +02:00
|
|
|
__tablename__ = 'shelf'
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-12-23 09:53:39 +01:00
|
|
|
id = Column(Integer, primary_key=True)
|
2016-04-27 10:35:23 +02:00
|
|
|
name = Column(String)
|
|
|
|
is_public = Column(Integer, default=0)
|
|
|
|
user_id = Column(Integer, ForeignKey('user.id'))
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
def __repr__(self):
|
2016-12-23 09:53:39 +01:00
|
|
|
return '<Shelf %r>' % self.name
|
2015-08-02 20:59:11 +02:00
|
|
|
|
|
|
|
class BookShelf(Base):
|
2016-04-27 10:35:23 +02:00
|
|
|
__tablename__ = 'book_shelf_link'
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
book_id = Column(Integer)
|
2016-12-26 11:33:32 +01:00
|
|
|
order = Column(Integer)
|
2016-04-27 10:35:23 +02:00
|
|
|
shelf = Column(Integer, ForeignKey('shelf.id'))
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
def __repr__(self):
|
2016-12-23 09:53:39 +01:00
|
|
|
return '<Book %r>' % self.id
|
2015-08-02 20:59:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Downloads(Base):
|
2016-04-27 10:35:23 +02:00
|
|
|
__tablename__ = 'downloads'
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
book_id = Column(Integer)
|
|
|
|
user_id = Column(Integer, ForeignKey('user.id'))
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
def __repr__(self):
|
2016-12-23 09:53:39 +01:00
|
|
|
return '<Download %r' % self.book_id
|
|
|
|
|
2015-08-02 21:23:24 +02:00
|
|
|
class Settings(Base):
|
2016-04-27 10:35:23 +02:00
|
|
|
__tablename__ = 'settings'
|
2015-08-02 21:23:24 +02:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
mail_server = Column(String)
|
|
|
|
mail_port = Column(Integer, default = 25)
|
|
|
|
mail_use_ssl = Column(SmallInteger, default = 0)
|
|
|
|
mail_login = Column(String)
|
|
|
|
mail_password = Column(String)
|
|
|
|
mail_from = Column(String)
|
2015-08-02 21:23:24 +02:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
def __repr__(self):
|
|
|
|
#return '<Smtp %r>' % (self.mail_server)
|
|
|
|
pass
|
2015-08-02 21:23:24 +02:00
|
|
|
|
2016-12-23 09:53:39 +01:00
|
|
|
|
2016-11-09 19:24:33 +01:00
|
|
|
def migrate_Database():
|
2017-01-12 20:43:36 +01:00
|
|
|
if session.query(User).filter(User.role.op('&')(ROLE_ANONYMOUS) == ROLE_ANONYMOUS).first() is None:
|
|
|
|
create_anonymous_user()
|
2016-11-09 19:24:33 +01:00
|
|
|
try:
|
|
|
|
session.query(exists().where(User.random_books)).scalar()
|
|
|
|
session.commit()
|
2016-12-23 09:53:39 +01:00
|
|
|
except exc.OperationalError: # Database is not compatible, some rows are missing
|
|
|
|
conn = engine.connect()
|
2016-11-09 19:24:33 +01:00
|
|
|
conn.execute("ALTER TABLE user ADD column random_books INTEGER DEFAULT 1")
|
|
|
|
conn.execute("ALTER TABLE user ADD column locale String(2) DEFAULT 'en'")
|
|
|
|
conn.execute("ALTER TABLE user ADD column default_language String(3) DEFAULT 'all'")
|
|
|
|
session.commit()
|
|
|
|
try:
|
|
|
|
session.query(exists().where(User.language_books)).scalar()
|
|
|
|
session.commit()
|
|
|
|
except exc.OperationalError: # Database is not compatible, some rows are missing
|
|
|
|
conn = engine.connect()
|
|
|
|
conn.execute("ALTER TABLE user ADD column language_books INTEGER DEFAULT 1")
|
|
|
|
conn.execute("ALTER TABLE user ADD column series_books INTEGER DEFAULT 1")
|
|
|
|
conn.execute("ALTER TABLE user ADD column category_books INTEGER DEFAULT 1")
|
|
|
|
conn.execute("ALTER TABLE user ADD column hot_books INTEGER DEFAULT 1")
|
|
|
|
session.commit()
|
2016-12-27 16:07:25 +01:00
|
|
|
try:
|
|
|
|
session.query(exists().where(BookShelf.order)).scalar()
|
|
|
|
session.commit()
|
|
|
|
except exc.OperationalError: # Database is not compatible, some rows are missing
|
|
|
|
conn = engine.connect()
|
2016-12-29 10:30:42 +01:00
|
|
|
conn.execute("ALTER TABLE book_shelf_link ADD column `order` INTEGER DEFAULT 1")
|
2016-12-27 16:07:25 +01:00
|
|
|
session.commit()
|
2016-11-09 19:24:33 +01:00
|
|
|
|
|
|
|
|
2015-08-02 21:23:24 +02:00
|
|
|
def create_default_config():
|
2016-04-27 10:35:23 +02:00
|
|
|
settings = Settings()
|
|
|
|
settings.mail_server = "mail.example.com"
|
|
|
|
settings.mail_port = 25
|
|
|
|
settings.mail_use_ssl = 0
|
|
|
|
settings.mail_login = "mail@example.com"
|
|
|
|
settings.mail_password = "mypassword"
|
|
|
|
settings.mail_from = "automailer <mail@example.com>"
|
2015-08-02 21:23:24 +02:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
session.add(settings)
|
|
|
|
session.commit()
|
2015-08-02 21:23:24 +02:00
|
|
|
|
2016-12-23 09:53:39 +01:00
|
|
|
|
2015-08-02 21:23:24 +02:00
|
|
|
def get_mail_settings():
|
2016-04-27 10:35:23 +02:00
|
|
|
settings = session.query(Settings).first()
|
2015-08-02 21:23:24 +02:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
if not settings:
|
2016-12-23 09:53:39 +01:00
|
|
|
return {}
|
2015-08-02 21:23:24 +02:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
data = {
|
2016-12-23 09:53:39 +01:00
|
|
|
'mail_server': settings.mail_server,
|
|
|
|
'mail_port': settings.mail_port,
|
|
|
|
'mail_use_ssl': settings.mail_use_ssl,
|
|
|
|
'mail_login': settings.mail_login,
|
|
|
|
'mail_password': settings.mail_password,
|
|
|
|
'mail_from': settings.mail_from
|
2016-04-27 10:35:23 +02:00
|
|
|
}
|
2015-08-02 21:23:24 +02:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
return data
|
2015-08-02 21:23:24 +02:00
|
|
|
|
2017-01-12 20:43:36 +01:00
|
|
|
def create_anonymous_user():
|
|
|
|
user = User()
|
|
|
|
user.nickname = _("Guest")
|
|
|
|
user.email='no@email'
|
|
|
|
user.role = ROLE_ANONYMOUS
|
|
|
|
user.password = generate_password_hash('1')
|
|
|
|
|
|
|
|
session.add(user)
|
|
|
|
try:
|
|
|
|
session.commit()
|
|
|
|
except:
|
|
|
|
session.rollback()
|
|
|
|
pass
|
|
|
|
|
2016-12-23 09:53:39 +01:00
|
|
|
|
2015-08-02 21:23:24 +02:00
|
|
|
def create_admin_user():
|
2016-04-27 10:35:23 +02:00
|
|
|
user = User()
|
|
|
|
user.nickname = "admin"
|
2016-04-27 19:50:04 +02:00
|
|
|
user.role = ROLE_USER + ROLE_ADMIN + ROLE_DOWNLOAD + ROLE_UPLOAD + ROLE_EDIT + ROLE_PASSWD
|
2016-04-27 10:35:23 +02:00
|
|
|
user.password = generate_password_hash(DEFAULT_PASS)
|
2015-08-02 21:23:24 +02:00
|
|
|
|
2016-04-27 10:35:23 +02:00
|
|
|
session.add(user)
|
2016-04-29 22:54:38 +02:00
|
|
|
try:
|
|
|
|
session.commit()
|
|
|
|
except:
|
|
|
|
session.rollback()
|
|
|
|
pass
|
2015-08-02 21:23:24 +02:00
|
|
|
|
2015-08-02 20:59:11 +02:00
|
|
|
Session = sessionmaker()
|
|
|
|
Session.configure(bind=engine)
|
|
|
|
session = Session()
|
2015-08-02 21:23:24 +02:00
|
|
|
|
|
|
|
if not os.path.exists(dbpath):
|
2016-04-27 10:35:23 +02:00
|
|
|
try:
|
|
|
|
Base.metadata.create_all(engine)
|
|
|
|
create_default_config()
|
|
|
|
create_admin_user()
|
2017-01-12 20:43:36 +01:00
|
|
|
create_anonymous_user()
|
2016-04-27 10:35:23 +02:00
|
|
|
except Exception:
|
|
|
|
pass
|
2016-11-09 19:24:33 +01:00
|
|
|
else:
|
|
|
|
migrate_Database()
|