maquinaweb-shared-auth 0.2.27__py3-none-any.whl → 0.2.29__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of maquinaweb-shared-auth might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: maquinaweb-shared-auth
3
- Version: 0.2.27
3
+ Version: 0.2.29
4
4
  Summary: Models read-only para autenticação compartilhada entre projetos Django.
5
5
  Author-email: Seu Nome <seuemail@dominio.com>
6
6
  License: MIT
@@ -5,17 +5,17 @@ shared_auth/conf.py,sha256=WlSXQB7p3BfE3BL6WR6EDYpCHQEjDlxQlyf8dTfClsk,621
5
5
  shared_auth/decorators.py,sha256=RT-Qfi7oGBo6PvWJRR1dqJUQdU6ZOf9p-8mV3rZmqQ0,3237
6
6
  shared_auth/exceptions.py,sha256=VKamHjBl2yjXG2RsMrLrXru1_Q9IJXmy4xmDcXlpWsU,627
7
7
  shared_auth/fields.py,sha256=RAcmFh1D_nkbai_7t_OrPZhfhAipesy5kKnEj4LUvvM,1254
8
- shared_auth/managers.py,sha256=xRj8d5r2Q5pBi36dQAOasS7EPvUUehb0eFbA-sAuzxY,6727
8
+ shared_auth/managers.py,sha256=WB0tU3asVxwS4tFq2R9-zXtWMBpvOuM94_SehGbVQFw,8268
9
9
  shared_auth/middleware.py,sha256=72GF8kGijbhw98v2Q-1sXBXk-7Bamfyvypm9h8xLX_I,6112
10
10
  shared_auth/mixins.py,sha256=BSYNTWYjLRGuxfy7x-uAaNZmrTMsm67ogAQzjrU2DoQ,7388
11
11
  shared_auth/models.py,sha256=vCyssDwKfWRE3ofl6LK_CB1-y5O-_6uvJyGZGoCnb44,6574
12
12
  shared_auth/permissions.py,sha256=FNIp12ePOUlXVp26zNMAyEtzX9kwyP7RuNIgaaCXtPA,2671
13
13
  shared_auth/router.py,sha256=zYidJ7j40lQLrhkCtAQAp-rQLhua_UF0X7SDzYRcV5w,668
14
- shared_auth/serializers.py,sha256=h50UhgOJ6Z6HNtxWjv6OK-Z9F_DOuV-PcCAg6FCzBtU,5125
14
+ shared_auth/serializers.py,sha256=VmajFqnAHB5wyxsdd5_WVegAQygi0KNOMk7vScCPHiQ,7385
15
15
  shared_auth/storage_backend.py,sha256=Eqkjz8aF5UrOpRwYl-J0Td95IObfxnJ8eLQDJVFM3Io,184
16
16
  shared_auth/urls.py,sha256=591wWEWJPaHEGkcOZ8yvfgxddRyOcZLgOc0vNtF7XRI,289
17
17
  shared_auth/views.py,sha256=2hyLnYSWUscfq-jVcskt-ukzDt4vg6IXeKjRDRu9RXk,1519
18
- maquinaweb_shared_auth-0.2.27.dist-info/METADATA,sha256=5xy3iG2RNHGZFu4eC_86xna6tzPpXfefwQLRk0inRJw,26331
19
- maquinaweb_shared_auth-0.2.27.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
20
- maquinaweb_shared_auth-0.2.27.dist-info/top_level.txt,sha256=msyYRy02ZV7zz7GR1raUI5LXGFIFn2TIkgkeKZqKufE,12
21
- maquinaweb_shared_auth-0.2.27.dist-info/RECORD,,
18
+ maquinaweb_shared_auth-0.2.29.dist-info/METADATA,sha256=0zltusF9fsm1p4ldeM4su1hxpCactgoNbIFQZhDQEJM,26331
19
+ maquinaweb_shared_auth-0.2.29.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
20
+ maquinaweb_shared_auth-0.2.29.dist-info/top_level.txt,sha256=msyYRy02ZV7zz7GR1raUI5LXGFIFn2TIkgkeKZqKufE,12
21
+ maquinaweb_shared_auth-0.2.29.dist-info/RECORD,,
shared_auth/managers.py CHANGED
@@ -89,13 +89,27 @@ class OrganizationQuerySetMixin:
89
89
 
90
90
  def with_organization_data(self):
91
91
  """
92
- Pré-carrega dados de organizações (evita N+1)
92
+ Pré-carrega dados de organizações (evita N+1 queries)
93
+
94
+ Faz uma única query bulk para buscar todas as organizações necessárias
95
+ e cacheia nos objetos usando _cached_organization.
96
+
97
+ Usage:
98
+ # Sem otimização (N+1 queries)
99
+ rascunhos = Rascunho.objects.all()
100
+ for r in rascunhos:
101
+ print(r.organization.name) # Query individual para cada
102
+
103
+ # Com otimização (2 queries total)
104
+ rascunhos = Rascunho.objects.all().with_organization_data()
105
+ for r in rascunhos:
106
+ print(r.organization.name) # Usa cache, sem queries extras
93
107
 
94
108
  Returns:
95
109
  Lista de objetos com _cached_organization
96
110
  """
97
111
  objects = list(self.all())
98
- from . import SharedOrganization
112
+ from .models import SharedOrganization
99
113
 
100
114
  if not objects:
101
115
  return objects
@@ -128,9 +142,15 @@ class UserQuerySetMixin:
128
142
 
129
143
  def with_user_data(self):
130
144
  """
131
- Pré-carrega dados de usuários (evita N+1)
145
+ Pré-carrega dados de usuários (evita N+1 queries)
146
+
147
+ Usage:
148
+ # Com otimização (2 queries total)
149
+ rascunhos = Rascunho.objects.all().with_user_data()
150
+ for r in rascunhos:
151
+ print(r.user.email) # Usa cache, sem queries extras
132
152
  """
133
- from . import SharedUser
153
+ from .models import User
134
154
 
135
155
  objects = list(self.all())
136
156
 
@@ -139,7 +159,7 @@ class UserQuerySetMixin:
139
159
 
140
160
  user_ids = set(obj.user_id for obj in objects)
141
161
 
142
- users = {user.pk: user for user in SharedUser.objects.filter(pk__in=user_ids)}
162
+ users = {user.pk: user for user in User.objects.filter(pk__in=user_ids)}
143
163
 
144
164
  for obj in objects:
145
165
  obj._cached_user = users.get(obj.user_id)
@@ -152,9 +172,27 @@ class OrganizationUserQuerySetMixin(OrganizationQuerySetMixin, UserQuerySetMixin
152
172
 
153
173
  def with_auth_data(self):
154
174
  """
155
- Pré-carrega dados de organizações E usuários (evita N+1)
175
+ Pré-carrega dados de organizações E usuários (evita N+1 queries)
176
+
177
+ Faz apenas 3 queries no total, independente da quantidade de objetos:
178
+ 1. Query dos objetos principais
179
+ 2. Query bulk das organizações
180
+ 3. Query bulk dos usuários
181
+
182
+ Usage:
183
+ # Sem otimização (1 + N + N queries)
184
+ rascunhos = Rascunho.objects.all() # 1 query
185
+ for r in rascunhos:
186
+ print(r.organization.name) # N queries
187
+ print(r.user.email) # N queries
188
+
189
+ # Com otimização (3 queries total)
190
+ rascunhos = Rascunho.objects.all().with_auth_data()
191
+ for r in rascunhos:
192
+ print(r.organization.name) # Cache
193
+ print(r.user.email) # Cache
156
194
  """
157
- from . import SharedOrganization, SharedUser
195
+ from .models import SharedOrganization, User
158
196
 
159
197
  objects = list(self.all())
160
198
 
@@ -170,7 +208,7 @@ class OrganizationUserQuerySetMixin(OrganizationQuerySetMixin, UserQuerySetMixin
170
208
  org.pk: org for org in SharedOrganization.objects.filter(pk__in=org_ids)
171
209
  }
172
210
 
173
- users = {user.pk: user for user in SharedUser.objects.filter(pk__in=user_ids)}
211
+ users = {user.pk: user for user in User.objects.filter(pk__in=user_ids)}
174
212
 
175
213
  # Cachear
176
214
  for obj in objects:
@@ -183,7 +221,7 @@ class OrganizationUserQuerySetMixin(OrganizationQuerySetMixin, UserQuerySetMixin
183
221
  """
184
222
  Cria objeto com validação de organização e usuário
185
223
  """
186
- from . import SharedMember, SharedOrganization
224
+ from .models import SharedMember, SharedOrganization
187
225
 
188
226
  # Valida organização
189
227
  SharedOrganization.objects.get_or_fail(organization_id)
@@ -1,12 +1,16 @@
1
1
  """
2
2
  Serializers compartilhados para DRF
3
3
  """
4
- from .models import SharedOrganization, User
4
+
5
5
  from rest_framework import serializers
6
6
 
7
+ from .models import SharedOrganization, User
8
+
9
+
7
10
  class OrganizationSerializerMixin(serializers.ModelSerializer):
8
11
  """
9
12
  Mixin para serializers que incluem dados de organização como objeto aninhado
13
+ e automaticamente setam organization_id no create a partir do request context.
10
14
 
11
15
  Retorna:
12
16
  {
@@ -38,7 +42,9 @@ class OrganizationSerializerMixin(serializers.ModelSerializer):
38
42
  "id": org.pk,
39
43
  "name": org.name,
40
44
  "fantasy_name": org.fantasy_name,
41
- "image_organization": org.image_organization.url if org.image_organization else None,
45
+ "image_organization": org.image_organization.url
46
+ if org.image_organization
47
+ else None,
42
48
  "cnpj": org.cnpj,
43
49
  "email": org.email,
44
50
  "telephone": org.telephone,
@@ -46,9 +52,18 @@ class OrganizationSerializerMixin(serializers.ModelSerializer):
46
52
  "is_branch": org.is_branch,
47
53
  "is_active": org.is_active(),
48
54
  }
49
- except Exception as e:
55
+ except Exception:
50
56
  return None
51
57
 
58
+ def create(self, validated_data):
59
+ """Automatically set organization_id from request context"""
60
+ if self.context.get("request") and hasattr(
61
+ self.context["request"], "organization_id"
62
+ ):
63
+ validated_data["organization_id"] = self.context["request"].organization_id
64
+ return super().create(validated_data)
65
+
66
+
52
67
  class OrganizationSerializer(serializers.ModelSerializer):
53
68
  class Meta:
54
69
  model = SharedOrganization
@@ -71,9 +86,11 @@ class UserSerializer(serializers.ModelSerializer):
71
86
  "last_login",
72
87
  ]
73
88
 
89
+
74
90
  class UserSerializerMixin(serializers.ModelSerializer):
75
91
  """
76
92
  Mixin para serializers que incluem dados de usuário como objeto aninhado
93
+ e automaticamente setam user_id no create a partir do request context.
77
94
 
78
95
  Retorna:
79
96
  {
@@ -111,13 +128,20 @@ class UserSerializerMixin(serializers.ModelSerializer):
111
128
  "full_name": user.get_full_name(),
112
129
  "is_active": user.is_active,
113
130
  }
114
- except Exception as e:
131
+ except Exception:
115
132
  return None
116
133
 
134
+ def create(self, validated_data):
135
+ """Automatically set user_id from request context"""
136
+ if self.context.get("request") and hasattr(self.context["request"], "user"):
137
+ validated_data["user_id"] = self.context["request"].user.id
138
+ return super().create(validated_data)
139
+
117
140
 
118
141
  class OrganizationUserSerializerMixin(OrganizationSerializerMixin, UserSerializerMixin):
119
142
  """
120
143
  Mixin combinado com organization e user como objetos aninhados
144
+ e automaticamente seta organization_id e user_id no create a partir do request context.
121
145
 
122
146
  Retorna:
123
147
  {
@@ -142,13 +166,22 @@ class OrganizationUserSerializerMixin(OrganizationSerializerMixin, UserSerialize
142
166
  fields = ['id', 'titulo', 'conteudo', 'organization', 'user']
143
167
  """
144
168
 
145
- pass
169
+ def create(self, validated_data):
170
+ """Automatically set both organization_id and user_id from request context"""
171
+ if self.context.get("request"):
172
+ request = self.context["request"]
173
+ if hasattr(request, "organization_id"):
174
+ validated_data["organization_id"] = request.organization_id
175
+ if hasattr(request, "user"):
176
+ validated_data["user_id"] = request.user.id
177
+ return super(UserSerializerMixin, self).create(validated_data)
146
178
 
147
179
 
148
180
  # Versões simplificadas (opcional)
149
181
  class OrganizationSimpleSerializerMixin(serializers.ModelSerializer):
150
182
  """
151
183
  Versão simplificada que retorna apenas campos essenciais da organização
184
+ e automaticamente seta organization_id no create a partir do request context.
152
185
  """
153
186
 
154
187
  organization = serializers.SerializerMethodField()
@@ -164,10 +197,19 @@ class OrganizationSimpleSerializerMixin(serializers.ModelSerializer):
164
197
  except:
165
198
  return None
166
199
 
200
+ def create(self, validated_data):
201
+ """Automatically set organization_id from request context"""
202
+ if self.context.get("request") and hasattr(
203
+ self.context["request"], "organization_id"
204
+ ):
205
+ validated_data["organization_id"] = self.context["request"].organization_id
206
+ return super().create(validated_data)
207
+
167
208
 
168
209
  class UserSimpleSerializerMixin(serializers.ModelSerializer):
169
210
  """
170
211
  Versão simplificada que retorna apenas campos essenciais do usuário
212
+ e automaticamente seta user_id no create a partir do request context.
171
213
  """
172
214
 
173
215
  user = serializers.SerializerMethodField()
@@ -182,3 +224,9 @@ class UserSimpleSerializerMixin(serializers.ModelSerializer):
182
224
  }
183
225
  except:
184
226
  return None
227
+
228
+ def create(self, validated_data):
229
+ """Automatically set user_id from request context"""
230
+ if self.context.get("request") and hasattr(self.context["request"], "user"):
231
+ validated_data["user_id"] = self.context["request"].user.id
232
+ return super().create(validated_data)