You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To use API keys in your views, you can inherit from `APIKeyView` and customize the [`use_api_key`](./views.py#use_api_key) method to set the `request.user` attribute (or any other attribute) to the object associated with the API key.
242
+
To use API keys in your views, you can inherit from `APIKeyView` and customize the [`use_api_key`](./views.py#use_api_key) method to associate the request with a user (or any other object) using `set_request_user()`.
233
243
234
244
```python
235
245
# app/api/views.py
236
246
from plain.api.views import APIKeyView, APIView
247
+
from plain.auth import set_request_user
237
248
238
249
239
250
classBaseAPIView(APIView, APIKeyView):
240
251
defuse_api_key(self):
241
252
super().use_api_key()
242
253
243
254
if user :=self.api_key.users.first():
244
-
self.request.user =user
255
+
set_request_user(self.request, user)
245
256
else:
246
257
raise ResponseException(
247
258
JsonResponse(
@@ -292,9 +303,10 @@ class CurrentUserAPIView(BaseAPIView):
Copy file name to clipboardExpand all lines: plain-sessions/plain/sessions/README.md
+41-13Lines changed: 41 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,28 +20,38 @@ Sessions are implemented as a dictionary-like object that automatically handles
20
20
21
21
## Basic usage
22
22
23
-
Sessions are automatically available on request objects when the middleware is installed. You can use `request.session` like a standard Python dictionary:
23
+
In views that inherit from `SessionViewMixin`, you can use `self.session` like a standard Python dictionary:
Outside of views, you can use `get_request_session()`:
49
+
50
+
```python
51
+
from plain.sessions import get_request_session
52
+
53
+
session = get_request_session(request)
54
+
session['key'] ='value'
45
55
```
46
56
47
57
The session data is automatically saved when you set or delete values. Sessions are stored in the database using the [`Session`](./models.py#Session) model.
@@ -89,23 +99,41 @@ The [`SessionStore`](./core.py#SessionStore) class provides additional methods f
89
99
To completely remove the current session data and regenerate the session key:
90
100
91
101
```python
92
-
# Delete all session data and get a new session key
93
-
request.session.flush()
102
+
# In a view with SessionViewMixin
103
+
self.session.flush()
104
+
105
+
# Outside a view
106
+
from plain.sessions import get_request_session
107
+
session = get_request_session(request)
108
+
session.flush()
94
109
```
95
110
96
111
### Cycling session keys
97
112
98
113
To create a new session key while retaining the current session data (useful for security purposes):
0 commit comments