1
- import uuid
2
-
3
1
from django .core .exceptions import ObjectDoesNotExist
4
- from django .db import connection , transaction
2
+ from django .db import transaction
5
3
from rest_framework import permissions
6
4
from rest_framework .exceptions import NotAuthenticated
7
5
from rest_framework .filters import SearchFilter
8
6
from rest_framework_json_api import filters
9
- from rest_framework_json_api .serializers import ValidationError
10
7
from rest_framework_json_api .views import ModelViewSet
11
8
from rest_framework_simplejwt .authentication import JWTAuthentication
12
9
10
+ from api .db_utils import POSTGRES_USER_VAR , tenant_transaction
13
11
from api .filters import CustomDjangoFilterBackend
14
12
from api .models import Role , Tenant
15
13
from api .db_router import MainRouter
@@ -50,13 +48,7 @@ def initial(self, request, *args, **kwargs):
50
48
if tenant_id is None :
51
49
raise NotAuthenticated ("Tenant ID is not present in token" )
52
50
53
- try :
54
- uuid .UUID (tenant_id )
55
- except ValueError :
56
- raise ValidationError ("Tenant ID must be a valid UUID" )
57
-
58
- with connection .cursor () as cursor :
59
- cursor .execute (f"SELECT set_config('api.tenant_id', '{ tenant_id } ', TRUE);" )
51
+ with tenant_transaction (tenant_id ):
60
52
self .request .tenant_id = tenant_id
61
53
return super ().initial (request , * args , ** kwargs )
62
54
@@ -110,8 +102,7 @@ def initial(self, request, *args, **kwargs):
110
102
):
111
103
user_id = str (request .user .id )
112
104
113
- with connection .cursor () as cursor :
114
- cursor .execute (f"SELECT set_config('api.user_id', '{ user_id } ', TRUE);" )
105
+ with tenant_transaction (value = user_id , parameter = POSTGRES_USER_VAR ):
115
106
return super ().initial (request , * args , ** kwargs )
116
107
117
108
# TODO: DRY this when we have time
@@ -122,13 +113,7 @@ def initial(self, request, *args, **kwargs):
122
113
if tenant_id is None :
123
114
raise NotAuthenticated ("Tenant ID is not present in token" )
124
115
125
- try :
126
- uuid .UUID (tenant_id )
127
- except ValueError :
128
- raise ValidationError ("Tenant ID must be a valid UUID" )
129
-
130
- with connection .cursor () as cursor :
131
- cursor .execute (f"SELECT set_config('api.tenant_id', '{ tenant_id } ', TRUE);" )
116
+ with tenant_transaction (tenant_id ):
132
117
self .request .tenant_id = tenant_id
133
118
return super ().initial (request , * args , ** kwargs )
134
119
@@ -149,12 +134,6 @@ def initial(self, request, *args, **kwargs):
149
134
if tenant_id is None :
150
135
raise NotAuthenticated ("Tenant ID is not present in token" )
151
136
152
- try :
153
- uuid .UUID (tenant_id )
154
- except ValueError :
155
- raise ValidationError ("Tenant ID must be a valid UUID" )
156
-
157
- with connection .cursor () as cursor :
158
- cursor .execute (f"SELECT set_config('api.tenant_id', '{ tenant_id } ', TRUE);" )
137
+ with tenant_transaction (tenant_id ):
159
138
self .request .tenant_id = tenant_id
160
139
return super ().initial (request , * args , ** kwargs )
0 commit comments