utg-base 1.13.1__py3-none-any.whl → 1.14.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.
@@ -1,5 +1,5 @@
1
1
  from functools import wraps
2
- from typing import Literal
2
+ from typing import Literal, Dict, Union, List
3
3
 
4
4
  from rest_framework.exceptions import PermissionDenied
5
5
 
@@ -23,3 +23,43 @@ def has_perm(*permission: str, operator: Literal["OR", "AND"] = "OR"):
23
23
  return view_func(self, request, *args, **kwargs)
24
24
  return _wrapped_view
25
25
  return decorator
26
+
27
+
28
+ def has_class_perm(permissions_map: Dict[str, Union[str, List[str]]], operator: Literal["OR", "AND"] = "OR"):
29
+ """
30
+ Usage:
31
+ @has_class_perm({
32
+ 'list': ['perm1', 'perm2'],
33
+ 'create': 'perm3',
34
+ 'update': ['perm4']
35
+ })
36
+ """
37
+ def decorator(cls):
38
+ for action, perms in permissions_map.items():
39
+ if isinstance(perms, str):
40
+ perms = [perms]
41
+
42
+ if hasattr(cls, action):
43
+ method = getattr(cls, action)
44
+ setattr(method, "_perms", perms)
45
+ original_initial = cls.initial
46
+
47
+ @wraps(original_initial)
48
+ def new_initial(self, request, *args, **kwargs):
49
+ action = getattr(self, 'action', None) or request.method.lower()
50
+
51
+ if action in permissions_map:
52
+ required_perms = permissions_map[action]
53
+
54
+ if isinstance(required_perms, str):
55
+ required_perms = [required_perms]
56
+
57
+ if not has_permissions(user_id=request.user.id, perms=required_perms, operator=operator):
58
+ raise PermissionDenied
59
+
60
+ return original_initial(self, request, *args, **kwargs)
61
+
62
+ cls.initial = new_initial
63
+ return cls
64
+
65
+ return decorator
@@ -1,3 +1,4 @@
1
+ import traceback
1
2
  from typing import Literal
2
3
 
3
4
  from django.http import HttpRequest
@@ -72,6 +73,6 @@ def sync_permissions():
72
73
  "service": to_snake_case(env('APP_NAME')),
73
74
  "codes": perm_codes
74
75
  })
75
- print(response.json())
76
+ print('[INFO] sync_permissions:', response.json())
76
77
  except Exception as e:
77
- print(e)
78
+ traceback.print_exc()
@@ -8,7 +8,7 @@ from drf_spectacular.utils import extend_schema, extend_schema_serializer
8
8
  from rest_framework import filters
9
9
  from rest_framework import viewsets, serializers
10
10
 
11
- from ..api.views import TranslatedListView
11
+ from utg_base.permissions.decorators import has_class_perm
12
12
 
13
13
  serializer_classes = {}
14
14
 
@@ -82,7 +82,7 @@ def create_view_set(model: Model):
82
82
  fields_list = get_model_fields_list(model)
83
83
 
84
84
  @extend_schema(tags=[get_url_prefix(model)])
85
- class ViewSet(viewsets.ModelViewSet, TranslatedListView):
85
+ class ViewSet(viewsets.ModelViewSet):
86
86
  http_method_names = get_api_meta_property(model, 'http_method_names') or ['get', 'patch']
87
87
  manager = model.objects.order_by(*get_ordering(model))
88
88
  filter_backends = [filters.SearchFilter]
@@ -98,6 +98,9 @@ def create_view_set(model: Model):
98
98
 
99
99
  return create_serializer(model)
100
100
 
101
+ if permissions := get_api_meta_property(model, 'permissions'):
102
+ has_class_perm(permissions)(ViewSet)
103
+
101
104
  return ViewSet
102
105
 
103
106
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: utg-base
3
- Version: 1.13.1
3
+ Version: 1.14.0
4
4
  Summary: UTG Base Package
5
5
  Author: Olimboy
6
6
  Author-email: shavkatov.olimboy@mail.ru
@@ -6,7 +6,6 @@ utg_base/api/permissions.py,sha256=RjkxYWPl5Xwgk5lIZrcBzIAwlXolX5XPlae1jr-Ln2w,3
6
6
  utg_base/api/routers.py,sha256=lU54MVN2BF_q1AWp9EdXkG3m_ivYRtvbNGXFIRKz7u0,177
7
7
  utg_base/api/serializers.py,sha256=qI6wWjwl1oeUPHCJUCpYFIaiRFvfQW6FM0xPC9fwfQI,1214
8
8
  utg_base/api/spectacular.py,sha256=W1G2c6Mw8OkTVwXHxJYzbadW9jykkkAn0C8tQu9gfuA,1047
9
- utg_base/api/views.py,sha256=yYCEJRouFA71cI2Ubc1A736oLg9NGWyTIVnD-Q85k6w,279
10
9
  utg_base/authentications/__init__.py,sha256=a6twO_bBf8FAHYl7PXawfR2UbBwwdueG1uS_dbq2g_I,109
11
10
  utg_base/authentications/microservice_authentication.py,sha256=6aAncxIpA4FcyRegd7QqRYvW5Wn8FxyPU0nQqCVuEs4,976
12
11
  utg_base/authentications/models.py,sha256=JQonSdXeSeoF003QlmPvH58nWmVJRKlWWjW_ySqXaYg,2496
@@ -40,15 +39,13 @@ utg_base/models/__init__.py,sha256=1zXygGICiR3iUCKdkNal9d3i3kNp654gFgBf_VlR2gI,6
40
39
  utg_base/models/jwt_user.py,sha256=6TQ5wB_OZBtGhRL-2MonBGZm0n0Y86s4BRTxiRlUJOk,375
41
40
  utg_base/models/timestamp.py,sha256=AkCliNXnvs8Z17b1mcS7gOK7v6h3Jul6WCyGyVAkb-w,217
42
41
  utg_base/permissions/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
43
- utg_base/permissions/decorators.py,sha256=JSxyt8LCZWSWBm3Dk9y4JrtgoFH0sWN-MISkn0Z6g7s,777
44
- utg_base/permissions/utils.py,sha256=IZfPxIzfZjOwdp06yF6Dp9lUn0o7M0DySIFOhrFwc0k,2527
42
+ utg_base/permissions/decorators.py,sha256=bDmC_fUuOIo-Lp92wO4B3q5XtwhC4uU4n3NamDft4vI,2066
43
+ utg_base/permissions/utils.py,sha256=zgwyp6r-CySPrUFCZ1qm8_4UzsbtMYPlgzyCBpOQ4BU,2585
45
44
  utg_base/references_api/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
46
- utg_base/references_api/admin.py,sha256=suMo4x8I3JBxAFBVIdE-5qnqZ6JAZV0FESABHOSc-vg,63
47
45
  utg_base/references_api/apps.py,sha256=thAGmO-ZT-OD9dHHBSQRL_RRt-Es_jt-mEmHgVTpERs,168
48
46
  utg_base/references_api/migrations/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
49
- utg_base/references_api/models.py,sha256=Vjc0p2XbAPgE6HyTF6vll98A4eDhA5AvaQqsc4kQ9AQ,57
50
47
  utg_base/references_api/urls.py,sha256=WkLACQ8GfK5pJkvt8FuYdOxcqSZHj7pYRw51M9WluGw,390
51
- utg_base/references_api/utils.py,sha256=o-mM5S6LRvMZFQU0BAKFD0Kido28_ZytN-DttME2Rjk,5794
48
+ utg_base/references_api/utils.py,sha256=ndbeLXqLMUyCqqs_xqEFOCaE1rYnLxxI-LMeQx2YsjY,5903
52
49
  utg_base/services/__init__.py,sha256=LqtwUiqEZPIbKRGJfve5D5m3ucV6Kw1Nbo5Jnj_hPhY,37
53
50
  utg_base/services/base_api.py,sha256=bMTmjy8TRN4WTCgV_1RvkquoqijJQ1hP7BBtPgW_AH0,5677
54
51
  utg_base/signals/__init__.py,sha256=5SKumqAQrfrXUzi4NO3zjPAXV2TG3EDKxOomaF-e15M,35
@@ -70,6 +67,6 @@ utg_base/utils/sql.py,sha256=rqIWcSjdjIMszdRnsnhV5TTYB8W17RPOujIQA9rKC_Y,762
70
67
  utg_base/utils/string.py,sha256=ATwIo9uLa00p85h_NjRYLcjRs8o3KSGF7s2yhTg5GiA,1073
71
68
  utg_base/utils/thread.py,sha256=4RqRnwtyHymY-dNcuPrMSTamE2V7wCMVfzzyIb0P4TI,2191
72
69
  utg_base/utils/translation.py,sha256=GxJHUt0iar_0E7RWBPbeLFQ4DhgXBjffHCmxfKyjFtk,463
73
- utg_base-1.13.1.dist-info/METADATA,sha256=ooe4mp1NlzWiplg5BpyZqymbZ5AzcOsBNjo1gQ5SHqY,917
74
- utg_base-1.13.1.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
75
- utg_base-1.13.1.dist-info/RECORD,,
70
+ utg_base-1.14.0.dist-info/METADATA,sha256=iXWdpkKrzStHPqX6amNh9A2mXU-F9xZRnHynmSUte1k,917
71
+ utg_base-1.14.0.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
72
+ utg_base-1.14.0.dist-info/RECORD,,
utg_base/api/views.py DELETED
@@ -1,13 +0,0 @@
1
-
2
- from django.db.models import Manager
3
- from rest_framework import generics
4
-
5
- from .base import BaseAPIView
6
-
7
-
8
- class TranslatedListView(generics.ListAPIView, BaseAPIView):
9
- manager: Manager
10
-
11
- def get_queryset(self):
12
- self.update_lang()
13
- return self.manager.all()
@@ -1,3 +0,0 @@
1
- from django.contrib import admin
2
-
3
- # Register your models here.
@@ -1,3 +0,0 @@
1
- from django.db import models
2
-
3
- # Create your models here.