]> git.ipfire.org Git - people/shoehn/ipfire.org.git/blob - webapp/backend/news.py
Major update of the webapp.
[people/shoehn/ipfire.org.git] / webapp / backend / news.py
1 #!/usr/bin/python
2
3 from misc import Object
4
5 class News(Object):
6 def get(self, uuid, lang="en"):
7 return self.db.get("SELECT * FROM news WHERE uuid = %s AND lang = %s \
8 AND published IS NOT NULL AND published <= NOW()", uuid, lang)
9
10 def get_by_slug(self, slug):
11 return self.db.get("SELECT * FROM news WHERE slug = %s \
12 AND published IS NOT NULL AND published <= NOW()", slug)
13
14 def get_latest(self, author=None, locale=None, limit=1, offset=0):
15 query = "SELECT * FROM news WHERE published IS NOT NULL AND published <= NOW()"
16 args = []
17
18 if author:
19 query += " AND author_id = %s"
20 args.append(author)
21
22 if locale:
23 query += " AND lang = %s"
24 args.append(locale.code[:2])
25
26 query += " ORDER BY published DESC"
27
28 if limit:
29 query += " LIMIT %s"
30 args.append(limit)
31
32 if offset:
33 query += " OFFSET %s"
34 args.append(offset)
35
36 return self.db.query(query, *args)
37
38 def get_by_year(self, year, locale=None):
39 query = "SELECT * FROM news WHERE published IS NOT NULL AND published <= NOW() \
40 AND EXTRACT(YEAR FROM published) = %s"
41 args = [year,]
42
43 if locale:
44 query += " AND lang = %s"
45 args.append(locale.code[:2])
46
47 query += " ORDER BY published DESC"
48
49 return self.db.query(query, *args)
50
51 @property
52 def years(self):
53 query = self.db.query("SELECT DISTINCT EXTRACT(YEAR FROM published)::integer AS year FROM news \
54 WHERE published IS NOT NULL AND published <= NOW() ORDER BY year DESC")
55
56 return [r.year for r in query]