diff --git a/cps/config.py b/cps/config.py index 8544809e..a7bbc875 100755 --- a/cps/config.py +++ b/cps/config.py @@ -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 ") - 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" diff --git a/cps/helper.py b/cps/helper.py index d1897162..64c770ef 100755 --- a/cps/helper.py +++ b/cps/helper.py @@ -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() diff --git a/cps/templates/email_edit.html b/cps/templates/email_edit.html new file mode 100644 index 00000000..31bd286d --- /dev/null +++ b/cps/templates/email_edit.html @@ -0,0 +1,30 @@ +{% extends "layout.html" %} +{% block body %} +
+

{{title}}

+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
+ +
+{% endblock %} diff --git a/cps/templates/user_list.html b/cps/templates/user_list.html index a42dfa75..acf64261 100644 --- a/cps/templates/user_list.html +++ b/cps/templates/user_list.html @@ -1,18 +1,14 @@ {% extends "layout.html" %} {% block body %}
-

{{title}}

- -
+

{{title}}

- - - - - + + + + + {% for user in content %} @@ -22,7 +18,27 @@ {% endfor %} +
nicknameemailkindledlsadminNicknameEmailKindleDLSAdmin
{{user.downloads.count()}} {% if user.role %}{% else %}{% endif %}
+
Add new user
+

SMTP mail settings

+ + + + + + + + + + + + + + + +
SMTP hostnameSMTP portSMTP loginSMTP passwordFrom mail
{{email.mail_server}}{{email.mail_port}}{{email.mail_login}}********{{email.mail_from}}
+ +
Change SMTP settings
- {% endblock %} diff --git a/cps/ub.py b/cps/ub.py index e28d564a..9028cd3f 100644 --- a/cps/ub.py +++ b/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 '' % (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 '' % (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 " + + 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() diff --git a/cps/web.py b/cps/web.py index 49398af8..139ac97c 100755 --- a/cps/web.py +++ b/cps/web.py @@ -322,7 +322,10 @@ def logout(): @app.route('/send/') @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/", methods = ["GET", "POST"]) @login_required def edit_user(user_id): diff --git a/kindlegen b/vendor/kindlegen similarity index 100% rename from kindlegen rename to vendor/kindlegen diff --git a/wsgi.py b/wsgi.py new file mode 100644 index 00000000..88736408 --- /dev/null +++ b/wsgi.py @@ -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)