fixes
[wolnelektury.git] / src / social / migrations / 0021_move_sets.py
1 # Generated by Django 4.0.8 on 2025-07-14 13:40
2
3 from django.db import migrations
4 from django.utils.timezone import now
5
6
7 def move_sets_to_userlists(apps, schema_editor):
8     UserList = apps.get_model('social', 'UserList')
9     UserListItem = apps.get_model('social', 'UserListItem')
10     Tag = apps.get_model('catalogue', 'Tag')
11
12     for tag in Tag.objects.filter(category='set'):
13         print()
14         print(tag)
15         ul = UserList.objects.create(
16             slug=tag.slug,
17             user=tag.user,
18             name=tag.name,
19             favorites=not tag.name,
20             public=not tag.name,
21             created_at=tag.created_at,
22             updated_at=tag.changed_at,
23         )
24
25         for i, item in enumerate(tag.items.all()):
26             #assert item.content_type_id == 12, item.content_type_id
27             print(item)
28             ul.userlistitem_set.create(
29                 order=i + 1,
30                 created_at=ul.updated_at,
31                 updated_at=ul.updated_at,
32                 book_id=item.object_id
33             )
34
35         tag.delete()
36
37
38 def rollback_userlists_to_sets(apps, schema_editor):
39     UserList = apps.get_model('social', 'UserList')
40     UserListItem = apps.get_model('social', 'UserListItem')
41     Tag = apps.get_model('catalogue', 'Tag')
42
43
44 class Migration(migrations.Migration):
45
46     dependencies = [
47         ('social', '0020_userlist_userlistitem'),
48         ('catalogue', '0001_initial'),
49     ]
50
51     operations = [
52         migrations.RunPython(
53             move_sets_to_userlists,
54             rollback_userlists_to_sets
55         )
56     ]