Refactor (settings database, etc...)
This commit is contained in:
parent
d514722fa7
commit
aef608aa10
|
@ -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())
|
||||
PORT = check_setting_int(CFG, 'General', 'PORT', 8083)
|
||||
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)
|
||||
|
||||
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')
|
||||
TITLE_REGEX = check_setting_str(CFG, 'Advanced', 'TITLE_REGEX', '^(Der|Die|Das|Ein|Eine)\s+')
|
||||
DEVELOPMENT = bool(check_setting_int(CFG, 'Advanced', 'DEVELOPMENT', 1))
|
||||
FIRST_RUN = bool(check_setting_int(CFG, 'Advanced', 'FIRST_RUN', 1))
|
||||
|
||||
SYS_ENCODING="UTF-8"
|
||||
|
||||
|
@ -73,13 +65,7 @@ configval["PORT"] = PORT
|
|||
configval["NEWEST_BOOKS"] = NEWEST_BOOKS
|
||||
configval["ALL_BOOKS"] = ALL_BOOKS
|
||||
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["FIRST_RUN"] = FIRST_RUN
|
||||
|
||||
def save_config(configval):
|
||||
new_config = ConfigObj()
|
||||
|
@ -91,16 +77,9 @@ def save_config(configval):
|
|||
new_config['General']['PORT'] = configval["PORT"]
|
||||
new_config['General']['NEWEST_BOOKS'] = configval["NEWEST_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']['TITLE_REGEX'] = configval["TITLE_REGEX"]
|
||||
new_config['Advanced']['DEVELOPMENT'] = int(configval["DEVELOPMENT"])
|
||||
new_config['Advanced']['FIRST_RUN'] = int(configval["FIRST_RUN"])
|
||||
new_config.write()
|
||||
return "Saved"
|
||||
|
||||
|
|
|
@ -25,7 +25,7 @@ def update_download(book_id, user_id):
|
|||
ub.session.commit()
|
||||
|
||||
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):
|
||||
return False
|
||||
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_epub = False
|
||||
settings = ub.get_mail_settings()
|
||||
# create MIME message
|
||||
msg = MIMEMultipart()
|
||||
msg['From'] = config.MAIL_FROM
|
||||
msg['From'] = settings["mail_from"]
|
||||
msg['To'] = kindle_mail
|
||||
msg['Subject'] = 'Sent to Kindle'
|
||||
text = 'This email has been automatically sent by library.'
|
||||
|
@ -93,10 +94,10 @@ def send_mail(book_id, kindle_mail):
|
|||
|
||||
# send email
|
||||
try:
|
||||
mail_server = smtplib.SMTP(host=config.MAIL_SERVER,
|
||||
port=config.MAIL_PORT)
|
||||
mail_server.login(config.MAIL_LOGIN, config.MAIL_PASSWORD)
|
||||
mail_server.sendmail(config.MAIL_LOGIN, kindle_mail, msg)
|
||||
mail_server = smtplib.SMTP(host=settings["mail_server"],
|
||||
port=settings["mail_port"])
|
||||
mail_server.login(settings["mail_login"], settings["mail_password"])
|
||||
mail_server.sendmail(settings["mail_login"], kindle_mail, msg)
|
||||
mail_server.close()
|
||||
except smtplib.SMTPException:
|
||||
traceback.print_exc()
|
||||
|
|
30
cps/templates/email_edit.html
Normal file
30
cps/templates/email_edit.html
Normal 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 %}
|
|
@ -1,18 +1,14 @@
|
|||
{% extends "layout.html" %}
|
||||
{% block body %}
|
||||
<div class="discover">
|
||||
<h1>{{title}}</h1>
|
||||
<div class="col-sm-10">
|
||||
<div class="btn btn-default"><a href="{{url_for('new_user')}}">create user</a></div>
|
||||
</div>
|
||||
</div>
|
||||
<h2>{{title}}</h2>
|
||||
<table class="table table-striped">
|
||||
<tr>
|
||||
<th>nickname</th>
|
||||
<th>email</th>
|
||||
<th>kindle</th>
|
||||
<th>dls</th>
|
||||
<th>admin</th>
|
||||
<th>Nickname</th>
|
||||
<th>Email</th>
|
||||
<th>Kindle</th>
|
||||
<th>DLS</th>
|
||||
<th>Admin</th>
|
||||
</tr>
|
||||
{% for user in content %}
|
||||
<tr>
|
||||
|
@ -22,7 +18,27 @@
|
|||
<td>{{user.downloads.count()}}</td>
|
||||
<td>{% if user.role %}<span class="glyphicon glyphicon-ok"></span>{% else %}<span class="glyphicon glyphicon-remove"></span>{% endif %}</td>
|
||||
{% 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>
|
||||
{% endblock %}
|
||||
|
|
58
cps/ub.py
58
cps/ub.py
|
@ -6,6 +6,7 @@ from sqlalchemy.ext.declarative import declarative_base
|
|||
from sqlalchemy.orm import *
|
||||
import os
|
||||
from cps import config
|
||||
from werkzeug.security import generate_password_hash
|
||||
|
||||
dbpath = os.path.join(config.MAIN_DIR, "app.db")
|
||||
engine = create_engine('sqlite:///{0}'.format(dbpath), echo=False)
|
||||
|
@ -13,6 +14,7 @@ Base = declarative_base()
|
|||
|
||||
ROLE_USER = 0
|
||||
ROLE_ADMIN = 1
|
||||
DEFAULT_PASS = "admin123"
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = 'user'
|
||||
|
@ -101,7 +103,61 @@ class Whish(Base):
|
|||
def __repr__(self):
|
||||
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.configure(bind=engine)
|
||||
session = Session()
|
||||
|
||||
if not os.path.exists(dbpath):
|
||||
Base.metadata.create_all(engine)
|
||||
create_default_config()
|
||||
create_admin_user()
|
||||
|
|
26
cps/web.py
26
cps/web.py
|
@ -322,7 +322,10 @@ def logout():
|
|||
@app.route('/send/<int:book_id>')
|
||||
@login_required
|
||||
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)
|
||||
if x:
|
||||
flash("mail successfully send to %s" % current_user.kindle_mail, category="success")
|
||||
|
@ -404,7 +407,8 @@ def profile():
|
|||
@login_required
|
||||
def user_list():
|
||||
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"])
|
||||
@login_required
|
||||
|
@ -424,6 +428,24 @@ def new_user():
|
|||
flash(e, category="error")
|
||||
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"])
|
||||
@login_required
|
||||
def edit_user(user_id):
|
||||
|
|
0
kindlegen → vendor/kindlegen
vendored
0
kindlegen → vendor/kindlegen
vendored
18
wsgi.py
Normal file
18
wsgi.py
Normal 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)
|
Loading…
Reference in New Issue
Block a user