comparison libervia/pages/files/list/page_meta.py @ 1124:28e3eb3bb217

files reorganisation and installation rework: - files have been reorganised to follow other SàT projects and usual Python organisation (no more "/src" directory) - VERSION file is now used, as for other SàT projects - replace the overcomplicated setup.py be a more sane one. Pyjamas part is not compiled anymore by setup.py, it must be done separatly - removed check for data_dir if it's empty - installation tested working in virtual env - libervia launching script is now in bin/libervia
author Goffi <goffi@goffi.org>
date Sat, 25 Aug 2018 17:59:48 +0200
parents src/pages/files/list/page_meta.py@cdd389ef97bc
children 29eb15062416
comparison
equal deleted inserted replaced
1123:63a4b8fe9782 1124:28e3eb3bb217
1 #!/usr/bin/env python2.7
2 # -*- coding: utf-8 -*-
3
4 from libervia.server.constants import Const as C
5 from sat.core.i18n import _
6 from twisted.internet import defer
7 from libervia.server import session_iface
8 from libervia.server import pages_tools
9 from sat.core.log import getLogger
10 from sat.tools.common import uri
11 import json
12 import os
13
14 log = getLogger("pages/files/list")
15 """files handling pages"""
16
17 name = u"files_list"
18 access = C.PAGES_ACCESS_PROFILE
19 template = u"file/overview.html"
20
21
22 def parse_url(self, request):
23 self.getPathArgs(request, ["service", "*path"], min_args=1, service="jid", path="")
24
25
26 @defer.inlineCallbacks
27 def prepare_render(self, request):
28 data = self.getRData(request)
29 thumb_limit = data.get("thumb_limit", 300)
30 template_data = request.template_data
31 service, path_elts = data[u"service"], data[u"path"]
32 path = u"/".join(path_elts)
33 profile = self.getProfile(request) or C.SERVICE_PROFILE
34
35 files_data = yield self.host.bridgeCall("FISList", service.full(), path, {}, profile)
36 for file_data in files_data:
37 try:
38 extra_raw = file_data[u"extra"]
39 except KeyError:
40 pass
41 else:
42 file_data[u"extra"] = json.loads(extra_raw) if extra_raw else {}
43 dir_path = path_elts + [file_data["name"]]
44 if file_data[u"type"] == C.FILE_TYPE_DIRECTORY:
45 page = self
46 elif file_data[u"type"] == C.FILE_TYPE_FILE:
47 page = self.getPageByName("files_view")
48
49 ## thumbnails ##
50 try:
51 thumbnails = file_data[u"extra"]["thumbnails"]
52 if not thumbnails:
53 raise KeyError
54 except KeyError:
55 pass
56 else:
57 thumbnails.sort(key=lambda t: t["size"])
58 thumb = thumbnails[0]
59 for thumb_data in thumbnails:
60 if thumb_data["size"][0] > thumb_limit:
61 break
62 thumb = thumb_data
63 if u"url" in thumb:
64 file_data["thumb_url"] = thumb["url"]
65 elif u"id" in thumb:
66 try:
67 thumb_path = yield self.host.bridgeCall(
68 "bobGetFile", service.full(), thumb[u"id"], profile
69 )
70 except Exception as e:
71 log.warning(
72 _(u"Can't retrieve thumbnail: {reason}").format(reason=e)
73 )
74 else:
75 filename = os.path.basename(thumb_path)
76 session_data = self.host.getSessionData(
77 request, session_iface.ISATSession
78 )
79 file_data["thumb_url"] = os.path.join(
80 session_data.cache_dir, filename
81 )
82 else:
83 raise ValueError(
84 u"unexpected file type: {file_type}".format(file_type=file_data[u"type"])
85 )
86 file_data[u"url"] = page.getURL(service.full(), *dir_path)
87
88 ## comments ##
89 comments_url = file_data.get(u"comments_url")
90 if comments_url:
91 parsed_url = uri.parseXMPPUri(comments_url)
92 comments_service = file_data[u"comments_service"] = parsed_url["path"]
93 comments_node = file_data[u"comments_node"] = parsed_url["node"]
94 try:
95 comments_count = file_data[u"comments_count"] = int(
96 file_data["comments_count"]
97 )
98 except KeyError:
99 comments_count = None
100 if comments_count and data.get("retrieve_comments", False):
101 file_data[u"comments"] = yield pages_tools.retrieveComments(
102 self, comments_service, comments_node, profile=profile
103 )
104
105 template_data[u"files_data"] = files_data
106 template_data[u"path"] = path
107 if path_elts:
108 template_data[u"parent_url"] = self.getURL(service.full(), *path_elts[:-1])