|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from __future__ import unicode_literals |
| 3 | + |
| 4 | +from django.db import migrations |
| 5 | + |
| 6 | +def fix_buggy_author_foreignkey(apps, schema_editor): |
| 7 | + DocumentAuthor = apps.get_model("doc", "DocumentAuthor") |
| 8 | + # apparently, we have a buggy key in the DB, fix it |
| 9 | + DocumentAuthor.objects.filter(author="[<Email: d3e3e3@gmail.com>]").update(author="d3e3e3@gmail.com") |
| 10 | + |
| 11 | +def save_all_documents_in_history(apps, schema_editor): |
| 12 | + State = apps.get_model("doc", "State") |
| 13 | + Document = apps.get_model("doc", "Document") |
| 14 | + DocHistory = apps.get_model("doc", "DocHistory") |
| 15 | + RelatedDocument = apps.get_model("doc", "RelatedDocument") |
| 16 | + RelatedDocHistory = apps.get_model("doc", "RelatedDocHistory") |
| 17 | + DocumentAuthor = apps.get_model("doc", "DocumentAuthor") |
| 18 | + DocHistoryAuthor = apps.get_model("doc", "DocHistoryAuthor") |
| 19 | + |
| 20 | + def canonical_name(self): |
| 21 | + name = self.name |
| 22 | + state = State.objects.filter(document=self, type_id=self.type_id).first() |
| 23 | + if self.type_id == "draft" and state.slug == "rfc": |
| 24 | + a = self.docalias_set.filter(name__startswith="rfc") |
| 25 | + if a: |
| 26 | + name = a[0].name |
| 27 | + elif self.type_id == "charter": |
| 28 | + return charter_name_for_group(self.chartered_group) |
| 29 | + return name |
| 30 | + |
| 31 | + def charter_name_for_group(group): |
| 32 | + if group.type_id == "rg": |
| 33 | + top_org = "irtf" |
| 34 | + else: |
| 35 | + top_org = "ietf" |
| 36 | + |
| 37 | + return "charter-%s-%s" % (top_org, group.acronym) |
| 38 | + |
| 39 | + def save_document_in_history(doc): |
| 40 | + """Save a snapshot of document and related objects in the database.""" |
| 41 | + def get_model_fields_as_dict(obj): |
| 42 | + return dict((field.name, getattr(obj, field.name)) |
| 43 | + for field in obj._meta.fields |
| 44 | + if field is not obj._meta.pk) |
| 45 | + |
| 46 | + # copy fields |
| 47 | + fields = get_model_fields_as_dict(doc) |
| 48 | + fields["doc"] = doc |
| 49 | + fields["name"] = canonical_name(doc) |
| 50 | + |
| 51 | + dochist = DocHistory(**fields) |
| 52 | + dochist.save() |
| 53 | + |
| 54 | + # copy many to many |
| 55 | + for field in doc._meta.many_to_many: |
| 56 | + if field.rel.through and field.rel.through._meta.auto_created: |
| 57 | + setattr(dochist, field.name, getattr(doc, field.name).all()) |
| 58 | + |
| 59 | + # copy remaining tricky many to many |
| 60 | + def transfer_fields(obj, HistModel): |
| 61 | + mfields = get_model_fields_as_dict(item) |
| 62 | + # map doc -> dochist |
| 63 | + for k, v in mfields.iteritems(): |
| 64 | + if v == doc: |
| 65 | + mfields[k] = dochist |
| 66 | + HistModel.objects.create(**mfields) |
| 67 | + |
| 68 | + for item in RelatedDocument.objects.filter(source=doc): |
| 69 | + transfer_fields(item, RelatedDocHistory) |
| 70 | + |
| 71 | + for item in DocumentAuthor.objects.filter(document=doc): |
| 72 | + transfer_fields(item, DocHistoryAuthor) |
| 73 | + |
| 74 | + return dochist |
| 75 | + |
| 76 | + from django.conf import settings |
| 77 | + settings.DEBUG = False # prevent out-of-memory problems |
| 78 | + |
| 79 | + for d in Document.objects.iterator(): |
| 80 | + save_document_in_history(d) |
| 81 | + |
| 82 | +class Migration(migrations.Migration): |
| 83 | + |
| 84 | + dependencies = [ |
| 85 | + ('doc', '0010_auto_20150930_0251'), |
| 86 | + ('group', '0007_auto_20150930_0758'), |
| 87 | + ] |
| 88 | + |
| 89 | + operations = [ |
| 90 | + migrations.RunPython(fix_buggy_author_foreignkey), |
| 91 | + migrations.RunPython(save_all_documents_in_history) |
| 92 | + ] |
0 commit comments