Esempio n. 1
0
    def convert_person(cls, value, **_):
        users = []

        for user in to_list(value):
            users += [["‣", [["u", extract_id(user)]]], [","]]

        return users[:-1]
Esempio n. 2
0
 def __contains__(self, item):
     if isinstance(item, str):
         item_id = extract_id(item)
     elif isinstance(item, Block):
         item_id = item.id
     else:
         return False
     return item_id in self._block_ids
Esempio n. 3
0
    def __contains__(self, item: Union[str, Block]):
        if isinstance(item, str):
            item_id = extract_id(item)
        elif isinstance(item, Block):
            item_id = item.id
        else:
            return False

        return item_id in self._content_list()
Esempio n. 4
0
    def __init__(self, client, block_id: str, *args, **kwargs):
        """
        Create record object and fill its fields.
        """
        self._children = None
        self._callbacks = []
        self._client = client
        self._id = extract_id(block_id)

        if self._client._monitor is not None:
            self._client._monitor.subscribe(self)
Esempio n. 5
0
 def get(self, table, url_or_id, force_refresh=False):
     rid = extract_id(url_or_id)
     # look up the record in the current local dataset
     result = self._get(table, rid)
     # if it's not found, try refreshing the record from the server
     if result is Missing or force_refresh:
         if table == "block":
             self.call_load_page_chunk(rid)
         else:
             self.call_get_record_values(**{table: rid})
         result = self._get(table, rid)
     return result if result is not Missing else None
Esempio n. 6
0
    def call_get_record_values(self, **kwargs):
        """
        Call the server's getRecordValues endpoint to update the local record store. The keyword arguments map
        table names into lists of (or singular) record IDs to load for that table. Use True to refresh all known
        records for that table.
        """

        requestlist = []

        for table, ids in kwargs.items():

            # ensure "ids" is a proper list
            if ids is True:
                ids = list(self._values.get(table, {}).keys())
            if isinstance(ids, str):
                ids = [ids]

            # if we're in a transaction, add the requested IDs to a queue to refresh when the transaction completes
            if self._client.in_transaction():
                self._records_to_refresh[table] = list(
                    set(self._records_to_refresh.get(table, []) + ids))
                continue

            requestlist += [{
                "table": table,
                "id": extract_id(id)
            } for id in ids]

        if requestlist:
            logger.debug(
                "Calling 'getRecordValues' endpoint for requests: {}".format(
                    requestlist))
            results = self._client.post("getRecordValues", {
                "requests": requestlist
            }).json()["results"]
            for request, result in zip(requestlist, results):
                self._update_record(
                    request["table"],
                    request["id"],
                    value=result.get("value"),
                    role=result.get("role"),
                )
Esempio n. 7
0
    def call_get_record_values(self, **kwargs):
        """
        Call the server's getRecordValues endpoint
        to update the local record store.
        The keyword arguments map table names into lists
        of (or singular) record IDs to load for that table.
        Use True to refresh all known records for that table.
        """
        requests = []

        for table, ids in kwargs.items():
            # TODO: ids can be `True` and if it is then we take every
            #       key from collection_view into consideration, is it OK?
            if ids is True:
                ids = self._values.get(table, {}).keys()
            ids = to_list(ids)

            # if we're in a transaction, add the requested IDs
            # to a queue to refresh when the transaction completes
            if self._client.in_transaction():
                records = self._records_to_refresh.get(table, []) + ids
                self._records_to_refresh[table] = list(set(records))
                continue

            requests += [{"table": table, "id": extract_id(i)} for i in ids]

        if requests:
            logger.debug(f"Calling 'getRecordValues' endpoint for requests: {requests}")
            data = {"requests": requests}
            data = self._client.post("getRecordValues", data).json()
            results = data["results"]

            for request, result in zip(requests, results):
                self._update_record(
                    table=request["table"],
                    record_id=request["id"],
                    value=result.get("value"),
                    role=result.get("role"),
                )
Esempio n. 8
0
    def get_block(self,
                  url_or_id: str,
                  force_refresh: bool = False) -> Optional[Block]:
        """
        Retrieve an instance of a subclass of Block that maps to
        the block/page identified by the URL or ID passed in.


        Arguments
        ---------
        url_or_id : str
            Path or ID to block.

        force_refresh : bool, optional
            Whether or not to force a refresh of data.
            Defaults to False.


        Returns
        -------
        Block or None
            Found block or None.
        """
        block_id = extract_id(url_or_id)
        block = self.get_record_data("block", block_id, force_refresh)

        if not block:
            return None

        if block.get("parent_table") == "collection":
            if block.get("is_template"):
                klass = TemplateBlock
            else:
                klass = CollectionRowBlock
        else:
            klass = get_block_type(block.get("type"))

        return klass(client=self, block_id=block_id)
Esempio n. 9
0
    def get_block(self, url_or_id: str, force_refresh: bool = False) -> Optional[Block]:
        """
        Retrieve an instance of a subclass of Block that maps to
        the block/page identified by the URL or ID passed in.


        Arguments
        ---------
        url_or_id : str
            Path or ID to block.

        force_refresh : bool, optional
            Whether or not to force a refresh of data.
            Defaults to False.


        Returns
        -------
        Block or None
            Found block or None.
        """
        block_id = extract_id(url_or_id)
        block = self.get_record_data("block", block_id, force_refresh=force_refresh)

        if not block:
            return None

        if block.get("parent_table") == "collection":
            if block.get("is_template"):
                return TemplateBlock(client=self, block_id=block_id)
            else:
                # TODO: this class does not inherit from Block, what's the difference?
                return CollectionBlock(client=self, block_id=block_id)

        klass = all_block_types().get(block.get("type", ""), Block)
        return klass(client=self, block_id=block_id)
 def __contains__(self, other: Union[Block, str]) -> bool:
     return extract_id(other) in self._block_ids
Esempio n. 11
0
 def convert_created_by(cls, value, **_):
     return extract_id(value)
Esempio n. 12
0
 def __contains__(self, other: Union[Block, str]):
     return extract_id(other) in self._content_list()