1 # Generated by Django 4.1.9 on 2023-09-05 14:14
3 from django.db import migrations
6 def migrate_external_ids(apps, schema_editor):
7 Book = apps.get_model('documents', 'Book')
8 Site = apps.get_model('depot', 'Site')
9 SiteBook = apps.get_model('depot', 'SiteBook')
10 SiteBookPublish = apps.get_model('depot', 'SiteBookPublish')
12 site_types = ('legimi', 'woblink')
13 for site_type in site_types:
14 site = Site.objects.filter(site_type=site_type).first()
17 for book in Book.objects.exclude(**{f'{site_type}_id': ''}):
18 SiteBook.objects.get_or_create(
20 external_id=getattr(book, f'{site_type}_id')
23 for p in SiteBookPublish.objects.all():
24 p.site_book, created = SiteBook.objects.get_or_create(
28 p.save(update_fields=['site_book'])
32 def remigrate_external_ids(apps, schema_editor):
33 Book = apps.get_model('documents', 'Book')
34 Site = apps.get_model('depot', 'Site')
35 SiteBook = apps.get_model('depot', 'SiteBook')
36 SiteBookPublish = apps.get_model('depot', 'SiteBookPublish')
38 for p in SiteBookPublish.objects.exclude(site_book=None):
39 p.site = p.site_book.site
40 p.book = p.site_book.book
41 p.save(update_fields=['site', 'book'])
43 site_types = ('legimi', 'woblink')
44 for site_type in site_types:
45 for site_book in SiteBook.objects.exclude(external_id=None):
47 setattr(book, f'{site_type}_id', site_book.external_id)
48 book.save(update_fields=[f'{site_type}_id'])
51 class Migration(migrations.Migration):
54 ("depot", "0009_sitebook_sitebookpublish_site_book"),
60 remigrate_external_ids,