1 # -*- coding: utf-8 -*-
4 ## This file is part of CDS Indico.
5 ## Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 CERN.
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
):
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.
43 _newsTypes
= [ ("general", L_("General News") ), ("future", L_("Coming soon") ),
44 ("new", L_("New features") ), ("bug", L_("Bug fixes") ) ]
49 self
._newsCounter
= Counter()
50 # days to keep a 'New' tag on recent news items
52 # fetch all news from the previous location.
54 NewsItem(_("Previous news"),
55 HelperMaKaCInfo
.getMaKaCInfoInstance().getNews(),
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
)
66 def removeNewsItem(self
, id):
67 item
= self
.getNewsItemById(id)
69 self
._newsItems
.remove(item
)
72 raise Exception(_("News item does not exist"))
74 def getNewsItemById(self
, id):
75 for new
in self
._newsItems
:
80 def getRecentDays(self
):
81 if not hasattr(self
, '_recentDays'):
83 return self
._recentDays
85 def setRecentDays(self
, recentDays
):
86 self
._recentDays
= recentDays
89 def getNewsTypes(self
):
90 return NewsModule
._newsTypes
93 def getNewsTypesAsDict(self
):
94 return dict(NewsModule
._newsTypes
)
97 class NewsItem(Persistent
, Fossilizable
):
99 fossilizes(INewsItemFossil
)
101 def __init__(self
, title
="", content
="", type=""):
103 self
._creationDate
= nowutc()
104 self
._content
= content
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
):
126 def setContent(self
, content
):
127 self
._content
= content
132 def setTitle(self
, title
):
138 def setType(self
, type):
141 def getHumanReadableType(self
):
142 return NewsModule
.getNewsTypesAsDict()[self
._type
]
145 newsModule
= ModuleHolder().getById("news")
146 return self
.getCreationDate() + \
147 timedelta(days
=newsModule
.getRecentDays()) > nowutc()