[FIX] daterange selector dialog
[cds-indico.git] / indico / modules / news.py
blob76315c3854b13b674c2b520ffb50469a84c7baf7
1 # -*- coding: utf-8 -*-
2 ##
3 ##
4 ## This file is part of CDS Indico.
5 ## Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 CERN.
6 ##
7 ## CDS Indico is free software; you can redistribute it and/or
8 ## modify it under the terms of the GNU General Public License as
9 ## published by the Free Software Foundation; either version 2 of the
10 ## License, or (at your option) any later version.
12 ## CDS Indico is distributed in the hope that it will be useful, but
13 ## WITHOUT ANY WARRANTY; without even the implied warranty of
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ## General Public License for more details.
17 ## You should have received a copy of the GNU General Public License
18 ## along with CDS Indico; if not, write to the Free Software Foundation, Inc.,
19 ## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
21 from datetime import timedelta
22 from persistent import Persistent
23 from MaKaC.common.Counter import Counter
24 from indico.modules import ModuleHolder, Module
25 from MaKaC.common.info import HelperMaKaCInfo
26 from indico.util.i18n import L_
27 from MaKaC.common.timezoneUtils import getAdjustedDate, nowutc, \
28 isTimezoneAware, setAdjustedDate
29 from MaKaC.common.fossilize import Fossilizable, fossilizes
30 from MaKaC.fossils.modules import INewsItemFossil
33 class NewsModule(Module):
34 """
35 This module holds all the information needed to keep the news in Indico.
36 That means all the news items and other related information.
38 _newsTypes is a tuple with 2 values: id, label. Keep the order as it will
39 appear in the display interface.
40 """
42 id = "news"
43 _newsTypes = [ ("general", L_("General News") ), ("future", L_("Coming soon") ),
44 ("new", L_("New features") ), ("bug", L_("Bug fixes") ) ]
46 def __init__(self):
47 self._newsItems = []
48 self._p_changed = 1
49 self._newsCounter = Counter()
50 # days to keep a 'New' tag on recent news items
51 self._recentDays = 14
52 # fetch all news from the previous location.
53 self.addNewsItem(
54 NewsItem(_("Previous news"),
55 HelperMaKaCInfo.getMaKaCInfoInstance().getNews(),
56 "general"))
58 def getNewsItemsList(self):
59 return self._newsItems
61 def addNewsItem(self, news):
62 news.setId(self._newsCounter.newCount())
63 self._newsItems.insert(0, news)
64 self._p_changed = 1
66 def removeNewsItem(self, id):
67 item = self.getNewsItemById(id)
68 if item:
69 self._newsItems.remove(item)
70 self._p_changed = 1
71 else:
72 raise Exception(_("News item does not exist"))
74 def getNewsItemById(self, id):
75 for new in self._newsItems:
76 if new.getId() == id:
77 return new
78 return None
80 def getRecentDays(self):
81 if not hasattr(self, '_recentDays'):
82 self._recentDays = 14
83 return self._recentDays
85 def setRecentDays(self, recentDays):
86 self._recentDays = recentDays
88 @classmethod
89 def getNewsTypes(self):
90 return NewsModule._newsTypes
92 @classmethod
93 def getNewsTypesAsDict(self):
94 return dict(NewsModule._newsTypes)
97 class NewsItem(Persistent, Fossilizable):
99 fossilizes(INewsItemFossil)
101 def __init__(self, title="", content="", type=""):
102 self._id = None
103 self._creationDate = nowutc()
104 self._content = content
105 self._title = title
106 self._type = type
107 self._new = True
109 def getId(self):
110 return self._id
112 def setId(self, id):
113 self._id = id
115 def getCreationDate(self):
116 if not isTimezoneAware(self._creationDate):
117 self._creationDate = setAdjustedDate(self._creationDate, tz = 'UTC')
118 return self._creationDate
120 def getAdjustedCreationDate(self, tz='UTC'):
121 return getAdjustedDate(self.getCreationDate(), tz=tz)
123 def getContent(self):
124 return self._content
126 def setContent(self, content):
127 self._content = content
129 def getTitle(self):
130 return self._title
132 def setTitle(self, title):
133 self._title = title
135 def getType(self):
136 return self._type
138 def setType(self, type):
139 self._type = type
141 def getHumanReadableType(self):
142 return NewsModule.getNewsTypesAsDict()[self._type]
144 def isNew(self):
145 newsModule = ModuleHolder().getById("news")
146 return self.getCreationDate() + \
147 timedelta(days=newsModule.getRecentDays()) > nowutc()