2022-10-27 09:20:31 +00:00
|
|
|
import uuid
|
|
|
|
|
2022-12-05 10:54:12 +00:00
|
|
|
from django.conf import settings
|
2022-10-27 09:20:31 +00:00
|
|
|
from django.db import models
|
2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2022-12-05 10:54:12 +00:00
|
|
|
|
|
|
|
import common.db.models
|
2023-10-10 09:52:52 +00:00
|
|
|
from common.utils.random import random_string
|
|
|
|
|
|
|
|
|
|
|
|
def default_secret():
|
2023-11-10 07:27:20 +00:00
|
|
|
return random_string(36)
|
2022-10-27 09:20:31 +00:00
|
|
|
|
|
|
|
|
2023-11-13 06:34:58 +00:00
|
|
|
def default_ip_group():
|
2023-10-31 06:15:07 +00:00
|
|
|
return ["*"]
|
|
|
|
|
|
|
|
|
2022-10-27 09:20:31 +00:00
|
|
|
class AccessKey(models.Model):
|
2023-10-10 09:52:52 +00:00
|
|
|
id = models.UUIDField(verbose_name='AccessKeyID', primary_key=True, default=uuid.uuid4, editable=False)
|
|
|
|
secret = models.CharField(verbose_name='AccessKeySecret', default=default_secret, max_length=36)
|
2023-11-13 06:34:58 +00:00
|
|
|
ip_group = models.JSONField(default=default_ip_group, verbose_name=_('IP group'))
|
2022-10-27 09:20:31 +00:00
|
|
|
user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='User',
|
2022-12-05 10:54:12 +00:00
|
|
|
on_delete=common.db.models.CASCADE_SIGNAL_SKIP, related_name='access_keys')
|
2022-10-27 09:20:31 +00:00
|
|
|
is_active = models.BooleanField(default=True, verbose_name=_('Active'))
|
2023-10-10 09:52:52 +00:00
|
|
|
date_last_used = models.DateTimeField(null=True, blank=True, verbose_name=_('Date last used'))
|
2022-10-27 09:20:31 +00:00
|
|
|
date_created = models.DateTimeField(auto_now_add=True)
|
|
|
|
|
|
|
|
def get_id(self):
|
|
|
|
return str(self.id)
|
|
|
|
|
|
|
|
def get_secret(self):
|
|
|
|
return str(self.secret)
|
|
|
|
|
|
|
|
def get_full_value(self):
|
|
|
|
return '{}:{}'.format(self.id, self.secret)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return str(self.id)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _("Access key")
|