Skip to content
Browse files

fixes issue #264

  • Loading branch information...
1 parent 984f548 commit 77a0b99dda91fc976d3b6dd4c9dc340f511c0217 @specialunderwear specialunderwear committed with digi604 Nov 30, 2009
Showing with 46 additions and 0 deletions.
  1. +46 −0 cms/utils/reversion_hacks.py
View
46 cms/utils/reversion_hacks.py
@@ -0,0 +1,46 @@
+import reversion
+from reversion.revisions import RegistrationError, RegistrationInfo
+from reversion.storage import VersionFileStorageWrapper
+from django.db import models
+from django.db.models.signals import post_save
+
+def register_draft_only(model_class, fields, follow, format):
+ """
+ version of the reversion register function that only
+ registers drafts and ignores public models
+ """
+ revision_manager = reversion.revision
+
+ if revision_manager.is_registered(model_class):
+ raise RegistrationError, "%r has already been registered with Reversion." % model_class
+ # Ensure the parent model of proxy models is registered.
+ if model_class._meta.proxy and not revision_manager.is_registered(model_class._meta.parents.keys()[0]):
+ raise RegistrationError, "%r is a proxy model, and its parent has not been registered with Reversion." % model_class
+ # Calculate serializable model fields.
+ opts = model_class._meta
+ local_fields = opts.local_fields + opts.local_many_to_many
+ if fields is None:
+ fields = [field.name for field in local_fields]
+ fields = tuple(fields)
+ # Calculate serializable model file fields.
+ file_fields = []
+ for field in local_fields:
+ if isinstance(field, models.FileField) and field.name in fields:
+ field.storage = VersionFileStorageWrapper(field.storage)
+ file_fields.append(field)
+ file_fields = tuple(file_fields)
+ # Register the generated registration information.
+ follow = tuple(follow)
+ registration_info = RegistrationInfo(fields, file_fields, follow, format)
+ revision_manager._registry[model_class] = registration_info
+ # Connect to the post save signal of the model.
+ post_save.connect(selective_post_save_receiver, model_class)
+
+def selective_post_save_receiver(instance, sender, **kwargs):
+ """
+ Only add to revision if it is a draft.
+ """
+ revision_manager = reversion.revision
+ if getattr(instance, 'publisher_is_draft', True):
+ if revision_manager.is_active():
+ revision_manager.add(instance)

0 comments on commit 77a0b99

Please sign in to comment.
Something went wrong with that request. Please try again.