]> git.ipfire.org Git - ipfire.org.git/blob - webapp/backend/planet.py
c4f432075bacc20b6ddbf764cf44fad86fd87349
[ipfire.org.git] / webapp / backend / planet.py
1 #!/usr/bin/python
2
3 import datetime
4 import re
5 import textile
6 import unicodedata
7
8 from misc import Object
9
10 class PlanetEntry(Object):
11 def __init__(self, backend, data):
12 Object.__init__(self, backend)
13
14 self.data = data
15
16 @property
17 def id(self):
18 return self.data.id
19
20 @property
21 def slug(self):
22 return self.data.slug
23
24 def set_title(self, title):
25 if self.title == title:
26 return
27
28 self.db.execute("UPDATE planet SET title = %s WHERE id = %s", title, self.id)
29 self.data["title"] = title
30
31 title = property(lambda s: s.data.title, set_title)
32
33 @property
34 def url(self):
35 return "http://planet.ipfire.org/post/%s" % self.slug
36
37 def set_published(self, published):
38 if self.published == published:
39 return
40
41 self.db.execute("UPDATE planet SET published = %s WHERE id = %s",
42 published, self.id)
43 self.data["published"] = published
44
45 published = property(lambda s: s.data.published, set_published)
46
47 @property
48 def year(self):
49 return self.published.year
50
51 @property
52 def month(self):
53 return self.published.month
54
55 @property
56 def updated(self):
57 return self.data.updated
58
59 def get_markdown(self):
60 return self.data.markdown
61
62 def set_markdown(self, markdown):
63 if self.markdown == markdown:
64 return
65
66 markup = self.render(markdown)
67 self.db.execute("UPDATE planet SET markdown = %s, markup = %s WHERE id = %s",
68 markdown, markup, self.id)
69
70 self.data.update({
71 "markdown" : markdown,
72 "markup" : markup,
73 })
74
75 markdown = property(get_markdown, set_markdown)
76
77 @property
78 def markup(self):
79 if self.data.markup:
80 return self.data.markup
81
82 return self.render(self.markdown)
83
84 @property
85 def abstract(self):
86 return self.render(self.markdown, 400)
87
88 def render(self, text, limit=0):
89 return self.planet.render(text, limit)
90
91 @property
92 def text(self):
93 # Compat for markup
94 return self.markup
95
96 @property
97 def author(self):
98 if not hasattr(self, "_author"):
99 self._author = self.accounts.get_by_uid(self.data.author_id)
100
101 return self._author
102
103 def set_status(self, status):
104 if self.status == status:
105 return
106
107 self.db.execute("UPDATE planet SET status = %s WHERE id = %s", status, self.id)
108 self.data["status"] = status
109
110 status = property(lambda s: s.data.status, set_status)
111
112 def is_draft(self):
113 return self.status == "draft"
114
115 def is_published(self):
116 return self.status == "published"
117
118 def increase_view_counter(self):
119 self.db.execute("UPDATE planet SET views = views + 1 WHERE id = %s", self.id)
120
121
122 class Planet(Object):
123 def get_authors(self):
124 query = self.db.query("SELECT DISTINCT author_id FROM planet WHERE status = %s \
125 AND published IS NOT NULL AND published <= NOW()", "published")
126
127 authors = []
128 for author in query:
129 author = self.accounts.search(author.author_id)
130 if author:
131 authors.append(author)
132
133 return sorted(authors)
134
135 def get_years(self):
136 res = self.db.query("SELECT DISTINCT EXTRACT(YEAR FROM published)::integer AS year \
137 FROM planet WHERE status = %s ORDER BY year DESC", "published")
138
139 return [row.year for row in res]
140
141 def get_entry_by_slug(self, slug):
142 entry = self.db.get("SELECT * FROM planet WHERE slug = %s", slug)
143
144 if entry:
145 return PlanetEntry(self.backend, entry)
146
147 def get_entry_by_id(self, id):
148 entry = self.db.get("SELECT * FROM planet WHERE id = %s", id)
149
150 if entry:
151 return PlanetEntry(self.backend, entry)
152
153 def get_entries(self, limit=3, offset=None, status="published", author_id=None):
154 query = "SELECT * FROM planet"
155 args, clauses = [], []
156
157 if status:
158 clauses.append("status = %s")
159 args.append(status)
160
161 if status == "published":
162 clauses.append("published <= NOW()")
163
164 if author_id:
165 clauses.append("author_id = %s")
166 args.append(author_id)
167
168 if clauses:
169 query += " WHERE %s" % " AND ".join(clauses)
170
171 query += " ORDER BY published DESC"
172
173 # Respect limit and offset
174 if limit:
175 query += " LIMIT %s"
176 args.append(limit)
177
178 if offset:
179 query += " OFFSET %s"
180 args.append(offset)
181
182 entries = []
183 for entry in self.db.query(query, *args):
184 entry = PlanetEntry(self.backend, entry)
185 entries.append(entry)
186
187 return entries
188
189 def get_entries_by_author(self, author_id, limit=None, offset=None):
190 return self.get_entries(limit=limit, offset=offset, author_id=author_id)
191
192 def get_entries_by_year(self, year):
193 entries = self.db.query("SELECT * FROM planet \
194 WHERE status = %s AND EXTRACT(YEAR FROM published) = %s \
195 ORDER BY published DESC", "published", year)
196
197 return [PlanetEntry(self.backend, e) for e in entries]
198
199 def render(self, text, limit=0):
200 if limit and len(text) >= limit:
201 text = text[:limit] + "..."
202
203 return textile.textile(text)
204
205 def _generate_slug(self, title):
206 slug = unicodedata.normalize("NFKD", title).encode("ascii", "ignore")
207 slug = re.sub(r"[^\w]+", " ", slug)
208 slug = "-".join(slug.lower().strip().split())
209
210 if not slug:
211 slug = "entry"
212
213 while True:
214 e = self.db.get("SELECT * FROM planet WHERE slug = %s", slug)
215 if not e:
216 break
217 slug += "-"
218
219 return slug
220
221 def create(self, title, markdown, author, status="published", published=None):
222 slug = self._generate_slug(title)
223 markup = self.render(markdown)
224
225 if published is None:
226 published = datetime.datetime.utcnow()
227
228 id = self.db.execute("INSERT INTO planet(author_id, slug, title, status, \
229 markdown, markup, published) VALUES(%s, %s, %s, %s, %s, %s, %s) RETURNING id",
230 author.uid, slug, title, status, markdown, markup, published)
231
232 if id:
233 return self.get_entry_by_id(id)
234
235 def update_entry(self, entry):
236 self.db.execute("UPDATE planet SET title = %s, markdown = %s WHERE id = %s",
237 entry.title, entry.markdown, entry.id)
238
239 def save_entry(self, entry):
240 slug = self._generate_slug(entry.title)
241
242 id = self.db.execute("INSERT INTO planet(author_id, title, slug, markdown, published) \
243 VALUES(%s, %s, %s, %s, NOW())", entry.author.uid, entry.title, slug, entry.markdown)
244
245 return id
246
247 def search(self, what):
248 res = self.db.query("WITH \
249 q AS (SELECT plainto_tsquery(%s, %s) AS query), \
250 ranked AS (SELECT id, query, ts_rank_cd(to_tsvector(%s, markdown), query) AS rank \
251 FROM planet, q WHERE markdown @@ query ORDER BY rank DESC) \
252 SELECT *, ts_headline(markup, ranked.query, 'MinWords=100, MaxWords=110') AS markup FROM planet \
253 JOIN ranked ON planet.id = ranked.id \
254 WHERE status = %s AND published IS NOT NULL AND published <= NOW() \
255 ORDER BY ranked DESC LIMIT 10",
256 "english", what, "english", "published")
257
258 return [PlanetEntry(self.backend, e) for e in res]