Mercurial > libervia-web
annotate src/pages/files/list/page_meta.py @ 1121:6d6c0694f72a
install (setup): set version 0.7.0a2.post1 to fix messed upload on pypi
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 15 Jul 2018 11:09:16 +0200 |
parents | cdd389ef97bc |
children |
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 |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
13 |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
14 log = getLogger("pages/files/list") |
1064
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 """files handling pages""" |
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
17 name = u"files_list" |
1064
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 access = C.PAGES_ACCESS_PROFILE |
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 template = u"file/overview.html" |
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
21 |
1064
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 def parse_url(self, request): |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
23 self.getPathArgs(request, ["service", "*path"], min_args=1, service="jid", path="") |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
24 |
1064
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 |
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 @defer.inlineCallbacks |
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 def prepare_render(self, request): |
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 data = self.getRData(request) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
29 thumb_limit = data.get("thumb_limit", 300) |
1064
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 template_data = request.template_data |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
31 service, path_elts = data[u"service"], data[u"path"] |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
32 path = u"/".join(path_elts) |
1064
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 profile = self.getProfile(request) or C.SERVICE_PROFILE |
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
35 files_data = yield self.host.bridgeCall("FISList", service.full(), path, {}, profile) |
1064
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 for file_data in files_data: |
1067
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
37 try: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
38 extra_raw = file_data[u"extra"] |
1067
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
39 except KeyError: |
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
40 pass |
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
41 else: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
42 file_data[u"extra"] = json.loads(extra_raw) if extra_raw else {} |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
43 dir_path = path_elts + [file_data["name"]] |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
44 if file_data[u"type"] == C.FILE_TYPE_DIRECTORY: |
1064
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 page = self |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
46 elif file_data[u"type"] == C.FILE_TYPE_FILE: |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
47 page = self.getPageByName("files_view") |
1069
2dab7692eae7
pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents:
1067
diff
changeset
|
48 |
2dab7692eae7
pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents:
1067
diff
changeset
|
49 ## thumbnails ## |
1067
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
50 try: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
51 thumbnails = file_data[u"extra"]["thumbnails"] |
1067
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
52 if not thumbnails: |
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
53 raise KeyError |
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
54 except KeyError: |
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
55 pass |
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
56 else: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
57 thumbnails.sort(key=lambda t: t["size"]) |
1067
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
58 thumb = thumbnails[0] |
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
59 for thumb_data in thumbnails: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
60 if thumb_data["size"][0] > thumb_limit: |
1067
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
61 break |
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
62 thumb = thumb_data |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
63 if u"url" in thumb: |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
64 file_data["thumb_url"] = thumb["url"] |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
65 elif u"id" in thumb: |
1067
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
66 try: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
67 thumb_path = yield self.host.bridgeCall( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
68 "bobGetFile", service.full(), thumb[u"id"], profile |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
69 ) |
1067
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
70 except Exception as e: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
71 log.warning( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
72 _(u"Can't retrieve thumbnail: {reason}").format(reason=e) |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
73 ) |
1067
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
74 else: |
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
75 filename = os.path.basename(thumb_path) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
76 session_data = self.host.getSessionData( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
77 request, session_iface.ISATSession |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
78 ) |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
79 file_data["thumb_url"] = os.path.join( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
80 session_data.cache_dir, filename |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
81 ) |
1064
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 else: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
83 raise ValueError( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
84 u"unexpected file type: {file_type}".format(file_type=file_data[u"type"]) |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
85 ) |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
86 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
|
87 |
2dab7692eae7
pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents:
1067
diff
changeset
|
88 ## comments ## |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
89 comments_url = file_data.get(u"comments_url") |
1069
2dab7692eae7
pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents:
1067
diff
changeset
|
90 if comments_url: |
2dab7692eae7
pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents:
1067
diff
changeset
|
91 parsed_url = uri.parseXMPPUri(comments_url) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
92 comments_service = file_data[u"comments_service"] = parsed_url["path"] |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
93 comments_node = file_data[u"comments_node"] = parsed_url["node"] |
1069
2dab7692eae7
pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents:
1067
diff
changeset
|
94 try: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
95 comments_count = file_data[u"comments_count"] = int( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
96 file_data["comments_count"] |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
97 ) |
1069
2dab7692eae7
pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents:
1067
diff
changeset
|
98 except KeyError: |
2dab7692eae7
pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents:
1067
diff
changeset
|
99 comments_count = None |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
100 if comments_count and data.get("retrieve_comments", False): |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
101 file_data[u"comments"] = yield pages_tools.retrieveComments( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
102 self, comments_service, comments_node, profile=profile |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
103 ) |
1069
2dab7692eae7
pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents:
1067
diff
changeset
|
104 |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
105 template_data[u"files_data"] = files_data |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
106 template_data[u"path"] = path |
1064
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 if path_elts: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
108 template_data[u"parent_url"] = self.getURL(service.full(), *path_elts[:-1]) |