python: Reformat with Black, except quotes.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2021-02-11 23:19:30 -08:00
committed by Tim Abbott
parent 5028c081cb
commit 11741543da
817 changed files with 44952 additions and 24860 deletions

View File

@@ -82,8 +82,11 @@ def ensure_no_empty_passwords(apps: StateApps, schema_editor: DatabaseSchemaEdit
# password, we can prove their current API key cannot have been
# exposed; we store those users in
# password_change_user_ids_no_reset_needed.
password_change_user_ids = set(RealmAuditLog.objects.filter(
event_type=USER_PASSWORD_CHANGED).values_list("modified_user_id", flat=True))
password_change_user_ids = set(
RealmAuditLog.objects.filter(event_type=USER_PASSWORD_CHANGED).values_list(
"modified_user_id", flat=True
)
)
password_change_user_ids_api_key_reset_needed: Set[int] = set()
password_change_user_ids_no_reset_needed: Set[int] = set()
@@ -93,8 +96,8 @@ def ensure_no_empty_passwords(apps: StateApps, schema_editor: DatabaseSchemaEdit
# We check if the user changed their API key since their first password change.
query = RealmAuditLog.objects.filter(
modified_user=user_id, event_type__in=[USER_PASSWORD_CHANGED,
USER_API_KEY_CHANGED],
modified_user=user_id,
event_type__in=[USER_PASSWORD_CHANGED, USER_API_KEY_CHANGED],
).order_by("event_time")
earliest_password_change = query.filter(event_type=USER_PASSWORD_CHANGED).first()
@@ -138,18 +141,20 @@ def ensure_no_empty_passwords(apps: StateApps, schema_editor: DatabaseSchemaEdit
AFFECTED_USER_TYPE_CHANGED_PASSWORD = 'changed_password'
MIGRATION_ID = '0209_user_profile_no_empty_password'
def write_realm_audit_log_entry(user_profile: Any,
event_time: Any, event_type: Any,
affected_user_type: str) -> None:
def write_realm_audit_log_entry(
user_profile: Any, event_time: Any, event_type: Any, affected_user_type: str
) -> None:
RealmAuditLog.objects.create(
realm=user_profile.realm,
modified_user=user_profile,
event_type=event_type,
event_time=event_time,
extra_data=orjson.dumps({
'migration_id': MIGRATION_ID,
'affected_user_type': affected_user_type,
}).decode(),
extra_data=orjson.dumps(
{
'migration_id': MIGRATION_ID,
'affected_user_type': affected_user_type,
}
).decode(),
)
# If Zulip's built-in password authentication is not enabled on
@@ -173,9 +178,9 @@ def ensure_no_empty_passwords(apps: StateApps, schema_editor: DatabaseSchemaEdit
# Change their password and record that we did so.
user_profile.password = make_password(None)
update_fields = ["password"]
write_realm_audit_log_entry(user_profile, event_time,
USER_PASSWORD_CHANGED,
AFFECTED_USER_TYPE_EMPTY_PASSWORD)
write_realm_audit_log_entry(
user_profile, event_time, USER_PASSWORD_CHANGED, AFFECTED_USER_TYPE_EMPTY_PASSWORD
)
if email_auth_enabled and not user_profile.is_bot:
# As explained above, if the built-in password authentication
@@ -185,22 +190,27 @@ def ensure_no_empty_passwords(apps: StateApps, schema_editor: DatabaseSchemaEdit
update_fields.append("api_key")
event_time = timezone_now()
write_realm_audit_log_entry(user_profile, event_time,
USER_API_KEY_CHANGED,
AFFECTED_USER_TYPE_EMPTY_PASSWORD)
write_realm_audit_log_entry(
user_profile,
event_time,
USER_API_KEY_CHANGED,
AFFECTED_USER_TYPE_EMPTY_PASSWORD,
)
user_profile.save(update_fields=update_fields)
continue
elif email_auth_enabled and \
user_profile.id in password_change_user_ids_api_key_reset_needed:
elif (
email_auth_enabled and user_profile.id in password_change_user_ids_api_key_reset_needed
):
# For these users, we just need to reset the API key.
reset_user_api_key(user_profile)
user_profile.save(update_fields=["api_key"])
write_realm_audit_log_entry(user_profile, event_time,
USER_API_KEY_CHANGED,
AFFECTED_USER_TYPE_CHANGED_PASSWORD)
write_realm_audit_log_entry(
user_profile, event_time, USER_API_KEY_CHANGED, AFFECTED_USER_TYPE_CHANGED_PASSWORD
)
def reset_user_api_key(user_profile: Any) -> None:
old_api_key = user_profile.api_key
@@ -212,10 +222,10 @@ def reset_user_api_key(user_profile: Any) -> None:
# could have been registered with the old API key. Fortunately,
# we can just write to the queue processor that handles sending
# those notices to the push notifications bouncer service.
event = {'type': 'clear_push_device_tokens',
'user_profile_id': user_profile.id}
event = {'type': 'clear_push_device_tokens', 'user_profile_id': user_profile.id}
queue_json_publish("deferred_work", event)
class Migration(migrations.Migration):
atomic = False
@@ -224,7 +234,7 @@ class Migration(migrations.Migration):
]
operations = [
migrations.RunPython(ensure_no_empty_passwords,
reverse_code=migrations.RunPython.noop,
elidable=True),
migrations.RunPython(
ensure_no_empty_passwords, reverse_code=migrations.RunPython.noop, elidable=True
),
]