|
18 | 18 | from ietf.person.models import Email, Person |
19 | 19 | from ietf.utils.admin import admin_link |
20 | 20 |
|
21 | | - |
22 | 21 | class StateType(models.Model): |
23 | 22 | slug = models.CharField(primary_key=True, max_length=30) # draft, draft-iesg, charter, ... |
24 | 23 | label = models.CharField(max_length=255, help_text="Label that should be used (e.g. in admin) for state drop-down for this type of state") # State, IESG state, WG state, ... |
@@ -433,6 +432,27 @@ def display_name(self): |
433 | 432 | name = name.upper() |
434 | 433 | return name |
435 | 434 |
|
| 435 | + def save_with_history(self, events): |
| 436 | + """Save document and put a snapshot in the history models where they |
| 437 | + can be retrieved later. You must pass in at least one event |
| 438 | + with a description of what happened.""" |
| 439 | + |
| 440 | + assert events, "You must always add at least one event to describe the changes in the history log" |
| 441 | + self.time = max(self.time, events[0].time) |
| 442 | + |
| 443 | + self._has_an_event_so_saving_is_allowed = True |
| 444 | + self.save() |
| 445 | + del self._has_an_event_so_saving_is_allowed |
| 446 | + |
| 447 | + from ietf.doc.utils import save_document_in_history |
| 448 | + save_document_in_history(self) |
| 449 | + |
| 450 | + def save(self, *args, **kwargs): |
| 451 | + # if there's no primary key yet, we can allow the save to go |
| 452 | + # through to break the cycle between the document and any |
| 453 | + # events |
| 454 | + assert kwargs.get("force_insert", False) or getattr(self, "_has_an_event_so_saving_is_allowed", None), "Use .save_with_history to save documents" |
| 455 | + super(Document, self).save(*args, **kwargs) |
436 | 456 |
|
437 | 457 | def telechat_date(self, e=None): |
438 | 458 | if not e: |
@@ -570,50 +590,6 @@ class Meta: |
570 | 590 | verbose_name = "document history" |
571 | 591 | verbose_name_plural = "document histories" |
572 | 592 |
|
573 | | -def save_document_in_history(doc): |
574 | | - """This should be called before saving changes to a Document instance, |
575 | | - so that the DocHistory entries contain all previous states, while |
576 | | - the Group entry contain the current state. XXX TODO: Call this |
577 | | - directly from Document.save(), and add event listeners for save() |
578 | | - on related objects so we can save as needed when they change, too. |
579 | | - """ |
580 | | - def get_model_fields_as_dict(obj): |
581 | | - return dict((field.name, getattr(obj, field.name)) |
582 | | - for field in obj._meta.fields |
583 | | - if field is not obj._meta.pk) |
584 | | - |
585 | | - # copy fields |
586 | | - fields = get_model_fields_as_dict(doc) |
587 | | - fields["doc"] = doc |
588 | | - fields["name"] = doc.canonical_name() |
589 | | - |
590 | | - dochist = DocHistory(**fields) |
591 | | - dochist.save() |
592 | | - |
593 | | - # copy many to many |
594 | | - for field in doc._meta.many_to_many: |
595 | | - if field.rel.through and field.rel.through._meta.auto_created: |
596 | | - setattr(dochist, field.name, getattr(doc, field.name).all()) |
597 | | - |
598 | | - # copy remaining tricky many to many |
599 | | - def transfer_fields(obj, HistModel): |
600 | | - mfields = get_model_fields_as_dict(item) |
601 | | - # map doc -> dochist |
602 | | - for k, v in mfields.iteritems(): |
603 | | - if v == doc: |
604 | | - mfields[k] = dochist |
605 | | - HistModel.objects.create(**mfields) |
606 | | - |
607 | | - for item in RelatedDocument.objects.filter(source=doc): |
608 | | - transfer_fields(item, RelatedDocHistory) |
609 | | - |
610 | | - for item in DocumentAuthor.objects.filter(document=doc): |
611 | | - transfer_fields(item, DocHistoryAuthor) |
612 | | - |
613 | | - return dochist |
614 | | - |
615 | | - |
616 | | - |
617 | 593 | class DocAlias(models.Model): |
618 | 594 | """This is used for documents that may appear under multiple names, |
619 | 595 | and in particular for RFCs, which for continuity still keep the |
@@ -695,7 +671,8 @@ class DocReminder(models.Model): |
695 | 671 |
|
696 | 672 | # RFC Editor |
697 | 673 | ("rfc_editor_received_announcement", "Announcement was received by RFC Editor"), |
698 | | - ("requested_publication", "Publication at RFC Editor requested") |
| 674 | + ("requested_publication", "Publication at RFC Editor requested"), |
| 675 | + ("sync_from_rfc_editor", "Received updated information from RFC Editor"), |
699 | 676 | ] |
700 | 677 |
|
701 | 678 | class DocEvent(models.Model): |
|
0 commit comments