]> git.ipfire.org Git - ipfire.org.git/blob - www/webapp/backend/ads.py
Remove obsolete pakfire CGI scripts.
[ipfire.org.git] / www / webapp / backend / ads.py
1 #!/usr/bin/python
2
3 from __future__ import division
4
5 import datetime
6 import textile
7
8 from databases import Databases
9 from misc import Singleton
10
11 class Advertisements(object):
12 __metaclass__ = Singleton
13
14 @property
15 def db(self):
16 return Databases().webapp
17
18 def get(self, where=None):
19 args = []
20 query = "SELECT * FROM advertisements \
21 WHERE DATE(NOW()) >= date_start AND DATE(NOW()) <= date_end AND published = 'Y'"
22
23 if where:
24 query += " AND `where` = %s"
25 args.append(where)
26
27 query += " ORDER BY RAND() LIMIT 1"
28
29 ad = self.db.get(query, *args)
30 if ad:
31 return Advert(self, ad.id, ad)
32
33
34 class Advert(object):
35 def __init__(self, advertisements, id, data=None):
36 self.advertisements = advertisements
37 self.id = id
38
39 self.__data = data
40
41 @property
42 def db(self):
43 return self.advertisements.db
44
45 @property
46 def data(self):
47 if self.__data is None:
48 self.__data = self.db.get("SELECT * FROM advertisements WHERE id = %s", self.id)
49 assert self.__data
50
51 return self.__data
52
53 @property
54 def company(self):
55 return self.data.company
56
57 @property
58 def text(self):
59 return self.data.text
60
61 @property
62 def url(self):
63 return self.data.url
64
65 @property
66 def who(self):
67 return """<a href="%s" target="_blank">%s</a>""" % (self.url, self.text or self.company)
68
69 def update_impressions(self):
70 self.db.execute("UPDATE advertisements SET impressions = impressions + 1 WHERE id = %s", self.id)