From 7e4784ea0e01d052e2e7ba364138b5e0f35fd06d Mon Sep 17 00:00:00 2001 From: cuom1999 Date: Sun, 10 Sep 2023 13:44:04 -0500 Subject: [PATCH] Add check for username in import user --- judge/tasks/import_users.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/judge/tasks/import_users.py b/judge/tasks/import_users.py index db8d5c5..5584687 100644 --- a/judge/tasks/import_users.py +++ b/judge/tasks/import_users.py @@ -1,5 +1,6 @@ import csv from tempfile import mktemp +import re from django.conf import settings from django.contrib.auth.models import User @@ -41,6 +42,11 @@ def csv_to_dict(csv_file): return res +def is_valid_username(username): + match = re.match(r"\w+", username) + return match is not None and match.group() == username + + # return result log def import_users(users): log = "" @@ -48,17 +54,18 @@ def import_users(users): cur_log = str(i + 1) + ". " username = row["username"] + if not is_valid_username(username): + log += username + ": Invalid username\n" + continue + cur_log += username + ": " - pwd = row["password"] - user, created = User.objects.get_or_create( username=username, defaults={ "is_active": True, }, ) - profile, _ = Profile.objects.get_or_create( user=user, defaults={