views:

348

answers:

2

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.

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.

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)

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

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)
+1  A: 

The code in your first snippet is not a model - it's a custom class that uses the low-level datastore module. If you want to extend it, I would recommend throwing it out and replacing it with actual models, along similar lines to the Article model you demonstrated in your second snippet.

Also, they're App Engine models, not Django models - Django models don't work on App Engine.

Nick Johnson
This seems like a pretty good answer, and given the choice again, I might go this way. However, I did hack out a solution to extend this code which I will post.
Andrew Johnson
A: 

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()

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()

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
Andrew Johnson
Your data model here is reasonable - it's what I would have suggested, for the most part. However, if you reworked Page to be a model instead of using the datastore directly, you could do a batch put on both the page and revision, and save a datastore roundtrip.
Nick Johnson