]>
git.ipfire.org Git - ipfire.org.git/blob - src/backend/blog.py
11 class Blog(misc
.Object
):
12 def _get_post(self
, query
, *args
):
13 res
= self
.db
.get(query
, *args
)
16 return Post(self
.backend
, res
.id, data
=res
)
18 def _get_posts(self
, query
, *args
):
19 res
= self
.db
.query(query
, *args
)
22 yield Post(self
.backend
, row
.id, data
=row
)
24 def get_by_id(self
, id):
25 return self
._get
_post
("SELECT * FROM blog \
28 def get_by_slug(self
, slug
, published
=True):
30 return self
._get
_post
("SELECT * FROM blog \
31 WHERE slug = %s AND published_at <= NOW()", slug
)
33 return self
._get
_post
("SELECT * FROM blog \
34 WHERE slug = %s", slug
)
36 def get_newest(self
, limit
=None):
37 return self
._get
_posts
("SELECT * FROM blog \
38 WHERE published_at IS NOT NULL \
39 AND published_at <= NOW() \
40 ORDER BY published_at DESC LIMIT %s", limit
)
42 def get_by_tag(self
, tag
, limit
=None):
43 return self
._get
_posts
("SELECT * FROM blog \
44 WHERE published_at IS NOT NULL \
45 AND published_at <= NOW() \
47 ORDER BY published_at DESC LIMIT %s", tag
, limit
)
49 def get_by_author(self
, author
, limit
=None):
50 return self
._get
_posts
("SELECT * FROM blog \
51 WHERE (author = %s OR author_uid = %s) \
52 AND published_at IS NOT NULL \
53 AND published_at <= NOW() \
54 ORDER BY published_at DESC LIMIT %s",
55 author
.name
, author
.uid
, limit
)
57 def get_by_year(self
, year
):
58 return self
._get
_posts
("SELECT * FROM blog \
59 WHERE EXTRACT(year FROM published_at) = %s \
60 AND published_at IS NOT NULL \
61 AND published_at <= NOW() \
62 ORDER BY published_at DESC", year
)
64 def get_drafts(self
, author
=None, limit
=None):
66 return self
._get
_posts
("SELECT * FROM blog \
67 WHERE author_uid = %s \
68 AND (published_at IS NULL OR published_at > NOW()) \
69 ORDER BY COALESCE(updated_at, created_at) DESC LIMIT %s",
72 return self
._get
_posts
("SELECT * FROM blog \
73 WHERE (published_at IS NULL OR published_at > NOW()) \
74 ORDER BY COALESCE(updated_at, created_at) DESC LIMIT %s", limit
)
76 def search(self
, query
, limit
=None):
77 return self
._get
_posts
("SELECT blog.* FROM blog \
78 LEFT JOIN blog_search_index search_index ON blog.id = search_index.post_id \
79 WHERE search_index.document @@ to_tsquery('english', %s) \
80 ORDER BY ts_rank(search_index.document, to_tsquery('english', %s)) DESC \
81 LIMIT %s", query
, query
, limit
)
83 def create_post(self
, title
, text
, author
, tags
=[]):
85 Creates a new post and returns the resulting Post object
87 return self
._get
_post
("INSERT INTO blog(title, slug, text, author_uid, tags) \
88 VALUES(%s, %s, %s, %s, %s) RETURNING *", title
, self
._make
_slug
(title
),
89 text
, author
.uid
, list(tags
))
91 def _make_slug(self
, s
):
92 # Remove any non-ASCII characters
94 s
= unicodedata
.normalize("NFKD", s
)
98 # Remove excessive whitespace
99 s
= re
.sub(r
"[^\w]+", " ", s
)
101 slug
= "-".join(s
.split()).lower()
104 e
= self
.db
.get("SELECT 1 FROM blog WHERE slug = %s", slug
)
114 Needs to be called after a post has been changed
115 and updates the search index.
117 self
.db
.execute("REFRESH MATERIALIZED VIEW blog_search_index")
121 res
= self
.db
.query("SELECT DISTINCT EXTRACT(year FROM published_at)::integer AS year \
122 FROM blog WHERE published_at IS NOT NULL AND published_at <= NOW() \
128 def update_feeds(self
):
130 Updates all enabled feeds
132 for feed
in self
.db
.query("SELECT * FROM blog_feeds WHERE enabled IS TRUE"):
134 f
= feedparser
.parse(feed
.url
)
135 except Exception as e
:
138 with self
.db
.transaction():
140 self
.db
.execute("UPDATE blog_feeds SET name = %s \
141 WHERE id = %s", f
.feed
.title
, feed
.id)
143 # Walk through all entries
144 for entry
in f
.entries
:
145 # Skip everything without the "blog.ipfire.org" tag
147 tags
= list((t
.term
for t
in entry
.tags
))
149 if not "blog.ipfire.org" in tags
:
151 except AttributeError:
154 # Get link to the posting site
155 link
= entry
.links
[0].href
157 # Check if the entry has already been imported
158 res
= self
.db
.get("SELECT id, (updated_at < %s) AS needs_update \
159 FROM blog WHERE feed_id = %s AND foreign_id = %s",
160 entry
.updated
, feed
.id, entry
.id)
162 # If the post needs to be updated, we do so
164 self
.db
.execute("UPDATE blog SET title = %s, author = %s, \
165 published_at = %s, updated_at = %s, html = %s, link = %s, \
166 tags = %s WHERE id = %s", entry
.title
, entry
.author
,
167 entry
.published
, entry
.updated
, entry
.summary
, link
,
168 feed
.tags
+ tags
, res
.id)
173 # Insert the new post
174 self
.db
.execute("INSERT INTO blog(title, slug, author, \
175 published_at, html, link, tags, updated_at, feed_id, foreign_id) \
176 VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
177 entry
.title
, self
._make
_slug
(entry
.title
), entry
.author
,
178 entry
.published
, entry
.summary
, link
, feed
.tags
+ tags
,
179 entry
.updated
, feed
.id, entry
.id)
181 # Mark feed as updated
182 self
.db
.execute("UPDATE blog_feeds SET last_updated_at = CURRENT_TIMESTAMP \
183 WHERE id = %s" % feed
.id)
185 # Refresh the search index
186 with self
.db
.transaction():
190 class Post(misc
.Object
):
191 def init(self
, id, data
=None):
199 return self
.data
.title
203 return self
.data
.slug
208 if self
.data
.author_uid
:
209 return self
.backend
.accounts
.get_by_uid(self
.data
.author_uid
)
211 return self
.data
.author
214 def created_at(self
):
215 return self
.data
.created_at
220 def published_at(self
):
221 return self
.data
.published_at
223 def is_published(self
):
225 Returns True if the post is already published
227 return self
.published_at
and self
.published_at
<= datetime
.datetime
.now()
230 if self
.is_published():
233 self
.db
.execute("UPDATE blog SET published_at = NOW() \
234 WHERE id = %s", self
.id)
236 # Update search indices
237 self
.backend
.blog
.refresh()
242 def updated_at(self
):
243 return self
.data
.updated_at
249 return self
.data
.text
256 Returns this post as rendered HTML
258 return self
.data
.html
or textile
.textile(self
.text
.decode("utf-8"))
264 return self
.data
.tags
270 return self
.data
.link
275 return self
.backend
.releases
._get
_release
("SELECT * FROM releases \
276 WHERE published IS NOT NULL AND published <= NOW() AND blog_id = %s", self
.id)
278 def is_editable(self
, editor
):
279 # Authors can edit their own posts
280 return self
.author
== editor
282 def update(self
, title
, text
, tags
=[]):
284 Called to update the content of this post
286 # Update slug when post isn't published yet
287 slug
= self
.backend
.blog
._make
_slug
(title
) \
288 if not self
.is_published() and not self
.title
== title
else self
.slug
292 self
.db
.execute("UPDATE blog SET title = %s, slug = %s, text = %s, \
293 tags = %s, updated_at = CURRENT_TIMESTAMP WHERE id = %s",
294 title
, slug
, text
, list(tags
), self
.id)
304 # Update search index if post is published
305 if self
.is_published():
306 self
.backend
.blog
.refresh()