Copy data from publicly viewable plugin to incoming draft plugin.

6 views
Skip to first unread message

Robert Archer

unread,
Jun 3, 2014, 1:14:41 PM6/3/14
to django-cms...@googlegroups.com
I'm working on a plugin that will allow users to log in and keep notes on a page.  Adding the plugin to a page shows a text field that people can write to and save.

Returning to the page will show the text that they have saved so far.

My problem is that when that page is edited in draft mode, and then published, the user data associated with that plugin goes away.  I've tried fixing the problem with both "copy_relations" and "post_copy", but both of those functions assume that you want to be copying data from the draft plugin to a new live plugin.  In neither case do you have access to the publicly visible plugin instance that is going to be replaced by the draft instance.

My Models (BaseSPLPluginModel is an abstract model that subclasses CMSPlugin):

class Note(BaseSPLPluginModel):

    class Meta:
        verbose_name = "My Note"

    def copy_relations(self, oldinstance):
        for note_instance in oldinstance.note_instance.all():
            # instance.pk = None; instance.pk.save() is the slightly odd but
            # standard Django way of copying a saved model instance
            note_instance.pk = None
            note_instance.plugin = self
            note_instance.save()

    def post_copy(self, old_instance, new_old_ziplist):
        print(self)
        print(self.instances.all())
        print(old_instance.note)
        print(old_instance.note.instances.all())
        for user_note in old_instance.note.instances.all():
            print(user_note.id)
            user_note.plugin = self  

 class NoteInstance(models.Model):
    '''
    This holds the actual note for a note entry for a user 
    '''
    note = models.TextField(blank=True)
    created = models.DateTimeField('date published', auto_now_add=True, editable=False)
    created_by = models.ForeignKey(User, editable=False, related_name='Note Author')
    # This is the Note plugin instance that this note is from
    plugin = models.ForeignKey(Note, related_name='instances')

    def __unicode__(self):
        return self.note[0:50]     
Reply all
Reply to author
Forward
0 new messages