]>
git.ipfire.org Git - 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
.get_by_uid(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"
119 class Planet(Object
):
120 def get_authors(self
):
121 query
= self
.db
.query("SELECT DISTINCT author_id FROM planet WHERE status = %s \
122 AND published IS NOT NULL AND published <= NOW()", "published")
126 author
= self
.accounts
.search(author
.author_id
)
128 authors
.append(author
)
130 return sorted(authors
)
133 res
= self
.db
.query("SELECT DISTINCT EXTRACT(YEAR FROM published)::integer AS year \
134 FROM planet WHERE status = %s ORDER BY year DESC", "published")
136 return [row
.year
for row
in res
]
138 def get_entry_by_slug(self
, slug
):
139 entry
= self
.db
.get("SELECT * FROM planet WHERE slug = %s", slug
)
142 return PlanetEntry(self
.backend
, entry
)
144 def get_entry_by_id(self
, id):
145 entry
= self
.db
.get("SELECT * FROM planet WHERE id = %s", id)
148 return PlanetEntry(self
.backend
, entry
)
150 def get_entries(self
, limit
=3, offset
=None, status
="published", author_id
=None):
151 query
= "SELECT * FROM planet"
152 args
, clauses
= [], []
155 clauses
.append("status = %s")
158 if status
== "published":
159 clauses
.append("published <= NOW()")
162 clauses
.append("author_id = %s")
163 args
.append(author_id
)
166 query
+= " WHERE %s" % " AND ".join(clauses
)
168 query
+= " ORDER BY published DESC"
170 # Respect limit and offset
176 query
+= " OFFSET %s"
180 for entry
in self
.db
.query(query
, *args
):
181 entry
= PlanetEntry(self
.backend
, entry
)
182 entries
.append(entry
)
186 def get_entries_by_author(self
, author_id
, limit
=None, offset
=None):
187 return self
.get_entries(limit
=limit
, offset
=offset
, author_id
=author_id
)
189 def get_entries_by_year(self
, year
):
190 entries
= self
.db
.query("SELECT * FROM planet \
191 WHERE status = %s AND EXTRACT(YEAR FROM published) = %s \
192 ORDER BY published DESC", "published", year
)
194 return [PlanetEntry(self
.backend
, e
) for e
in entries
]
196 def render(self
, text
, limit
=0):
197 if limit
and len(text
) >= limit
:
198 text
= text
[:limit
] + "..."
200 return textile
.textile(text
)
202 def _generate_slug(self
, title
):
203 slug
= unicodedata
.normalize("NFKD", title
).encode("ascii", "ignore")
204 slug
= re
.sub(r
"[^\w]+", " ", slug
)
205 slug
= "-".join(slug
.lower().strip().split())
211 e
= self
.db
.get("SELECT * FROM planet WHERE slug = %s", slug
)
218 def create(self
, title
, markdown
, author
, status
="published", published
=None):
219 slug
= self
._generate
_slug
(title
)
220 markup
= self
.render(markdown
)
222 if published
is None:
223 published
= datetime
.datetime
.utcnow()
225 id = self
.db
.execute("INSERT INTO planet(author_id, slug, title, status, \
226 markdown, markup, published) VALUES(%s, %s, %s, %s, %s, %s, %s) RETURNING id",
227 author
.uid
, slug
, title
, status
, markdown
, markup
, published
)
230 return self
.get_entry_by_id(id)
232 def update_entry(self
, entry
):
233 self
.db
.execute("UPDATE planet SET title = %s, markdown = %s WHERE id = %s",
234 entry
.title
, entry
.markdown
, entry
.id)
236 def save_entry(self
, entry
):
237 slug
= self
._generate
_slug
(entry
.title
)
239 id = self
.db
.execute("INSERT INTO planet(author_id, title, slug, markdown, published) \
240 VALUES(%s, %s, %s, %s, NOW())", entry
.author
.uid
, entry
.title
, slug
, entry
.markdown
)
244 def search(self
, what
):
245 res
= self
.db
.query("WITH \
246 q AS (SELECT plainto_tsquery(%s, %s) AS query), \
247 ranked AS (SELECT id, query, ts_rank_cd(to_tsvector(%s, markdown), query) AS rank \
248 FROM planet, q WHERE markdown @@ query ORDER BY rank DESC) \
249 SELECT *, ts_headline(markup, ranked.query, 'MinWords=100, MaxWords=110') AS markup FROM planet \
250 JOIN ranked ON planet.id = ranked.id \
251 WHERE status = %s AND published IS NOT NULL AND published <= NOW() \
252 ORDER BY ranked DESC LIMIT 10",
253 "english", what
, "english", "published")
255 return [PlanetEntry(self
.backend
, e
) for e
in res
]