zerver/migrations: Use python 3 syntax for typing.

This commit is contained in:
rht
2017-10-26 11:36:30 +02:00
committed by Tim Abbott
parent a311678190
commit c2a5b5f160
22 changed files with 50 additions and 86 deletions

View File

@@ -12,8 +12,8 @@ import zerver.models
from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps
def migrate_existing_attachment_data(apps, schema_editor):
# type: (StateApps, DatabaseSchemaEditor) -> None
def migrate_existing_attachment_data(apps: StateApps,
schema_editor: DatabaseSchemaEditor) -> None:
Attachment = apps.get_model('zerver', 'Attachment')
Recipient = apps.get_model('zerver', 'Recipient')
Stream = apps.get_model('zerver', 'Stream')

View File

@@ -6,8 +6,7 @@ from django.db.migrations.state import StateApps
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
def set_subdomain_of_default_realm(apps, schema_editor):
# type: (StateApps, DatabaseSchemaEditor) -> None
def set_subdomain_of_default_realm(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
if settings.DEVELOPMENT:
Realm = apps.get_model('zerver', 'Realm')
try:

View File

@@ -18,15 +18,13 @@ import hashlib
# 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.
def patched_user_avatar_path(user_profile):
# type: (UserProfile) -> Text
def patched_user_avatar_path(user_profile: UserProfile) -> Text:
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)
def verify_medium_avatar_image(apps, schema_editor):
# type: (StateApps, DatabaseSchemaEditor) -> None
def verify_medium_avatar_image(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
user_profile_model = apps.get_model('zerver', 'UserProfile')
for user_profile in user_profile_model.objects.filter(avatar_source=u"U"):
upload_backend.ensure_medium_avatar_image(user_profile)

View File

@@ -4,8 +4,7 @@ from django.db import migrations
from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps
def add_domain_to_realm_alias_if_needed(apps, schema_editor):
# type: (StateApps, DatabaseSchemaEditor) -> None
def add_domain_to_realm_alias_if_needed(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
Realm = apps.get_model('zerver', 'Realm')
RealmAlias = apps.get_model('zerver', 'RealmAlias')

View File

@@ -5,8 +5,7 @@ from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps
from django.db import migrations, models
def set_string_id_using_domain(apps, schema_editor):
# type: (StateApps, DatabaseSchemaEditor) -> None
def set_string_id_using_domain(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
Realm = apps.get_model('zerver', 'Realm')
for realm in Realm.objects.all():
if not realm.string_id:

View File

@@ -10,8 +10,8 @@ from django.db.migrations.state import StateApps
from zerver.lib.upload import attachment_url_re, attachment_url_to_path_id
def check_and_create_attachments(apps, schema_editor):
# type: (StateApps, DatabaseSchemaEditor) -> None
def check_and_create_attachments(apps: StateApps,
schema_editor: DatabaseSchemaEditor) -> None:
STREAM = 2
Message = apps.get_model('zerver', 'Message')
Attachment = apps.get_model('zerver', 'Attachment')

View File

@@ -10,8 +10,7 @@ import django.db.models.deletion
from django.utils.timezone import now as timezone_now
def backfill_user_activations_and_deactivations(apps, schema_editor):
# type: (StateApps, DatabaseSchemaEditor) -> None
def backfill_user_activations_and_deactivations(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
migration_time = timezone_now()
RealmAuditLog = apps.get_model('zerver', 'RealmAuditLog')
UserProfile = apps.get_model('zerver', 'UserProfile')
@@ -26,8 +25,7 @@ def backfill_user_activations_and_deactivations(apps, schema_editor):
event_type='user_deactivated', event_time=migration_time,
backfilled=True)
def reverse_code(apps, schema_editor):
# type: (StateApps, DatabaseSchemaEditor) -> None
def reverse_code(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
RealmAuditLog = apps.get_model('zerver', 'RealmAuditLog')
RealmAuditLog.objects.filter(event_type='user_created').delete()
RealmAuditLog.objects.filter(event_type='user_deactivated').delete()

View File

@@ -15,8 +15,7 @@ import requests
import os
def mkdirs(path):
# type: (Text) -> None
def mkdirs(path: Text) -> None:
dirname = os.path.dirname(path)
if not os.path.isdir(dirname):
os.makedirs(dirname)
@@ -24,8 +23,7 @@ def mkdirs(path):
class MissingAvatarException(Exception):
pass
def move_local_file(type, path_src, path_dst):
# type: (Text, Text, Text) -> None
def move_local_file(type: Text, path_src: Text, path_dst: Text) -> None:
src_file_path = os.path.join(settings.LOCAL_UPLOADS_DIR, type, path_src)
dst_file_path = os.path.join(settings.LOCAL_UPLOADS_DIR, type, path_dst)
if os.path.exists(dst_file_path):
@@ -37,8 +35,7 @@ def move_local_file(type, path_src, path_dst):
mkdirs(dst_file_path)
os.rename(src_file_path, dst_file_path)
def move_avatars_to_be_uid_based(apps, schema_editor):
# type: (StateApps, DatabaseSchemaEditor) -> None
def move_avatars_to_be_uid_based(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
user_profile_model = apps.get_model('zerver', 'UserProfile')
if settings.LOCAL_UPLOADS_DIR is not None:
for user_profile in user_profile_model.objects.filter(avatar_source=u"U"):

View File

@@ -14,8 +14,7 @@ import os
class MissingUploadFileException(Exception):
pass
def get_file_size_local(path_id):
# type: (Text) -> int
def get_file_size_local(path_id: Text) -> int:
file_path = os.path.join(settings.LOCAL_UPLOADS_DIR, 'files', path_id)
try:
size = os.path.getsize(file_path)
@@ -23,8 +22,7 @@ def get_file_size_local(path_id):
raise MissingUploadFileException
return size
def sync_filesizes(apps, schema_editor):
# type: (StateApps, DatabaseSchemaEditor) -> None
def sync_filesizes(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
attachments = apps.get_model('zerver', 'Attachment')
if settings.LOCAL_UPLOADS_DIR is not None:
for attachment in attachments.objects.all():
@@ -49,8 +47,7 @@ def sync_filesizes(apps, schema_editor):
attachment.size = new_size
attachment.save(update_fields=["size"])
def reverse_sync_filesizes(apps, schema_editor):
# type: (StateApps, DatabaseSchemaEditor) -> None
def reverse_sync_filesizes(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
"""Does nothing"""
return None

View File

@@ -6,8 +6,7 @@ from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps
from django.db.models import Count
def fix_duplicate_attachments(apps, schema_editor):
# type: (StateApps, DatabaseSchemaEditor) -> None
def fix_duplicate_attachments(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
"""Migration 0041 had a bug, where if multiple messages referenced the
same attachment, rather than creating a single attachment object
for all of them, we would incorrectly create one for each message.

View File

@@ -25,8 +25,7 @@ from typing import Dict, Text, Tuple, Optional, Union
from six import binary_type
def force_str(s, encoding='utf-8'):
# type: (Union[Text, binary_type], Text) -> str
def force_str(s: Union[Text, binary_type], encoding: Text='utf-8') -> str:
"""converts a string to a native string"""
if isinstance(s, str):
return s
@@ -39,23 +38,20 @@ def force_str(s, encoding='utf-8'):
class Uploader(object):
def __init__(self):
# type: () -> None
def __init__(self) -> None:
self.path_template = "{realm_id}/emoji/{emoji_file_name}"
self.emoji_size = (64, 64)
def upload_files(self, response, resized_image, dst_path_id):
# type: (Response, binary_type, Text) -> None
def upload_files(self, response: Response, resized_image: binary_type,
dst_path_id: Text) -> None:
raise NotImplementedError()
def get_dst_path_id(self, realm_id, url, emoji_name):
# type: (int, Text, Text) -> Tuple[Text,Text]
def get_dst_path_id(self, realm_id: int, url: Text, emoji_name: Text) -> Tuple[Text, Text]:
_, image_ext = os.path.splitext(url)
file_name = ''.join((emoji_name, image_ext))
return file_name, self.path_template.format(realm_id=realm_id, emoji_file_name=file_name)
def resize_emoji(self, image_data):
# type: (binary_type) -> Optional[binary_type]
def resize_emoji(self, image_data: binary_type) -> Optional[binary_type]:
im = Image.open(io.BytesIO(image_data))
format_ = im.format
if format_ == 'GIF' and im.is_animated:
@@ -65,8 +61,8 @@ class Uploader(object):
im.save(out, format_)
return out.getvalue()
def upload_emoji(self, realm_id, image_url, emoji_name):
# type: (int, Text, Text) -> Optional[Text]
def upload_emoji(self, realm_id: int, image_url: Text,
emoji_name: Text) -> Optional[Text]:
file_name, dst_path_id = self.get_dst_path_id(realm_id, image_url, emoji_name)
if image_url.startswith("/"):
# Handle relative URLs.
@@ -86,25 +82,22 @@ class Uploader(object):
class LocalUploader(Uploader):
def __init__(self):
# type: () -> None
def __init__(self) -> None:
super(LocalUploader, self).__init__()
@staticmethod
def mkdirs(path):
# type: (Text) -> None
def mkdirs(path: Text) -> None:
dirname = os.path.dirname(path)
if not os.path.isdir(dirname):
os.makedirs(dirname)
def write_local_file(self, path, file_data):
# type: (Text, binary_type) -> None
def write_local_file(self, path: Text, file_data: binary_type) -> None:
self.mkdirs(path)
with open(path, 'wb') as f:
f.write(file_data)
def upload_files(self, response, resized_image, dst_path_id):
# type: (Response, binary_type, Text) -> None
def upload_files(self, response: Response, resized_image: binary_type,
dst_path_id: Text) -> None:
dst_file = os.path.join(settings.LOCAL_UPLOADS_DIR, 'avatars', dst_path_id)
if resized_image:
self.write_local_file(dst_file, resized_image)
@@ -114,21 +107,20 @@ class LocalUploader(Uploader):
class S3Uploader(Uploader):
def __init__(self):
# type: () -> None
def __init__(self) -> None:
super(S3Uploader, self).__init__()
conn = S3Connection(settings.S3_KEY, settings.S3_SECRET_KEY)
bucket_name = settings.S3_AVATAR_BUCKET
self.bucket = conn.get_bucket(bucket_name, validate=False)
def upload_to_s3(self, path, file_data, headers):
# type: (Text, binary_type, Optional[Dict[Text, Text]]) -> None
def upload_to_s3(self, path: Text, file_data: binary_type,
headers: Optional[Dict[Text, Text]]) -> None:
key = Key(self.bucket)
key.key = path
key.set_contents_from_string(force_str(file_data), headers=headers)
def upload_files(self, response, resized_image, dst_path_id):
# type: (Response, binary_type, Text) -> None
def upload_files(self, response: Response, resized_image: binary_type,
dst_path_id: Text) -> None:
headers = None # type: Optional[Dict[Text, Text]]
content_type = response.headers.get(str("Content-Type")) or guess_type(dst_path_id)[0]
if content_type:
@@ -139,15 +131,13 @@ class S3Uploader(Uploader):
self.upload_to_s3(dst_path_id, response.content, headers)
self.upload_to_s3('.'.join((dst_path_id, 'original')), response.content, headers)
def get_uploader():
# type: () -> Uploader
def get_uploader() -> Uploader:
if settings.LOCAL_UPLOADS_DIR is None:
return S3Uploader()
return LocalUploader()
def upload_emoji_to_storage(apps, schema_editor):
# type: (StateApps, DatabaseSchemaEditor) -> None
def upload_emoji_to_storage(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
realm_emoji_model = apps.get_model('zerver', 'RealmEmoji')
uploader = get_uploader() # type: Uploader
for emoji in realm_emoji_model.objects.all():

View File

@@ -5,8 +5,7 @@ from django.db import migrations
from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps
def delete_old_scheduled_jobs(apps, schema_editor):
# type: (StateApps, DatabaseSchemaEditor) -> None
def delete_old_scheduled_jobs(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
"""Delete any old scheduled jobs, to handle changes in the format of
that table. Ideally, we'd translate the jobs, but it's not really
worth the development effort to save a few invitation reminders

View File

@@ -13,8 +13,7 @@ class Migration(migrations.Migration):
('zerver', '0080_realm_description_length'),
]
def emoji_to_lowercase(apps, schema_editor):
# type: (StateApps, DatabaseSchemaEditor) -> None
def emoji_to_lowercase(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
RealmEmoji = apps.get_model("zerver", "RealmEmoji")
emoji = RealmEmoji.objects.all()
for e in emoji:

View File

@@ -5,8 +5,7 @@ from django.db import migrations, models
from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps
def fix_bot_type(apps, schema_editor):
# type: (StateApps, DatabaseSchemaEditor) -> None
def fix_bot_type(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
UserProfile = apps.get_model("zerver", "UserProfile")
bots = UserProfile.objects.filter(is_bot=True, bot_type=None)
for bot in bots:

View File

@@ -5,8 +5,7 @@ from django.db import migrations
from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps
def delete_old_scheduled_jobs(apps, schema_editor):
# type: (StateApps, DatabaseSchemaEditor) -> None
def delete_old_scheduled_jobs(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
"""Delete any old scheduled jobs, to handle changes in the format of
send_email. Ideally, we'd translate the jobs, but it's not really
worth the development effort to save a few invitation reminders

View File

@@ -9,8 +9,7 @@ from django.utils.timezone import now as timezone_now
from typing import List
def backfill_subscription_log_events(apps, schema_editor):
# type: (StateApps, DatabaseSchemaEditor) -> None
def backfill_subscription_log_events(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
migration_time = timezone_now()
RealmAuditLog = apps.get_model('zerver', 'RealmAuditLog')
Subscription = apps.get_model('zerver', 'Subscription')
@@ -47,8 +46,7 @@ def backfill_subscription_log_events(apps, schema_editor):
RealmAuditLog.objects.bulk_create(objects_to_create)
objects_to_create = []
def reverse_code(apps, schema_editor):
# type: (StateApps, DatabaseSchemaEditor) -> None
def reverse_code(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
RealmAuditLog = apps.get_model('zerver', 'RealmAuditLog')
RealmAuditLog.objects.filter(event_type='subscription_created').delete()
RealmAuditLog.objects.filter(event_type='subscription_deactivated').delete()

View File

@@ -9,8 +9,7 @@ from django.db import migrations, models
from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps
def populate_new_fields(apps, schema_editor):
# type: (StateApps, DatabaseSchemaEditor) -> None
def populate_new_fields(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
# Open the JSON file which contains the data to be used for migration.
MIGRATION_DATA_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)), "management", "data")
path_to_unified_reactions = os.path.join(MIGRATION_DATA_PATH, "unified_reactions.json")

View File

@@ -6,8 +6,7 @@ from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps
from django.db import connection, migrations
def convert_muted_topics(apps, schema_editor):
# type: (StateApps, DatabaseSchemaEditor) -> None
def convert_muted_topics(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
stream_query = '''
SELECT
zerver_stream.name,

View File

@@ -8,8 +8,7 @@ from django.db import migrations
from zerver.lib.fix_unreads import fix
def fix_unreads(apps, schema_editor):
# type: (StateApps, DatabaseSchemaEditor) -> None
def fix_unreads(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
UserProfile = apps.get_model("zerver", "UserProfile")
user_profiles = list(UserProfile.objects.filter(is_bot=False))
for user_profile in user_profiles:

View File

@@ -5,8 +5,7 @@ from django.db import migrations
from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps
def fix_realm_string_ids(apps, schema_editor):
# type: (StateApps, DatabaseSchemaEditor) -> None
def fix_realm_string_ids(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
Realm = apps.get_model('zerver', 'Realm')
if Realm.objects.filter(deactivated=False).count() != 2:
return

View File

@@ -3,8 +3,7 @@ from django.db import models, migrations
from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps
def set_tutorial_status_to_finished(apps, schema_editor):
# type: (StateApps, DatabaseSchemaEditor) -> None
def set_tutorial_status_to_finished(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
UserProfile = apps.get_model('zerver', 'UserProfile')
UserProfile.objects.update(tutorial_status=u'F')

View File

@@ -4,8 +4,7 @@ from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps
from django.db import connection, migrations, models
def populate_is_zephyr(apps, schema_editor):
# type: (StateApps, DatabaseSchemaEditor) -> None
def populate_is_zephyr(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
Realm = apps.get_model("zerver", "Realm")
Stream = apps.get_model("zerver", "Stream")