]> git.ipfire.org Git - ipfire.org.git/blame - src/backend/news.py
Introduce autotools
[ipfire.org.git] / src / backend / news.py
CommitLineData
940227cb
MT
1#!/usr/bin/python
2
9068dba1 3from misc import Object
940227cb 4
9068dba1 5class News(Object):
2cb93d13
MT
6 def get(self, uuid, locale=None):
7 res = self.db.get("SELECT * FROM news WHERE uuid = %s \
8 AND published IS NOT NULL AND published <= NOW()", uuid)
940227cb 9
2cb93d13
MT
10 if res and locale:
11 res = self._translate_news_one(res, locale=locale)
12
13 return res
14
15 def get_by_id(self, id, locale=None):
16 res = self.db.get("SELECT * FROM news WHERE id = %s \
17 AND published IS NOT NULL AND published <= NOW()", id)
18
19 if res and locale:
20 res = self._translate_news_one(res, locale=locale)
21
22 return res
23
24 def get_by_slug(self, slug, locale=None):
25 res = self.db.get("SELECT * FROM news WHERE slug = %s \
9068dba1 26 AND published IS NOT NULL AND published <= NOW()", slug)
940227cb 27
2cb93d13
MT
28 if res:
29 return res
30
31 res = self.db.get("SELECT news_id FROM news_translations WHERE slug = %s", slug)
32 if res:
33 return self.get_by_id(res.news_id, locale=locale)
34
940227cb 35 def get_latest(self, author=None, locale=None, limit=1, offset=0):
9068dba1
MT
36 query = "SELECT * FROM news WHERE published IS NOT NULL AND published <= NOW()"
37 args = []
940227cb
MT
38
39 if author:
9068dba1
MT
40 query += " AND author_id = %s"
41 args.append(author)
940227cb 42
9068dba1 43 query += " ORDER BY published DESC"
940227cb
MT
44
45 if limit:
9068dba1
MT
46 query += " LIMIT %s"
47 args.append(limit)
940227cb 48
9068dba1
MT
49 if offset:
50 query += " OFFSET %s"
51 args.append(offset)
940227cb 52
2cb93d13
MT
53 news = self.db.query(query, *args)
54
55 if locale:
56 news = self._translate_news(news, locale=locale)
57
58 return news
940227cb 59
7771acea 60 def get_by_year(self, year, locale=None):
2cb93d13
MT
61 res = self.db.query("SELECT * FROM news \
62 WHERE published IS NOT NULL AND published <= NOW() \
63 AND EXTRACT(YEAR FROM published) = %s ORDER BY published DESC", year)
7771acea 64
2cb93d13
MT
65 if res and locale:
66 res = self._translate_news(res, locale=locale)
7771acea 67
2cb93d13
MT
68 return res
69
70 def _translate_news(self, news, locale):
71 ret = []
72
73 for n in news:
74 n = self._translate_news_one(n, locale)
75 ret.append(n)
76
77 return ret
78
79 def _translate_news_one(self, news, locale):
80 lang = locale.code[:2]
81
82 res = self.db.get("SELECT * FROM news_translations \
83 WHERE news_id = %s AND lang = %s", news.id, lang)
84
85 if res:
86 news.update(res)
7771acea 87
2cb93d13 88 return news
7771acea
MT
89
90 @property
91 def years(self):
9068dba1
MT
92 query = self.db.query("SELECT DISTINCT EXTRACT(YEAR FROM published)::integer AS year FROM news \
93 WHERE published IS NOT NULL AND published <= NOW() ORDER BY year DESC")
940227cb 94
9068dba1 95 return [r.year for r in query]