|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Generated by Django 1.10.5 on 2017-03-04 08:30 |
| 3 | +from __future__ import unicode_literals, print_function |
| 4 | + |
| 5 | +from tqdm import tqdm |
| 6 | + |
| 7 | +from django.db import migrations |
| 8 | + |
| 9 | +import debug # pyflakes:ignore |
| 10 | + |
| 11 | +from ietf.doc.models import DocHistory |
| 12 | + |
| 13 | +def ename(event): |
| 14 | + return u"%s %s by %s at %s" % (event.doc.name, event.get_type_display().lower(), event.by.name, event.time) |
| 15 | + |
| 16 | +def get_dochistory(event): |
| 17 | + h = DocHistory.objects.filter(time__lte=event.time,doc__name=event.doc.name).order_by('-time', '-pk') |
| 18 | + if not h.exists(): |
| 19 | + h = DocHistory.objects.filter(time__gte=event.time,doc__name=event.doc.name).order_by('time', 'pk') |
| 20 | + return h |
| 21 | + |
| 22 | +def get_history_rev(e): |
| 23 | + h = e.get_dochistory() |
| 24 | + rev = None |
| 25 | + if h.exists(): |
| 26 | + for i in h: |
| 27 | + if i.rev: |
| 28 | + break |
| 29 | + if i and i.rev: |
| 30 | + rev = i.rev |
| 31 | + return rev |
| 32 | + |
| 33 | +def forwards(apps,schema_editor): |
| 34 | + DocEvent = apps.get_model('doc', 'DocEvent') |
| 35 | + DocEvent.get_dochistory = get_dochistory |
| 36 | + NewRevisionDocEvent = apps.get_model('doc', 'NewRevisionDocEvent') |
| 37 | + SubmissionDocEvent = apps.get_model('doc', 'SubmissionDocEvent') |
| 38 | + print("\nProcessing NewRevisionDocEvents:") |
| 39 | + for e in tqdm(list(NewRevisionDocEvent.objects.filter(rev=''))): |
| 40 | + if e.revision: |
| 41 | + e.rev = e.revision |
| 42 | + e.save() |
| 43 | + print("\nProcessing SubmissionDocEvents:") |
| 44 | + for e in tqdm(list(SubmissionDocEvent.objects.filter(rev=''))): |
| 45 | + if e.revision: |
| 46 | + e.rev = e.revision |
| 47 | + e.save() |
| 48 | + print("\nProcessing remaining DocEvents:") |
| 49 | + for e in tqdm(list(DocEvent.objects.filter(rev=''))): |
| 50 | + rev = get_history_rev(e) |
| 51 | + if rev: |
| 52 | + e.rev = rev |
| 53 | + e.save() |
| 54 | + |
| 55 | +def backwards(apps,schema_editor): |
| 56 | + DocEvent = apps.get_model('doc', 'DocEvent') |
| 57 | + print("\nProcessing DocEvents:") |
| 58 | + for e in tqdm(list(DocEvent.objects.exclude(rev=''))): |
| 59 | + e.rev = '' |
| 60 | + e.save() |
| 61 | + |
| 62 | +class Migration(migrations.Migration): |
| 63 | + |
| 64 | + dependencies = [ |
| 65 | + ('doc', '0022_add_docevent_rev'), |
| 66 | + ] |
| 67 | + |
| 68 | + operations = [ |
| 69 | + migrations.RunPython(forwards, backwards), |
| 70 | + ] |
0 commit comments