Permalink
Browse files

Simplified handling of ID.

  • Loading branch information...
1 parent c4865fb commit 3b71ebcb80916f88503f3d60a9d0f83fb430ca46 @bmc committed Aug 10, 2008
Showing with 21 additions and 29 deletions.
  1. +3 −3 admin.py
  2. +15 −25 models.py
  3. +3 −1 templates/admin-edit.html
View
@@ -48,7 +48,8 @@ class SaveArticleHandler(request.BlogRequestHandler):
def post(self):
title = cgi.escape(self.request.get('title'))
body = cgi.escape(self.request.get('content'))
- id = int(cgi.escape(self.request.get('id')))
+ s_id = cgi.escape(self.request.get('id'))
+ id = int(s_id) if s_id else None
tags = cgi.escape(self.request.get('tags'))
published_when = cgi.escape(self.request.get('published_when'))
draft = cgi.escape(self.request.get('draft'))
@@ -63,7 +64,7 @@ def post(self):
else:
draft = (draft.lower() == 'on')
- article = Article.get(id)
+ article = Article.get(id) if id else None
if article:
# It's an edit of an existing item.
article.title = title
@@ -75,7 +76,6 @@ def post(self):
article = Article(title=title,
body=body,
tags=tags,
- id=id,
draft=draft)
article.save()
View
@@ -11,27 +11,9 @@ class Article(db.Model):
body = db.TextProperty()
published_when = db.DateTimeProperty(auto_now_add=True)
tags = db.ListProperty(db.Category)
- id = db.IntegerProperty(required=True)
+ id = db.IntegerProperty()
draft = db.BooleanProperty(required=True, default=False)
- def __init__(self, *args, **kw):
- if not ('id' in kw):
- id = Article.max_id() + 1
- kw['id'] = id
- db.Model.__init__(self, *args, **kw)
-
- @classmethod
- def max_id(cls):
- q = db.Query(Article)
- q.order('-id')
- articles = q.fetch(1)
- if not articles:
- id = 0
- else:
- id = articles[0].id
-
- return id
-
@classmethod
def get_all(cls):
q = db.Query(Article)
@@ -140,10 +122,18 @@ def save(self):
# Going from draft to published. Update the timestamp.
self.published_when = datetime.datetime.now()
- self.put()
+ try:
+ obj_id = self.key().id()
+ resave = False
+ except db.NotSavedError:
+ # No key, hence no ID yet. This one hasn't been saved.
+ # We'll save it once without the ID field; this first
+ # save will cause GAE to assign it a key. Then, we can
+ # extract the ID, put it in our ID field, and resave
+ # the object.
+ resave = True
- def put(self):
- import time
- if not self.id:
- self.id = str(int(time.time()))
- db.Model.put(self)
+ self.put()
+ if resave:
+ self.id = self.key().id()
+ self.put()
@@ -117,7 +117,9 @@ <h1 class="admin-page-title">Edit article</h1>
</tr>
</table>
+{% if article.id %}
<input type="hidden" name="id" value="{{ article.id }}"/>
+{% endif %}
<input type="hidden" name="loaded_draft" value="{{ article.draft }}"/>
<input type="hidden" name="edit_again" value="false"/>
</form>
@@ -129,8 +131,8 @@ <h1 class="admin-page-title">Preview:</h1>
<iframe src="/id/{{ article.id }}" width="97%" scrolling="auto" height="750"
frameborder="0">
</iframe>
-
</div>
+
</td></tr></table>
</body>
</html>

0 comments on commit 3b71ebc

Please sign in to comment.