]>
Commit | Line | Data |
---|---|---|
940227cb MT |
1 | #!/usr/bin/python |
2 | ||
3 | from databases import Databases | |
4 | from misc import Singleton | |
5 | ||
6 | class News(object): | |
7 | __metaclass__ = Singleton | |
8 | ||
9 | @property | |
10 | def db(self): | |
11 | return Databases().webapp | |
12 | ||
13 | def list(self): | |
14 | return [i.uuid for i in self.db.query("SELECT DISTINCT uuid FROM news ORDER BY date")] | |
15 | ||
16 | def get(self, uuid, lang="en"): | |
17 | return self.db.get("SELECT * FROM news WHERE uuid=%s AND lang=%s", | |
18 | uuid, lang) | |
19 | ||
20 | def get_by_slug(self, slug): | |
21 | return self.db.get("SELECT * FROM news WHERE slug=%s", slug) | |
22 | ||
23 | def get_latest(self, author=None, locale=None, limit=1, offset=0): | |
940227cb MT |
24 | query = "SELECT * FROM news WHERE published='Y'" |
25 | ||
26 | if author: | |
27 | query += " AND author_id='%s'" % author | |
28 | ||
29 | if locale: | |
30 | query += " AND lang='%s'" % locale.code[:2] | |
31 | ||
32 | query += " ORDER BY date DESC" | |
33 | ||
34 | if limit: | |
de683d7c MT |
35 | if offset: |
36 | query += " LIMIT %d,%d" % (offset, limit) | |
37 | else: | |
38 | query += " LIMIT %d" % limit | |
940227cb MT |
39 | |
40 | news = self.db.query(query) | |
41 | ||
940227cb MT |
42 | return news |
43 | ||
7771acea MT |
44 | def get_by_year(self, year, locale=None): |
45 | query = "SELECT * FROM news WHERE published = 'Y' AND YEAR(date) = %s" | |
46 | args = [year,] | |
47 | ||
48 | if locale: | |
49 | query += " AND lang = %s" | |
50 | args.append(locale.code[:2]) | |
51 | ||
52 | query += " ORDER BY date DESC" | |
53 | ||
54 | return self.db.query(query, *args) | |
55 | ||
56 | @property | |
57 | def years(self): | |
58 | years = [] | |
59 | ||
60 | for row in self.db.query("SELECT DISTINCT YEAR(date) AS year FROM news \ | |
61 | WHERE published = 'Y' ORDER BY year DESC"): | |
62 | years.append(row.year) | |
63 | ||
64 | return years | |
65 | ||
66 | ||
940227cb MT |
67 | if __name__ == "__main__": |
68 | n = News() | |
69 | ||
70 | print n.list() | |
71 | print n.get_latest() |