Compare commits

...

2 Commits

Author SHA1 Message Date
feb4a67291 Use proper SQL concatenation 2026-01-23 19:41:37 +01:00
f0b924ea56 Use context manager for SMTP 2026-01-23 19:40:58 +01:00
2 changed files with 5 additions and 7 deletions

View File

@@ -76,11 +76,10 @@ class EmailService:
smtp_host = self.config["smtp_host"] smtp_host = self.config["smtp_host"]
smtp_port = self.config["smtp_port"] smtp_port = self.config["smtp_port"]
server = smtplib.SMTP(smtp_host, smtp_port) with smtplib.SMTP(smtp_host, smtp_port) as server:
if self.config.get("smtp_starttls", False): if self.config.get("smtp_starttls", False):
server.starttls() server.starttls()
try:
if self.config.get("smtp_user"): if self.config.get("smtp_user"):
server.login( server.login(
self.config["smtp_user"], self.config["smtp_user"],
@@ -89,8 +88,6 @@ class EmailService:
server.sendmail(msg["From"], [to], msg.as_string()) server.sendmail(msg["From"], [to], msg.as_string())
logging.info(f"Email sent: to={to} subject={subject}") logging.info(f"Email sent: to={to} subject={subject}")
return True return True
finally:
server.quit()
except Exception as e: except Exception as e:
logging.error(f"Email send failed: to={to} error={e}") logging.error(f"Email send failed: to={to} error={e}")

View File

@@ -9,6 +9,7 @@ from peewee import (
CharField, CharField,
DateTimeField, DateTimeField,
DoesNotExist, DoesNotExist,
fn,
ForeignKeyField, ForeignKeyField,
IntegerField, IntegerField,
Model, Model,
@@ -297,7 +298,7 @@ def get_hostname_for_user(hostname: str, user: User):
Raises: Raises:
DoesNotExist: If hostname not found or not owned by user. DoesNotExist: If hostname not found or not owned by user.
""" """
fqdn = Hostname.hostname + '.' + Hostname.zone fqdn = fn.Concat(Hostname.hostname, '.', Hostname.zone)
return Hostname.get((fqdn == hostname) & (Hostname.user == user)) return Hostname.get((fqdn == hostname) & (Hostname.user == user))