Browse Source

Refactor for starting with authorization methods

master
alfred 3 months ago
parent
commit
5edefd86e7

+ 4
- 20
apps/authentication/authenticators.py View File

@@ -1,25 +1,9 @@
1
-from dataclasses import asdict
2
-from apps.authentication import models
3
-from apps.authentication import forms
4
-from apps.authentication import mixins
1
+from apps.authentication import models, forms, mixins
2
+from apps.base.classes import DJaWThObject
5 3
 
6 4
 
7
-class Authenticator:
8
-    alias = ''
9
-    model = None
10
-    default = None
11
-    template = ''
12
-    help = ''
13
-    form = None
14
-
15
-    def __init__(self, **kwargs):
16
-        self.data = self.model(**kwargs)
17
-
18
-    def get_dict(self):
19
-        return asdict(self.data)
20
-
21
-    def __str__(self):
22
-        return self.alias
5
+class Authenticator(DJaWThObject):
6
+    pass
23 7
 
24 8
 
25 9
 class PasswordlessAuthenticator(mixins.PasswordlessMixin, Authenticator):

+ 1
- 43
apps/authentication/forms.py View File

@@ -1,47 +1,5 @@
1
-from django import forms
1
+from apps.base.forms import DataclassForm
2 2
 from .models import PasswordlessModel
3
-import dataclasses
4
-
5
-
6
-field_types = {
7
-    str: {
8
-        'field': forms.CharField,
9
-        'parameters': {}
10
-    },
11
-    bool: {
12
-        'field': forms.BooleanField,
13
-        'parameters': {
14
-            'required': False,
15
-            'widget': forms.CheckboxInput(
16
-                attrs={'style': 'opacity: 1; pointer-events: auto; position: inherit; margin-left: 10px;'}
17
-            )
18
-        }
19
-    }
20
-}
21
-
22
-
23
-class DataclassForm(forms.BaseForm):
24
-    dataclass = None
25
-    instance = None
26
-
27
-    def __init__(self, *args, **kwargs):
28
-        self.base_fields = {}
29
-        self.generate_fields()
30
-        super().__init__(*args, **kwargs)
31
-
32
-    def generate_fields(self):
33
-        dataclass = self.dataclass
34
-        for field in dataclasses.fields(dataclass):
35
-            if hasattr(self, field.name) and issubclass(type(getattr(self, field.name)), forms.Field):
36
-                self.base_fields[field.name] = getattr(self, field.name)
37
-                continue
38
-            f = field_types.get(field.type)
39
-            field_cls = f.get('field')
40
-            parameters = {
41
-                **f.get('parameters', {}),
42
-                **field.metadata
43
-            }
44
-            self.base_fields[field.name] = field_cls(**parameters)
45 3
 
46 4
 
47 5
 class PasswordlessForm(DataclassForm):

+ 1
- 1
apps/authentication/mixins/passwordless.py View File

@@ -1,2 +1,2 @@
1 1
 class PasswordlessMixin:
2
-    pass
2
+    pass

+ 5
- 0
apps/authorization/__init__.py View File

@@ -0,0 +1,5 @@
1
+from apps.authorization import authorizers as auth
2
+
3
+authorization_methods = {
4
+    'jwt': auth.JwtAuthorizer
5
+}

+ 5
- 0
apps/authorization/apps.py View File

@@ -0,0 +1,5 @@
1
+from django.apps import AppConfig
2
+
3
+
4
+class AuthorizationConfig(AppConfig):
5
+    name = 'authorization'

+ 9
- 0
apps/authorization/authorizers.py View File

@@ -0,0 +1,9 @@
1
+from apps.base.classes import DJaWThObject
2
+
3
+
4
+class Authorizer(DJaWThObject):
5
+    pass
6
+
7
+
8
+class JwtAuthorizer(Authorizer):
9
+    pass

+ 0
- 0
apps/authorization/forms.py View File


+ 0
- 0
apps/authorization/models.py View File


+ 18
- 0
apps/base/classes.py View File

@@ -0,0 +1,18 @@
1
+from dataclasses import asdict
2
+
3
+
4
+class DJaWThObject:
5
+    alias = ''
6
+    model = None
7
+    template = ''
8
+    help = ''
9
+    form = None
10
+
11
+    def __init__(self, **kwargs):
12
+        self.data = self.model(**kwargs)
13
+
14
+    def get_dict(self):
15
+        return asdict(self.data)
16
+
17
+    def __str__(self):
18
+        return self.alias

+ 43
- 0
apps/base/forms.py View File

@@ -0,0 +1,43 @@
1
+from django import forms
2
+import dataclasses
3
+
4
+
5
+field_types = {
6
+    str: {
7
+        'field': forms.CharField,
8
+        'parameters': {}
9
+    },
10
+    bool: {
11
+        'field': forms.BooleanField,
12
+        'parameters': {
13
+            'required': False,
14
+            'widget': forms.CheckboxInput(
15
+                attrs={'style': 'opacity: 1; pointer-events: auto; position: inherit; margin-left: 10px;'}
16
+            )
17
+        }
18
+    }
19
+}
20
+
21
+
22
+class DataclassForm(forms.BaseForm):
23
+    dataclass = None
24
+    instance = None
25
+
26
+    def __init__(self, *args, **kwargs):
27
+        self.base_fields = {}
28
+        self.generate_fields()
29
+        super().__init__(*args, **kwargs)
30
+
31
+    def generate_fields(self):
32
+        dataclass = self.dataclass
33
+        for field in dataclasses.fields(dataclass):
34
+            if hasattr(self, field.name) and issubclass(type(getattr(self, field.name)), forms.Field):
35
+                self.base_fields[field.name] = getattr(self, field.name)
36
+                continue
37
+            f = field_types.get(field.type)
38
+            field_cls = f.get('field')
39
+            parameters = {
40
+                **f.get('parameters', {}),
41
+                **field.metadata
42
+            }
43
+            self.base_fields[field.name] = field_cls(**parameters)

+ 1
- 1
apps/base/templates/base.html View File

@@ -29,7 +29,7 @@
29 29
         </div>
30 30
         {% if user.is_authenticated %}
31 31
         <div>
32
-            <a href="{% url 'logout' %}">Logout</a>
32
+            <a href="{% url 'home' %}">My projects</a> | <a href="{% url 'logout' %}">Logout</a>
33 33
         </div>
34 34
         {% endif %}
35 35
         {% endblock %}

+ 1
- 0
requirements.txt View File

@@ -1,5 +1,6 @@
1 1
 asgiref==3.2.3
2 2
 Django==3.0.1
3 3
 psycopg2-binary==2.8.4
4
+PyJWT==1.7.1
4 5
 pytz==2019.3
5 6
 sqlparse==0.3.0

Loading…
Cancel
Save