Skip to content

Commit bcdeffc

Browse files
Fixed logic efficiency with set
1 parent c0e53f5 commit bcdeffc

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

app/logic/certification.py

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,21 @@ def termsAttended(certification=None, username=None):
2424

2525
def termsMissed(certification=None, username=None):
2626
'''
27-
Populate the maximum amount of certification a student can miss based on class classification
27+
Calculate how many certification-eligible terms a student has missed based on their class level
28+
and attendance record.
29+
30+
Logic:
31+
- Each class level is expected to participate in 2 terms per year.
32+
- If the user is currently in their final spring term (e.g., Spring of senior year),
33+
they are expected to have completed all terms: missedTerms = (level + 1) * 2.
34+
- Otherwise, assume they’ve had one fewer term to attend: missedTerms = ((level + 1) * 2) - 1.
35+
- If the user's classification is None, assume just 1 expected term.
36+
- Subtract the number of terms the student has attended from the expected total to get the missed count.
2837
'''
29-
sh = 0
3038
classLevel = ["Freshman", "Sophomore", "Junior", "Senior"]
3139
currentTerm = g.current_term
3240
currentDescription = currentTerm.description
41+
3342
# looking into a scenario where the current term is summer so that we can reassigned the current term variable to the next term
3443
if currentTerm.isSummer == True:
3544
currentDescription = f'Fall {currentTerm.year}'
@@ -45,8 +54,10 @@ def termsMissed(certification=None, username=None):
4554
missedTerms = ((level + 1) * 2) - 1
4655
elif str(user.rawClassLevel) == "None":
4756
missedTerms = 1
57+
4858
attendedTerms = termsAttended(certification, username)
4959
missedTerms = missedTerms - len(attendedTerms)
60+
5061
return missedTerms
5162

5263
def getCertRequirementsWithCompletion(*, certification, username):
@@ -85,7 +96,7 @@ def getCertRequirements(certification=None, username=None):
8596

8697
# we have to add the is not null check so that `cert.requirement` always exists
8798
reqList = reqList.where(Certification.id == certification, CertificationRequirement.id.is_null(False))
88-
certs = []
99+
certificationList = []
89100
for cert in reqList:
90101
if username:
91102
cert.requirement.completed = bool(cert.__dict__['completed'])
@@ -94,25 +105,29 @@ def getCertRequirements(certification=None, username=None):
94105
cert.requirement.missedTerms = termsMissed(cert.requirement.id, username)
95106
cert.requirement.attendedTerms = len(termsAttended(cert.requirement.id, username))
96107
cert.requirement.attendedDescriptions = termsAttended(cert.requirement.id, username)
97-
certs.append(cert.requirement)
108+
certificationList.append(cert.requirement)
98109

99110
# the .distinct() doesn't work efficiently, so we have to manually go through the list and removed duplicates that exist
100-
newCerts = []
101-
certsIndex = 0
102-
for cert in certs:
103-
if certs[certsIndex] not in newCerts:
104-
newCerts.append(certs[certsIndex])
105-
certsIndex += 1
106-
certs = newCerts
107-
return certs
111+
validCertification = set()
112+
certificationIndex = 0
113+
114+
for cert in certificationList:
115+
if certificationList[certificationIndex] not in validCertification:
116+
validCertification.add(certificationList[certificationIndex])
117+
certificationIndex += 1
118+
119+
certificationList = list(validCertification)
120+
121+
return certificationList
108122

109-
certs = {}
123+
certificationDict = {}
110124
for cert in reqList:
111-
if cert.id not in certs.keys():
112-
certs[cert.id] = {"data": cert, "requirements": []}
125+
if cert.id not in certificationDict.keys():
126+
certificationDict[cert.id] = {"data": cert, "requirements": []}
113127
if getattr(cert, 'requirement', None):
114-
certs[cert.id]["requirements"].append(cert.requirement)
115-
return certs
128+
certificationDict[cert.id]["requirements"].append(cert.requirement)
129+
130+
return certificationDict
116131

117132
def updateCertRequirements(certId, newRequirements):
118133
"""

tests/code/test_certification.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,13 @@ def test_getCertRequirementsWithCompletion():
109109
EventParticipant.create(event_id=14, user_id='ramsayb2')
110110
RequirementMatch.create(event_id=13, requirement_id=10)
111111
EventParticipant.create(event_id=13, user_id='ramsayb2')
112+
112113
cprCert = 3
113-
114114
cprReqs = getCertRequirementsWithCompletion(certification=cprCert, username='ramsayb2')
115+
115116
assert len(cprReqs) == 2
116-
assert not cprReqs[0].completed, "The first event should not be completed"
117-
assert cprReqs[1].completed, "The second event should be completed"
117+
assert cprReqs[0].completed, "The first event should be marked as completed"
118+
assert not cprReqs[1].completed, "The second event should not be marked as completed"
118119

119120
transaction.rollback()
120121

@@ -169,9 +170,10 @@ def test_updateCertRequirements():
169170
'required': False}
170171
]
171172
returnedIds = updateCertRequirements(otherId, newRequirements)
172-
selectedIds = getCertRequirements(certification=otherId)
173+
selectedIds = (getCertRequirements(certification=otherId))
174+
fetchedIds = list(CertificationRequirement.select().where(CertificationRequirement.certification == otherId).order_by(CertificationRequirement.order))
173175

174-
assert selectedIds == list(CertificationRequirement.select().where(CertificationRequirement.certification == otherId).order_by(CertificationRequirement.order))
176+
assert selectedIds == fetchedIds
175177
assert returnedIds == selectedIds
176178
assert returnedIds[1].name == "CPR 2"
177179
assert returnedIds[1].frequency == "once"

0 commit comments

Comments
 (0)