-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
100 lines (84 loc) · 2.38 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from uuid import uuid4
from django.db import models
from django.utils.translation import ugettext_lazy as _
# Create your models here.
class AbstractKey(models.Model):
"""
This is an abstract class that defines the basic items of the key model.
"""
id = models.AutoField(
verbose_name=_('ID'),
primary_key=True,
)
key = models.UUIDField(
verbose_name=_('Key'),
default=uuid4,
unique=True,
)
updated_at = models.DateTimeField(
verbose_name=_('Updated at'),
auto_now=True,
)
class Meta:
abstract = True
def __str__(self):
return self.key
@property
def generate_key(self):
"""
To generate a unique key, use uuid4.
"""
return uuid4().hex
class AuthKey(AbstractKey):
"""
This is the model that defines the authentication key.
Authentication key shall be used from the front end.
The user can have only one authentication key.
"""
owner = models.OneToOneField(
'auth.User',
verbose_name=_('Owner'),
related_name=_('auth_key'),
on_delete=models.CASCADE,
)
class Meta:
verbose_name = _('Auth key')
verbose_name_plural = _('Auth keys')
db_table = 'jk_auth_keys'
class RefreshKey(AbstractKey):
"""
This is the model that defines the refresh key.
Refresh keys are used to update the same user's authentication key.
The user can have only one refresh key.
"""
owner = models.OneToOneField(
'auth.User',
verbose_name=_('Owner'),
related_name=_('refresh_key'),
on_delete=models.CASCADE,
)
class Meta:
verbose_name = _('Refresh key')
verbose_name_plural = _('Refresh keys')
db_table = 'jk_refresh_keys'
class AccessKey(AbstractKey):
"""
This is the model that defines the access key.
Access key shall be used from the other systems.
A user can have multiple access keys.
"""
name = models.CharField(
verbose_name=_('Name'),
max_length=255,
blank=True,
)
owner = models.ForeignKey(
'auth.User',
verbose_name=_('Owner'),
related_name=_('access_keys'),
on_delete=models.CASCADE,
)
class Meta:
verbose_name = _('Access key')
verbose_name_plural = _('Access keys')
db_table = 'jk_access_keys'