#!/usr/bin/python
+import datetime
+import feedparser
+import re
+import textile
+import unicodedata
+
from . import misc
class Blog(misc.Object):
AND %s = ANY(tags) \
ORDER BY published_at DESC LIMIT %s", tag, limit)
- def get_by_author(self, uid, limit=None):
+ def get_by_author(self, author, limit=None):
+ return self._get_posts("SELECT * FROM blog \
+ WHERE (author = %s OR author_uid = %s) \
+ AND published_at IS NOT NULL \
+ AND published_at <= NOW() \
+ ORDER BY published_at DESC LIMIT %s",
+ author.name, author.uid, limit)
+
+ def get_by_year(self, year):
return self._get_posts("SELECT * FROM blog \
- WHERE author_uid = %s \
+ WHERE EXTRACT(year FROM published_at) = %s \
AND published_at IS NOT NULL \
AND published_at <= NOW() \
- ORDER BY published_at DESC LIMIT %s", uid, limit)
+ ORDER BY published_at DESC", year)
+
+ def get_drafts(self, author=None, limit=None):
+ if author:
+ return self._get_posts("SELECT * FROM blog \
+ WHERE author_uid = %s \
+ AND (published_at IS NULL OR published_at > NOW()) \
+ ORDER BY COALESCE(updated_at, created_at) DESC LIMIT %s",
+ author.uid, limit)
+
+ return self._get_posts("SELECT * FROM blog \
+ WHERE (published_at IS NULL OR published_at > NOW()) \
+ ORDER BY COALESCE(updated_at, created_at) DESC LIMIT %s", limit)
def search(self, query, limit=None):
return self._get_posts("SELECT blog.* FROM blog \
ORDER BY ts_rank(search_index.document, to_tsquery('english', %s)) DESC \
LIMIT %s", query, query, limit)
+ def create_post(self, title, text, author, tags=[]):
+ """
+ Creates a new post and returns the resulting Post object
+ """
+ return self._get_post("INSERT INTO blog(title, slug, text, author_uid, tags) \
+ VALUES(%s, %s, %s, %s, %s) RETURNING *", title, self._make_slug(title),
+ text, author.uid, list(tags))
+
+ def _make_slug(self, s):
+ # Remove any non-ASCII characters
+ try:
+ s = unicodedata.normalize("NFKD", s)
+ except TypeError:
+ pass
+
+ # Remove excessive whitespace
+ s = re.sub(r"[^\w]+", " ", s)
+
+ slug = "-".join(s.split()).lower()
+
+ while True:
+ e = self.db.get("SELECT 1 FROM blog WHERE slug = %s", slug)
+ if not e:
+ break
+
+ slug += "-"
+
+ return slug
+
def refresh(self):
"""
Needs to be called after a post has been changed
"""
self.db.execute("REFRESH MATERIALIZED VIEW blog_search_index")
+ @property
+ def years(self):
+ res = self.db.query("SELECT DISTINCT EXTRACT(year FROM published_at)::integer AS year \
+ FROM blog WHERE published_at IS NOT NULL AND published_at <= NOW() \
+ ORDER BY year DESC")
+
+ for row in res:
+ yield row.year
+
+ def update_feeds(self):
+ """
+ Updates all enabled feeds
+ """
+ for feed in self.db.query("SELECT * FROM blog_feeds WHERE enabled IS TRUE"):
+ try:
+ f = feedparser.parse(feed.url)
+ except Exception as e:
+ raise e
+
+ with self.db.transaction():
+ # Update name
+ self.db.execute("UPDATE blog_feeds SET name = %s \
+ WHERE id = %s", f.feed.title, feed.id)
+
+ # Walk through all entries
+ for entry in f.entries:
+ # Skip everything without the "blog.ipfire.org" tag
+ try:
+ tags = list((t.term for t in entry.tags))
+
+ if not "blog.ipfire.org" in tags:
+ continue
+ except AttributeError:
+ continue
+
+ # Get link to the posting site
+ link = entry.links[0].href
+
+ # Check if the entry has already been imported
+ res = self.db.get("SELECT id, (updated_at < %s) AS needs_update \
+ FROM blog WHERE feed_id = %s AND foreign_id = %s",
+ entry.updated, feed.id, entry.id)
+ if res:
+ # If the post needs to be updated, we do so
+ if res.needs_update:
+ self.db.execute("UPDATE blog SET title = %s, author = %s, \
+ published_at = %s, updated_at = %s, html = %s, link = %s, \
+ tags = %s WHERE id = %s", entry.title, entry.author,
+ entry.published, entry.updated, entry.summary, link,
+ feed.tags + tags, res.id)
+
+ # Done here
+ continue
+
+ # Insert the new post
+ self.db.execute("INSERT INTO blog(title, slug, author, \
+ published_at, html, link, tags, updated_at, feed_id, foreign_id) \
+ VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
+ entry.title, self._make_slug(entry.title), entry.author,
+ entry.published, entry.summary, link, feed.tags + tags,
+ entry.updated, feed.id, entry.id)
+
+ # Mark feed as updated
+ self.db.execute("UPDATE blog_feeds SET last_updated_at = CURRENT_TIMESTAMP \
+ WHERE id = %s" % feed.id)
+
+ # Refresh the search index
+ with self.db.transaction():
+ self.refresh()
+
class Post(misc.Object):
def init(self, id, data=None):
self.id = id
self.data = data
- @property
- def title(self):
+ # Title
+
+ def get_title(self):
return self.data.title
+ def set_title(self, title):
+ self.db.execute("UPDATE blog SET title = %s \
+ WHERE id = %s", title, self.id)
+
+ # Update slug if post is not published, yet
+ if not self.is_published():
+ self.db.execute("UPDATE blog SET slug = %s \
+ WHERE id = %s", self.backend.blog._make_slug(title), self.id)
+
+ title = property(get_title, set_title)
+
@property
def slug(self):
return self.data.slug
if self.data.author_uid:
return self.backend.accounts.get_by_uid(self.data.author_uid)
+ return self.data.author
+
@property
def created_at(self):
return self.data.created_at
+ # Published?
+
@property
def published_at(self):
return self.data.published_at
+ def is_published(self):
+ """
+ Returns True if the post is already published
+ """
+ return self.published_at and self.published_at <= datetime.datetime.now()
+
+ def publish(self):
+ if self.is_published():
+ return
+
+ self.db.execute("UPDATE blog SET published_at = NOW() \
+ WHERE id = %s", self.id)
+
+ # Update search indices
+ self.backend.blog.refresh()
+
+ # Updated?
+
+ @property
+ def updated_at(self):
+ return self.data.updated_at
+
+ def updated(self):
+ self.db.execute("UPDATE blog SET updated_at = NOW() \
+ WHERE id = %s", self.id)
+
+ # Update search indices
+ self.backend.blog.refresh()
+
+ # Text
+
+ def get_text(self):
+ return self.data.text
+
+ def set_text(self, text):
+ self.db.execute("UPDATE blog SET text = %s \
+ WHERE id = %s", text, self.id)
+
+ text = property(get_text, set_text)
+
+ # HTML
+
@property
def html(self):
"""
Returns this post as rendered HTML
"""
- return self.data.html
+ return self.data.html or textile.textile(self.text.decode("utf-8"))
- @property
- def tags(self):
+ # Tags
+
+ def get_tags(self):
return self.data.tags
+ def set_tags(self, tags):
+ self.db.execute("UPDATE blog SET tags = %s \
+ WHERE id = %s", list(tags), self.id)
+
+ tags = property(get_tags, set_tags)
+
@property
def link(self):
return self.data.link
+
+ # XXX needs caching
+ @property
+ def release(self):
+ return self.backend.releases._get_release("SELECT * FROM releases \
+ WHERE published IS NOT NULL AND published <= NOW() AND blog_id = %s", self.id)