Format using black

This commit is contained in:
Sylvia van Os 2021-05-25 00:00:07 +02:00
parent 1694923e6c
commit f0fa7032c1
3 changed files with 39 additions and 16 deletions

View file

@ -323,7 +323,11 @@ def get_alias_infos_with_pagination_v3(
elif alias_filter == "disabled": elif alias_filter == "disabled":
q = q.filter(Alias.enabled.is_(False)) q = q.filter(Alias.enabled.is_(False))
elif alias_filter == "hibp": elif alias_filter == "hibp":
q = q.filter(or_(Alias.hibp_breaches_notified_user, Alias.hibp_breaches_not_notified_user)) q = q.filter(
or_(
Alias.hibp_breaches_notified_user, Alias.hibp_breaches_not_notified_user
)
)
q = q.order_by(Alias.pinned.desc()) q = q.order_by(Alias.pinned.desc())

View file

@ -2083,7 +2083,11 @@ class DomainMailbox(db.Model, ModelMixin):
class HibpHibpDataClass(db.Model, ModelMixin): class HibpHibpDataClass(db.Model, ModelMixin):
__tablename__ = "hibp_hibpdataclass" __tablename__ = "hibp_hibpdataclass"
__table_args__ = (db.UniqueConstraint("hibp_id", "hibp_dataclass_id", name="uq_hibp_hibpdataclass"),) __table_args__ = (
db.UniqueConstraint(
"hibp_id", "hibp_dataclass_id", name="uq_hibp_hibpdataclass"
),
)
hibp_id = db.Column(db.Integer(), db.ForeignKey("hibp.id")) hibp_id = db.Column(db.Integer(), db.ForeignKey("hibp.id"))
hibp_dataclass_id = db.Column(db.Integer(), db.ForeignKey("hibpdataclass.id")) hibp_dataclass_id = db.Column(db.Integer(), db.ForeignKey("hibpdataclass.id"))
@ -2092,7 +2096,8 @@ class HibpHibpDataClass(db.Model, ModelMixin):
"Hibp", backref=db.backref("hibp_hibpdataclass", cascade="all, delete-orphan") "Hibp", backref=db.backref("hibp_hibpdataclass", cascade="all, delete-orphan")
) )
hibpdataclass = db.relationship( hibpdataclass = db.relationship(
"HibpDataClass", backref=db.backref("hibp_hibpdataclass", cascade="all, delete-orphan") "HibpDataClass",
backref=db.backref("hibp_hibpdataclass", cascade="all, delete-orphan"),
) )

40
cron.py
View file

@ -56,7 +56,7 @@ from app.models import (
DeletedAlias, DeletedAlias,
DomainDeletedAlias, DomainDeletedAlias,
Hibp, Hibp,
HibpDataClass HibpDataClass,
) )
from app.utils import sanitize_email from app.utils import sanitize_email
from server import create_app from server import create_app
@ -794,9 +794,7 @@ async def _hibp_check(api_key, queue):
if r.status_code == 200: if r.status_code == 200:
# Breaches found # Breaches found
breaches = [ breaches = [Hibp.get_by(name=entry["Name"]) for entry in r.json()]
Hibp.get_by(name=entry["Name"]) for entry in r.json()
]
if len(alias.hibp_breaches) > 0: if len(alias.hibp_breaches) > 0:
LOG.w("%s appears in HIBP breaches %s", alias, alias.hibp_breaches) LOG.w("%s appears in HIBP breaches %s", alias, alias.hibp_breaches)
elif r.status_code == 404: elif r.status_code == 404:
@ -811,8 +809,16 @@ async def _hibp_check(api_key, queue):
) )
return return
alias.hibp_breaches_not_notified_user = [breach for breach in breaches if breach not in alias.hibp_breaches_notified_user] alias.hibp_breaches_not_notified_user = [
alias.hibp_breaches_notified_user = [breach for breach in breaches if breach not in alias.hibp_breaches_not_notified_user] breach
for breach in breaches
if breach not in alias.hibp_breaches_notified_user
]
alias.hibp_breaches_notified_user = [
breach
for breach in breaches
if breach not in alias.hibp_breaches_not_notified_user
]
alias.hibp_last_check = arrow.utcnow() alias.hibp_last_check = arrow.utcnow()
db.session.add(alias) db.session.add(alias)
@ -847,7 +853,8 @@ async def check_hibp():
hibp_entry.date = arrow.get(entry["BreachDate"]) hibp_entry.date = arrow.get(entry["BreachDate"])
hibp_entry.description = entry["Description"] hibp_entry.description = entry["Description"]
hibp_entry.data_classes = [ hibp_entry.data_classes = [
HibpDataClass.get_by(description=description) for description in entry["DataClasses"] HibpDataClass.get_by(description=description)
for description in entry["DataClasses"]
] ]
db.session.commit() db.session.commit()
@ -893,7 +900,11 @@ def notify_hibp():
Send aggregated email reports for HIBP breaches Send aggregated email reports for HIBP breaches
""" """
for user in User.query.all(): for user in User.query.all():
new_breaches = Alias.query.filter(Alias.user_id == user.id).filter(Alias.hibp_breaches_not_notified_user).all() new_breaches = (
Alias.query.filter(Alias.user_id == user.id)
.filter(Alias.hibp_breaches_not_notified_user)
.all()
)
if len(new_breaches) == 0: if len(new_breaches) == 0:
return return
@ -906,17 +917,20 @@ def notify_hibp():
render( render(
"transactional/hibp-new-breaches.txt", "transactional/hibp-new-breaches.txt",
user=user, user=user,
breached_aliases=new_breaches breached_aliases=new_breaches,
), ),
render( render(
"transactional/hibp-new-breaches.html", "transactional/hibp-new-breaches.html",
user=user, user=user,
breached_aliases=new_breaches breached_aliases=new_breaches,
), ),
) )
for alias in new_breaches: for alias in new_breaches:
alias.hibp_breaches_notified_user = alias.hibp_breaches_notified_user + alias.hibp_breaches_not_notified_user alias.hibp_breaches_notified_user = (
alias.hibp_breaches_notified_user
+ alias.hibp_breaches_not_notified_user
)
alias.hibp_breaches_not_notified_user = [] alias.hibp_breaches_not_notified_user = []
db.session.add(alias) db.session.add(alias)
@ -942,7 +956,7 @@ if __name__ == "__main__":
"delete_old_monitoring", "delete_old_monitoring",
"check_custom_domain", "check_custom_domain",
"check_hibp", "check_hibp",
"notify_hibp" "notify_hibp",
], ],
) )
args = parser.parse_args() args = parser.parse_args()
@ -982,4 +996,4 @@ if __name__ == "__main__":
asyncio.run(check_hibp()) asyncio.run(check_hibp())
elif args.job == "notify_hibp": elif args.job == "notify_hibp":
LOG.d("Notify users with new HIBP breaches") LOG.d("Notify users with new HIBP breaches")
notify_hibp() notify_hibp()