Coverage for app/models/user.py: 98%

52 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-09-06 19:35 +0000

1from app.models import * 

2 

3 

4class User(baseModel): 

5 username = CharField(primary_key = True) 

6 bnumber = CharField(unique = True) 

7 email = CharField() 

8 phoneNumber = CharField(null = True) 

9 firstName = CharField() 

10 lastName = CharField() 

11 cpoNumber = CharField(null=False) 

12 isStudent = BooleanField(default = False) 

13 major = CharField(null = True) 

14 classLevel = CharField(null = True) 

15 isFaculty = BooleanField(default = False) 

16 isStaff = BooleanField(default = False) 

17 isCeltsAdmin = BooleanField(default =False) 

18 isCeltsStudentStaff = BooleanField(default = False) 

19 dietRestriction = TextField(null=True) 

20 minorInterest = BooleanField(default=False) 

21 hasGraduated = BooleanField(default=False) 

22 

23 # override BaseModel's __init__ so that we can set up an instance attribute for cache 

24 def __init__(self,*args, **kwargs): 

25 super().__init__(*args,**kwargs) 

26 

27 self._pmCache = {} 

28 self._bsCache = None 

29 

30 @property 

31 def isAdmin(self): 

32 return (self.isCeltsAdmin or self.isCeltsStudentStaff) 

33 

34 @property 

35 def isBonnerScholar(self): 

36 from app.models.bonnerCohort import BonnerCohort 

37 if self._bsCache is None: 

38 # TODO should we exclude users who are banned from Bonner here? 

39 self._bsCache = BonnerCohort.select().where(BonnerCohort.user == self).exists() 

40 

41 return self._bsCache 

42 

43 @property 

44 def fullName(self): 

45 return f"{self.firstName} {self.lastName}" 

46 

47 def addProgramManager(self, program): 

48 # Makes a user a Program Manager 

49 from app.models.programManager import ProgramManager 

50 ProgramManager.create(user = self, program = program) 

51 

52 return (f' {self} added as Program Manager') 

53 

54 def removeProgramManager(self, program): 

55 # Removes an existing Program Manager from being a Program Manager 

56 from app.models.programManager import ProgramManager 

57 ProgramManager.delete().where(ProgramManager.user == self, ProgramManager.program == program).execute() 

58 

59 return (f'{self} removed from Program Manager') 

60 

61 def isProgramManagerFor(self, program): 

62 # Looks to see who is the Program Manager for a program 

63 from app.models.programManager import ProgramManager # Must defer import until now to avoid circular reference 

64 if not program: 

65 return False 

66 

67 if program not in self._pmCache: 

68 self._pmCache[program] = ProgramManager.select().where(ProgramManager.user == self, ProgramManager.program == program).exists() 

69 

70 return self._pmCache[program] 

71 

72 def isProgramManagerForEvent(self, event): 

73 # Looks to see who the Program Manager for a specific event is 

74 return self.isProgramManagerFor(event.program) 

75 

76