Skip to content

Commit

Permalink
User.get_or_create: consolidate puts better
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Aug 6, 2024
1 parent 479acea commit 0f14410
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion models.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,20 +262,25 @@ def get_or_create(cls, id, propagate=False, allow_opt_out=False, **kwargs):
user.existing = True

# TODO: propagate more fields?
changed = False
for field in ['direct', 'obj', 'obj_key']:
old_val = getattr(user, field, None)
new_val = kwargs.get(field)
if ((old_val is None and new_val is not None)
or (field == 'direct' and not old_val and new_val)):
setattr(user, field, new_val)
changed = True

if enabled_protocols := kwargs.get('enabled_protocols'):
user.enabled_protocols = (set(user.enabled_protocols)
| set(enabled_protocols))
changed = True

if not propagate:
user.put()
if changed:
user.put()
return user

else:
if orig := get_original(id):
if orig.status and not allow_opt_out:
Expand Down

0 comments on commit 0f14410

Please sign in to comment.