comparison src/server/pages_tools.py @ 1068:5e809a49971c

pages: new pages_tools module: this module will have helper methods which are common but not common enough to be in the main LiberviaPage class. A first method retrieve comments and convert them to data objects.
author Goffi <goffi@goffi.org>
date Sun, 18 Mar 2018 11:34:01 +0100
parents
children cdd389ef97bc
comparison
equal deleted inserted replaced
1067:808ec98de8b3 1068:5e809a49971c
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Libervia: a Salut à Toi frontend
5 # Copyright (C) 2011-2018 Jérôme Poisson <goffi@goffi.org>
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 """Helper methods for common operations on pages"""
21
22 from sat.core.i18n import _
23 from libervia.server.constants import Const as C
24 from twisted.internet import defer
25 from sat.core.log import getLogger
26 log = getLogger(__name__)
27 from sat.tools.common import data_objects
28
29
30 def commentsDataToObjects(comments_data):
31 return data_objects.BlogItems(comments_data)
32
33
34 def retrieveComments(self, service, node, profile, pass_exceptions=True):
35 """Retrieve comments from server and convert them to data objects
36
37 @param service(unicode): service holding the comments
38 @param node(unicode): node to retrieve
39 @param profile(unicode): profile of the user willing to find comments
40 @param pass_exceptions(bool): if True bridge exceptions will be ignored but logged
41 else exception will be raised
42 """
43 try:
44 d = self.host.bridgeCall(u'mbGet',
45 service,
46 node,
47 C.NO_LIMIT,
48 [],
49 {},
50 profile)
51 except Exception as e:
52 if not pass_exceptions:
53 raise e
54 else:
55 log.warning(_(u"Can't get comments at {node} (service: {service}): {msg}").format(
56 service=service,
57 node=node,
58 msg=e))
59 return defer.succeed([])
60
61 d.addCallback(commentsDataToObjects)
62 return d