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