58 lines
2.4 KiB
Python
58 lines
2.4 KiB
Python
# Generated by Django 4.1.1 on 2022-10-01 06:55
|
|
from django.apps.registry import Apps
|
|
from django.db import migrations
|
|
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
|
|
from django.urls import reverse
|
|
|
|
|
|
def replace_with_wrapper_url(
|
|
apps: Apps, schema_editor: BaseDatabaseSchemaEditor
|
|
) -> None:
|
|
Attachment = apps.get_model("attachments", "Attachment")
|
|
Article = apps.get_model("articles", "Article")
|
|
db_alias = schema_editor.connection.alias
|
|
attachments = Attachment.objects.using(db_alias).all()
|
|
modified = []
|
|
for article in Article.objects.using(db_alias).all():
|
|
for attachment in attachments:
|
|
article.content = article.content.replace(
|
|
attachment.original_file.url,
|
|
reverse("attachments:original", kwargs={"pk": attachment.pk}),
|
|
)
|
|
if attachment.processed_file:
|
|
article.content = article.content.replace(
|
|
attachment.processed_file.url,
|
|
reverse("attachments:processed", kwargs={"pk": attachment.pk}),
|
|
)
|
|
modified.append(article)
|
|
Article.objects.using(db_alias).bulk_update(modified, ["content"])
|
|
|
|
|
|
def replace_with_file_url(apps: Apps, schema_editor: BaseDatabaseSchemaEditor) -> None:
|
|
Attachment = apps.get_model("attachments", "Attachment")
|
|
Article = apps.get_model("articles", "Article")
|
|
db_alias = schema_editor.connection.alias
|
|
attachments = Attachment.objects.using(db_alias).all()
|
|
modified = []
|
|
for article in Article.objects.using(db_alias).all():
|
|
for attachment in attachments:
|
|
article.content = article.content.replace(
|
|
reverse("attachments:original", kwargs={"pk": attachment.pk}),
|
|
attachment.original_file.url,
|
|
)
|
|
if attachment.processed_file:
|
|
article.content = article.content.replace(
|
|
reverse("attachments:processed", kwargs={"pk": attachment.pk}),
|
|
attachment.processed_file.url,
|
|
)
|
|
modified.append(article)
|
|
Article.objects.using(db_alias).bulk_update(modified, ["content"])
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
dependencies = [
|
|
("articles", "0033_alter_article_options"),
|
|
("attachments", "0008_attachment_updated_at"),
|
|
]
|
|
|
|
operations = [migrations.RunPython(replace_with_wrapper_url, replace_with_file_url)]
|