views.py 5.74 KB
Newer Older
1
from django.shortcuts import render, get_object_or_404
2
from trajectories.models import Course, CourseCollection, Program, Student, Trajectory
Daniel W Bond's avatar
Daniel W Bond committed
3
from django.db.models import Max
Daniel Warren Bond's avatar
Daniel Warren Bond committed
4

Daniel W Bond's avatar
Daniel W Bond committed
5
# processing functions
Daniel W Bond's avatar
Daniel W Bond committed
6

Daniel W Bond's avatar
Daniel W Bond committed
7
# run on each coursecollection
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def remainingReqCourses(requiredCourses, coursesTaken):
    """ returns remaining courses for program """
    remainingReqCourses = []
    for course in requiredCourses:
        if course not in coursesTaken:
            remainingReqCourses.append(course)

def allPrereqCoreq(course, remainingReqCourses):
    """ returns required courses that have course as a prereq or coreq """
    allPrereqCoreq = []
    for requiredCourse in remainingReqCourses:
        for prereq in course.prereqs:
            if prereq is requiredCourse:
                allPrereqCoreq.append(prereq)
        for coreq in course.coreqs:
            if coreq is requiredCourse:
                allPrereqCoreq.append(coreq)
    return allPreqCoreq

Daniel W Bond's avatar
Daniel W Bond committed
27
# run on each coursecollection
28
29
30
31
32
33
34
35
36
37
def nextCourses(coursesTaken, remainingReqCourses):
     """ returns the courses student can take given what's been taken """
     nextCourses = []
     for course in remainingReqCourses:
         if course.prereqs not in coursesTaken:
             nextCourses.append(course)
         # you have to take the prereqs for something first
         elif course.coreq not in coursesTaken:
             nextCourses.append(course)
         else:
Daniel W Bond's avatar
Daniel W Bond committed
38
39
             possibility = allPrereqCoreq(course, remainingReqCourses)
             nextCourses.append(possibility)
40
41
     return nextCourses

42
43
44
# this algorithm sucks because if there's a change to the number of allowed
# majors then this will break
def getGenEds(programs, isHonors):
Daniel W Bond's avatar
Daniel W Bond committed
45
46
    """ returns the gen eds a student has to take based on their selected
        programs """
47
48
49
50
51
52
53
54
55
56
    genEdList = []
    if isHonors:
        genEdList.append("Honors")
	# will require a program in the database called "Honors"
	return genEdList
    else:
        firstMajorType = program[0].degreeType
	genEdList.append(firstMajorType)
	try:
	    secondMajor = program[1].degreeType
Daniel W Bond's avatar
Daniel W Bond committed
57
	    if firstMajorType is secondMajorType:
58
59
60
61
62
	        genEdList.append(secondMajorType)
		return genEdList
	except:
	    return genEdList

63
def maxProgramsAllowed(programList, maxProgramNumber):
Daniel W Bond's avatar
Daniel W Bond committed
64
    """ called to ensure student can't sign up for more than 2 majors, etc """
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    if (len(programList) + 1) > maxProgramNumer:
        return False
    else:
        return True

def fulfilledReq(coursesTaken, courseCollection, numReq):
    """ makes CourseCollection.isCompleted True if completed
        should only use where isCompleted is False
	can also be used for Program.isCompleted """
    finished = 0
    for reqCourse in courseCollection:
	if reqCourse in coursesTaken:
	    finished += 1
	    if finished >= numReq:
                courseCollection.isCompleted = True
	        break

82
def topTrajectories(trajectories):
Daniel W Bond's avatar
Daniel W Bond committed
83
84
85
86
87
88
89
90
91
    """ Only shows the uppermost level of trajectories--
        there's no reason that *all* of the previously loaded classes
	need be gone through; furthermore, students only need to see
	the top level of each of their trajectories on their homepage """
    
    names = set()
    for trajectory in trajectories:
        names.append(trajectory.name)

92
    topTrajectories = []
Daniel W Bond's avatar
Daniel W Bond committed
93
94
95
96
97
    for name in names:
        namedTrajectories = trajectories.filter(name = name)
	topNamedTrajectory = namedTrajectories.aggregate(Max('semester'))
	topTrajectories.append(topNamedTrajectory)

98
    return topTrajectories
99

100
def enoughCourses(coursesTaken, degreeCreditsReqNum):
Daniel W Bond's avatar
Daniel W Bond committed
101
    """ required credits for degree program """
102
    if len(coursesTaken) > degreeCreditsReqNum: #120
103
104
105
106
        return True
    else:
        return False

107
108
109
110
111
112
113
114
115
116
117
118
#def enoughUpperLevelCourses(coursesTaken, upperLevelReqNum):
#    """ required upper level courses for program """
#    upperLevelCourses = []
#    for course in coursesTaken:
#        if course.isUpperClass:
#            course.append(upperLevelCourses)
#
#    if len(upperLevelCourses > upperLevelNum):
#        return True
#    else:
#        return False

Daniel W Bond's avatar
Daniel W Bond committed
119
# note: coreq requirements are fulfilled through onpage javascript, not here
Daniel W Bond's avatar
Daniel W Bond committed
120

Daniel W Bond's avatar
Daniel W Bond committed
121
122
# page render functions

Daniel W Bond's avatar
Daniel W Bond committed
123
124
125
# a page for creating new trajectories
# @login_required
def new(request):
126
127
128
    programs = Program.objects.all()
    courses = Course.objects.all()
    # select year
Daniel W Bond's avatar
urls    
Daniel W Bond committed
129

Daniel W Bond's avatar
Daniel W Bond committed
130
    return render(request, 'new.html', {
131
        'programs' : programs,
Daniel W Bond's avatar
Daniel W Bond committed
132
    }
Daniel W Bond's avatar
urls    
Daniel W Bond committed
133

Daniel W Bond's avatar
Daniel W Bond committed
134
135
# student selects the classes for their trajectories
# @login_required
136
# def create(request, slug): slug is the user's
137
def create(request):
Daniel W Bond's avatar
Daniel W Bond committed
138

139
    # programs = theProgramThatWasJustSelected 
140

141
    return render(request, 'create.html', {
Daniel W Bond's avatar
Daniel W Bond committed
142
143
144
145
    
    },
    )

Daniel W Bond's avatar
Daniel W Bond committed
146
# student's page; shows saved trajectories
Daniel W Bond's avatar
Daniel W Bond committed
147
# @login_required
Daniel W Bond's avatar
Daniel W Bond committed
148
def student(request, slug):
149
150
151
    student = get_object_or_404(Student, user__username=username)
    trajectories = Trajectory.objects.filter(student__user__username=username)
    topTrajectories = topTrajectories(trajectories)
152

Daniel W Bond's avatar
navbar    
Daniel W Bond committed
153
    return render(request, 'student.html', {
154
155
        'student' : student,
        'topTrajectories' : topTrajectories,
156

Daniel W Bond's avatar
Daniel W Bond committed
157
158
159
    },
    )

160
161
162
# simply displays a page for the course
def course(request, slug):
    course = get_object_or_404(Course, slug=slug)
163

164
165
    return render(request, 'course.html', {
        'course' : course,
Daniel W Bond's avatar
Daniel W Bond committed
166
167
    },
    )
Daniel W Bond's avatar
Daniel W Bond committed
168

169
# simply returns a page showing a program
170
171
172
173
174
175
176
177
# @login_required
def program (request):
    program = get_object_or_404(Program, slug=slug)
    
    return render(request, 'program.html', {
        'program' : program,
    },
    )
178
179
180
181
182
183

# simply displays a page for an individual trajectory, (along with edit links)
# @login_required
def trajectory(request, slug):
# actually needs more than one slug, the one for the user
    trajectory = get_object_or_404(Trajectory, slug=slug) 
184
    
185
186
187
188
    return render(request, 'trajectory.html', {
        'trajectory' : trajectory,
    },
    )