import dbm import shelve from flask import current_app, g from flask_login import current_user from .model.read_mails import QMailAuthuser def build_qma(username, password): authenticator = current_app.config["JWEBMAIL"]["READ_MAILS"]["AUTHENTICATOR"] backend = current_app.config["JWEBMAIL"]["READ_MAILS"]["BACKEND"] mailbox = current_app.config["JWEBMAIL"]["READ_MAILS"]["MAILBOX"] mailbox_user = current_app.config["JWEBMAIL"]["READ_MAILS"]["MAILBOX_USER"] return QMailAuthuser( username, password, backend, mailbox, mailbox_user, authenticator ) def login(username, password): return build_qma(username, password).verify_user() def add_user(user): with shelve.open("user_sessions", flag="c") as s: s[user.get_id()] = user def load_user(username): try: with shelve.open("user_sessions", flag="r") as s: user = s[username] return user except dbm.error: return None except KeyError: return None def get_read_mails_logged_in(): if "read_mails" in g: return g.read_mails with shelve.open("user_sessions", flag="r") as s: user_data = s[current_user.get_id()] qma = build_qma(current_user.get_id(), user_data.password) g.read_mails = qma return qma