annotate src/pages/files/list/page_meta.py @ 1077:880ea673aaff

blog: moved blog page from /common to /blog: - removed common pages (it was only used for blog so far, it may come back in the future if needed) - /blog now prepares a discover page by default
author Goffi <goffi@goffi.org>
date Mon, 26 Mar 2018 08:20:41 +0200
parents 2dab7692eae7
children cdd389ef97bc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1064
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2.7
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 from libervia.server.constants import Const as C
1067
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
5 from sat.core.i18n import _
1064
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 from twisted.internet import defer
1067
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
7 from libervia.server import session_iface
1069
2dab7692eae7 pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
8 from libervia.server import pages_tools
1064
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 from sat.core.log import getLogger
1069
2dab7692eae7 pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
10 from sat.tools.common import uri
1067
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
11 import json
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
12 import os
1069
2dab7692eae7 pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
13 log = getLogger('pages/files/list')
1064
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 """files handling pages"""
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
1069
2dab7692eae7 pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
16 name = u'files_list'
1064
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 access = C.PAGES_ACCESS_PROFILE
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 template = u"file/overview.html"
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 def parse_url(self, request):
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 self.getPathArgs(request, ['service', '*path'], min_args=1, service='jid', path='')
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
22
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 @defer.inlineCallbacks
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 def prepare_render(self, request):
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 data = self.getRData(request)
1069
2dab7692eae7 pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
26 thumb_limit = data.get('thumb_limit', 300)
1064
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 template_data = request.template_data
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 service, path_elts = data[u'service'], data[u'path']
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 path = u'/'.join(path_elts)
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 profile = self.getProfile(request) or C.SERVICE_PROFILE
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 files_data = yield self.host.bridgeCall('FISList', service.full(), path, {}, profile)
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 for file_data in files_data:
1067
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
34 try:
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
35 extra_raw = file_data[u'extra']
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
36 except KeyError:
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
37 pass
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
38 else:
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
39 file_data[u'extra'] = json.loads(extra_raw) if extra_raw else {}
1064
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 dir_path = path_elts + [file_data['name']]
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 if file_data[u'type'] == C.FILE_TYPE_DIRECTORY:
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 page = self
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 elif file_data[u'type'] == C.FILE_TYPE_FILE:
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 page = self.getPageByName('files_view')
1069
2dab7692eae7 pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
45
2dab7692eae7 pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
46 ## thumbnails ##
1067
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
47 try:
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
48 thumbnails = file_data[u'extra']['thumbnails']
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
49 if not thumbnails:
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
50 raise KeyError
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
51 except KeyError:
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
52 pass
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
53 else:
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
54 thumbnails.sort(key = lambda t: t['size'])
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
55 thumb = thumbnails[0]
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
56 for thumb_data in thumbnails:
1069
2dab7692eae7 pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
57 if thumb_data['size'][0] > thumb_limit:
1067
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
58 break
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
59 thumb = thumb_data
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
60 if u'url' in thumb:
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
61 file_data['thumb_url'] = thumb['url']
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
62 elif u'id' in thumb:
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
63 try:
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
64 thumb_path = yield self.host.bridgeCall('bobGetFile', service.full(), thumb[u'id'], profile)
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
65 except Exception as e:
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
66 log.warning(_(u"Can't retrieve thumbnail: {reason}").format(reason=e))
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
67 else:
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
68 filename = os.path.basename(thumb_path)
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
69 session_data = self.host.getSessionData(request, session_iface.ISATSession)
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
70 file_data['thumb_url'] = os.path.join(session_data.cache_dir, filename)
1064
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 else:
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 raise ValueError(u'unexpected file type: {file_type}'.format(file_type=file_data[u'type']))
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 file_data[u'url'] = page.getURL(service.full(), *dir_path)
1069
2dab7692eae7 pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
74
2dab7692eae7 pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
75 ## comments ##
2dab7692eae7 pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
76 comments_url = file_data.get(u'comments_url')
2dab7692eae7 pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
77 if comments_url:
2dab7692eae7 pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
78 parsed_url = uri.parseXMPPUri(comments_url)
2dab7692eae7 pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
79 comments_service = file_data[u'comments_service'] = parsed_url['path']
2dab7692eae7 pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
80 comments_node = file_data[u'comments_node'] = parsed_url['node']
2dab7692eae7 pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
81 try:
2dab7692eae7 pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
82 comments_count = file_data[u'comments_count'] = int(file_data['comments_count'])
2dab7692eae7 pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
83 except KeyError:
2dab7692eae7 pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
84 comments_count = None
2dab7692eae7 pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
85 if comments_count and data.get('retrieve_comments', False):
2dab7692eae7 pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
86 file_data[u'comments'] = yield pages_tools.retrieveComments(self,
2dab7692eae7 pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
87 comments_service,
2dab7692eae7 pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
88 comments_node,
2dab7692eae7 pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
89 profile=profile)
2dab7692eae7 pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
90
1064
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 template_data[u'files_data'] = files_data
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 template_data[u'path'] = path
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 if path_elts:
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 template_data[u'parent_url'] = self.getURL(service.full(), *path_elts[:-1])
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
95