Skip to content

Commit

Permalink
generate unique usernames
Browse files Browse the repository at this point in the history
  • Loading branch information
Her Email authored and alphatownsman committed Nov 9, 2023
1 parent 8cca52c commit 620df78
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
.venv
/.env
/neodb.env
/compose.override.yml
/neodb-takahe


# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
2 changes: 2 additions & 0 deletions users/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ class Meta:

def clean_username(self):
username = self.cleaned_data.get("username")
if not username:
raise forms.ValidationError(_("Username is required."))
if username and self.instance and self.instance.username:
username = self.instance.username
elif (
Expand Down
31 changes: 31 additions & 0 deletions users/management/commands/generate_username.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from datetime import timedelta

from django.core.management.base import BaseCommand
from django.utils import timezone
from tqdm import tqdm

from users.models import User
from users.models.user import _RESERVED_USERNAMES


class Command(BaseCommand):
help = "Generate unique username"

def handle(self, *args, **options):
count = 0
for user in User.objects.filter(username__isnull=True).order_by("date_joined"):
if not user.is_active:
un = f"-{user.pk}-"
else:
un = user.mastodon_username
if not un:
un = f"_{user.pk}"
if un.lower() in _RESERVED_USERNAMES:
un = f"__{un}"
if User.objects.filter(username=un).exists():
un = f"{un}_{user.pk}"
print(f"{user} -> un")
user.username = un
user.save(update_fields=["username"])
count += 1
print(f"{count} users updated")
12 changes: 12 additions & 0 deletions users/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,20 @@
"oauth2_login",
"__",
"admin",
"admins",
"administrator",
"administrators",
"api",
"me",
"neodb",
"relay",
"__relay__",
"system",
"__system__",
"root",
"announce",
"announcement",
"announcements",
]


Expand Down

0 comments on commit 620df78

Please sign in to comment.