Coverage for app/logic/utils.py: 65%
49 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-07-24 12:19 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2024-07-24 12:19 +0000
1from datetime import datetime
2import collections.abc as collections
4from flask import session
5from peewee import DoesNotExist
7from app.models.term import Term
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.
15 To get only the current and future terms, pass prevTerms=0.
16 """
18 # Find the starting term
19 startOrder = currentTerm.termOrder
20 while(prevTerms > 0):
21 orderPieces = startOrder.split('-')
22 if(orderPieces[1] == "1"):
23 orderPieces[0] = int(orderPieces[0]) - 1
24 orderPieces[1] = 3
25 else:
26 orderPieces[1] = int(orderPieces[1]) - 1
27 startOrder = f"{orderPieces[0]}-{orderPieces[1]}"
29 prevTerms -= 1
31 surroundingTerms = (Term.select()
32 .where(Term.termOrder >= startOrder)
33 .where((Term.year <= currentTerm.year + 2))
34 .order_by(Term.termOrder))
36 return surroundingTerms
38def getStartofCurrentAcademicYear(currentTerm):
39 if ("Summer" in currentTerm.description) or ("Spring" in currentTerm.description):
40 fallTerm = Term.select().where(Term.year==currentTerm.year-1, Term.description == f"Fall {currentTerm.year-1}").get()
41 return fallTerm
42 return currentTerm
44def format24HourTime(unformattedTime):
45 """
46 Turns a time string or datetime object into a string with a time in 24 hour format
47 unformattedTime: expects a string with format HH:mm AM/PM or HH:mm OR a datetime object
48 returns: a string in 24 hour format HH:mm
49 """
50 if type(unformattedTime) == str:
51 try:
52 formattedTime = datetime.strptime(unformattedTime, "%I:%M %p").strftime("%H:%M") # Converts string to datetime then back to string and formats correctly
53 return formattedTime
54 except ValueError:
55 # calling strptime here to explicitly raise an exception if it wasn't properly in 24 hour format
56 formattedTime = datetime.strptime(unformattedTime, "%H:%M")
57 return unformattedTime
58 else:
59 formattedTime = unformattedTime.strftime("%H:%M")
60 return formattedTime
62def getUsernameFromEmail(email):
63 return email.split("@")[0]
65def getFilesFromRequest(request):
66 attachmentFiles = request.files.getlist("attachmentObject")
67 fileDoesNotExist = attachmentFiles[0].content_type == "application/octet-stream"
68 if fileDoesNotExist:
69 attachmentFiles = None
71 return attachmentFiles
73def getRedirectTarget(popTarget=False):
74 """
75 This function returns a string with the URL or route to a page in the Application
76 saved with setRedirectTarget() and is able to pop the value from the session
77 to make it an empty value
78 popTarget: expects a bool value to determine whether or not to reset
79 redirectTarget to an emtpy value
80 return: a string with the URL or route to a page in the application that was
81 saved in setRedirectTarget()
82 """
83 if "redirectTarget" not in session:
84 return ''
86 target = session["redirectTarget"]
87 if popTarget:
88 session.pop("redirectTarget")
89 return target
91def setRedirectTarget(target):
92 """
93 This function saves the target URL in the session for future redirection
94 to said page
95 target: expects a string that is a URL or a route to a page in the application
96 return: None
97 """
98 session["redirectTarget"] = target