Coverage for django_napse/auth/models/permissions/permission.py: 54%

28 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-03-12 13:49 +0000

1import uuid 

2 

3from django.db import models 

4 

5from django_napse.utils.constants import PERMISSION_TYPES 

6 

7 

8class KeyPermission(models.Model): 

9 uuid = models.UUIDField(unique=True, editable=False, default=uuid.uuid4) 

10 

11 key = models.ForeignKey("NapseAPIKey", on_delete=models.CASCADE, related_name="permissions") 

12 space = models.ForeignKey("django_napse_core.NapseSpace", on_delete=models.CASCADE, related_name="permissions") 

13 approved = models.BooleanField(default=False) 

14 revoked = models.BooleanField(default=False) 

15 permission_type = models.CharField(max_length=200) 

16 

17 class Meta: 

18 unique_together = ["key", "space", "permission_type"] 

19 

20 def __str__(self): 

21 return f"NAPSE KEY PERMISSION: {self.permission_type}" 

22 

23 def save(self, *args, **kwargs): 

24 if self.permission_type not in PERMISSION_TYPES: 

25 error_msg = f"Permission type ({self.permission_type}) is not valid. Valid permission types are: {PERMISSION_TYPES}" 

26 raise ValueError(error_msg) 

27 super().save(*args, **kwargs) 

28 

29 def info(self, verbose=True, beacon=""): 

30 string = "" 

31 string += f"{beacon}Napse Key Permission ({self.pk=}):\n" 

32 string += f"{beacon}Args:\n" 

33 string += f"{beacon}\t{self.key=}\n" 

34 string += f"{beacon}\t{self.space=}\n" 

35 string += f"{beacon}\t{self.permission_type=}\n" 

36 string += f"{beacon}\t{self.valid=}\n" 

37 if verbose: # pragma: no cover 

38 print(string) 

39 return string