]>
git.ipfire.org Git - ipfire.org.git/blob - src/backend/blog.py
12 # Used to automatically link some things
15 (re
.compile(r
"(?:#(\d+))", re
.I
), r
"https://bugzilla.ipfire.org/show_bug.cgi?id=\1"),
18 (re
.compile(r
"([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)"), r
"mailto:\1"),
21 (re
.compile(r
"(?:CVE)[\s\-](\d{4}\-\d+)"), r
"http://cve.mitre.org/cgi-bin/cvename.cgi?name=\1"),
24 class Blog(misc
.Object
):
25 def _get_post(self
, query
, *args
):
26 res
= self
.db
.get(query
, *args
)
29 return Post(self
.backend
, res
.id, data
=res
)
31 def _get_posts(self
, query
, *args
):
32 res
= self
.db
.query(query
, *args
)
35 yield Post(self
.backend
, row
.id, data
=row
)
37 def get_by_id(self
, id):
38 return self
._get
_post
("SELECT * FROM blog \
41 def get_by_slug(self
, slug
, published
=True):
43 return self
._get
_post
("SELECT * FROM blog \
44 WHERE slug = %s AND published_at <= NOW()", slug
)
46 return self
._get
_post
("SELECT * FROM blog \
47 WHERE slug = %s", slug
)
49 def get_newest(self
, limit
=None):
50 return self
._get
_posts
("SELECT * FROM blog \
51 WHERE published_at IS NOT NULL \
52 AND published_at <= NOW() \
53 ORDER BY published_at DESC LIMIT %s", limit
)
55 def get_by_tag(self
, tag
, limit
=None):
56 return self
._get
_posts
("SELECT * FROM blog \
57 WHERE published_at IS NOT NULL \
58 AND published_at <= NOW() \
60 ORDER BY published_at DESC LIMIT %s", tag
, limit
)
62 def get_by_author(self
, author
, limit
=None):
63 return self
._get
_posts
("SELECT * FROM blog \
64 WHERE (author = %s OR author_uid = %s) \
65 AND published_at IS NOT NULL \
66 AND published_at <= NOW() \
67 ORDER BY published_at DESC LIMIT %s",
68 author
.name
, author
.uid
, limit
)
70 def get_by_year(self
, year
):
71 return self
._get
_posts
("SELECT * FROM blog \
72 WHERE EXTRACT(year FROM published_at) = %s \
73 AND published_at IS NOT NULL \
74 AND published_at <= NOW() \
75 ORDER BY published_at DESC", year
)
77 def get_drafts(self
, author
=None, limit
=None):
79 return self
._get
_posts
("SELECT * FROM blog \
80 WHERE author_uid = %s \
81 AND (published_at IS NULL OR published_at > NOW()) \
82 ORDER BY COALESCE(updated_at, created_at) DESC LIMIT %s",
85 return self
._get
_posts
("SELECT * FROM blog \
86 WHERE (published_at IS NULL OR published_at > NOW()) \
87 ORDER BY COALESCE(updated_at, created_at) DESC LIMIT %s", limit
)
89 def search(self
, query
, limit
=None):
90 return self
._get
_posts
("SELECT blog.* FROM blog \
91 LEFT JOIN blog_search_index search_index ON blog.id = search_index.post_id \
92 WHERE search_index.document @@ to_tsquery('english', %s) \
93 ORDER BY ts_rank(search_index.document, to_tsquery('english', %s)) DESC \
94 LIMIT %s", query
, query
, limit
)
96 def create_post(self
, title
, text
, author
, tags
=[], lang
="markdown"):
98 Creates a new post and returns the resulting Post object
101 html
= self
._render
_text
(text
, lang
=lang
)
103 return self
._get
_post
("INSERT INTO blog(title, slug, text, html, lang, author_uid, tags) \
104 VALUES(%s, %s, %s, %s, %s, %s, %s) RETURNING *", title
, self
._make
_slug
(title
), text
,
105 html
, lang
, author
.uid
, list(tags
))
107 def _make_slug(self
, s
):
108 # Remove any non-ASCII characters
110 s
= unicodedata
.normalize("NFKD", s
)
114 # Remove excessive whitespace
115 s
= re
.sub(r
"[^\w]+", " ", s
)
117 slug
= "-".join(s
.split()).lower()
120 e
= self
.db
.get("SELECT 1 FROM blog WHERE slug = %s", slug
)
128 def _render_text(self
, text
, lang
="markdown"):
129 if lang
== "markdown":
130 return markdown2
.markdown(text
, link_patterns
=link_patterns
,
131 extras
=["footnotes", "link-patterns", "wiki-tables"])
133 elif lang
== "textile":
134 return textile
.textile(text
)
140 Needs to be called after a post has been changed
141 and updates the search index.
143 self
.db
.execute("REFRESH MATERIALIZED VIEW blog_search_index")
147 res
= self
.db
.query("SELECT DISTINCT EXTRACT(year FROM published_at)::integer AS year \
148 FROM blog WHERE published_at IS NOT NULL AND published_at <= NOW() \
154 def update_feeds(self
):
156 Updates all enabled feeds
158 for feed
in self
.db
.query("SELECT * FROM blog_feeds WHERE enabled IS TRUE"):
160 f
= feedparser
.parse(feed
.url
)
161 except Exception as e
:
164 with self
.db
.transaction():
166 self
.db
.execute("UPDATE blog_feeds SET name = %s \
167 WHERE id = %s", f
.feed
.title
, feed
.id)
169 # Walk through all entries
170 for entry
in f
.entries
:
171 # Skip everything without the "blog.ipfire.org" tag
173 tags
= list((t
.term
for t
in entry
.tags
))
175 if not "blog.ipfire.org" in tags
:
177 except AttributeError:
180 # Get link to the posting site
181 link
= entry
.links
[0].href
183 # Check if the entry has already been imported
184 res
= self
.db
.get("SELECT id, (updated_at < %s) AS needs_update \
185 FROM blog WHERE feed_id = %s AND foreign_id = %s",
186 entry
.updated
, feed
.id, entry
.id)
188 # If the post needs to be updated, we do so
190 self
.db
.execute("UPDATE blog SET title = %s, author = %s, \
191 published_at = %s, updated_at = %s, html = %s, link = %s, \
192 tags = %s WHERE id = %s", entry
.title
, entry
.author
,
193 entry
.published
, entry
.updated
, entry
.summary
, link
,
194 feed
.tags
+ tags
, res
.id)
199 # Insert the new post
200 self
.db
.execute("INSERT INTO blog(title, slug, author, \
201 published_at, html, link, tags, updated_at, feed_id, foreign_id) \
202 VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
203 entry
.title
, self
._make
_slug
(entry
.title
), entry
.author
,
204 entry
.published
, entry
.summary
, link
, feed
.tags
+ tags
,
205 entry
.updated
, feed
.id, entry
.id)
207 # Mark feed as updated
208 self
.db
.execute("UPDATE blog_feeds SET last_updated_at = CURRENT_TIMESTAMP \
209 WHERE id = %s" % feed
.id)
211 # Refresh the search index
212 with self
.db
.transaction():
216 class Post(misc
.Object
):
217 def init(self
, id, data
=None):
225 return self
.data
.title
229 return self
.data
.slug
234 if self
.data
.author_uid
:
235 return self
.backend
.accounts
.get_by_uid(self
.data
.author_uid
)
237 return self
.data
.author
240 def created_at(self
):
241 return self
.data
.created_at
245 return self
.data
.lang
250 def published_at(self
):
251 return self
.data
.published_at
253 def is_published(self
):
255 Returns True if the post is already published
257 return self
.published_at
and self
.published_at
<= datetime
.datetime
.now()
259 def publish(self
, when
=None):
260 if self
.is_published():
263 self
.db
.execute("UPDATE blog SET published_at = COALESCE(%s, CURRENT_TIMESTAMP) \
264 WHERE id = %s", when
, self
.id)
266 # Update search indices
267 self
.backend
.blog
.refresh()
272 def updated_at(self
):
273 return self
.data
.updated_at
279 return self
.data
.text
286 Returns this post as rendered HTML
288 return self
.data
.html
or self
.backend
.blog
._render
_text
(self
.text
, lang
=self
.lang
)
294 return self
.data
.tags
300 return self
.data
.link
305 return self
.backend
.releases
._get
_release
("SELECT * FROM releases \
306 WHERE published IS NOT NULL AND published <= NOW() AND blog_id = %s", self
.id)
308 def is_editable(self
, editor
):
309 # Authors can edit their own posts
310 return self
.author
== editor
312 def update(self
, title
, text
, tags
=[]):
314 Called to update the content of this post
316 # Update slug when post isn't published yet
317 slug
= self
.backend
.blog
._make
_slug
(title
) \
318 if not self
.is_published() and not self
.title
== title
else self
.slug
320 # Render and cache HTML
321 html
= self
.backend
.blog
._render
_text
(text
, lang
=self
.lang
)
323 self
.db
.execute("UPDATE blog SET title = %s, slug = %s, text = %s, html = %s, \
324 tags = %s, updated_at = CURRENT_TIMESTAMP WHERE id = %s",
325 title
, slug
, text
, html
, list(tags
), self
.id)
335 # Update search index if post is published
336 if self
.is_published():
337 self
.backend
.blog
.refresh()