utg-base 1.15.0__py3-none-any.whl → 1.16.0__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.
@@ -37,3 +37,20 @@ class DateTimePeriodSerializer(DateSerializer):
37
37
  })
38
38
  return attrs
39
39
 
40
+
41
+ class RelatedObjectField(serializers.PrimaryKeyRelatedField):
42
+ """
43
+ POST / PUT -> UUID (string)
44
+ RESPONSE -> {id, name}
45
+ """
46
+ fields = []
47
+
48
+ def __init__(self, **kwargs):
49
+ self.fields = kwargs.pop("fields", ("id", "name"))
50
+ super().__init__(**kwargs)
51
+
52
+ def use_pk_only_optimization(self):
53
+ return False
54
+
55
+ def to_representation(self, value):
56
+ return {f: str(getattr(value, f)) for f in self.fields}
@@ -18,7 +18,7 @@ def has_perm(*permission: str, operator: Literal["OR", "AND"] = "OR"):
18
18
  view_func._perms = permission
19
19
  @wraps(view_func)
20
20
  def _wrapped_view(self, request, *args, **kwargs):
21
- if not has_permissions(user_id=request.user.id, perms=list(permission), operator=operator):
21
+ if not has_permissions(user_id=request.user.id, perms=list(permission), operator=operator, request=request):
22
22
  raise PermissionDenied
23
23
  return view_func(self, request, *args, **kwargs)
24
24
  return _wrapped_view
@@ -54,7 +54,7 @@ def has_class_perm(permissions_map: Dict[str, Union[str, List[str]]], operator:
54
54
  if isinstance(required_perms, str):
55
55
  required_perms = [required_perms]
56
56
 
57
- if not has_permissions(user_id=request.user.id, perms=required_perms, operator=operator):
57
+ if not has_permissions(user_id=request.user.id, perms=required_perms, operator=operator, request=request):
58
58
  raise PermissionDenied
59
59
 
60
60
  return original_initial(self, request, *args, **kwargs)
@@ -18,13 +18,17 @@ def generate_perm_cache_key(user_id: str):
18
18
  return f"user:{user_id}:permissions"
19
19
 
20
20
 
21
- def has_perm(user_id, perm):
21
+ def has_perm(user_id, perm, request=None):
22
+ if request is not None and isinstance(request, Request) and request.user.is_superuser:
23
+ return True
22
24
  redis_conn: Redis = get_redis_connection("shared")
23
25
  perm = f"{to_snake_case(env('APP_NAME'))}:{perm}"
24
26
  return bool(redis_conn.sismember(generate_perm_cache_key(user_id), perm))
25
27
 
26
28
 
27
- def has_perms(user_id: str, perms: list[str], operator: Literal["OR", "AND"] = "OR"):
29
+ def has_perms(user_id: str, perms: list[str], operator: Literal["OR", "AND"] = "OR", request=None):
30
+ if request is not None and isinstance(request, Request) and request.user.is_superuser:
31
+ return True
28
32
  redis_conn: Redis = get_redis_connection("shared")
29
33
  perms = [f"{to_snake_case(env('APP_NAME'))}:{perm}" for perm in perms]
30
34
  result = redis_conn.smismember(generate_perm_cache_key(user_id), perms)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: utg-base
3
- Version: 1.15.0
3
+ Version: 1.16.0
4
4
  Summary: UTG Base Package
5
5
  Author: Olimboy
6
6
  Author-email: shavkatov.olimboy@mail.ru
@@ -4,7 +4,7 @@ utg_base/api/base.py,sha256=giGKC68sL1j1NQbbkhIWzhHPcPlvjTPG2evra-xAJ3s,2574
4
4
  utg_base/api/pagination.py,sha256=zzIUwW3iF5G_11gFsno9y1DmgFiULQIWRHUj0LKhYfE,854
5
5
  utg_base/api/permissions.py,sha256=RjkxYWPl5Xwgk5lIZrcBzIAwlXolX5XPlae1jr-Ln2w,323
6
6
  utg_base/api/routers.py,sha256=lU54MVN2BF_q1AWp9EdXkG3m_ivYRtvbNGXFIRKz7u0,177
7
- utg_base/api/serializers.py,sha256=qI6wWjwl1oeUPHCJUCpYFIaiRFvfQW6FM0xPC9fwfQI,1214
7
+ utg_base/api/serializers.py,sha256=d3tfiIqgKk1TMe3GNXchZY0LWhjoFFZBRouT9kAhPlw,1668
8
8
  utg_base/api/spectacular.py,sha256=W1G2c6Mw8OkTVwXHxJYzbadW9jykkkAn0C8tQu9gfuA,1047
9
9
  utg_base/authentications/__init__.py,sha256=a6twO_bBf8FAHYl7PXawfR2UbBwwdueG1uS_dbq2g_I,109
10
10
  utg_base/authentications/microservice_authentication.py,sha256=6aAncxIpA4FcyRegd7QqRYvW5Wn8FxyPU0nQqCVuEs4,976
@@ -39,9 +39,9 @@ utg_base/models/__init__.py,sha256=1zXygGICiR3iUCKdkNal9d3i3kNp654gFgBf_VlR2gI,6
39
39
  utg_base/models/jwt_user.py,sha256=6TQ5wB_OZBtGhRL-2MonBGZm0n0Y86s4BRTxiRlUJOk,375
40
40
  utg_base/models/timestamp.py,sha256=AkCliNXnvs8Z17b1mcS7gOK7v6h3Jul6WCyGyVAkb-w,217
41
41
  utg_base/permissions/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
42
- utg_base/permissions/decorators.py,sha256=bDmC_fUuOIo-Lp92wO4B3q5XtwhC4uU4n3NamDft4vI,2066
42
+ utg_base/permissions/decorators.py,sha256=chjOUOQAF0qbedDtOebGzp1HUeyhFLlt9fnWmnsg6u0,2100
43
43
  utg_base/permissions/folder.py,sha256=uJv40FVb7R379qss66a5oUcLK7KCUIL6DPbzEcGOw38,694
44
- utg_base/permissions/utils.py,sha256=8nbs9PwWEtInFv8wRDMre1pAPu_FwmVz7A07i_WikU8,2591
44
+ utg_base/permissions/utils.py,sha256=Jhm3RIe9Pcm5eZIHrRMiHo8jv7py5Xl3GGNOBumKl6k,2841
45
45
  utg_base/references_api/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
46
46
  utg_base/references_api/apps.py,sha256=thAGmO-ZT-OD9dHHBSQRL_RRt-Es_jt-mEmHgVTpERs,168
47
47
  utg_base/references_api/migrations/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -68,6 +68,6 @@ utg_base/utils/sql.py,sha256=rqIWcSjdjIMszdRnsnhV5TTYB8W17RPOujIQA9rKC_Y,762
68
68
  utg_base/utils/string.py,sha256=ATwIo9uLa00p85h_NjRYLcjRs8o3KSGF7s2yhTg5GiA,1073
69
69
  utg_base/utils/thread.py,sha256=4RqRnwtyHymY-dNcuPrMSTamE2V7wCMVfzzyIb0P4TI,2191
70
70
  utg_base/utils/translation.py,sha256=GxJHUt0iar_0E7RWBPbeLFQ4DhgXBjffHCmxfKyjFtk,463
71
- utg_base-1.15.0.dist-info/METADATA,sha256=1ltxZeXg6sjr09qwXTbJivshbgsYh42Dy2yVuyoK8GE,917
72
- utg_base-1.15.0.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
73
- utg_base-1.15.0.dist-info/RECORD,,
71
+ utg_base-1.16.0.dist-info/METADATA,sha256=H-3iQRd2ckqkfzUM2uEYDsoCDX-oQsudU6_DcQytabQ,917
72
+ utg_base-1.16.0.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
73
+ utg_base-1.16.0.dist-info/RECORD,,