Mercurial > libervia-backend
comparison src/tools/common/data_objects.py @ 2160:e67e8cd24141
core (tools/common): data objects first draft:
this module aims is to help manipulate complex data from bridge, mainly for the template system.
It is in common and not only in frontends as it may be used in some case by backend, if it needs to use template system in the future.
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 21 Feb 2017 21:01:40 +0100 |
parents | |
children | cf6c539672c7 |
comparison
equal
deleted
inserted
replaced
2159:5734b0994cf0 | 2160:e67e8cd24141 |
---|---|
1 #!/usr/bin/env python2 | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SAT: a jabber client | |
5 # Copyright (C) 2009-2016 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 """ Objects handling bridge data, with jinja2 safe markup handling""" | |
21 | |
22 from sat.tools.common import data_format | |
23 try: | |
24 from jinja2 import Markup as safe | |
25 except ImportError: | |
26 safe = unicode | |
27 | |
28 | |
29 class BlogItem(object): | |
30 | |
31 def __init__(self, mb_data): | |
32 self.mb_data = mb_data | |
33 self._tags = None | |
34 self._groups = None | |
35 self._comments = None | |
36 | |
37 @property | |
38 def id(self): | |
39 return self.mb_data.get(u'id') | |
40 | |
41 @property | |
42 def atom_id(self): | |
43 return self.mb_data.get(u'atom_id') | |
44 | |
45 @property | |
46 def published(self): | |
47 return self.mb_data.get(u'published') | |
48 | |
49 @property | |
50 def updated(self): | |
51 return self.mb_data.get(u'updated') | |
52 | |
53 @property | |
54 def author(self): | |
55 return self.mb_data.get(u'author') | |
56 | |
57 @property | |
58 def author_jid(self): | |
59 return self.mb_data.get(u'author_jid') | |
60 | |
61 @property | |
62 def author_jid_verified(self): | |
63 return self.mb_data.get(u'author_jid_verified') | |
64 | |
65 @property | |
66 def author_email(self): | |
67 return self.mb_data.get(u'author_email') | |
68 | |
69 @property | |
70 def tags(self): | |
71 if self._tags is None: | |
72 self._tags = data_format.dict2iter('tag', self.mb_data) | |
73 return self._tags | |
74 | |
75 @property | |
76 def groups(self): | |
77 if self._groups is None: | |
78 self._groups = data_format.dict2iter('group', self.mb_data) | |
79 return self._groups | |
80 | |
81 @property | |
82 def title(self): | |
83 return self.mb_data.get(u'title') | |
84 | |
85 @property | |
86 def title_xhtml(self): | |
87 try: | |
88 return safe(self.mb_data[u'title_xhtml']) | |
89 except KeyError: | |
90 return None | |
91 | |
92 @property | |
93 def content(self): | |
94 return self.mb_data.get(u'content') | |
95 | |
96 @property | |
97 def content_xhtml(self): | |
98 try: | |
99 return safe(self.mb_data[u'content_xhtml']) | |
100 except KeyError: | |
101 return None | |
102 | |
103 @property | |
104 def comments(self): | |
105 if self._comments is None: | |
106 self._comments = data_format.dict2iterdict(u'comments', self.mb_data, (u'node', u'service')) | |
107 return self._comments | |
108 | |
109 @property | |
110 def comments_service(self): | |
111 return self.mb_data.get(u'comments_service') | |
112 | |
113 @property | |
114 def comments_node(self): | |
115 return self.mb_data.get(u'comments_node') | |
116 | |
117 | |
118 class BlogItems(object): | |
119 | |
120 def __init__(self, mb_data): | |
121 self.mb_data = mb_data | |
122 self.items = [BlogItem(i) for i in mb_data[0]] | |
123 self.metadata = mb_data[1] | |
124 | |
125 @property | |
126 def service(self): | |
127 return self.metadata[u'service'] | |
128 | |
129 @property | |
130 def node(self): | |
131 return self.metadata[u'node'] | |
132 | |
133 @property | |
134 def uri(self): | |
135 return self.metadata[u'uri'] | |
136 | |
137 def __len__(self): | |
138 return self.items.__len__() | |
139 | |
140 def __missing__(self, key): | |
141 return self.items.__missing__(key) | |
142 | |
143 def __getitem__(self, key): | |
144 return self.items.__getitem__(key) | |
145 | |
146 def __iter__(self): | |
147 return self.items.__iter__() | |
148 | |
149 def __reversed__(self): | |
150 return self.items.__reversed__() | |
151 | |
152 def __contains__(self, item): | |
153 return self.items.__contains__(item) |