Files
zulip/zerver/migrations/0041_create_attachments_for_old_messages.py
Anders Kaseorg 69730a78cc 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-06-11 16:04:12 -07:00

55 lines
2.2 KiB
Python

import os
import re
from django.db import migrations, models
from django.db.backends.postgresql.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps
attachment_url_re = re.compile(r'[/\-]user[\-_]uploads[/\.-].*?(?=[ )]|\Z)')
def attachment_url_to_path_id(attachment_url: str) -> str:
path_id_raw = re.sub(r'[/\-]user[\-_]uploads[/\.-]', '', attachment_url)
# Remove any extra '.' after file extension. These are probably added by the user
return re.sub('[.]+$', '', path_id_raw, re.M)
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')
Stream = apps.get_model('zerver', 'Stream')
for message in Message.objects.filter(has_attachment=True, attachment=None):
attachment_url_list = attachment_url_re.findall(message.content)
for url in attachment_url_list:
path_id = attachment_url_to_path_id(url)
user_profile = message.sender
is_message_realm_public = False
if message.recipient.type == STREAM:
stream = Stream.objects.get(id=message.recipient.type_id)
is_message_realm_public = not stream.invite_only and stream.realm.domain != "mit.edu"
if path_id is not None:
attachment = Attachment.objects.create(
file_name=os.path.basename(path_id), path_id=path_id, owner=user_profile,
realm=user_profile.realm, is_realm_public=is_message_realm_public)
attachment.messages.add(message)
class Migration(migrations.Migration):
dependencies = [
('zerver', '0040_realm_authentication_methods'),
]
operations = [
# The TextField change was originally in the next migration,
# but because it fixes a problem that causes the RunPython
# part of this migration to crash, we've copied it here.
migrations.AlterField(
model_name='attachment',
name='file_name',
field=models.TextField(db_index=True),
),
migrations.RunPython(check_and_create_attachments, elidable=True),
]