annotate src/tools/common/data_objects.py @ 2228:cf6c539672c7

tools (common/data_objects): comments items can now be chained to their parent
author Goffi <goffi@goffi.org>
date Mon, 17 Apr 2017 20:34:43 +0200
parents e67e8cd24141
children 0ffa8ba0857c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT: a jabber client
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """ Objects handling bridge data, with jinja2 safe markup handling"""
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
21
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.tools.common import data_format
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 try:
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from jinja2 import Markup as safe
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 except ImportError:
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 safe = unicode
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 class BlogItem(object):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 def __init__(self, mb_data):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 self.mb_data = mb_data
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 self._tags = None
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 self._groups = None
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 self._comments = None
2228
cf6c539672c7 tools (common/data_objects): comments items can now be chained to their parent
Goffi <goffi@goffi.org>
parents: 2160
diff changeset
36 self._comments_items_list = None
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 def id(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 return self.mb_data.get(u'id')
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 def atom_id(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 return self.mb_data.get(u'atom_id')
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
45
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 def published(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 return self.mb_data.get(u'published')
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
49
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 def updated(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 return self.mb_data.get(u'updated')
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 def author(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 return self.mb_data.get(u'author')
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 def author_jid(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 return self.mb_data.get(u'author_jid')
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
61
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 def author_jid_verified(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 return self.mb_data.get(u'author_jid_verified')
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 def author_email(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 return self.mb_data.get(u'author_email')
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 def tags(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 if self._tags is None:
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 self._tags = data_format.dict2iter('tag', self.mb_data)
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 return self._tags
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
75
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 def groups(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 if self._groups is None:
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 self._groups = data_format.dict2iter('group', self.mb_data)
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 return self._groups
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
81
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 def title(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 return self.mb_data.get(u'title')
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 def title_xhtml(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 try:
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 return safe(self.mb_data[u'title_xhtml'])
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 except KeyError:
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 return None
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
92
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 def content(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 return self.mb_data.get(u'content')
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
96
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 def content_xhtml(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 try:
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 return safe(self.mb_data[u'content_xhtml'])
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 except KeyError:
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 return None
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
103
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 def comments(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 if self._comments is None:
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 self._comments = data_format.dict2iterdict(u'comments', self.mb_data, (u'node', u'service'))
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 return self._comments
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
109
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 def comments_service(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 return self.mb_data.get(u'comments_service')
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
113
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 def comments_node(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 return self.mb_data.get(u'comments_node')
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
117
2228
cf6c539672c7 tools (common/data_objects): comments items can now be chained to their parent
Goffi <goffi@goffi.org>
parents: 2160
diff changeset
118 @property
cf6c539672c7 tools (common/data_objects): comments items can now be chained to their parent
Goffi <goffi@goffi.org>
parents: 2160
diff changeset
119 def comments_items_list(self):
cf6c539672c7 tools (common/data_objects): comments items can now be chained to their parent
Goffi <goffi@goffi.org>
parents: 2160
diff changeset
120 return [] if self._comments_items_list is None else self._comments_items_list
cf6c539672c7 tools (common/data_objects): comments items can now be chained to their parent
Goffi <goffi@goffi.org>
parents: 2160
diff changeset
121
cf6c539672c7 tools (common/data_objects): comments items can now be chained to their parent
Goffi <goffi@goffi.org>
parents: 2160
diff changeset
122 def appendCommentsItems(self, items):
cf6c539672c7 tools (common/data_objects): comments items can now be chained to their parent
Goffi <goffi@goffi.org>
parents: 2160
diff changeset
123 """append comments items to self.comments_items"""
cf6c539672c7 tools (common/data_objects): comments items can now be chained to their parent
Goffi <goffi@goffi.org>
parents: 2160
diff changeset
124 if self._comments_items_list is None:
cf6c539672c7 tools (common/data_objects): comments items can now be chained to their parent
Goffi <goffi@goffi.org>
parents: 2160
diff changeset
125 self._comments_items_list = []
cf6c539672c7 tools (common/data_objects): comments items can now be chained to their parent
Goffi <goffi@goffi.org>
parents: 2160
diff changeset
126 self._comments_items_list.append(items)
cf6c539672c7 tools (common/data_objects): comments items can now be chained to their parent
Goffi <goffi@goffi.org>
parents: 2160
diff changeset
127
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
128
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 class BlogItems(object):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
130
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 def __init__(self, mb_data):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 self.mb_data = mb_data
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 self.items = [BlogItem(i) for i in mb_data[0]]
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 self.metadata = mb_data[1]
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
135
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 def service(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 return self.metadata[u'service']
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
139
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 def node(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 return self.metadata[u'node']
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
143
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 def uri(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 return self.metadata[u'uri']
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
147
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 def __len__(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 return self.items.__len__()
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
150
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 def __missing__(self, key):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 return self.items.__missing__(key)
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
153
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 def __getitem__(self, key):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 return self.items.__getitem__(key)
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
156
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 def __iter__(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 return self.items.__iter__()
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
159
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 def __reversed__(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 return self.items.__reversed__()
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
162
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 def __contains__(self, item):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 return self.items.__contains__(item)