]>
git.ipfire.org Git - people/shoehn/ipfire.org.git/blob - webapp/backend/planet.py
8 from misc
import Object
10 class PlanetEntry(Object
):
11 def __init__(self
, backend
, data
):
12 Object
.__init
__(self
, backend
)
24 def set_title(self
, title
):
25 if self
.title
== title
:
28 self
.db
.execute("UPDATE planet SET title = %s WHERE id = %s", title
, self
.id)
29 self
.data
["title"] = title
31 title
= property(lambda s
: s
.data
.title
, set_title
)
35 return "http://planet.ipfire.org/post/%s" % self
.slug
37 def set_published(self
, published
):
38 if self
.published
== published
:
41 self
.db
.execute("UPDATE planet SET published = %s WHERE id = %s",
43 self
.data
["published"] = published
45 published
= property(lambda s
: s
.data
.published
, set_published
)
49 return self
.published
.year
53 return self
.published
.month
57 return self
.data
.updated
59 def get_markdown(self
):
60 return self
.data
.markdown
62 def set_markdown(self
, markdown
):
63 if self
.markdown
== markdown
:
66 markup
= self
.render(markdown
)
67 self
.db
.execute("UPDATE planet SET markdown = %s, markup = %s WHERE id = %s",
68 markdown
, markup
, self
.id)
71 "markdown" : markdown
,
75 markdown
= property(get_markdown
, set_markdown
)
80 return self
.data
.markup
82 return self
.render(self
.markdown
)
86 return self
.render(self
.markdown
, 400)
88 def render(self
, text
, limit
=0):
89 return self
.planet
.render(text
, limit
)
98 if not hasattr(self
, "__author"):
99 self
.__author
= self
.accounts
.search(self
.data
.author_id
)
103 def set_status(self
, status
):
104 if self
.status
== status
:
107 self
.db
.execute("UPDATE planet SET status = %s WHERE id = %s", status
, self
.id)
108 self
.data
["status"] = status
110 status
= property(lambda s
: s
.data
.status
, set_status
)
113 return self
.status
== "draft"
115 def is_published(self
):
116 return self
.status
== "published"
121 if not hasattr(self
, "__tags"):
122 res
= self
.db
.query("SELECT tag FROM planet_tags \
123 WHERE post_id = %s ORDER BY tag", self
.id)
126 self
.__tags
.append(row
.tag
)
130 def set_tags(self
, tags
):
131 # Delete all existing tags.
132 self
.db
.execute("DELETE FROM planet_tags WHERE post_id = %s", self
.id)
134 self
.db
.executemany("INSERT INTO planet_tags(post_id, tag) VALUES(%s, %s)",
135 ((self
.id, tag
) for tag
in tags
))
141 tags
= property(get_tags
, set_tags
)
144 class Planet(Object
):
145 def get_authors(self
):
146 query
= self
.db
.query("SELECT DISTINCT author_id FROM planet WHERE status = %s \
147 AND published IS NOT NULL AND published <= NOW()", "published")
151 author
= self
.accounts
.search(author
.author_id
)
153 authors
.append(author
)
155 return sorted(authors
)
158 res
= self
.db
.query("SELECT DISTINCT EXTRACT(YEAR FROM published)::integer AS year \
159 FROM planet WHERE status = %s ORDER BY year DESC", "published")
161 return [row
.year
for row
in res
]
163 def get_entry_by_slug(self
, slug
):
164 entry
= self
.db
.get("SELECT * FROM planet WHERE slug = %s", slug
)
167 return PlanetEntry(self
.backend
, entry
)
169 def get_entry_by_id(self
, id):
170 entry
= self
.db
.get("SELECT * FROM planet WHERE id = %s", id)
173 return PlanetEntry(self
.backend
, entry
)
175 def get_entries(self
, limit
=3, offset
=None, status
="published", author_id
=None):
176 query
= "SELECT * FROM planet"
177 args
, clauses
= [], []
180 clauses
.append("status = %s")
184 clauses
.append("author_id = %s")
185 args
.append(author_id
)
188 query
+= " WHERE %s" % " AND ".join(clauses
)
190 query
+= " ORDER BY published DESC"
192 # Respect limit and offset
198 query
+= " OFFSET %s"
202 for entry
in self
.db
.query(query
, *args
):
203 entry
= PlanetEntry(self
.backend
, entry
)
204 entries
.append(entry
)
208 def get_entries_by_author(self
, author_id
, limit
=None, offset
=None):
209 return self
.get_entries(limit
=limit
, offset
=offset
, author_id
=author_id
)
211 def get_entries_by_year(self
, year
):
212 entries
= self
.db
.query("SELECT * FROM planet \
213 WHERE status = %s AND EXTRACT(YEAR FROM published) = %s \
214 ORDER BY published DESC", "published", year
)
216 return [PlanetEntry(self
.backend
, e
) for e
in entries
]
218 def render(self
, text
, limit
=0):
219 if limit
and len(text
) >= limit
:
220 text
= text
[:limit
] + "..."
222 return textile
.textile(text
)
224 def _generate_slug(self
, title
):
225 slug
= unicodedata
.normalize("NFKD", title
).encode("ascii", "ignore")
226 slug
= re
.sub(r
"[^\w]+", " ", slug
)
227 slug
= "-".join(slug
.lower().strip().split())
233 e
= self
.db
.get("SELECT * FROM planet WHERE slug = %s", slug
)
240 def create(self
, title
, markdown
, author
, status
="published", tags
=None, published
=None):
241 slug
= self
._generate
_slug
(title
)
242 markup
= self
.render(markdown
)
244 if published
is None:
245 published
= datetime
.datetime
.utcnow()
247 id = self
.db
.execute("INSERT INTO planet(author_id, slug, title, status, \
248 markdown, markup, published) VALUES(%s, %s, %s, %s, %s, %s, %s)",
249 author
.uid
, slug
, title
, status
, markdown
, markup
, published
)
251 entry
= self
.get_entry_by_id(id)
258 def update_entry(self
, entry
):
259 self
.db
.execute("UPDATE planet SET title = %s, markdown = %s WHERE id = %s",
260 entry
.title
, entry
.markdown
, entry
.id)
262 def save_entry(self
, entry
):
263 slug
= self
._generate
_slug
(entry
.title
)
265 id = self
.db
.execute("INSERT INTO planet(author_id, title, slug, markdown, published) \
266 VALUES(%s, %s, %s, %s, NOW())", entry
.author
.uid
, entry
.title
, slug
, entry
.markdown
)
270 def search(self
, what
):
274 query
= "SELECT planet.* FROM planet INNER JOIN ( \
275 SELECT post_id FROM planet_tags \
276 INNER JOIN planet ON planet_tags.post_id = planet.id \
277 WHERE %s GROUP BY post_id HAVING COUNT(post_id) = %%s \
278 ) pt ON planet.id = pt.post_id ORDER BY published DESC"
280 args
= (tags
, len(tags
))
282 clauses
, args
= [], tags
284 clauses
.append("planet_tags.tag = %s")
285 args
.append(len(tags
))
287 entries
= self
.db
.query(query
% " OR ".join(clauses
), *args
)
288 return [PlanetEntry(self
.backend
, e
) for e
in entries
]
290 def search_autocomplete(self
, what
):
292 last_tag
= tags
.pop()
294 res
= self
.db
.query("SELECT tag, COUNT(tag) AS count FROM planet_tags \
295 WHERE tag LIKE %s GROUP BY tag ORDER BY count DESC", "%s%%" % last_tag
)
298 return ["%s %s" % (" ".join(tags
), row
.tag
) for row
in res
]
300 return [row
.tag
for row
in res
]