-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmodels.py
79 lines (62 loc) · 2.19 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
from datetime import datetime
from django.db import models
from django.contrib.auth.models import (
BaseUserManager,
AbstractBaseUser,
PermissionsMixin,
)
from django.core.exceptions import ValidationError
from web3 import Web3
def validate_ethereum_address(value):
if not Web3.isChecksumAddress(value):
raise ValidationError
class WalletManager(BaseUserManager):
def create_user(self, ethereum_address: str):
"""
Creates and saves a User with the given ethereum address.
"""
if not ethereum_address:
raise ValueError("Users must have an ethereum address")
wallet = self.model()
wallet.ethereum_address = ethereum_address
wallet.save(using=self._db)
return wallet
def create_superuser(self, ethereum_address: str):
"""
Creates and saves a superuser with the given ethereum address.
"""
user = self.create_user(
ethereum_address=ethereum_address,
)
user.set_unusable_password()
user.is_admin = True
user.is_superuser = True
user.save(using=self._db)
return user
class Wallet(AbstractBaseUser, PermissionsMixin):
# EIP-55 compliant: https://eips.ethereum.org/EIPS/eip-55
ethereum_address = models.CharField(
unique=True,
primary_key=True,
max_length=42,
validators=[validate_ethereum_address],
)
ens_name = models.CharField(max_length=255, blank=True, null=True)
ens_avatar = models.CharField(max_length=255, blank=True, null=True)
created = models.DateTimeField("datetime created", auto_now_add=True)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
USERNAME_FIELD = "ethereum_address"
objects = WalletManager()
def __str__(self):
return self.ethereum_address
@property
def is_staff(self):
"Is the user a member of staff?"
# Simplest possible answer: All admins are staff
return self.is_admin
class Nonce(models.Model):
value = models.CharField(max_length=24, primary_key=True)
expiration = models.DateTimeField()
def __str__(self):
return self.value