comparison sat/plugins/plugin_comp_file_sharing_management.py @ 3529:698579bedd6f

component file sharing (plugin management): new command to get available quota/used space.
author Goffi <goffi@goffi.org>
date Wed, 05 May 2021 15:37:33 +0200
parents be6d91572633
children a8259a1f89b2
comparison
equal deleted inserted replaced
3528:849374e59178 3529:698579bedd6f
21 from functools import partial 21 from functools import partial
22 from sat.core.i18n import _, D_ 22 from sat.core.i18n import _, D_
23 from sat.core import exceptions 23 from sat.core import exceptions
24 from sat.core.constants import Const as C 24 from sat.core.constants import Const as C
25 from sat.core.log import getLogger 25 from sat.core.log import getLogger
26 from sat.tools.common import utils
26 from wokkel import data_form 27 from wokkel import data_form
27 from twisted.internet import defer 28 from twisted.internet import defer
28 from twisted.words.protocols.jabber import jid 29 from twisted.words.protocols.jabber import jid
29 30
30 log = getLogger(__name__) 31 log = getLogger(__name__)
48 49
49 NS_FILE_MANAGEMENT = "https://salut-a-toi.org/protocol/file-management:0" 50 NS_FILE_MANAGEMENT = "https://salut-a-toi.org/protocol/file-management:0"
50 NS_FILE_MANAGEMENT_PERM = "https://salut-a-toi.org/protocol/file-management:0#perm" 51 NS_FILE_MANAGEMENT_PERM = "https://salut-a-toi.org/protocol/file-management:0#perm"
51 NS_FILE_MANAGEMENT_DELETE = "https://salut-a-toi.org/protocol/file-management:0#delete" 52 NS_FILE_MANAGEMENT_DELETE = "https://salut-a-toi.org/protocol/file-management:0#delete"
52 NS_FILE_MANAGEMENT_THUMB = "https://salut-a-toi.org/protocol/file-management:0#thumb" 53 NS_FILE_MANAGEMENT_THUMB = "https://salut-a-toi.org/protocol/file-management:0#thumb"
54 NS_FILE_MANAGEMENT_QUOTA = "https://salut-a-toi.org/protocol/file-management:0#quota"
53 55
54 56
55 class WorkflowError(Exception): 57 class WorkflowError(Exception):
56 """Raised when workflow can't be completed""" 58 """Raised when workflow can't be completed"""
57 59
94 allowed_magics=C.ENTITY_ALL, 96 allowed_magics=C.ENTITY_ALL,
95 ) 97 )
96 self._c.addAdHocCommand( 98 self._c.addAdHocCommand(
97 client, self._onGenThumbnails, "Generate Thumbnails", 99 client, self._onGenThumbnails, "Generate Thumbnails",
98 node=NS_FILE_MANAGEMENT_THUMB, 100 node=NS_FILE_MANAGEMENT_THUMB,
101 allowed_magics=C.ENTITY_ALL,
102 )
103 self._c.addAdHocCommand(
104 client, self._onQuota, "Get Quota",
105 node=NS_FILE_MANAGEMENT_QUOTA,
99 allowed_magics=C.ENTITY_ALL, 106 allowed_magics=C.ENTITY_ALL,
100 ) 107 )
101 108
102 def _delete(self, service_jid_s, path, namespace, profile): 109 def _delete(self, service_jid_s, path, namespace, profile):
103 client = self.host.getClient(profile) 110 client = self.host.getClient(profile)
456 # job done, we can end the session 463 # job done, we can end the session
457 status = self._c.STATUS.COMPLETED 464 status = self._c.STATUS.COMPLETED
458 payload = None 465 payload = None
459 note = (self._c.NOTE.INFO, _("thumbnails generated")) 466 note = (self._c.NOTE.INFO, _("thumbnails generated"))
460 defer.returnValue((payload, status, None, note)) 467 defer.returnValue((payload, status, None, note))
468
469 async def _onQuota(self, client, command_elt, session_data, action, node):
470 requestor = session_data['requestor']
471 quota = self.host.plugins["file_sharing"].getQuota(client, requestor)
472 try:
473 size_used = await self.host.memory.fileGetUsedSpace(client, requestor)
474 except exceptions.PermissionError:
475 raise WorkflowError(self._err(_("forbidden")))
476 status = self._c.STATUS.COMPLETED
477 payload = None
478 note = (
479 self._c.NOTE.INFO,
480 _("You are currently using {size_used} on {size_quota}").format(
481 size_used = utils.getHumanSize(size_used),
482 size_quota = (
483 _("unlimited quota") if quota is None
484 else utils.getHumanSize(quota)
485 )
486 )
487 )
488 return (payload, status, None, note)