def course_page(dept, course_id): try: #format id, else tell user that id is invalid formatted_id = id_from_url(course_id) except ValueError: return '\'' + course_id + '\' is not a valid course id.' try: res = dbsession.query(Course) course = res.filter(Course.id == formatted_id).one() except: return 'Course \'' + course_id + '\' does not exist.' #get alt descs for course res = dbsession.query(AltDesc) alt_desc_list = list(res.filter(AltDesc.course_id == formatted_id)) #get alt descs for courses that are the same (like CS220 and MATH220) #and concatenate them together for same_course in listify(course.same_as): additional_alt_descs = dbsession.query(AltDesc).filter( AltDesc.course_id == same_course) alt_desc_list += list(additional_alt_descs) #remove unapproved alt descs alt_desc_list = filter(lambda alt_desc: alt_desc.approved, alt_desc_list) sections = dbsession.query(Section).\ filter( Section.course_id == formatted_id ) term_offerings = OrderedDict() for term in terms: term_offerings[term] = sections.filter(Section.term == term) term_offerings[term] = list(term_offerings[term]) course = search(dbsession, course_id=course.id) course = course[course.keys()[0]] form = AltDescForm() #Appends course title to history history = History() history.add(course) #Sets history cookie resp = make_response( render_template("course.html", course=course, form=form, alt_descs=alt_desc_list, terms=term_offerings, history=history)) resp.set_cookie('history', str(history), max_age=365 * 24 * 60 * 60) #cookie lasts a year return resp
def course_page(dept, course_id): try: #format id, else tell user that id is invalid formatted_id = id_from_url(course_id) except ValueError: return '\''+course_id+'\' is not a valid course id.' try: res = dbsession.query(Course) course = res.filter(Course.id == formatted_id).one() except: return 'Course \'' + course_id + '\' does not exist.' #get alt descs for course res = dbsession.query(AltDesc) alt_desc_list = list(res.filter(AltDesc.course_id == formatted_id)) #get alt descs for courses that are the same (like CS220 and MATH220) #and concatenate them together for same_course in listify(course.same_as): additional_alt_descs = dbsession.query(AltDesc).filter(AltDesc.course_id == same_course) alt_desc_list += list(additional_alt_descs) #remove unapproved alt descs alt_desc_list = filter(lambda alt_desc: alt_desc.approved, alt_desc_list) sections = dbsession.query(Section).\ filter( Section.course_id == formatted_id ) term_offerings = OrderedDict() for term in terms: term_offerings[term] = sections.filter(Section.term==term) term_offerings[term] = list(term_offerings[term]) course = search(dbsession, course_id = course.id) course = course[course.keys()[0]] form = AltDescForm() #Appends course title to history history = History() history.add(course) #Sets history cookie resp = make_response( render_template("course.html", course=course, form=form, alt_descs=alt_desc_list, terms=term_offerings, history=history) ) resp.set_cookie('history', str(history), max_age=365*24*60*60) #cookie lasts a year return resp
def search(session, course_id=None, dept=None, keywords=[], gen_ed_abbrs=[], sort=None): res = session.query(Course) if course_id: res = res.filter(Course.id == course_id) if dept: res = res.filter(Course.dept == dept) if gen_ed_abbrs: new_res = None for abbr in gen_ed_abbrs: temp_res = res.filter(Course.gen_eds.any(abbr=abbr)) if new_res: #new_res exists, so combine with temp_res new_res = temp_res.union(new_res) else: #new_res is None, so make new_res temp_res new_res = temp_res res = new_res result_dict = {} for course in res: #course_gen_eds = res.filter(Course.gen_eds.all()) result_dict[course.id] = {'id':course.id, 'number':course.number, 'dept':course.dept, 'hours':course.hours, 'title':course.title, 'desc':course.desc, 'same_as':course.same_as, 'prereqs':course.prereqs, 'gen_eds': ', '.join( [gen_ed.abbr for gen_ed in session.query(GenEd).filter(GenEd.courses.any(id=course.id)) ] ) } for course in result_dict: count = 0 for word in keywords: count += result_dict[course]['title'].lower().count(word) count += result_dict[course]['desc'].lower().count(word) result_dict[course]['keyword_count'] = count gen_ed_list = listify(result_dict[course]['gen_eds']) result_dict[course]['gen_ed_count'] = len(gen_ed_list) count = 0 for abbr in gen_ed_abbrs: if abbr in gen_ed_list: count += 1 result_dict[course]['searched_gen_ed_count'] = count if keywords: for course_id in result_dict.keys(): if result_dict[course_id]['keyword_count'] == 0: del result_dict[course_id] if sort: if sort == 'alpha': sorted_keys = sorted(result_dict) elif sort == 'keyword': sorted_keys = sorted(result_dict, key = lambda k: result_dict[k]['keyword_count'], reverse=True) sorted_results = OrderedDict() for course_id in sorted_keys: sorted_results[course_id] = result_dict[course_id] return sorted_results else: return result_dict