1010from django .template .loader import render_to_string
1111
1212from rest_framework .exceptions import ValidationError
13- from api .utils import validate_reserved_names , get_local_host
13+ from api .utils import validate_reserved_names , get_local_host , get_next_uid
14+ from api .models .base import UuidAuditedModel
1415
1516User = get_user_model ()
1617logger = logging .getLogger (__name__ )
1718
1819
19- def validate_workspace_name (value ):
20+ def validate_workspace_id (value ):
2021 """
2122 Check that the value follows the kubernetes name constraints
2223 """
@@ -27,25 +28,29 @@ def validate_workspace_name(value):
2728 validate_reserved_names (value )
2829
2930
30- class Workspace (models . Model ):
31- name = models .SlugField (
31+ class Workspace (UuidAuditedModel ):
32+ id = models .SlugField (
3233 _ ("workspace name" ),
3334 max_length = 150 ,
3435 unique = True ,
35- validators = [validate_workspace_name ],
36+ validators = [validate_workspace_id ],
3637 )
38+ uid = models .PositiveIntegerField (unique = True , editable = False )
3739 email = models .EmailField (_ ("email address" ))
38- created = models .DateTimeField (auto_now_add = True )
39- updated = models .DateTimeField (auto_now = True )
4040
4141 def has_member (self , user , role = None ):
4242 kwargs = {'user' : user , 'workspace' : self }
4343 if role :
4444 kwargs ['role' ] = role
4545 return WorkspaceMember .objects .filter (** kwargs ).exists ()
4646
47+ def save (self , * args , ** kwargs ):
48+ if not self .uid :
49+ self .uid = get_next_uid (Workspace )
50+ super ().save (* args , ** kwargs )
51+
4752 def __str__ (self ):
48- return self .name
53+ return self .id
4954
5055
5156class WorkspaceMember (models .Model ):
@@ -63,7 +68,7 @@ class WorkspaceMember(models.Model):
6368 workspace = models .ForeignKey (Workspace , on_delete = models .CASCADE )
6469
6570 def __str__ (self ):
66- return f"{ self .user .username } - { self .workspace .name } ({ self .role } )"
71+ return f"{ self .user .username } - { self .workspace .id } ({ self .role } )"
6772
6873 class Meta :
6974 unique_together = ('user' , 'workspace' )
@@ -97,12 +102,12 @@ def send_email(self, request):
97102 if count > settings .DRYCC_INVITATION_EMAIL_LIMIT :
98103 raise ValidationError ("Too many invitation emails, please try again later" )
99104 domain = get_local_host (request )
100- mail_subject = f'We Invite You to Join the { self .workspace .name } Workspace.'
105+ mail_subject = f'We Invite You to Join the { self .workspace .id } Workspace.'
101106 message = render_to_string (
102107 'workspace/workspace_invitation.html' ,
103108 {'domain' : domain , 'invitation' : self }
104109 )
105110 send_mail (mail_subject , message , None , [self .email ], fail_silently = True )
106111
107112 def __str__ (self ):
108- return f"Invitation for { self .email } to join { self .workspace .name } "
113+ return f"Invitation for { self .email } to join { self .workspace .id } "
0 commit comments