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
|
2020-04-27 07:19:08 +02:00
|
|
|
from django.db.backends.postgresql.schema import DatabaseSchemaEditor
|
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)
|
|
|
|
|
|
|
|
@patch('zerver.lib.upload.user_avatar_path', patched_user_avatar_path)
|
2017-10-26 11:36:30 +02:00
|
|
|
def verify_medium_avatar_image(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
|
2016-09-20 21:48:48 +02: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"):
|
2017-05-23 21:17:47 +02:00
|
|
|
upload_backend.ensure_medium_avatar_image(user_profile)
|
2016-09-20 21:48:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
|
|
|
|
dependencies = [
|
|
|
|
('zerver', '0031_remove_system_avatar_source'),
|
|
|
|
]
|
|
|
|
|
|
|
|
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
|
|
|
]
|