Write implementation

This is the official documentation of the forestadmin-agent-django and forestadmin-agent-flask Python agents.

Making your records editable is achieved by implementing the create, update and delete methods.

The 3 methods take a filter as a parameter but note that, unlike the list method, there is no need to support paging.

from typings import Optional

from forestadmin.agent_toolkit.utils.context import User
from forestadmin.datasource_toolkit.collections import Collection
from forestadmin.datasource_toolkit.interfaces.records import RecordsDataAlias

import requests

class MyCollection(Collection):
    def __init__(self):
        self.addField('id', {
            "is_read_only": True,
            # ...
        })
        self.addField('title', {
            "is_read_only": False,
            # ...
        })

    async def create(
        self, caller: User, List[RecordsDataAlias]
    ) -> List[RecordsDataAlias]:
        records_to_return = []
        for record in data:
            response = requests.post('https://my-api/my-collection', json=record)
            records_to_return = response.json()

        return records_to_return

    async def update(
        self, caller: User, filter_: Optional[Filter], patch: RecordsDataAlias
    ) -> None:
        record_ids = await self.list(caller, filter_, ["id"])
        for record_id in record_ids:
            requests.patch(f'https://my-api/my-collection/{record_id}', json=patch)

    async def delete(
        self, caller: User, filter_: Optional[Filter]
    ) -> None:
        record_ids = await self.list(caller, filter_, ["id"])
        for record_id in record_ids:
            requests.delete(f'https://my-api/my-collection/{record_id}')

Last updated