Skip to content
Snippets Groups Projects
README.md 2.51 KiB
Newer Older
  • Learn to ignore specific revisions
  • Fynn Becker's avatar
    Fynn Becker committed
    ### postgrestutils
    
    A very basic POSTGREST client and utils
    
    #### Setup
    
    Fynn Becker's avatar
    Fynn Becker committed
    
    ##### Django
    
    Fynn Becker's avatar
    Fynn Becker committed
      - add `"postgrestutils"` to your `INSTALLED_APPS` setting
      - add `POSTGREST_UTILS_BASE_URI` (should default to the most frequently used POSTGREST instance in the future) and `POSTGREST_UTILS_JWT` to your project settings
    
    
    Fynn Becker's avatar
    Fynn Becker committed
    ```python
    from postgrestutils.client import pgrest_client
    
    Fynn Becker's avatar
    Fynn Becker committed
        "select": "id,forename",
        "forename": "eq.Jebediah"
    
    Fynn Becker's avatar
    Fynn Becker committed
    
    # this will send a request to 'POSTGREST_UTILS_BASE_URI/kerbals?select=id,forename&forename=eq.Jebediah'
    res = pgrest_client.get("kerbals", params=payload)
    
    Fynn Becker's avatar
    Fynn Becker committed
    ```
    
    ##### Other projects
    
    Fynn Becker's avatar
    Fynn Becker committed
    
    ```python
    
    Fynn Becker's avatar
    Fynn Becker committed
    from postgrestutils.client import pgrest_client
    
    pgrest_client.configure('your-JWT', base_uri='http://127.0.0.1:3000')
    
    Fynn Becker's avatar
    Fynn Becker committed
    
    
    payload = {
        "select": "id,forename"
    }
    res = pgrest_client.get("kerbals", params=payload)
    
    Fynn Becker's avatar
    Fynn Becker committed
    ```
    
    schulmax's avatar
    schulmax committed
    ### Filtering
    
    Fynn Becker's avatar
    Fynn Becker committed
    http://postgrest.org/en/stable/api.html
    
    schulmax's avatar
    schulmax committed
    
    
    #### Django helpers
    
    ##### custom `user_account_fetched` signal
    
    `postgrestutils` provides a custom signal called `user_account_fetched` which provides the current request and the account of the current user on login.
    To use this feature (and you really should 99.9% of the time) configure your settings accordingly by specifying the columns you need from the `account` endpoint:
    
    ```python
    # settings.py
    POSTGREST_UTILS_AUTOFETCH = 'person_id,email'  # comma-separated string
    ```
    
    When connecting to the signal you will have to provide a callback function.
    That function is a great place for your custom logic to fetch and store additional information your app frequently needs into the session.
    You can connect to it like so:
    
    ```python
    # apps.py
    from django.apps import AppConfig
    from postgrestutils.signals import user_account_fetched
    from your_app.utils import your_callback_func
    
    
    class YourAppConfig(AppConfig):
        def ready(self, *args, **kwargs):
            super().ready(*args, **kwargs)
            user_account_fetched.connect(your_callback_func)
    ```
    
    Your callback function could look something like this:
    
    ```python
    # utils.py
    from postgrestutils.client import pgrest_client
    
    
    def your_callback_func(sender, **kwargs):
        request = kwargs['request']
        account = kwargs['account']
        # fetching some addtional data your project needs frequently using the pgrest_client (person, memberships etc.)
    
        # 'caching' that data in the session
        request.session['account'] = account
        request.session['person'] = person
        request.session['memberships'] = memberships
    ```
    
    For more information on signals refer to the django docs. They are great. Really.