Enable CSRF checks for attachments
[cds-indico.git] / indico / modules / news.py
blobe76212aa3fa642f4f3ad5203d89abda380e0071c
1 # This file is part of Indico.
2 # Copyright (C) 2002 - 2015 European Organization for Nuclear Research (CERN).
4 # Indico is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 3 of the
7 # License, or (at your option) any later version.
9 # Indico is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with Indico; if not, see <http://www.gnu.org/licenses/>.
17 from datetime import timedelta
18 from persistent import Persistent
19 from MaKaC.common.Counter import Counter
20 from indico.modules import ModuleHolder, Module
21 from MaKaC.common.info import HelperMaKaCInfo
22 from indico.util.i18n import L_
23 from MaKaC.common.timezoneUtils import getAdjustedDate, nowutc, \
24 isTimezoneAware, setAdjustedDate
25 from MaKaC.common.fossilize import Fossilizable, fossilizes
26 from MaKaC.fossils.modules import INewsItemFossil
29 class NewsModule(Module):
30 """
31 This module holds all the information needed to keep the news in Indico.
32 That means all the news items and other related information.
34 _newsTypes is a tuple with 2 values: id, label. Keep the order as it will
35 appear in the display interface.
36 """
38 id = "news"
39 _newsTypes = [ ("general", L_("General News") ), ("future", L_("Coming soon") ),
40 ("new", L_("New features") ), ("bug", L_("Bug fixes") ) ]
42 def __init__(self):
43 self._newsItems = []
44 self._p_changed = 1
45 self._newsCounter = Counter()
46 # days to keep a 'New' tag on recent news items
47 self._recentDays = 14
48 # fetch all news from the previous location.
49 self.addNewsItem(
50 NewsItem(_("Previous news"),
51 HelperMaKaCInfo.getMaKaCInfoInstance().getNews(),
52 "general"))
54 def getNewsItemsList(self):
55 return self._newsItems
57 def addNewsItem(self, news):
58 news.setId(self._newsCounter.newCount())
59 self._newsItems.insert(0, news)
60 self._p_changed = 1
62 def removeNewsItem(self, id):
63 item = self.getNewsItemById(id)
64 if item:
65 self._newsItems.remove(item)
66 self._p_changed = 1
67 else:
68 raise Exception(_("News item does not exist"))
70 def getNewsItemById(self, id):
71 for new in self._newsItems:
72 if new.getId() == id:
73 return new
74 return None
76 def getRecentDays(self):
77 if not hasattr(self, '_recentDays'):
78 self._recentDays = 14
79 return self._recentDays
81 def setRecentDays(self, recentDays):
82 self._recentDays = recentDays
84 @classmethod
85 def getNewsTypes(self):
86 return NewsModule._newsTypes
88 @classmethod
89 def getNewsTypesAsDict(self):
90 return dict(NewsModule._newsTypes)
93 class NewsItem(Persistent, Fossilizable):
95 fossilizes(INewsItemFossil)
97 def __init__(self, title="", content="", type=""):
98 self._id = None
99 self._creationDate = nowutc()
100 self._content = content
101 self._title = title
102 self._type = type
103 self._new = True
105 def getId(self):
106 return self._id
108 def setId(self, id):
109 self._id = id
111 def getCreationDate(self):
112 if not isTimezoneAware(self._creationDate):
113 self._creationDate = setAdjustedDate(self._creationDate, tz = 'UTC')
114 return self._creationDate
116 def getAdjustedCreationDate(self, tz='UTC'):
117 return getAdjustedDate(self.getCreationDate(), tz=tz)
119 def getContent(self):
120 return self._content
122 def setContent(self, content):
123 self._content = content
125 def getTitle(self):
126 return self._title
128 def setTitle(self, title):
129 self._title = title
131 def getType(self):
132 return self._type
134 def setType(self, type):
135 self._type = type
137 def getHumanReadableType(self):
138 return NewsModule.getNewsTypesAsDict()[self._type]
140 def isNew(self):
141 newsModule = ModuleHolder().getById("news")
142 return self.getCreationDate() + \
143 timedelta(days=newsModule.getRecentDays()) > nowutc()