88from django .core .validators import EmailValidator
99from django .utils import timezone
1010
11- from .role import Role , Roles
1211from .utility import Utility
1312from .state import State
1413
@@ -45,8 +44,13 @@ def create_superuser(self, email, password=None, **extra_fields):
4544 if extra_fields .get ("is_superuser" ) is not True :
4645 raise ValueError ("Superuser must have is_superuser=True." )
4746
48- admin_role = Role .objects .get (pk = Roles .ADMINISTRATOR )
49- return self ._create_user (email , admin_role , password , ** extra_fields )
47+ return self ._create_user (email , Roles .ADMINISTRATOR , password , ** extra_fields )
48+
49+
50+ class Roles (models .TextChoices ):
51+ CONTRIBUTOR = "C"
52+ VALIDATOR = "V"
53+ ADMINISTRATOR = "A"
5054
5155
5256class User (AbstractBaseUser , PermissionsMixin ):
@@ -61,11 +65,10 @@ class User(AbstractBaseUser, PermissionsMixin):
6165 date_joined = models .DateTimeField (default = timezone .now )
6266 has_admin_generated_password = models .BooleanField (default = True )
6367
64- role = models .ForeignKey (
65- Role ,
66- related_name = "actors" ,
67- on_delete = models .PROTECT ,
68+ role = models .CharField (
6869 default = Roles .CONTRIBUTOR ,
70+ choices = Roles .choices ,
71+ max_length = 1 ,
6972 )
7073
7174 utilities = models .ManyToManyField (
@@ -81,11 +84,7 @@ class User(AbstractBaseUser, PermissionsMixin):
8184 )
8285
8386 def clean (self ):
84- if (
85- self .id
86- and self .role .pk == Roles .CONTRIBUTOR
87- and not self .utilities .exists ()
88- ):
87+ if self .id and self .role == Roles .CONTRIBUTOR and not self .utilities .exists ():
8988 raise ValidationError ("Contributors must be assigned a utility." )
9089
9190 super ().clean ()
0 commit comments