4
4
5
5
from django .contrib .auth import get_user_model
6
6
from django .db .models import F , Value , Case , When , CharField
7
+ from django .db .models .query import QuerySet
7
8
from django .db .models .functions import Concat
8
9
from django .core .management .base import BaseCommand
9
10
11
+
10
12
log = getLogger (__name__ )
11
13
User = get_user_model ()
12
14
@@ -35,8 +37,13 @@ def handle(self, *args, **options):
35
37
36
38
if usernames :
37
39
users = users .filter (username__in = usernames )
40
+
41
+ total = users .count ()
42
+
43
+ log .info ("Syncing %s users with wikimedia usernames" , total )
38
44
39
- self ._update_user_with_wikimedia_username (users )
45
+ stats = self ._update_user_with_wikimedia_username (users )
46
+ self ._print_stats (total , stats )
40
47
41
48
def _get_tpa_users (self ):
42
49
"""
@@ -64,19 +71,33 @@ def _get_tpa_users(self):
64
71
65
72
return users
66
73
67
- def _update_user_with_wikimedia_username (self , users : list [User ]):
74
+ def _update_user_with_wikimedia_username (self , users : QuerySet [User ]) -> dict :
68
75
"""
69
76
Checks if "wiki_username" was the original username provided by Wikimedia
70
77
and updates the Wikilearn username with it.
71
78
"""
72
79
total = len (users )
80
+ stats = {
81
+ "correct_username" : 0 ,
82
+ "updated_username" : 0 ,
83
+ "skipped_username" : 0 ,
84
+ }
73
85
for i , user in enumerate (users ):
74
86
index = i + 1
75
- if self ._username_exists (user .wiki_username ):
87
+ if self ._username_exists (user .username ):
88
+ # This check is to avoid updating username if it is already same as Wikimedia's.
89
+ log .info (f'{ index } /{ total } : SKIPPED: { user .username } is CORRECT' )
90
+ stats ["correct_username" ] += 1
91
+ elif self ._username_exists (user .wiki_username ):
92
+ log .info (f"{ index } /{ total } : UPDATING: { user .username } with { user .wiki_username } " )
76
93
self ._update_user (user )
77
- log . info ( f" { index } / { total } : UPDATED { user . username } with { user . wiki_username } " )
94
+ stats [ "updated_username" ] += 1
78
95
else :
79
- log .info (f'{ index } /{ total } : SKIPPED { user ["username" ]} ' )
96
+ # This means both the username and our guess (first_name+last_name) is wrong.
97
+ log .info (f'{ index } /{ total } : SKIPPED: { user .username } ' )
98
+ stats ["skipped_username" ] += 1
99
+
100
+ return stats
80
101
81
102
def _username_exists (self , username : str ) -> bool :
82
103
"""
@@ -92,3 +113,9 @@ def _username_exists(self, username: str) -> bool:
92
113
def _update_user (self , user : User ):
93
114
user .username = user .wiki_username
94
115
user .save ()
116
+
117
+ def _print_stats (self , total : int , stats : dict ):
118
+ log .info (f"Total mismatched users: { total } " )
119
+ log .info (f"Correct usernames: { stats ['correct_username' ]} " )
120
+ log .info (f"Updated usernames: { stats ['updated_username' ]} " )
121
+ log .info (f"Skipped usernames: { stats ['skipped_username' ]} " )
0 commit comments