Browse files

SiteBlogPost tag support

BlogPost support not fully hooked up yet.
  • Loading branch information...
1 parent 57ad21d commit 4bdcf20ff90ca0db1adbba6424428dea1a8651f5 @mjibson committed Jun 2, 2012
Showing with 76 additions and 2 deletions.
  1. +4 −0 filters.py
  2. +2 −0 main.py
  3. +59 −0 models.py
  4. +5 −0 templates/blog-base.html
  5. +3 −1 templates/blog-post.html
  6. +3 −1 templates/blog.html
View
4 filters.py
@@ -66,6 +66,9 @@ def markdown(text):
import utils
return utils.markdown(text)
+def tags(t):
+ return ['<span style="font-size: %i%%">%s</span>' %(c * 175 + 25, v) for v, c in t]
+
filters = dict([(i, globals()[i]) for i in [
'date',
'editline',
@@ -80,5 +83,6 @@ def markdown(text):
'linkmap',
'markdown',
'rss_date',
+ 'tags',
'url',
]])
View
2 main.py
@@ -881,6 +881,7 @@ def get(self, year=0, month=0):
'nextpage': next,
'posts': posts,
'rss': webapp2.uri_for('blog-rss'),
+ 'tags': models.SiteTag.get(),
})
class BlogPost(BaseHandler):
@@ -896,6 +897,7 @@ def get(self, link):
'months': models.SiteBlogPost.months(),
'p': p.get(),
'prev': models.SiteBlogPost.prev(p),
+ 'tags': models.SiteTag.get(),
})
class Admin(BaseHandler):
View
59 models.py
@@ -384,6 +384,21 @@ def link_filter(prop, value):
raise ValueError('link cannot be a valid number')
+class Tag(ndb.Model):
+ count = ndb.IntegerProperty('c', indexed=False)
+
+ @classmethod
+ def get(cls, parent=None):
+ tags = list(cls.query(ancestor=parent))
+ m = float(max([i.count for i in tags]))
+ return [(i.key.id(), i.count / m) for i in tags if i.count > 0]
+
+class SiteTag(Tag):
+ pass
+
+class TagIndex(ndb.Model):
+ keys = ndb.KeyProperty('k', repeated=True)
+
class BlogPost(ndb.Model):
title = ndb.StringProperty('l', indexed=False, required=True)
image = ndb.KeyProperty('i', indexed=False, required=True)
@@ -408,6 +423,8 @@ def _pre_put_hook(self):
self.link = link
+ deferred.defer(update_tags, self.key)
+
def short(self, length=50):
s = re.sub(r'<.+?>', ' ', self.text)[:length]
@@ -432,6 +449,10 @@ def url(self):
p = self.key.parent().get()
return '%s/%s' %(p.name, self.key.id())
+ @property
+ def has_tags(self):
+ return self.tags and self.tags[0]
+
@classmethod
def posts(cls, year, month, parent=None):
nextyear = year if month < 12 else year + 1
@@ -454,6 +475,12 @@ def link_key(cls, link, parent=None):
class SiteBlogPost(BlogPost):
html = ndb.TextProperty('h', compressed=True)
+ @property
+ def tag_index_keys(self):
+ return [ndb.Key('SiteTag', i, 'TagIndex', i) for i in self.tags if i]
+
+ tag_kind = SiteTag
+
def _pre_put_hook(self):
super(SiteBlogPost, self)._pre_put_hook()
@@ -540,6 +567,8 @@ def set_blob(self):
class SiteImageBlob(ImageBlob):
date = ndb.DateTimeProperty('t', auto_now_add=True)
+ attrib_link = ndb.TextProperty('k', indexed=False)
+ attrib_name = ndb.TextProperty('m', indexed=False)
@classmethod
def images(cls):
@@ -550,3 +579,33 @@ class Config(ndb.Expando):
values = ndb.TextProperty('v', repeated=True)
dates = ndb.DateProperty('d', repeated=True)
+
+def update_tags(key):
+ p = key.get()
+
+ tis = dict([(i.key, i) for i in TagIndex.query().filter(TagIndex.keys == key)])
+
+ for i in p.tag_index_keys:
+ if i not in tis:
+ tis[i] = i.get()
+
+ for k, v in tis.items():
+ if not v:
+ m = TagIndex(key=k, keys=[key])
+ m.put()
+ tis[k] = m
+ elif k.id() in p.tags and key not in v.keys:
+ v.keys.append(key)
+ v.put()
+ elif k.id() not in p.tags and key in v.keys:
+ v.keys.remove(key)
+ v.put()
+
+ for k, v in tis.iteritems():
+ t = k.parent().get()
+
+ if not t:
+ t = p.tag_kind(key=k.parent())
+
+ t.count = len(v.keys)
+ t.put()
View
5 templates/blog-base.html
@@ -68,6 +68,11 @@
<div id="sneek-twitter-2" class="widget sneek-widget-twitter">
</div>
+ <div class="widget">
+ <h3 class="title">Tags</h3>
+ {{ tags|tags|join(', ') }}
+ </div>
+
<div id="archives-2" class="widget widget_archive">
<h3 class="title">Archives</h3>
<ul>
View
4 templates/blog-post.html
@@ -23,7 +23,9 @@ <h1 id="title">{{ p.title }}</h1>
<span class="author">{{ p.author }}</span>
<span class="date">{{ p.date|fdate }}</span>
<span class="comments"><a href="#disqus_thread" data-disqus-identifier="{{ p.key.id() }}">comments</a></span>
- <span class="categories">{{ p.tags[:1]|join('') }}</span>
+ {% if p.has_tags -%}
+ <span class="categories">{{ p.tags|join(', ') }}</span>
+ {%- endif %}
</div>
</div>
View
4 templates/blog.html
@@ -71,7 +71,9 @@ <h2 class="title"><a title="{{ p.title }}" href="{{ p.url }}">{{ p.title }}</a><
<p class="meta">
<span class="date">{{ p.date|fdate }}</span>
<span class="comments"><a href="{{ p.url }}#disqus_thread" data-disqus-identifier="{{ p.key.id() }}">comments</a></span>
- <span class="categories">{{ p.tags[:1]|join('') }}</span>
+ {% if p.has_tags -%}
+ <span class="categories">{{ p.tags|join(', ') }}</span>
+ {%- endif %}
</p>
</div>
</div>

0 comments on commit 4bdcf20

Please sign in to comment.