From c7970d93829e201cf27de290ca53f6a0709cb368 Mon Sep 17 00:00:00 2001 From: Paul Bienkowski Date: Tue, 13 Sep 2022 08:56:12 +0200 Subject: [PATCH] Rename users when they log in with a new preferred_username --- api/obs/api/db.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/api/obs/api/db.py b/api/obs/api/db.py index f7716a6..585bf1c 100644 --- a/api/obs/api/db.py +++ b/api/obs/api/db.py @@ -408,6 +408,29 @@ class User(Base): self.username = new_name + async def rename(self, config, new_name): + old_name = self.username + + renames = [ + (join(basedir, old_name), join(basedir, new_name)) + for basedir in [config.PROCESSING_OUTPUT_DIR, config.TRACKS_DIR] + ] + + for src, dst in renames: + if exists(dst): + raise FileExistsError( + f"cannot move {src!r} to {dst!r}, destination exists" + ) + + for src, dst in renames: + if not exists(src): + log.debug("Rename user %s: Not moving %s, not found", self.id, src) + else: + log.info("Rename user %s: Moving %s to %s", self.id, src, dst) + os.rename(src, dst) + + self.username = new_name + class Comment(Base): __tablename__ = "comment"