1
0
mirror of https://github.com/janeczku/calibre-web synced 2024-11-30 21:40:00 +00:00

Refactor (settings database, etc...)

This commit is contained in:
Jan Broer 2015-08-02 21:23:24 +02:00
parent d514722fa7
commit aef608aa10
8 changed files with 164 additions and 42 deletions

View File

@ -48,20 +48,12 @@ TEMPLATEDIR = check_setting_str(CFG, 'General', 'TEMPLATEDIR', os.path.join(os.g
MAIN_DIR = check_setting_str(CFG, 'General', 'MAIN_DIR', os.getcwd()) MAIN_DIR = check_setting_str(CFG, 'General', 'MAIN_DIR', os.getcwd())
PORT = check_setting_int(CFG, 'General', 'PORT', 8083) PORT = check_setting_int(CFG, 'General', 'PORT', 8083)
NEWEST_BOOKS = check_setting_str(CFG, 'General', 'NEWEST_BOOKS', 60) NEWEST_BOOKS = check_setting_str(CFG, 'General', 'NEWEST_BOOKS', 60)
RANDOM_BOOKS = check_setting_int(CFG, 'General', 'RANDOM_BOOKS', 6) RANDOM_BOOKS = check_setting_int(CFG, 'General', 'RANDOM_BOOKS', 4)
ALL_BOOKS = check_setting_str(CFG, 'General', 'ALL_BOOKS', 100) ALL_BOOKS = check_setting_str(CFG, 'General', 'ALL_BOOKS', 100)
CheckSection('Mail')
MAIL_SERVER = check_setting_str(CFG, 'Mail', 'MAIL_SERVER', 'mail.example.com')
MAIL_LOGIN = check_setting_str(CFG, 'Mail', 'MAIL_LOGIN', "mail@example.com")
MAIL_PASSWORD = check_setting_str(CFG, 'Mail', 'MAIL_PASSWORD', "mypassword")
MAIL_PORT = check_setting_int(CFG, 'Mail', 'MAIL_PORT', 25)
MAIL_FROM = check_setting_str(CFG, 'Mail', 'MAIL_FROM', "library automailer <mail@example.com>")
CheckSection('Advanced') CheckSection('Advanced')
TITLE_REGEX = check_setting_str(CFG, 'Advanced', 'TITLE_REGEX', '^(Der|Die|Das|Ein|Eine)\s+') TITLE_REGEX = check_setting_str(CFG, 'Advanced', 'TITLE_REGEX', '^(Der|Die|Das|Ein|Eine)\s+')
DEVELOPMENT = bool(check_setting_int(CFG, 'Advanced', 'DEVELOPMENT', 1)) DEVELOPMENT = bool(check_setting_int(CFG, 'Advanced', 'DEVELOPMENT', 1))
FIRST_RUN = bool(check_setting_int(CFG, 'Advanced', 'FIRST_RUN', 1))
SYS_ENCODING="UTF-8" SYS_ENCODING="UTF-8"
@ -73,13 +65,7 @@ configval["PORT"] = PORT
configval["NEWEST_BOOKS"] = NEWEST_BOOKS configval["NEWEST_BOOKS"] = NEWEST_BOOKS
configval["ALL_BOOKS"] = ALL_BOOKS configval["ALL_BOOKS"] = ALL_BOOKS
configval["DEVELOPMENT"] = DEVELOPMENT configval["DEVELOPMENT"] = DEVELOPMENT
configval["MAIL_SERVER"] = MAIL_SERVER
configval["MAIL_FROM"] = MAIL_FROM
configval["MAIL_PORT"] = MAIL_PORT
configval["MAIL_LOGIN"] = MAIL_LOGIN
configval["MAIL_PASSWORD"] = MAIL_PASSWORD
configval["TITLE_REGEX"] = TITLE_REGEX configval["TITLE_REGEX"] = TITLE_REGEX
configval["FIRST_RUN"] = FIRST_RUN
def save_config(configval): def save_config(configval):
new_config = ConfigObj() new_config = ConfigObj()
@ -91,16 +77,9 @@ def save_config(configval):
new_config['General']['PORT'] = configval["PORT"] new_config['General']['PORT'] = configval["PORT"]
new_config['General']['NEWEST_BOOKS'] = configval["NEWEST_BOOKS"] new_config['General']['NEWEST_BOOKS'] = configval["NEWEST_BOOKS"]
new_config['General']['ALL_BOOKS'] = configval["ALL_BOOKS"] new_config['General']['ALL_BOOKS'] = configval["ALL_BOOKS"]
new_config['Mail'] = {}
new_config['Mail']['MAIL_PORT'] = int(configval["MAIL_PORT"])
new_config['Mail']['MAIL_SERVER'] = configval["MAIL_SERVER"]
new_config['Mail']['MAIL_FROM'] = configval["MAIL_FROM"]
new_config['Mail']['MAIL_LOGIN'] = configval["MAIL_LOGIN"]
new_config['Mail']['MAIL_PASSWORD'] = configval["MAIL_PASSWORD"]
new_config['Advanced'] = {} new_config['Advanced'] = {}
new_config['Advanced']['TITLE_REGEX'] = configval["TITLE_REGEX"] new_config['Advanced']['TITLE_REGEX'] = configval["TITLE_REGEX"]
new_config['Advanced']['DEVELOPMENT'] = int(configval["DEVELOPMENT"]) new_config['Advanced']['DEVELOPMENT'] = int(configval["DEVELOPMENT"])
new_config['Advanced']['FIRST_RUN'] = int(configval["FIRST_RUN"])
new_config.write() new_config.write()
return "Saved" return "Saved"

View File

@ -25,7 +25,7 @@ def update_download(book_id, user_id):
ub.session.commit() ub.session.commit()
def make_mobi(book_id): def make_mobi(book_id):
kindlegen = os.path.join(config.MAIN_DIR, "kindlegen") kindlegen = os.path.join(config.MAIN_DIR, "vendor", "kindlegen")
if not os.path.exists(kindlegen): if not os.path.exists(kindlegen):
return False return False
book = db.session.query(db.Books).filter(db.Books.id == book_id).first() book = db.session.query(db.Books).filter(db.Books.id == book_id).first()
@ -54,9 +54,10 @@ def send_mail(book_id, kindle_mail):
is_mobi = False is_mobi = False
is_epub = False is_epub = False
settings = ub.get_mail_settings()
# create MIME message # create MIME message
msg = MIMEMultipart() msg = MIMEMultipart()
msg['From'] = config.MAIL_FROM msg['From'] = settings["mail_from"]
msg['To'] = kindle_mail msg['To'] = kindle_mail
msg['Subject'] = 'Sent to Kindle' msg['Subject'] = 'Sent to Kindle'
text = 'This email has been automatically sent by library.' text = 'This email has been automatically sent by library.'
@ -93,10 +94,10 @@ def send_mail(book_id, kindle_mail):
# send email # send email
try: try:
mail_server = smtplib.SMTP(host=config.MAIL_SERVER, mail_server = smtplib.SMTP(host=settings["mail_server"],
port=config.MAIL_PORT) port=settings["mail_port"])
mail_server.login(config.MAIL_LOGIN, config.MAIL_PASSWORD) mail_server.login(settings["mail_login"], settings["mail_password"])
mail_server.sendmail(config.MAIL_LOGIN, kindle_mail, msg) mail_server.sendmail(settings["mail_login"], kindle_mail, msg)
mail_server.close() mail_server.close()
except smtplib.SMTPException: except smtplib.SMTPException:
traceback.print_exc() traceback.print_exc()

View File

@ -0,0 +1,30 @@
{% extends "layout.html" %}
{% block body %}
<div class="discover">
<h1>{{title}}</h1>
<form role="form" method="POST">
<div class="form-group">
<label for="mail_server">SMTP hostname</label>
<input type="text" class="form-control" name="mail_server" id="mail_server" value="{{content.mail_server}}">
</div>
<div class="form-group">
<label for="mail_port">SMTP port</label>
<input type="text" class="form-control" name="mail_port" id="mail_port" value="{{content.mail_port}}">
</div>
<div class="form-group">
<label for="mail_login">SMTP login</label>
<input type="text" class="form-control" name="mail_login" id="mail_login" value="{{content.mail_login}}">
</div>
<div class="form-group">
<label for="mail_password">SMTP password</label>
<input type="password" class="form-control" name="mail_password" id="mail_password" value="{{content.mail_password}}">
</div>
<div class="form-group">
<label for="mail_from">From e-mail</label>
<input type="text" class="form-control" name="mail_from" id="mail_from" value="{{content.mail_from}}">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
{% endblock %}

View File

@ -1,18 +1,14 @@
{% extends "layout.html" %} {% extends "layout.html" %}
{% block body %} {% block body %}
<div class="discover"> <div class="discover">
<h1>{{title}}</h1> <h2>{{title}}</h2>
<div class="col-sm-10">
<div class="btn btn-default"><a href="{{url_for('new_user')}}">create user</a></div>
</div>
</div>
<table class="table table-striped"> <table class="table table-striped">
<tr> <tr>
<th>nickname</th> <th>Nickname</th>
<th>email</th> <th>Email</th>
<th>kindle</th> <th>Kindle</th>
<th>dls</th> <th>DLS</th>
<th>admin</th> <th>Admin</th>
</tr> </tr>
{% for user in content %} {% for user in content %}
<tr> <tr>
@ -22,7 +18,27 @@
<td>{{user.downloads.count()}}</td> <td>{{user.downloads.count()}}</td>
<td>{% if user.role %}<span class="glyphicon glyphicon-ok"></span>{% else %}<span class="glyphicon glyphicon-remove"></span>{% endif %}</td> <td>{% if user.role %}<span class="glyphicon glyphicon-ok"></span>{% else %}<span class="glyphicon glyphicon-remove"></span>{% endif %}</td>
{% endfor %} {% endfor %}
</table>
<div class="btn btn-default"><a href="{{url_for('new_user')}}">Add new user</a></div>
<h2>SMTP mail settings</h2>
<table class="table table-striped">
<tr>
<th>SMTP hostname</th>
<th>SMTP port</th>
<th>SMTP login</th>
<th>SMTP password</th>
<th>From mail</th>
</tr>
<tr>
<td>{{email.mail_server}}</td>
<td>{{email.mail_port}}</td>
<td>{{email.mail_login}}</td>
<td>********</td>
<td>{{email.mail_from}}</td>
</table>
<div class="btn btn-default"><a href="{{url_for('edit_mailsettings')}}">Change SMTP settings</a></div>
</tabel>
</div> </div>
{% endblock %} {% endblock %}

View File

@ -6,6 +6,7 @@ from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import * from sqlalchemy.orm import *
import os import os
from cps import config from cps import config
from werkzeug.security import generate_password_hash
dbpath = os.path.join(config.MAIN_DIR, "app.db") dbpath = os.path.join(config.MAIN_DIR, "app.db")
engine = create_engine('sqlite:///{0}'.format(dbpath), echo=False) engine = create_engine('sqlite:///{0}'.format(dbpath), echo=False)
@ -13,6 +14,7 @@ Base = declarative_base()
ROLE_USER = 0 ROLE_USER = 0
ROLE_ADMIN = 1 ROLE_ADMIN = 1
DEFAULT_PASS = "admin123"
class User(Base): class User(Base):
__tablename__ = 'user' __tablename__ = 'user'
@ -101,7 +103,61 @@ class Whish(Base):
def __repr__(self): def __repr__(self):
return '<Whish %r>' % (self.title) return '<Whish %r>' % (self.title)
Base.metadata.create_all(engine) class Settings(Base):
__tablename__ = 'settings'
id = Column(Integer, primary_key=True)
mail_server = Column(String)
mail_port = Column(Integer, default = 25)
mail_login = Column(String)
mail_password = Column(String)
mail_from = Column(String)
def __repr__(self):
#return '<Smtp %r>' % (self.mail_server)
pass
def create_default_config():
settings = Settings()
settings.mail_server = "mail.example.com"
settings.mail_port = 25
settings.mail_login = "mail@example.com"
settings.mail_password = "mypassword"
settings.mail_from = "automailer <mail@example.com>"
session.add(settings)
session.commit()
def get_mail_settings():
settings = session.query(Settings).first()
if not settings:
return {}
data = {
'mail_server': settings.mail_server,
'mail_port': settings.mail_port,
'mail_login': settings.mail_login,
'mail_password': settings.mail_password,
'mail_from': settings.mail_from
}
return data
def create_admin_user():
user = User()
user.nickname = "admin"
user.role = 1
user.password = generate_password_hash(DEFAULT_PASS)
session.add(user)
session.commit()
Session = sessionmaker() Session = sessionmaker()
Session.configure(bind=engine) Session.configure(bind=engine)
session = Session() session = Session()
if not os.path.exists(dbpath):
Base.metadata.create_all(engine)
create_default_config()
create_admin_user()

View File

@ -322,7 +322,10 @@ def logout():
@app.route('/send/<int:book_id>') @app.route('/send/<int:book_id>')
@login_required @login_required
def send_to_kindle(book_id): def send_to_kindle(book_id):
if current_user.kindle_mail: settings = ub.get_mail_settings()
if settings.get("mail_server", "mail.example.com") == "mail.example.com":
flash("please configure your email account settings first...", category="error")
elif current_user.kindle_mail:
x = helper.send_mail(book_id, current_user.kindle_mail) x = helper.send_mail(book_id, current_user.kindle_mail)
if x: if x:
flash("mail successfully send to %s" % current_user.kindle_mail, category="success") flash("mail successfully send to %s" % current_user.kindle_mail, category="success")
@ -404,7 +407,8 @@ def profile():
@login_required @login_required
def user_list(): def user_list():
content = ub.session.query(ub.User).all() content = ub.session.query(ub.User).all()
return render_template("user_list.html", content=content, title="User list") settings = ub.session.query(ub.Settings).first()
return render_template("user_list.html", content=content, email=settings, title="User list")
@app.route("/admin/user/new", methods = ["GET", "POST"]) @app.route("/admin/user/new", methods = ["GET", "POST"])
@login_required @login_required
@ -424,6 +428,24 @@ def new_user():
flash(e, category="error") flash(e, category="error")
return render_template("user_edit.html", content=content, title="User list") return render_template("user_edit.html", content=content, title="User list")
@app.route("/admin/user/mailsettings", methods = ["GET", "POST"])
@login_required
def edit_mailsettings():
content = ub.session.query(ub.Settings).first()
if request.method == "POST":
to_save = request.form.to_dict()
content.mail_server = to_save["mail_server"]
content.mail_port = int(to_save["mail_port"])
content.mail_login = to_save["mail_login"]
content.mail_password = to_save["mail_password"]
content.mail_from = to_save["mail_from"]
try:
ub.session.commit()
flash("Mail settings updated", category="success")
except (e):
flash(e, category="error")
return render_template("email_edit.html", content=content, title="Edit mail settings")
@app.route("/admin/user/<int:user_id>", methods = ["GET", "POST"]) @app.route("/admin/user/<int:user_id>", methods = ["GET", "POST"])
@login_required @login_required
def edit_user(user_id): def edit_user(user_id):

View File

18
wsgi.py Normal file
View File

@ -0,0 +1,18 @@
import os
import sys
base_path = os.path.dirname(os.path.abspath(__file__))
# Insert local directories into path
sys.path.append(os.path.join(base_path, 'lib'))
from cps import web
from cps import config
global title_sort
def title_sort(title):
return title
if __name__ == "__main__":
web.app.run(host="0.0.0.0",port=config.PORT, debug=True)