Newer
Older
### postgrestutils
A very basic POSTGREST client and utils
#### Setup
- 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
# this will send a request to 'POSTGREST_UTILS_BASE_URI/kerbals?select=id,forename&forename=eq.Jebediah'
res = pgrest_client.get("kerbals", params=payload)
pgrest_client.configure('your-JWT', base_uri='http://127.0.0.1:3000')
payload = {
"select": "id,forename"
}
res = pgrest_client.get("kerbals", params=payload)
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#### 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.