1import binascii
2import os
3import uuid
4
5from plain import models
6
7
8def generate_token():
9 return binascii.hexlify(os.urandom(20)).decode()
10
11
12class APIKey(models.Model):
13 uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
14 created_at = models.DateTimeField(auto_now_add=True)
15 updated_at = models.DateTimeField(auto_now=True)
16 expires_at = models.DateTimeField(blank=True, null=True)
17 last_used_at = models.DateTimeField(blank=True, null=True)
18
19 name = models.CharField(max_length=255, blank=True)
20
21 token = models.CharField(max_length=40, default=generate_token, unique=True)
22
23 # Connect to a user, for example, from your own model:
24 # api_key = models.OneToOneField(
25 # APIKey,
26 # on_delete=models.CASCADE,
27 # related_name="user",
28 # null=True,
29 # blank=True,
30 # )
31
32 def __str__(self):
33 return self.name or str(self.uuid)