File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ from django .core .management .base import BaseCommand
2+ from django .db import transaction
3+
4+ from ...models import IndividualMember
5+
6+
7+ class Command (BaseCommand ):
8+ def add_arguments (self , parser ):
9+ parser .add_argument (
10+ "--no-logging" ,
11+ action = "store_true" ,
12+ help = "Disable writing number of updated records to stdout." ,
13+ )
14+
15+ def handle (self , * args , ** options ):
16+ with transaction .atomic ():
17+ count = IndividualMember .match_and_set_users_by_email ()
18+ if not options ["no_logging" ]:
19+ self .stdout .write (f"Updated: { count } " )
Original file line number Diff line number Diff line change 22from enum import StrEnum
33
44from django .conf import settings
5+ from django .contrib .auth import get_user_model
56from django .core import signing
67from django .core .mail import send_mail
78from django .db import models , transaction
1718
1819from djangoproject .thumbnails import LogoThumbnailMixin
1920
21+ User = get_user_model ()
22+
23+
2024BRONZE_MEMBERSHIP = 1
2125SILVER_MEMBERSHIP = 2
2226GOLD_MEMBERSHIP = 3
@@ -83,6 +87,22 @@ class Meta:
8387 ),
8488 ]
8589
90+ @classmethod
91+ def match_and_set_users_by_email (cls , queryset = None ):
92+ updated_count = 0
93+ if queryset is None :
94+ queryset = cls .objects .all ()
95+ queryset = queryset .filter (user_id__isnull = True )
96+ # Wait for other active transactions to prevent race conditions.
97+ queryset = queryset .select_for_update (nowait = False )
98+ users_queryset = User .objects .filter (
99+ email__in = queryset .values_list ("email" , flat = True ),
100+ )
101+ for user in users_queryset .iterator ():
102+ with transaction .atomic ():
103+ updated_count += cls .objects .filter (email = user .email ).update (user = user )
104+ return updated_count
105+
86106 @classmethod
87107 def send_account_invite_mails (cls , queryset ):
88108 results = {}
You can’t perform that action at this time.
0 commit comments