From 62cd667db5ce9e24aee697862b6b1dab6ff14589 Mon Sep 17 00:00:00 2001 From: Julian Schacher Date: Mon, 25 Nov 2024 19:02:19 +0100 Subject: [PATCH] add option for excluding notes or revisions from the action This is useful, if one just wants to either expire notes or revisions. --- hedgedoc-expire.py | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/hedgedoc-expire.py b/hedgedoc-expire.py index fbcc805..e4fd8a0 100644 --- a/hedgedoc-expire.py +++ b/hedgedoc-expire.py @@ -26,6 +26,7 @@ class Config: self.verbose = False self.revision_age = timedelta(days=14) self.note_age = timedelta(days=95) + self.exclude = [] self.postgres_hostname = getenv('POSTGRES_HOSTNAME', 'localhost') self.postgres_username = getenv('POSTGRES_USERNAME', 'hedgedoc') @@ -262,13 +263,30 @@ class HedgedocExpire: def cmd_check(self) -> None: with pgsql.Connection((self.config.postgres_hostname, self.config.postgres_port), self.config.postgres_username, self.config.postgres_password) as db: - print(self.check_revisions_to_be_expired(db) + - self.check_notes_to_be_expired(db)) + if 'revision' not in self.config.exclude: + print(self.check_revisions_to_be_expired(db)) + elif self.config.verbose: + print("Revisions were excluded from check, not checking.\n") + + if 'note' not in self.config.exclude: + print(self.check_notes_to_be_expired(db)) + elif self.config.verbose: + print("Notes were excluded from check, not checking.\n") def cmd_emailcheck(self) -> None: with pgsql.Connection((self.config.postgres_hostname, self.config.postgres_port), self.config.postgres_username, self.config.postgres_password) as db: - report = self.check_revisions_to_be_expired(db) + self.check_notes_to_be_expired(db) + report = '' + + if 'revision' not in self.config.exclude: + report += self.check_revisions_to_be_expired(db) + else: + report += "Revisions were excluded from check.\n" + + if 'note' not in self.config.exclude: + report += self.check_notes_to_be_expired(db) + else: + report += "Notes were excluded from check.\n" msg = MIMEMultipart() msg['From'] = self.email_sender.mail_from msg['To'] = self.email_sender.mail_from @@ -285,8 +303,15 @@ class HedgedocExpire: def cmd_expire(self) -> None: with pgsql.Connection((self.config.postgres_hostname, self.config.postgres_port), self.config.postgres_username, self.config.postgres_password) as db: - self.expire_old_revisions(db) - self.expire_old_notes(db) + if 'revision' not in self.config.exclude: + self.expire_old_revisions(db) + elif self.config.verbose: + print("Revisions were excluded from action, not expiring.\n") + + if 'note' not in self.config.exclude: + self.expire_old_notes(db) + elif self.config.verbose: + print("Notes were excluded from action, not expiring.\n") def main(): @@ -317,12 +342,17 @@ def main(): help='action to perform') parser.add_argument('-v', '--verbose', action='store_true', default=False, help='print more info while running') + parser.add_argument('--exclude', nargs="+", choices=['revision', 'note'], + help='Type ("revision" or "note") to exclude from the action.') args = parser.parse_args() config = Config() config.note_age = timedelta(days=args.notes) config.revision_age = timedelta(days=args.revisions) config.verbose = args.verbose + if (args.exclude): + config.exclude = args.exclude + print(config.exclude) mail = EmailSender(config.smtp_hostname, config.smtp_port, config.smtp_username, config.smtp_password, config.smtp_from) hedgedoc_expire = HedgedocExpire(config, mail)