Coverage for app/logic/utils.py: 68%

31 statements  

« prev     ^ index     » next       coverage.py v7.2.5, created at 2023-05-24 14:13 +0000

1import collections.abc as collections 

2from peewee import DoesNotExist 

3from app.models.term import Term 

4from datetime import datetime 

5 

6def selectSurroundingTerms(currentTerm, prevTerms=2): 

7 """ 

8 Returns a list of term objects around the provided Term object for the current term. 

9 Chooses the previous terms according to the prevTerms parameter (defaulting to 2), 

10 and then chooses terms for the next two years after the current term. 

11 

12 To get only the current and future terms, pass prevTerms=0. 

13 """ 

14 startTerm = max(1, currentTerm.id - prevTerms) 

15 surroundingTerms = (Term.select() 

16 .where(Term.id >= startTerm) 

17 .where((Term.year <= currentTerm.year + 2))) 

18 

19 return surroundingTerms 

20 

21def getStartofCurrentAcademicYear(currentTerm): 

22 if ("Summer" in currentTerm.description) or ("Spring" in currentTerm.description): 

23 fallTerm = Term.select().where(Term.year==currentTerm.year-1, Term.description == f"Fall {currentTerm.year-1}").get() 

24 return fallTerm 

25 return currentTerm 

26 

27def format24HourTime(unformattedTime): 

28 """ 

29 Turns a time string or datetime object into a string with a time in 24 hour format 

30 unformattedTime: expects a string with format HH:mm AM/PM or HH:mm OR a datetime object 

31 returns: a string in 24 hour format HH:mm 

32 """ 

33 if type(unformattedTime) == str: 

34 try: 

35 formattedTime = datetime.strptime(unformattedTime, "%I:%M %p").strftime("%H:%M") # Converts string to datetime then back to string and formats correctly 

36 return formattedTime 

37 except ValueError: 

38 # calling strptime here to explicitly raise an exception if it wasn't properly in 24 hour format 

39 formattedTime = datetime.strptime(unformattedTime, "%H:%M") 

40 return unformattedTime 

41 else: 

42 formattedTime = unformattedTime.strftime("%H:%M") 

43 return formattedTime 

44 

45def getUsernameFromEmail(email): 

46 return email.split("@")[0] 

47 

48def getFilesFromRequest(request): 

49 attachmentFiles = request.files.getlist("attachmentObject") 

50 fileDoesNotExist = attachmentFiles[0].content_type == "application/octet-stream" 

51 if fileDoesNotExist: 

52 attachmentFiles = None 

53 

54 return attachmentFiles