开发者

Using Django Flatpages in production: how to archive changes for recovery purposes

开发者 https://www.devze.com 2023-01-10 17:56 出处:网络
I am using Django Flatpages in a production site. I\'d like to figure out the best way to开发者_开发知识库 make sure if an admin makes a mistake editing a file, old version of the page can be retrieve

I am using Django Flatpages in a production site. I'd like to figure out the best way to开发者_开发知识库 make sure if an admin makes a mistake editing a file, old version of the page can be retrieved. We have backups and such, but that's a bit more cumbersome to recover from (i.e. involves sysadmin, not website editor).

First, is there a package that does this? I don't need something that archives all db changes to the filesystem or MongelDB, but perhaps a Flatpages add-on.

If not, I came up with two alternatives:

  1. Just have a staging server where all the live changes happen, then load on production

    Django flatpages backup?

  2. Some external script that monitors DB, and upon seeing changes to Flatpages contents, save a copy of the latest content in some other table, like FlatpagesHistory. Then website editor can just use Admin service to retrieve old copies of the pages from FlatpagesHistory. Has anyone done this?

I appreciate your feedback.

Thanks.


Try django-reversion https://github.com/etianen/django-reversion


Didn't get a response, so I was digging a bit. Turns out you can just implement this very easily.

Create a model like this:

class OldPage(models.Model):
    """
    Keep old contents from flatpages.
    """
    url = models.CharField('URL',max_length=100, db_index=True)
    title = models.CharField(max_length=200)
    content = models.TextField(blank=True)
    mtime = models.DateTimeField('Modified', db_index=True, auto_now_add=True)
    user = models.ForeignKey(django.contrib.auth.models.User)

Then in a admin.py file, override flatpage admin like this

class MyFlatPageAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        """save a copy of the Flatpage to OldPage"""
        history = OldPage()
        history.url = obj.url
        history.title = obj.title
        history.content = obj.content
        history.user = request.user
        history.save()
        obj.save()

admin.site.unregister(FlatPage)
admin.site.register(FlatPage, MyFlatPageAdmin)

And you can have a "read-only" admin interface for your OldPage, like this

class OldPageAdmin(admin.ModelAdmin):
    readonly_fields = ('url','title','content','mtime','user')
    list_display = ('url','mtime','user','title')
    date_hierarchy = 'mtime'

admin.site.register(lims.pages.models.OldPage, OldPageAdmin)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号