2017-11-16 00:44:00 +01:00
|
|
|
import hashlib
|
2020-06-11 00:54:34 +02:00
|
|
|
from unittest.mock import patch
|
2017-11-16 00:44:00 +01:00
|
|
|
|
2017-09-15 22:23:17 +02:00
|
|
|
from django.conf import settings
|
2016-09-20 21:48:48 +02:00
|
|
|
from django.db import migrations
|
2022-05-27 23:33:51 +02:00
|
|
|
from django.db.backends.postgresql.schema import BaseDatabaseSchemaEditor
|
2016-09-20 21:48:48 +02:00
|
|
|
from django.db.migrations.state import StateApps
|
2017-11-16 00:44:00 +01:00
|
|
|
|
2016-09-20 21:48:48 +02:00
|
|
|
from zerver.lib.upload import upload_backend
|
2017-11-16 00:44:00 +01:00
|
|
|
from zerver.lib.utils import make_safe_digest
|
2017-09-15 22:23:17 +02:00
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2017-09-15 22:23:17 +02:00
|
|
|
# We hackishly patch this function in order to revert it to the state
|
|
|
|
# it had when this migration was first written. This is a balance
|
|
|
|
# between copying in a historical version of hundreds of lines of code
|
|
|
|
# from zerver.lib.upload (which would pretty annoying, but would be a
|
|
|
|
# pain) and just using the current version, which doesn't work
|
|
|
|
# since we rearranged the avatars in Zulip 1.6.
|
2020-11-17 02:39:23 +01:00
|
|
|
def patched_user_avatar_path(user_profile: UserProfile) -> str:
|
2017-09-15 22:23:17 +02:00
|
|
|
email = user_profile.email
|
|
|
|
user_key = email.lower() + settings.AVATAR_SALT
|
|
|
|
return make_safe_digest(user_key, hashlib.sha1)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-12-14 21:51:37 +01:00
|
|
|
@patch("zerver.lib.upload.s3.user_avatar_path", patched_user_avatar_path)
|
|
|
|
@patch("zerver.lib.upload.local.user_avatar_path", patched_user_avatar_path)
|
2022-05-27 23:33:51 +02:00
|
|
|
def verify_medium_avatar_image(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile_model = apps.get_model("zerver", "UserProfile")
|
2017-11-02 09:17:27 +01:00
|
|
|
for user_profile in user_profile_model.objects.filter(avatar_source="U"):
|
2021-03-17 17:54:23 +01:00
|
|
|
upload_backend.ensure_avatar_image(user_profile, is_medium=True)
|
2016-09-20 21:48:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
dependencies = [
|
2021-02-12 08:20:45 +01:00
|
|
|
("zerver", "0031_remove_system_avatar_source"),
|
2016-09-20 21:48:48 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
operations = [
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
migrations.RunPython(verify_medium_avatar_image, elidable=True),
|
2016-09-20 21:48:48 +02:00
|
|
|
]
|