comparison src/pages/common/blog/page_meta.py @ 1016:fc1c913cc9d1

pages (blog_new, common/blog): various blog improvments: - item_id can now be specified in URL parsing, and a filter_keyword is used so future variants can be implemented (e.g. tags) - if item_id is specified, rsm is discarded and only the requested item is retrieved - new template data "items_http_uri" is filled, with links to HTTP versions of items - blog_new is now a simple redirection to common/blog as the later should be generic enough to handle all use cases.
author Goffi <goffi@goffi.org>
date Fri, 19 Jan 2018 18:09:39 +0100
parents fe08a5c95b27
children 2ae3b6291456
comparison
equal deleted inserted replaced
1015:16d52917666c 1016:fc1c913cc9d1
21 node = urllib.quote_plus(uri_data[u'node']) 21 node = urllib.quote_plus(uri_data[u'node'])
22 return service + u'/' + node 22 return service + u'/' + node
23 23
24 24
25 def parse_url(self, request): 25 def parse_url(self, request):
26 """URL is /[service]/[node] 26 """URL is /[service]/[node]/[filter_keyword]/[item]|[other]
27 27
28 if [node] is not found, default namespace is used 28 if [node] is '@', default namespace is used
29 if both [service] and [node] are not found, default service is used too 29 if a value is unset, default one will be used
30 keyword can be one of:
31 id: next value is a item id
32 tag: next value is a blog tag
30 """ 33 """
31 data = self.getRData(request) 34 data = self.getRData(request)
32 35
33 try: 36 try:
34 service = self.nextPath(request) 37 service = self.nextPath(request)
43 46
44 try: 47 try:
45 data['node'] = self.nextPath(request) 48 data['node'] = self.nextPath(request)
46 except IndexError: 49 except IndexError:
47 data['node'] = u'' 50 data['node'] = u''
51 else:
52 if data['node'] == u'@':
53 data['node'] = u''
54
55 try:
56 filter_kw = data['filter_keyword'] = self.nextPath(request)
57 except IndexError:
58 pass
59 else:
60 if filter_kw == u'id':
61 try:
62 data['item'] = self.nextPath(request)
63 except IndexError:
64 self.pageError(request, C.HTTP_BAD_REQUEST)
65 else:
66 # invalid filter keyword
67 log.warning(_(u"invalid filter keyword: {filter_kw}").format(filter_kw=filter_kw))
68 self.pageError(request, C.HTTP_BAD_REQUEST)
48 69
49 70
50 @defer.inlineCallbacks 71 @defer.inlineCallbacks
51 def appendComments(self, blog_items, identities, profile): 72 def appendComments(self, blog_items, identities, profile):
52 for blog_item in blog_items: 73 for blog_item in blog_items:
75 comments = data_objects.BlogItems(comments_data) 96 comments = data_objects.BlogItems(comments_data)
76 blog_item.appendCommentsItems(comments) 97 blog_item.appendCommentsItems(comments)
77 yield appendComments(self, comments, identities, profile) 98 yield appendComments(self, comments, identities, profile)
78 99
79 @defer.inlineCallbacks 100 @defer.inlineCallbacks
80 def getBlogData(self, request, service, node, extra, profile): 101 def getBlogData(self, request, service, node, item_id, extra, profile):
81 try: 102 try:
103 if item_id:
104 items_id = [item_id]
105 else:
106 items_id = []
82 blog_data = yield self.host.bridge.mbGet( 107 blog_data = yield self.host.bridge.mbGet(
83 service.userhost(), 108 service.userhost(),
84 node, 109 node,
85 C.NO_LIMIT, 110 C.NO_LIMIT,
86 [], 111 items_id,
87 extra, 112 extra,
88 profile) 113 profile)
89 except Exception as e: 114 except Exception as e:
90 # FIXME: need a better way to test errors in bridge errback 115 # FIXME: need a better way to test errors in bridge errback
91 if u"forbidden" in unicode(e): 116 if u"forbidden" in unicode(e):
98 123
99 @defer.inlineCallbacks 124 @defer.inlineCallbacks
100 def prepare_render(self, request): 125 def prepare_render(self, request):
101 data = self.getRData(request) 126 data = self.getRData(request)
102 # if the comments are not explicitly hidden, we show them 127 # if the comments are not explicitly hidden, we show them
103 service, node, show_comments = data.get(u'service', u''), data.get(u'node', u''), data.get(u'show_comments', True) 128 service, node, item_id, show_comments = data.get(u'service', u''), data.get(u'node', u''), data.get(u'item'), data.get(u'show_comments', True)
104 profile = self.getProfile(request) 129 profile = self.getProfile(request)
105 if profile is None: 130 if profile is None:
106 profile = C.SERVICE_PROFILE 131 profile = C.SERVICE_PROFILE
107 132
108 params = self.getAllPostedData(request, multiple=False) 133 params = self.getAllPostedData(request, multiple=False)
109 extra = {u'rsm_max': u'10'} 134 if item_id:
110 if u'after' in params: 135 extra = {}
111 extra[u'rsm_after'] = params[u'after'] 136 else:
112 elif u'before' in params: 137 extra = {u'rsm_max': u'10'}
113 extra[u'rsm_before'] = params[u'before'] 138 if u'after' in params:
139 extra[u'rsm_after'] = params[u'after']
140 elif u'before' in params:
141 extra[u'rsm_before'] = params[u'before']
114 142
115 blog_data, items = yield getBlogData(self, request, service, node, extra, profile) 143 blog_data, items = yield getBlogData(self, request, service, node, item_id, extra, profile)
116 template_data = request.template_data 144 template_data = request.template_data
117 if items: 145 if items:
118 last_id = items[-1].id 146 if not item_id:
119 template_data['older_url'] = self.getParamURL(request, after=last_id) 147 last_id = items[-1].id
120 if u'before' in params or u'after' in params: 148 template_data['older_url'] = self.getParamURL(request, after=last_id)
121 first_id = items[0].id 149 if u'before' in params or u'after' in params:
122 template_data['newer_url'] = self.getParamURL(request, before=first_id) 150 first_id = items[0].id
151 template_data['newer_url'] = self.getParamURL(request, before=first_id)
123 else: 152 else:
153 if item_id:
154 # if item id has been specified in URL and it's not found,
155 # we must return an error
156 self.pageError(request, C.HTTP_NOT_FOUND)
157
124 # no items, we have requested items before last post, or blog is empty 158 # no items, we have requested items before last post, or blog is empty
125 extra = {u'rsm_max': u'10'} 159 extra = {u'rsm_max': u'10'}
126 blog_data, items = yield getBlogData(self, request, service, node, extra, profile) 160 blog_data, items = yield getBlogData(self, request, service, node, None, extra, profile)
127 if items: 161 if items:
128 last_id = items[-1].id 162 last_id = items[-1].id
129 template_data['older_url'] = self.getParamURL(request, after=last_id) 163 template_data['older_url'] = self.getParamURL(request, after=last_id)
130 164
131 identities = template_data[u'identities'] = self.host.getSessionData(request, session_iface.ISATSession).identities 165 identities = template_data[u'identities'] = self.host.getSessionData(request, session_iface.ISATSession).identities
132 166
133 if show_comments: 167 if show_comments:
134 yield appendComments(self, items, identities, profile) 168 yield appendComments(self, items, identities, profile)
135 169
136 template_data[u'items'] = items 170 blog_view = self.getPageByName(u'blog_view')
171 template_data[u'items'] = data[u'items'] = items
172 template_data[u'items_http_uri'] = {item.id: self.host.getExtBaseURL(request, blog_view.getURL(service.full(), node or '@', u'id', item.id)) for item in items}
137 template_data[u'allow_commenting'] = data.get(u'allow_commenting', False) 173 template_data[u'allow_commenting'] = data.get(u'allow_commenting', False)
138 174
139 175
140 @defer.inlineCallbacks 176 @defer.inlineCallbacks
141 def on_data_post(self, request): 177 def on_data_post(self, request):