2016-08-24 19:41:29 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
from django.db import migrations, models
|
|
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
|
|
|
|
def add_ebooks_to_ebfs(apps, schema_editor):
|
2016-09-22 21:28:49 +00:00
|
|
|
"""
|
|
|
|
Now that EbookFile has ebook foreign key, this migration populates that key
|
|
|
|
"""
|
2016-08-24 19:41:29 +00:00
|
|
|
EbookFile = apps.get_model('core', 'EbookFile')
|
|
|
|
Ebook = apps.get_model('core', 'Ebook')
|
|
|
|
for ebf in EbookFile.objects.all():
|
2016-09-22 21:28:49 +00:00
|
|
|
|
|
|
|
# Connect each ebf (ebookfile) based on common edition (excluding the unglue.it provider) or URL.
|
|
|
|
|
2016-08-24 19:41:29 +00:00
|
|
|
for ebook in Ebook.objects.filter(edition=ebf.edition, format=ebf.format).exclude(provider='Unglue.it'):
|
|
|
|
ebf.ebook = ebook
|
|
|
|
ebf.save()
|
|
|
|
for ebook in Ebook.objects.filter(url=ebf.file.url):
|
|
|
|
ebf.ebook = ebook
|
|
|
|
ebf.save()
|
2016-09-22 21:28:49 +00:00
|
|
|
|
|
|
|
# if the ebookfile is still not connected to an ebook...
|
2016-08-24 19:41:29 +00:00
|
|
|
if not ebf.ebook:
|
2016-09-22 21:28:49 +00:00
|
|
|
# and the edition is associated with a THANKS campaign
|
2016-08-24 19:41:29 +00:00
|
|
|
if ebf.edition.work.campaigns.filter(type=3):
|
|
|
|
ebf.ebook = Ebook.objects.create(
|
|
|
|
edition=ebf.edition,
|
|
|
|
active=False,
|
|
|
|
url=ebf.file.url,
|
|
|
|
provider='Unglue.it',
|
|
|
|
format=ebf.format,
|
|
|
|
rights=ebf.edition.work.campaigns.order_by('-created')[0].license
|
|
|
|
)
|
|
|
|
ebf.save()
|
2016-09-22 21:28:49 +00:00
|
|
|
|
|
|
|
# Buy to unglue campaign
|
2016-08-24 19:41:29 +00:00
|
|
|
elif ebf.edition.work.campaigns.filter(type=2):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
print 'ebf {} is dangling'.format(ebf.id)
|
2016-09-22 21:28:49 +00:00
|
|
|
|
2016-08-24 19:41:29 +00:00
|
|
|
def noop(apps, schema_editor):
|
|
|
|
pass
|
|
|
|
|
|
|
|
dependencies = [
|
|
|
|
('core', '0005_ebookfile_ebook'),
|
|
|
|
]
|
|
|
|
|
|
|
|
operations = [
|
|
|
|
migrations.RunPython(add_ebooks_to_ebfs, reverse_code=noop, hints={'core': 'EbookFile'}),
|
|
|
|
]
|