Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
142 lines (119 sloc) 4.21 KB
import datetime
import sys
from google.appengine.ext import db
# If the local platform is 64 bit, just using sys.maxint can cause problems.
# It will evaluate to a number that's too large for GAE's 32-bit environment.
# So, force it to a 32-bit number.
FETCH_THEM_ALL = ((sys.maxint - 1) >> 32) & 0xffffffff
class Article(db.Model):
title = db.StringProperty(required=True)
body = db.TextProperty()
published_when = db.DateTimeProperty(auto_now_add=True)
tags = db.ListProperty(db.Category)
id = db.IntegerProperty()
draft = db.BooleanProperty(required=True, default=False)
def get_all(cls):
q = db.Query(Article)
return q.fetch(FETCH_THEM_ALL)
def get(cls, id):
q = db.Query(Article)
q.filter('id = ', id)
return q.get()
def published_query(cls):
q = db.Query(Article)
q.filter('draft = ', False)
return q
def published(cls):
return Article.published_query()\
def get_all_tags(cls):
Return all tags, as TagCount objects, optionally sorted by frequency
(highest to lowest).
tag_counts = {}
for article in Article.published():
for tag in article.tags:
tag = unicode(tag)
tag_counts[tag] += 1
except KeyError:
tag_counts[tag] = 1
return tag_counts
def get_all_datetimes(cls):
dates = {}
for article in Article.published():
date = datetime.datetime(article.published_when.year,
dates[date] += 1
except KeyError:
dates[date] = 1
return dates
def all_for_month(cls, year, month):
start_date =, month, 1)
if start_date.month == 12:
next_year = start_date.year + 1
next_month = 1
next_year = start_date.year
next_month = start_date.month + 1
end_date =, next_month, 1)
return Article.published_query()\
.filter('published_when >=', start_date)\
.filter('published_when <', end_date)\
def all_for_tag(cls, tag):
return Article.published_query()\
.filter('tags = ', tag)\
def convert_string_tags(cls, tags):
new_tags = []
for t in tags:
if type(t) == db.Category:
return new_tags
def __unicode__(self):
return self.__str__()
def __str__(self):
return '[%s] %s' %\
(self.published_when.strftime('%Y/%m/%d %H:%M'), self.title)
def save(self):
previous_version = Article.get(
draft = previous_version.draft
except AttributeError:
draft = False
if draft and (not self.draft):
# Going from draft to published. Update the timestamp.
self.published_when =
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
if resave: = self.key().id()
Something went wrong with that request. Please try again.