def home(request): try: userDistrict = District.getUserDistrict(request) ###retrieve all of the schools and computers in the user's district schools = School.objects.filter(district=userDistrict) computers = Computer.objects.filter(district=userDistrict) ###filter if len(request.GET.getlist('school_type')) > 0: schools = School.objects.filter(school_type__in=request.GET.getlist('school_type')) filterForm = SchoolFilterForm(request.GET) else: filterForm = SchoolFilterForm() computers = Computer.objects.filter(school__in=schools) schoolCount = len(schools) computerCount = len(computers) ###compute counts for pill displays teacherCount = sum(school.teacher_count or 0 for school in schools) elementarySchoolCount = len([school for school in schools if school.school_type == 1]) k8Count = len([school for school in schools if school.school_type == 2]) juniorHighSchoolCount = len([school for school in schools if school.school_type == 3]) highSchoolCount = len([school for school in schools if school.school_type == 4]) uniqueSchoolCount = len([school for school in schools if school.school_type == 5]) ###generate a pie chart of school types (elementary, junion high, high schools) schoolTypeChart = Pie3D([elementarySchoolCount,k8Count, juniorHighSchoolCount, highSchoolCount, uniqueSchoolCount], encoding='text') schoolTypeChart.size(500,150) schoolTypeChart.color('4d89f9') schoolTypeChart.label('Elementary Schools', 'K-8', 'Junior High Schools', 'High Schools', 'Unique Schools') #compute ratio lists for schools, computer, and teachers studentsPerComputerRatioList, teachersPerComputerRatioList, computersPerSchoolCountList = ratios.computeDistrictRatios(schools, computers) return render_to_response('home.html', {'userDistrict': userDistrict, 'schools': schools, 'schoolCount':schoolCount, 'computerCount': computerCount, 'teacherCount': teacherCount, 'elementarySchoolCount': elementarySchoolCount, 'k8Count': k8Count, 'juniorHighSchoolCount': juniorHighSchoolCount, 'highSchoolCount': highSchoolCount, 'uniqueSchoolCount': uniqueSchoolCount, 'studentsPerComputerRatioList': studentsPerComputerRatioList[:5], 'teachersPerComputerRatioList': teachersPerComputerRatioList[:5], 'computersPerSchoolCountList': computersPerSchoolCountList[:5], 'schoolTypeChart': schoolTypeChart, 'filterForm': filterForm, }, context_instance=RequestContext(request)) ###exceptions except DistrictUserProfile.DoesNotExist: return HttpResponseNotFound('District profile not found') except District.DoesNotExist: return HttpResponseNotFound('District not found')
def schoolDetail(request, school_id, school_room_form=''): try: userDistrict = District.getUserDistrict(request) ###retrieve all of the schools and computers in the user's district allSchools = School.objects.filter(district=userDistrict) allComputers = Computer.objects.filter(district=userDistrict) ###retrieve the school, school rooms, and computer objects school = School.objects.get(pk=school_id, district=userDistrict) school_rooms = SchoolRoom.objects.filter(school=school).order_by('room_name') computers = Computer.objects.filter(school=school).order_by('os', 'ram') computerCount = len(computers) ###filter if len(request.GET.getlist('school_type')) > 0: allSchools = list(School.objects.filter(school_type__in=request.GET.getlist('school_type'))) if school not in allSchools: allSchools.append(school) print allSchools allComputers = [computer for computer in allComputers if computer.school in allSchools] allComputers = allComputers + list(computers) filterForm = SchoolFilterForm(request.GET) else: filterForm = SchoolFilterForm() try: address = Address.objects.get(school=school) except Address.DoesNotExist: address = None ###generate the operating systems pie chart osxCount, linuxCount, windowsCount, osChart = chart.generateOsPieChart(computers) if computers else ['','','',''] ###generate the hard drive pie chart hdChart = chart.generateHdPieChart(computers) if computers else ['','',''] ###generate the ram pie chart ramChart = chart.generateRamPieChart(computers) if computers else ['','','',''] ###compute ratios for each school (district level) studentsPerComputerRatioList, teachersPerComputerRatioList, computersPerSchoolCountList = ratios.computeDistrictRatios(allSchools, allComputers) studentsPerComputerRatio = [item[1] for item in studentsPerComputerRatioList if item[0] == school ] studentsPerComputerRatio = str(round(studentsPerComputerRatio[0], 5)) if len(studentsPerComputerRatio) > 0 else "0" teachersPerComputerRatio = [item[1] for item in teachersPerComputerRatioList if item[0] == school ] teachersPerComputerRatio = str(round(teachersPerComputerRatio[0],5)) if len(teachersPerComputerRatio) > 0 else "0" computersPerSchoolCount = [item[1] for item in computersPerSchoolCountList if item[0] == school ] computersPerSchoolCount = str(computersPerSchoolCount[0]) if len(computersPerSchoolCount) > 0 else "0" ###compute ranking of students to computer (district level) studentToComputerRank = rank.findStudentToComputerRanking(school, studentsPerComputerRatioList) if studentToComputerRank: studentToComputerRankAllSchools = str(len(studentsPerComputerRatioList)) else: studentToComputerRank = 'Not available' studentToComputerRankAllSchools = None ###compute ranking of teachers per computer teacherToComputerRank = rank.findTeacherToComputerRanking(school, teachersPerComputerRatioList) if teacherToComputerRank: teacherToComputerRankAllSchools = str(len(teachersPerComputerRatioList)) else: teacherToComputerRank = 'Not available' teacherToComputerRankAllSchools = None ###compute overall computer count ranking (overall number of computers) computerCountRank = rank.findComputerCountRanking(school, computersPerSchoolCountList) if computerCountRank: computerCountRankAllSchools = str(len(computersPerSchoolCountList)) else: computerCountRank = 'Not available' computerCountRankAllSchools = None return render_to_response('schoolDetail.html', {'school':school, 'address': address, 'allSchools': allSchools, 'filterForm': filterForm, 'computers':computers, 'computerCount':computerCount, 'osxCount':osxCount, 'linuxCount':linuxCount, 'windowsCount':windowsCount, 'hdChoices':HD_SIZE_CHOICES, 'ramChoices':RAM_SIZE_CHOICES, 'osChart':osChart, 'hdChart':hdChart, 'ramChart':ramChart, 'osChoices':OS_CHOICES, 'schoolRooms':school_rooms, 'studentToComputerRank': studentToComputerRank, 'studentToComputerRankAllSchools': studentToComputerRankAllSchools, 'studentsPerComputerRatio': studentsPerComputerRatio, 'teacherToComputerRank': teacherToComputerRank, 'teacherToComputerRankAllSchools': teacherToComputerRankAllSchools, 'teachersPerComputerRatio': teachersPerComputerRatio, 'computerCountRank': computerCountRank, 'computerCountRankAllSchools': computerCountRankAllSchools, 'computersPerSchoolCount': computersPerSchoolCount, 'schoolRoomForm': school_room_form }, context_instance=RequestContext(request)) except School.DoesNotExist: return HttpResponseNotFound('School not found')
def home(request): try: ###retrieve the district that the current user is assigned to districtUserProfile = DistrictUserProfile.objects.filter(user=request.user) userDistrict = District.objects.get(pk=districtUserProfile) ###retrieve all of the schools and computers in the user's district schools = School.objects.filter(district=userDistrict) schoolCount = len(schools) computers = Computer.objects.filter(district=userDistrict) computerCount = len(computers) ###compute counts for pill displays teacherCount = sum(school.teacher_count or 0 for school in schools) elementarySchoolCount = len([school for school in schools if school.school_type == 1]) middleSchoolCount = len([school for school in schools if school.school_type == 2]) highSchoolCount = len([school for school in schools if school.school_type == 3]) schoolTypeChart = Pie3D([elementarySchoolCount,middleSchoolCount, highSchoolCount], encoding='text') schoolTypeChart.size(500,150) schoolTypeChart.color('4d89f9') schoolTypeChart.label('Elementary Schools', 'Middle Schools', 'High Schools') #compute ratios for schools, computer, and teachers studentsPerComputerRatioList, teachersPerComputerRatioList, computersPerSchoolCountList = ratios.computeDistrictRatios(schools, computers) studentsPerComputerDataset = [school[1] for school in studentsPerComputerRatioList] studentsPerComputerLabels = [school[0].full_name for school in studentsPerComputerRatioList] studentsPerComputerChart = VerticalBarStack(studentsPerComputerDataset) if studentsPerComputerDataset else '' if studentsPerComputerDataset: studentsPerComputerChart.color('0074CC') studentsPerComputerChart.bar(50,15) studentsPerComputerChart.marker('N*','black',0,-1,15) return render_to_response('home.html', {'userDistrict': userDistrict, 'schools': schools, 'schoolCount':schoolCount, 'computerCount': computerCount, 'teacherCount': teacherCount, 'elementarySchoolCount': elementarySchoolCount, 'middleSchoolCount': middleSchoolCount, 'highSchoolCount': highSchoolCount, 'studentsPerComputerRatioList': studentsPerComputerRatioList, 'teachersPerComputerRatioList': teachersPerComputerRatioList, 'computersPerSchoolCountList': computersPerSchoolCountList, 'studentsPerComputerChart': studentsPerComputerChart, 'schoolTypeChart': schoolTypeChart }, context_instance=RequestContext(request)) ###exceptions except DistrictUserProfile.DoesNotExist: return HttpResponseNotFound('District profile not found') except District.DoesNotExist: return HttpResponseNotFound('District not found')