mirror of
https://github.com/inventree/InvenTree.git
synced 2026-09-02 02:01:20 +00:00
Update data migration
- Find any NoteImage items which do not link to a model - Try to associate them with an existing note
This commit is contained in:
@@ -42,12 +42,6 @@ def migrate_notes(apps, schema_editor):
|
|||||||
expected_note_count = initial_note_count + with_notes.count()
|
expected_note_count = initial_note_count + with_notes.count()
|
||||||
|
|
||||||
for instance in with_notes:
|
for instance in with_notes:
|
||||||
# Tasks:
|
|
||||||
# [x] - Convert markdown notes to HTML format
|
|
||||||
# [x] - Create a new Note instance (mark it as 'primary')
|
|
||||||
# [x] - Copy the content of the 'notes' field to the Note instance
|
|
||||||
# [x] - Link the Note instance to the original object (using GenericForeignKey)
|
|
||||||
# [ ] - Handle any associated images (if applicable)
|
|
||||||
|
|
||||||
content = markdown_to_html(instance.notes)
|
content = markdown_to_html(instance.notes)
|
||||||
|
|
||||||
@@ -59,18 +53,40 @@ def migrate_notes(apps, schema_editor):
|
|||||||
primary=True
|
primary=True
|
||||||
)
|
)
|
||||||
|
|
||||||
progress.update(1)
|
|
||||||
|
|
||||||
# Find any existing NotesImage objects associated with this instance
|
# Find any existing NotesImage objects associated with this instance
|
||||||
images = NotesImage.objects.filter(model_type__iexact=model, model_id=instance.pk)
|
images = NotesImage.objects.filter(model_type__iexact=model, model_id=instance.pk)
|
||||||
|
images.update(note=note)
|
||||||
|
|
||||||
# Point any associated images to the new Note instance
|
# Find any notes images which do not directly reference the model instance, but are still referenced in the markdown content
|
||||||
for image in images:
|
unlinked_images = NotesImage.objects.filter(
|
||||||
|
note__isnull=True,
|
||||||
|
model_id__isnull=True
|
||||||
|
).exclude(
|
||||||
|
image__isnull=True
|
||||||
|
)
|
||||||
|
|
||||||
|
for image in unlinked_images:
|
||||||
|
# If the image is referenced in the markdown content, link it to the note
|
||||||
|
if image.image.url in instance.notes:
|
||||||
image.note = note
|
image.note = note
|
||||||
image.save()
|
image.save()
|
||||||
|
|
||||||
|
progress.update(1)
|
||||||
|
|
||||||
assert Note.objects.count() == expected_note_count, f"Expected {expected_note_count} notes, but found {Note.objects.count()} after migration."
|
assert Note.objects.count() == expected_note_count, f"Expected {expected_note_count} notes, but found {Note.objects.count()} after migration."
|
||||||
|
|
||||||
|
|
||||||
|
def remove_unlinked_images(apps, schema_editor):
|
||||||
|
"""Remove any NoteImage objects which are not linked to a Note instance."""
|
||||||
|
|
||||||
|
NotesImage = apps.get_model('common', 'NotesImage')
|
||||||
|
|
||||||
|
unlinked_images = NotesImage.objects.filter(note__isnull=True)
|
||||||
|
|
||||||
|
for image in unlinked_images:
|
||||||
|
image.delete()
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
# Ensure that each app which supports 'notes' is up-to-date first
|
# Ensure that each app which supports 'notes' is up-to-date first
|
||||||
@@ -87,5 +103,9 @@ class Migration(migrations.Migration):
|
|||||||
migrations.RunPython(
|
migrations.RunPython(
|
||||||
code=migrate_notes,
|
code=migrate_notes,
|
||||||
reverse_code=migrations.RunPython.noop,
|
reverse_code=migrations.RunPython.noop,
|
||||||
|
),
|
||||||
|
migrations.RunPython(
|
||||||
|
code=remove_unlinked_images,
|
||||||
|
reverse_code=migrations.RunPython.noop,
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user