feat(translations): Factorize the user renaming scheme, and update the remote data
This commit is contained in:
parent
be0cf4c0f5
commit
5019b89ef2
1 changed files with 18 additions and 19 deletions
|
@ -87,22 +87,27 @@ class Translation(models.Model):
|
|||
def __str__(self) -> str:
|
||||
return f"{self.cas_login} → {self.username}"
|
||||
|
||||
def update_user(self, username: str):
|
||||
# Update the username of the person with the required cas_login
|
||||
try:
|
||||
# Find out if a user exists with the cas_login to update
|
||||
account = SocialAccount.objects.get(provider="ens_cas", uid=self.cas_login)
|
||||
|
||||
# WARNING: This updates the remote data, we need to be careful with what we do
|
||||
async_to_sync(klient.person_account_update)(account.user.username, username)
|
||||
|
||||
account.user.username = username
|
||||
account.user.save()
|
||||
except SocialAccount.DoesNotExist:
|
||||
# No user has registered with this cas_login yet
|
||||
pass
|
||||
|
||||
def save(self, *args, **kwargs) -> None:
|
||||
# INFO: Only update the model if it does not already exist
|
||||
# This will prevent a lot of pain
|
||||
if self.pk is None:
|
||||
try:
|
||||
# Find out if a user exists with the cas_login to update
|
||||
account = SocialAccount.objects.get(
|
||||
provider="ens_cas", uid=self.cas_login
|
||||
)
|
||||
account.user.username = self.username
|
||||
account.user.save()
|
||||
self.update_user(self.username)
|
||||
|
||||
# TODO: Update the distant kanidm data
|
||||
except SocialAccount.DoesNotExist:
|
||||
# No user has registered with this cas_login yet
|
||||
pass
|
||||
return super().save(*args, **kwargs)
|
||||
|
||||
class Meta: # pyright: ignore
|
||||
|
@ -118,14 +123,8 @@ def restore_username(**kwargs):
|
|||
Restore the username to the cas_login
|
||||
"""
|
||||
|
||||
cas_login = kwargs["instance"].cas_login
|
||||
try:
|
||||
account = SocialAccount.objects.get(provider="ens_cas", uid=cas_login)
|
||||
account.user.username = cas_login
|
||||
account.user.save()
|
||||
except SocialAccount.DoesNotExist:
|
||||
# No user has registered with this cas_login yet
|
||||
pass
|
||||
self = kwargs["instance"]
|
||||
self.update_user(self.cas_login)
|
||||
|
||||
|
||||
@dataclass
|
||||
|
|
Loading…
Add table
Reference in a new issue