在Google App Engine / Django上存储Wiki修订版-修改此现有代码 [英] Storing wiki revisions on Google App Engine/Django - Modifying This Existing Code

查看:75
本文介绍了在Google App Engine / Django上存储Wiki修订版-修改此现有代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去,我创建了一个Django Wiki,创建当前Wiki条目的Page表,然后将旧修订存储到Revision表中非常简单。

In the past, I created a Django wiki, and it was fairly straightforward to make a Page table for the current wiki entries, and then to store old revisions into a Revision table.

最近,我决定在Google App Engine上建立一个网站,并使用了另一个程序员编写的Wiki代码。因为他使用实体以某种复杂的方式(至少对我来说是复杂的)创建了Page模型,所以我不确定如何创建Revision表并将其与他的Page模型集成。

More recently, I decided to set up a website on Google App Engine, and I used some wiki code that another programmer wrote. Because he created his Page model in sort of a complicated way (complicated to me at least) using Entities, I am unsure about how to create the Revision table and integrate it with his Page model.

这是相关的代码。有人可以帮我编写修订版本模型,并将保存修订与Page模型的Save方法集成吗?

Here is the relevant code. Could someone help me write the Revision model, and integrate saving the revisions with the Save method of the Page model?

class Page(object):

  def __init__(self, name, entity=None):
    self.name = name
    self.entity = entity
    if entity:
      self.content = entity['content']
      if entity.has_key('user'):
        self.user = entity['user']
      else:
        self.user = None
      self.created = entity['created']
      self.modified = entity['modified']
    else:
      # New pages should start out with a simple title to get the user going
      now = datetime.datetime.now()
      self.content = '<h1>' + cgi.escape(name) + '</h1>'
      self.user = None
      self.created = now
      self.modified = now

  def save(self):
    """Creates or edits this page in the datastore."""
    now = datetime.datetime.now()
    if self.entity:
      entity = self.entity
    else:
      entity = datastore.Entity('Page')
      entity['name'] = self.name
      entity['created'] = now
    entity['content'] = datastore_types.Text(self.content)
    entity['modified'] = now

    if users.GetCurrentUser():
      entity['user'] = users.GetCurrentUser()
    elif entity.has_key('user'):
      del entity['user']

    datastore.Put(entity)

顺便说一句,这段代码来自: http://code.google.com/p/google-app- engine-samples / downloads / list

By the way, this code comes from: http://code.google.com/p/google-app-engine-samples/downloads/list

我对GAE Django模型非常缺乏经验,而我的情况往往很简单。例如,这是我的博客文章模型:

I'm pretty inexperienced with GAE Django models, and mine tend to be very simple. For example, here's my model for a blog Article:

class Article(db.Model):
  author = db.UserProperty()
  title = db.StringProperty(required=True)
  text = db.TextProperty(required=True)
  tags = db.StringProperty(required=True)
  date_created = db.DateProperty(auto_now_add=True)


推荐答案

I创建了该模型(模仿Page类):

I created this model (which mimics the Page class):

class Revision (db.Model):
  name = db.StringProperty(required=True)
  created = db.DateTimeProperty(required=True)
  modified = db.DateTimeProperty(auto_now_add=True)
  content = db.TextProperty(required=True)
  user = db.UserProperty()

在Page类的Save()方法中,在用新数据更新字段之前,我添加了以下代码以保存修订:

In the Save() method of the Page class, I added this code to save a Revision, before I updated the fields with the new data:

r = Revision(name = self.name,
             content = self.content,
             created = self.created,
             modified = self.modified,
             user = self.user)
r.put()

我现在已将Wiki设置为接受GET参数,以指定要查看或编辑的修订版。当用户想要修订时,我从数据库中获取Page,然后用修订的内容替换Page的内容:

I have the wiki set up now to accept a GET parameter to specify which revision you want to see or edit. When the user wants a revision, I fetch the Page from the database, and replace the Page's Content with the Revision's Content:

page = models.Page.load(title)

if request.GET.get('rev'):
  query = db.Query(models.Revision)
  query.filter('name =', title).order('created')
  rev = request.GET.get('rev')
  rev_page = query.fetch(1, int(rev))
  page.content = rev_page.content

这篇关于在Google App Engine / Django上存储Wiki修订版-修改此现有代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆