import datetime
import feedparser
+import html2text
+import markdown
import re
import textile
import unicodedata
from . import misc
+from . import wiki
+from .decorators import *
class Blog(misc.Object):
def _get_post(self, query, *args):
def get_by_slug(self, slug):
return self._get_post("SELECT * FROM blog \
- WHERE slug = %s AND published_at <= NOW()", slug)
+ WHERE slug = %s", slug)
def get_newest(self, limit=None):
- return self._get_posts("SELECT * FROM blog \
+ posts = self._get_posts("SELECT * FROM blog \
WHERE published_at IS NOT NULL \
AND published_at <= NOW() \
ORDER BY published_at DESC LIMIT %s", limit)
+ return list(posts)
+
def get_by_tag(self, tag, limit=None):
return self._get_posts("SELECT * FROM blog \
WHERE published_at IS NOT NULL \
AND %s = ANY(tags) \
ORDER BY published_at DESC LIMIT %s", tag, limit)
- 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 EXTRACT(year FROM published_at) = %s \
AND published_at <= NOW() \
ORDER BY published_at DESC", year)
+ def get_drafts(self, author, limit=None):
+ 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)
+
def search(self, query, limit=None):
- return self._get_posts("SELECT blog.* FROM blog \
+ posts = self._get_posts("SELECT blog.* FROM blog \
LEFT JOIN blog_search_index search_index ON blog.id = search_index.post_id \
- WHERE search_index.document @@ to_tsquery('english', %s) \
- ORDER BY ts_rank(search_index.document, to_tsquery('english', %s)) DESC \
+ WHERE search_index.document @@ websearch_to_tsquery('english', %s) \
+ ORDER BY ts_rank(search_index.document, websearch_to_tsquery('english', %s)) DESC \
LIMIT %s", query, query, limit)
- def create_post(self, title, text, author, tags=[]):
+ return list(posts)
+
+ def has_had_recent_activity(self, **kwargs):
+ t = datetime.timedelta(**kwargs)
+
+ res = self.db.get("SELECT COUNT(*) AS count FROM blog \
+ WHERE published_at IS NOT NULL AND published_at BETWEEN NOW() - %s AND NOW()", t)
+
+ if res and res.count > 0:
+ return True
+
+ return False
+
+ def create_post(self, title, text, author, tags=[], lang="markdown"):
"""
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))
+ # Pre-render HTML
+ html = self._render_text(text, lang=lang)
+
+ return self._get_post("INSERT INTO blog(title, slug, text, html, lang, author_uid, tags) \
+ VALUES(%s, %s, %s, %s, %s, %s, %s) RETURNING *", title, self._make_slug(title), text,
+ html, lang, author.uid, list(tags))
def _make_slug(self, s):
# Remove any non-ASCII characters
return slug
+ def _render_text(self, text, lang="markdown"):
+ if lang == "markdown":
+ return markdown.markdown(text,
+ extensions=[
+ wiki.PrettyLinksExtension(),
+ "codehilite",
+ "fenced_code",
+ "footnotes",
+ "nl2br",
+ "sane_lists",
+ "tables",
+ "toc",
+ ])
+
+ elif lang == "textile":
+ return textile.textile(text)
+
+ else:
+ return text
+
def refresh(self):
"""
Needs to be called after a post has been changed
for row in res:
yield row.year
- def update_feeds(self):
+ async def announce(self):
+ posts = self._get_posts("SELECT * FROM blog \
+ WHERE (published_at IS NOT NULL AND published_at <= NOW()) \
+ AND announced_at IS NULL")
+
+ for post in posts:
+ await post.announce()
+
+ async def update_feeds(self):
"""
Updates all enabled feeds
"""
# Title
- def get_title(self):
+ @property
+ def 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
- # XXX needs caching
- @property
+ @lazy_property
def author(self):
if self.data.author_uid:
return self.backend.accounts.get_by_uid(self.data.author_uid)
def created_at(self):
return self.data.created_at
+ @property
+ def lang(self):
+ return self.data.lang
+
# Published?
@property
"""
return self.published_at and self.published_at <= datetime.datetime.now()
- def publish(self):
+ def publish(self, when=None):
if self.is_published():
return
- self.db.execute("UPDATE blog SET published_at = NOW() \
- WHERE id = %s", self.id)
+ self.db.execute("UPDATE blog SET published_at = COALESCE(%s, CURRENT_TIMESTAMP) \
+ WHERE id = %s", when, self.id)
# Update search indices
self.backend.blog.refresh()
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):
+ @property
+ def 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
+ @lazy_property
def html(self):
"""
Returns this post as rendered HTML
"""
- return self.data.html or textile.textile(self.text.decode("utf-8"))
+ return self.data.html or self.backend.blog._render_text(self.text, lang=self.lang)
+
+ @lazy_property
+ def plaintext(self):
+ h = html2text.HTML2Text()
+ h.ignore_links = True
+
+ return h.handle(self.html)
+
+ # Excerpt
+
+ @property
+ def excerpt(self):
+ paragraphs = self.plaintext.split("\n\n")
+
+ excerpt = []
+
+ for paragraph in paragraphs:
+ excerpt.append(paragraph)
+
+ # Add another paragraph if we encountered a headline
+ if paragraph.startswith("#"):
+ continue
+
+ # End if this paragraph was long enough
+ if len(paragraph) >= 40:
+ break
+
+ return "\n\n".join(excerpt)
# Tags
- def get_tags(self):
+ @property
+ def 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)
+ # Link
@property
def link(self):
return self.data.link
- # XXX needs caching
- @property
+ @lazy_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)
+
+ def is_editable(self, user):
+ # Anonymous users cannot do anything
+ if not user:
+ return False
+
+ # Admins can edit anything
+ if user.is_admin():
+ return True
+
+ # User must have permission for the blog
+ if not user.is_blog_author():
+ return False
+
+ # Authors can edit their own posts
+ return self.author == user
+
+ def update(self, title, text, tags=[]):
+ """
+ Called to update the content of this post
+ """
+ # Update slug when post isn't published yet
+ slug = self.backend.blog._make_slug(title) \
+ if not self.is_published() and not self.title == title else self.slug
+
+ # Render and cache HTML
+ html = self.backend.blog._render_text(text, lang=self.lang)
+
+ self.db.execute("UPDATE blog SET title = %s, slug = %s, text = %s, html = %s, \
+ tags = %s, updated_at = CURRENT_TIMESTAMP WHERE id = %s",
+ title, slug, text, html, list(tags), self.id)
+
+ # Update cache
+ self.data.update({
+ "title" : title,
+ "slug" : slug,
+ "text" : text,
+ "html" : html,
+ "tags" : tags,
+ })
+
+ # Update search index if post is published
+ if self.is_published():
+ self.backend.blog.refresh()
+
+ def delete(self):
+ self.db.execute("DELETE FROM blog WHERE id = %s", self.id)
+
+ # Update search indices
+ self.backend.blog.refresh()
+
+ async def announce(self):
+ # Get people who should receive this message
+ group = self.backend.groups.get_by_gid("promotional-consent")
+ if not group:
+ return
+
+ with self.db.transaction():
+ # Generate an email for everybody in this group
+ for account in group:
+ self.backend.messages.send_template("blog/messages/announcement",
+ account=account, post=self)
+
+ # Mark this post as announced
+ self.db.execute("UPDATE blog SET announced_at = CURRENT_TIMESTAMP \
+ WHERE id = %s", self.id)