app/init_app.py

64 lines
2 KiB
Python
Raw Normal View History

2020-03-09 06:09:32 +08:00
"""Initial loading script"""
2020-10-15 22:21:31 +08:00
from app.config import ALIAS_DOMAINS, PREMIUM_ALIAS_DOMAINS
2020-10-15 22:51:07 +08:00
from app.models import Mailbox, Contact, SLDomain
2020-03-09 06:09:32 +08:00
from app.log import LOG
from app.extensions import db
from app.pgp_utils import load_public_key
from server import create_app
2020-06-07 18:46:59 +08:00
def load_pgp_public_keys():
2020-03-09 06:09:32 +08:00
"""Load PGP public key to keyring"""
2020-12-06 20:37:45 +08:00
for mailbox in Mailbox.query.filter(Mailbox.pgp_public_key.isnot(None)).all():
2020-06-07 18:46:59 +08:00
LOG.d("Load PGP key for mailbox %s", mailbox)
fingerprint = load_public_key(mailbox.pgp_public_key)
2020-03-09 06:09:32 +08:00
2020-06-07 18:46:59 +08:00
# sanity check
if fingerprint != mailbox.pgp_finger_print:
LOG.exception(
"fingerprint %s different for mailbox %s", fingerprint, mailbox
)
2020-06-07 18:46:59 +08:00
mailbox.pgp_finger_print = fingerprint
db.session.commit()
2020-12-06 20:37:45 +08:00
for contact in Contact.query.filter(Contact.pgp_public_key.insnot(None)).all():
LOG.d("Load PGP key for %s", contact)
fingerprint = load_public_key(contact.pgp_public_key)
# sanity check
if fingerprint != contact.pgp_finger_print:
LOG.exception(
"fingerprint %s different for contact %s", fingerprint, contact
)
contact.pgp_finger_print = fingerprint
2020-03-09 06:09:32 +08:00
2020-06-07 18:46:59 +08:00
db.session.commit()
2020-03-09 06:09:32 +08:00
2020-03-15 05:24:02 +08:00
LOG.d("Finish load_pgp_public_keys")
2020-03-09 06:09:32 +08:00
2020-10-15 22:51:07 +08:00
def add_sl_domains():
for alias_domain in ALIAS_DOMAINS:
2020-10-15 22:51:07 +08:00
if SLDomain.get_by(domain=alias_domain):
2020-10-16 00:38:39 +08:00
LOG.d("%s is already a SL domain", alias_domain)
else:
2020-10-16 00:38:39 +08:00
LOG.info("Add %s to SL domain", alias_domain)
2020-10-15 22:51:07 +08:00
SLDomain.create(domain=alias_domain)
2020-10-15 22:21:31 +08:00
for premium_domain in PREMIUM_ALIAS_DOMAINS:
2020-10-15 22:51:07 +08:00
if SLDomain.get_by(domain=premium_domain):
2020-10-16 00:38:39 +08:00
LOG.d("%s is already a SL domain", premium_domain)
2020-10-15 22:21:31 +08:00
else:
2020-10-16 00:38:39 +08:00
LOG.info("Add %s to SL domain", premium_domain)
2020-10-15 22:51:07 +08:00
SLDomain.create(domain=premium_domain, premium_only=True)
2020-10-15 22:21:31 +08:00
db.session.commit()
2020-03-09 06:09:32 +08:00
if __name__ == "__main__":
app = create_app()
with app.app_context():
2020-06-07 18:46:59 +08:00
load_pgp_public_keys()
2020-10-15 22:51:07 +08:00
add_sl_domains()