downloads: Fix lists.
[people/shoehn/ipfire.org.git] / www / webapp / backend / releases.py
CommitLineData
940227cb
MT
1#!/usr/bin/python
2
3import logging
4import urlparse
5
6from databases import Databases
7from misc import Singleton
8from settings import Settings
9
10class File(object):
11 def __init__(self, release, id):
12 self.release = release
13
14 # get all data from database
15 self.__data = self.db.get("SELECT * FROM files WHERE id = %s", id)
16
17 @property
18 def db(self):
19 return self.release.db
20
21 @property
22 def type(self):
23 return self.__data.get("filetype")
24
25 @property
26 def url(self):
27 baseurl = Settings().get("download_url")
28
29 return urlparse.urljoin(baseurl, self.filename)
30
31 @property
32 def desc(self):
33 _ = lambda x: x
34
35 descriptions = {
36 "iso" : _("Installable CD image"),
37 "torrent" : _("Torrent file"),
38 "flash" : _("Flash image"),
39 "alix" : _("Alix image"),
40 "usbfdd" : _("USB FDD Image"),
41 "usbhdd" : _("USB HDD Image"),
42 "xen" : _("Pregenerated Xen image"),
43 }
44
45 try:
46 return descriptions[self.type]
47 except KeyError:
48 return _("Unknown image type")
49
50 @property
51 def prio(self):
52 priorities = {
53 "iso" : 10,
54 "torrent" : 20,
55 "flash" : 40,
56 "alix" : 41,
57 "usbfdd" : 31,
58 "usbhdd" : 30,
59 "xen" : 50,
60 }
61
62 try:
63 return priorities[self.type]
64 except KeyError:
65 return 999
66
67 @property
68 def rem(self):
69 _ = lambda x: x
70
71 remarks = {
72 "iso" : _("Use this image to burn a CD and install IPFire from it."),
73 "torrent" : _("Download the CD image from the torrent network."),
74 "flash" : _("An image that is meant to run on embedded devices."),
75 "alix" : _("Flash image where a serial console is enabled by default."),
76 "usbfdd" : _("Install IPFire from a floppy-formated USB key."),
77 "usbhdd" : _("If the floppy image doesn't work, use this image instead."),
78 "xen" : _("A ready-to-run image for Xen."),
79 }
80
81 try:
82 return remarks[self.type]
83 except KeyError:
84 return _("Unknown image type")
85
86 @property
87 def sha1(self):
88 return self.__data.get("sha1")
89
90 @property
91 def filename(self):
92 return self.__data.get("filename")
93
94
95class Release(object):
96 @property
97 def db(self):
98 return Releases().db
99
100 def __init__(self, id):
101 self.id = id
102
103 # get all data from database
104 self.__data = \
105 self.db.get("SELECT * FROM releases WHERE id = %s", self.id)
106 assert self.__data
107
108 self.__files = []
109
110 def __repr__(self):
111 return "<%s %s>" % (self.__class__.__name__, self.name)
112
113 @property
114 def files(self):
115 if not self.__files:
116 files = self.db.query("SELECT id FROM files WHERE releases = %s \
117 AND loadable = 'Y'", self.id)
118
119 self.__files = [File(self, f.id) for f in files]
120 self.__files.sort(lambda a, b: cmp(a.prio, b.prio))
121
122 return self.__files
123
124 @property
125 def name(self):
126 return self.__data.get("name")
127
128 @property
129 def stable(self):
130 return self.__data.get("stable")
131
132 @property
133 def published(self):
134 return self.__data.get("published")
135
136 @property
137 def date(self):
138 return self.__data.get("date")
139
140 @property
141 def torrent_hash(self):
142 h = self.__data.get("torrent_hash")
143 if h:
144 return h.lower()
145
146 def get_file(self, type):
147 for file in self.files:
148 if file.type == type:
149 return file
150
151
152class Releases(object):
153 __metaclass__ = Singleton
154
155 @property
156 def db(self):
157 return Databases().webapp
158
159 def list(self):
160 return [Release(r.id) for r in self.db.query("SELECT id FROM releases ORDER BY date DESC")]
161
162 def get_by_id(self, id):
163 id = int(id)
164 if id in [r.id for r in self.db.query("SELECT id FROM releases")]:
165 return Release(id)
166
167 def get_latest(self, stable=1):
168 query = "SELECT id FROM releases WHERE published='Y' AND"
169 if stable:
170 query += " stable='Y'"
171 else:
172 query += " stable='N'"
173
174 query += " ORDER BY date DESC LIMIT 1"
175
176 release = self.db.get(query)
177 if release:
178 return Release(release.id)
179
180 def get_stable(self):
181 releases = self.db.query("""SELECT id FROM releases
182 WHERE published='Y' AND stable='Y'
183 ORDER BY date DESC""")
184
185 return [Release(r.id) for r in releases]
186
187 def get_unstable(self):
188 releases = self.db.query("""SELECT id FROM releases
189 WHERE published='Y' AND stable='N'
190 ORDER BY date DESC""")
191
192 return [Release(r.id) for r in releases]
193
194 def get_all(self):
195 releases = self.db.query("""SELECT id FROM releases
196 WHERE published='Y' ORDER BY date DESC""")
197
198 return [Release(r.id) for r in releases]
199
200
201if __name__ == "__main__":
202 r = Releases()
203
204 for release in r.get_all():
205 print release.name
206
207 print r.get_latest()