|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from __future__ import unicode_literals |
| 3 | + |
| 4 | +from django.db import migrations |
| 5 | + |
| 6 | +def set_state(doc, state): |
| 7 | + already_set = doc.states.filter(type=state.type) |
| 8 | + others = [s for s in already_set if s != state] |
| 9 | + if others: |
| 10 | + doc.states.remove(*others) |
| 11 | + if state not in already_set: |
| 12 | + doc.states.add(state) |
| 13 | + doc.state_cache = None |
| 14 | + |
| 15 | +def forward_archive_slides(apps,schema_editor): |
| 16 | + Document = apps.get_model('doc', 'Document') |
| 17 | + State = apps.get_model('doc','State') |
| 18 | + archived = State.objects.get(type__slug='slides',slug='archived') |
| 19 | + for doc in Document.objects.filter(name__startswith='slides-92-',states__type__slug='slides',states__slug='active'): |
| 20 | + set_state(doc,archived) |
| 21 | + |
| 22 | +def reverse_archive_slides(apps,schema_editor): |
| 23 | + Document = apps.get_model('doc', 'Document') |
| 24 | + State = apps.get_model('doc','State') |
| 25 | + active = State.objects.get(type__slug='slides',slug='active') |
| 26 | + for doc in Document.objects.filter(name__startswith='slides-92-',states__type__slug='slides',states__slug='archived'): |
| 27 | + set_state(doc,active) |
| 28 | + |
| 29 | +class Migration(migrations.Migration): |
| 30 | + |
| 31 | + dependencies = [ |
| 32 | + ('doc', '0003_auto_20150326_0728'), |
| 33 | + ] |
| 34 | + |
| 35 | + operations = [ |
| 36 | + migrations.RunPython(forward_archive_slides,reverse_archive_slides), |
| 37 | + ] |
0 commit comments