1
0
mirror of https://gitlab.crans.org/bde/nk20-scripts synced 2025-06-25 01:10:27 +02:00

When data is imported from the NK15, prevent users whenever some aliases are deleted

This commit is contained in:
Yohann D'ANELLO
2020-08-24 12:41:51 +02:00
parent 81709539a2
commit 4179cad611
3 changed files with 83 additions and 3 deletions

View File

@ -6,6 +6,7 @@ import psycopg2.extras as pge
import datetime
import json
from django.template.loader import render_to_string
from django.utils.timezone import make_aware, now
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
@ -33,6 +34,11 @@ MAP_IDBDE = {
# some Aliases have been created in the fixtures
ALIAS_SET = {a[0] for a in Alias.objects.all().values_list("normalized_name")}
# Some people might loose some aliases due to normalization. We warn them on them.
LOST_ALIASES = {}
# In some rare cases, the username might be in conflict with some others. We change them and warn the users.
CHANGED_USERNAMES = []
note_user_type = ContentType.objects.get(app_label="note", model="noteuser")
note_club_type = ContentType.objects.get(app_label="note", model="noteclub")
@ -85,9 +91,10 @@ class Command(ImportCommand):
pseudo = row["pseudo"]
pseudo_norm = Alias.normalize(pseudo)
self.update_line(idx, n, pseudo)
# clean pseudo (normalized pseudo must be unique)
if pseudo_norm in ALIAS_SET:
# clean pseudo (normalized pseudo must be unique and not empty)
if not pseudo_norm or pseudo_norm in ALIAS_SET:
pseudo = pseudo + str(row["idbde"])
CHANGED_USERNAMES.append((pk_note, row[pseudo], pseudo))
else:
ALIAS_SET.add(pseudo_norm)
# clean date
@ -206,7 +213,9 @@ class Command(ImportCommand):
alias_norm = Alias.normalize(alias_name)
self.update_line(idx, n, alias_norm)
# clean pseudo (normalized pseudo must be unique)
if alias_norm in ALIAS_SET:
if not alias_norm or alias_norm in ALIAS_SET:
LOST_ALIASES.setdefault(MAP_IDBDE[row["idbde"]], [])
LOST_ALIASES[MAP_IDBDE[row["idbde"]]].append(alias_name)
continue
else:
ALIAS_SET.add(alias_norm)
@ -237,3 +246,20 @@ class Command(ImportCommand):
filename = kwargs["save"]
with open(filename, 'w') as fp:
json.dump(MAP_IDBDE, fp, sort_keys=True, indent=2)
for pk_user, old_username, new_username in CHANGED_USERNAMES:
user = User.objects.get(pk_user)
mail_text = render_to_string("scripts/unsupported_username.txt", dict(
user=user,
old_username=old_username,
new_username=new_username,
))
user.email_user("Transition à la Note Kfet 2020 : pseudo non supporté", mail_text)
for pk_user, aliases_list in CHANGED_USERNAMES:
user = User.objects.get(pk_user)
mail_text = render_to_string("scripts/deleted_aliases.txt", dict(
user=user,
aliases_list=aliases_list,
))
user.email_user("Transition à la Note Kfet 2020 : suppression d'alias", mail_text)