add option for excluding notes or revisions from the action

This is useful, if one just wants to either expire notes or revisions.
This commit is contained in:
Julian Schacher 2024-11-25 19:02:19 +01:00
parent 86e03c0d4e
commit 62cd667db5

View file

@ -26,6 +26,7 @@ class Config:
self.verbose = False self.verbose = False
self.revision_age = timedelta(days=14) self.revision_age = timedelta(days=14)
self.note_age = timedelta(days=95) self.note_age = timedelta(days=95)
self.exclude = []
self.postgres_hostname = getenv('POSTGRES_HOSTNAME', 'localhost') self.postgres_hostname = getenv('POSTGRES_HOSTNAME', 'localhost')
self.postgres_username = getenv('POSTGRES_USERNAME', 'hedgedoc') self.postgres_username = getenv('POSTGRES_USERNAME', 'hedgedoc')
@ -262,13 +263,30 @@ class HedgedocExpire:
def cmd_check(self) -> None: def cmd_check(self) -> None:
with pgsql.Connection((self.config.postgres_hostname, self.config.postgres_port), with pgsql.Connection((self.config.postgres_hostname, self.config.postgres_port),
self.config.postgres_username, self.config.postgres_password) as db: self.config.postgres_username, self.config.postgres_password) as db:
print(self.check_revisions_to_be_expired(db) + if 'revision' not in self.config.exclude:
self.check_notes_to_be_expired(db)) 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: def cmd_emailcheck(self) -> None:
with pgsql.Connection((self.config.postgres_hostname, self.config.postgres_port), with pgsql.Connection((self.config.postgres_hostname, self.config.postgres_port),
self.config.postgres_username, self.config.postgres_password) as db: 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 = MIMEMultipart()
msg['From'] = self.email_sender.mail_from msg['From'] = self.email_sender.mail_from
msg['To'] = self.email_sender.mail_from msg['To'] = self.email_sender.mail_from
@ -285,8 +303,15 @@ class HedgedocExpire:
def cmd_expire(self) -> None: def cmd_expire(self) -> None:
with pgsql.Connection((self.config.postgres_hostname, self.config.postgres_port), with pgsql.Connection((self.config.postgres_hostname, self.config.postgres_port),
self.config.postgres_username, self.config.postgres_password) as db: self.config.postgres_username, self.config.postgres_password) as db:
self.expire_old_revisions(db) if 'revision' not in self.config.exclude:
self.expire_old_notes(db) 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(): def main():
@ -317,12 +342,17 @@ def main():
help='action to perform') help='action to perform')
parser.add_argument('-v', '--verbose', action='store_true', default=False, parser.add_argument('-v', '--verbose', action='store_true', default=False,
help='print more info while running') 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() args = parser.parse_args()
config = Config() config = Config()
config.note_age = timedelta(days=args.notes) config.note_age = timedelta(days=args.notes)
config.revision_age = timedelta(days=args.revisions) config.revision_age = timedelta(days=args.revisions)
config.verbose = args.verbose 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, mail = EmailSender(config.smtp_hostname, config.smtp_port, config.smtp_username, config.smtp_password,
config.smtp_from) config.smtp_from)
hedgedoc_expire = HedgedocExpire(config, mail) hedgedoc_expire = HedgedocExpire(config, mail)