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

51 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-07-11 17:51 +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 

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

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

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

25 

26 self._pmCache = {} 

27 self._bsCache = None 

28 

29 @property 

30 def isAdmin(self): 

31 return (self.isCeltsAdmin or self.isCeltsStudentStaff) 

32 

33 @property 

34 def isBonnerScholar(self): 

35 from app.models.bonnerCohort import BonnerCohort 

36 if self._bsCache is None: 

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

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

39 

40 return self._bsCache 

41 

42 @property 

43 def fullName(self): 

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

45 

46 def addProgramManager(self, program): 

47 # Makes a user a Program Manager 

48 from app.models.programManager import ProgramManager 

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

50 

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

52 

53 def removeProgramManager(self, program): 

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

55 from app.models.programManager import ProgramManager 

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

57 

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

59 

60 def isProgramManagerFor(self, program): 

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

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

63 if not program: 

64 return False 

65 

66 if program not in self._pmCache: 

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

68 

69 return self._pmCache[program] 

70 

71 def isProgramManagerForEvent(self, event): 

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

73 return self.isProgramManagerFor(event.program) 

74 

75