2017-11-16 00:44:00 +01:00
|
|
|
import hashlib
|
|
|
|
from typing import Text
|
|
|
|
|
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-09-15 22:23:17 +02:00
|
|
|
from mock import patch
|
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.
|
2017-10-26 11:36:30 +02:00
|
|
|
def patched_user_avatar_path(user_profile: UserProfile) -> Text:
|
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 = [
|
2020-04-29 08:43:25 +02:00
|
|
|
migrations.RunPython(verify_medium_avatar_image, elidable=True)
|
2016-09-20 21:48:48 +02:00
|
|
|
]
|