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
):
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.
39 _newsTypes
= [ ("general", L_("General News") ), ("future", L_("Coming soon") ),
40 ("new", L_("New features") ), ("bug", L_("Bug fixes") ) ]
45 self
._newsCounter
= Counter()
46 # days to keep a 'New' tag on recent news items
48 # fetch all news from the previous location.
50 NewsItem(_("Previous news"),
51 HelperMaKaCInfo
.getMaKaCInfoInstance().getNews(),
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
)
62 def removeNewsItem(self
, id):
63 item
= self
.getNewsItemById(id)
65 self
._newsItems
.remove(item
)
68 raise Exception(_("News item does not exist"))
70 def getNewsItemById(self
, id):
71 for new
in self
._newsItems
:
76 def getRecentDays(self
):
77 if not hasattr(self
, '_recentDays'):
79 return self
._recentDays
81 def setRecentDays(self
, recentDays
):
82 self
._recentDays
= recentDays
85 def getNewsTypes(self
):
86 return NewsModule
._newsTypes
89 def getNewsTypesAsDict(self
):
90 return dict(NewsModule
._newsTypes
)
93 class NewsItem(Persistent
, Fossilizable
):
95 fossilizes(INewsItemFossil
)
97 def __init__(self
, title
="", content
="", type=""):
99 self
._creationDate
= nowutc()
100 self
._content
= content
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
):
122 def setContent(self
, content
):
123 self
._content
= content
128 def setTitle(self
, title
):
134 def setType(self
, type):
137 def getHumanReadableType(self
):
138 return NewsModule
.getNewsTypesAsDict()[self
._type
]
141 newsModule
= ModuleHolder().getById("news")
142 return self
.getCreationDate() + \
143 timedelta(days
=newsModule
.getRecentDays()) > nowutc()