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

41 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-01-16 20:21 +0000

1from datetime import datetime 

2import collections.abc as collections 

3 

4from flask import session 

5from peewee import DoesNotExist 

6 

7from app.models.term import Term 

8 

9def selectSurroundingTerms(currentTerm, prevTerms=2): 

10 """ 

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

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

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

14 

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

16 """ 

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

18 surroundingTerms = (Term.select() 

19 .where(Term.id >= startTerm) 

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

21 .order_by(Term.termOrder)) 

22 

23 return surroundingTerms 

24 

25def getStartofCurrentAcademicYear(currentTerm): 

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

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

28 return fallTerm 

29 return currentTerm 

30 

31def format24HourTime(unformattedTime): 

32 """ 

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

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

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

36 """ 

37 if type(unformattedTime) == str: 

38 try: 

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

40 return formattedTime 

41 except ValueError: 

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

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

44 return unformattedTime 

45 else: 

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

47 return formattedTime 

48 

49def getUsernameFromEmail(email): 

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

51 

52def getFilesFromRequest(request): 

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

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

55 if fileDoesNotExist: 

56 attachmentFiles = None 

57 

58 return attachmentFiles 

59 

60def getRedirectTarget(popTarget=False): 

61 """ 

62 This function returns a string with the URL or route to a page in the Application 

63 saved with setRedirectTarget() and is able to pop the value from the session 

64 to make it an empty value 

65 popTarget: expects a bool value to determine whether or not to reset 

66 redirectTarget to an emtpy value 

67 return: a string with the URL or route to a page in the application that was 

68 saved in setRedirectTarget() 

69 """ 

70 if "redirectTarget" not in session: 

71 return '' 

72 

73 target = session["redirectTarget"] 

74 if popTarget: 

75 session.pop("redirectTarget") 

76 return target 

77 

78def setRedirectTarget(target): 

79 """ 

80 This function saves the target URL in the session for future redirection 

81 to said page 

82 target: expects a string that is a URL or a route to a page in the application 

83 return: None 

84 """ 

85 session["redirectTarget"] = target 

86