-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.py
93 lines (70 loc) · 2.51 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from cs50 import SQL
from flask import Flask, flash, redirect, render_template, request, session
from flask_session import Session
from functools import wraps
db = SQL("sqlite:///data.db")
def get_key_semester(dict):
return dict['semester']
def login_required(f):
"""
Decorate routes to require login.
http://flask.pocoo.org/docs/0.12/patterns/viewdecorators/
"""
@wraps(f)
def decorated_function(*args, **kwargs):
if session.get("user_id") is None:
return redirect("/login")
return f(*args, **kwargs)
return decorated_function
def max_semesters(id):
max = db.execute("SELECT semesters FROM target WHERE user_id = ?", id)
if max:
return max[0]["semesters"]
else:
return 8
def current_semesters(id):
listOfSems = db.execute("SELECT semester FROM profile WHERE user_id = ?", id)
if listOfSems:
#finding the dict that has the max value
max_dict = max (listOfSems, key=get_key_semester)
return max_dict['semester'] #return current max semester number
else:
return 0
def calculate_gpa(user_id, semester_id):
semester_data = db.execute("SELECT credit,gpa FROM profile WHERE user_id = ? AND semester = ?", user_id, semester_id)
if not semester_data:
return 0
else:
# calculation: (SUMMATION(credit*gpa)) / (total credits)
sum = 0
totalCredits = 0
for row in semester_data:
sum += row["credit"] * row["gpa"]
totalCredits += row["credit"]
gpa = sum/totalCredits
return gpa
def calculate_cgpa(id):
current_max = current_semesters(id)
if current_max <= 0:
return 0
# calculation: SUMMATION(gpa)/current max semester
gpa_sum = 0
for i in range(current_max):
gpa_sum += calculate_gpa(id, i+1)
cgpa = gpa_sum / current_max
return cgpa
def required_gpa(id):
max = max_semesters(id)
current = current_semesters(id)
semesters_left = max - current
if semesters_left <= 0:
return "Congrats on graduating!"
else:
# calculation: ( (target cgpa * max semester) - (current cgpa) ) / remaining semesters
target = db.execute("SELECT cgpa FROM target WHERE user_id = ?", id)
cgpa = calculate_cgpa(id)
required = ( (target[0]["cgpa"] * max) - (cgpa * current) ) / (semesters_left)
if required < 0 or required > 4:
return "Not possible :("
else:
return "Get " + str(round(required,2)) + " GPA each semester"