Add system check for non-unique case-insensitive usernames
[mygpo.git] / mygpo / users / checks.py
blob1634224dd10a4abc05a4a64bd7f97c5d18b7784c
1 from django.core.checks import register, Warning
2 from django.db import connection
5 SQL = """
6 SELECT count(*), lower(username)
7 FROM auth_user
8 GROUP BY lower(username)
9 HAVING count(*) > 1;
10 """
12 @register()
13 def check_case_insensitive_users(app_configs=None, **kwargs):
14 errors = []
16 cursor = connection.cursor()
17 cursor.execute(SQL)
18 non_unique = cursor.fetchall()
20 usernames = [t[1] for t in non_unique]
22 if len(non_unique) > 0:
23 txt = 'There are {0} non-unique usernames: {1}'.format(
24 len(non_unique),
25 ', '.join(usernames[:10] + ['...'])
27 wid='users.W001'
28 errors.append(Warning(txt, id=wid))
29 return errors