def get(self, request, id=None): pincode_id = id year = request.GET.get('year', settings.DEFAULT_ACADEMIC_YEAR) try: academic_year = AcademicYear.objects.get(name=year) except AcademicYear.DoesNotExist: raise APIError( 'Academic year is not valid. It should be in the form of 2011-2012.', 404) try: pincode = Postal.objects.get( Q(pk=pincode_id) | Q(pincode=pincode_id)) except Exception: raise APIError('Pincode not found', 404) active_schools = School.objects.filter( id__in=pincode.schooldetails_set.all(), status=2) agg = self.get_aggregations(active_schools, academic_year) agg['pincode'] = PincodeSerializer(pincode).data return Response(agg)
def get_report_data(self, electedrepid): # Get the academic year year = self.request.GET.get('year', settings.DEFAULT_ACADEMIC_YEAR) try: academic_year = AcademicYear.objects.get(name=year) except AcademicYear.DoesNotExist: raise APIError( 'Academic year is not valid.\ It should be in the form of 2011-2012.', 404) self.reportInfo["academic_year"] = year # Check if electedrep id is valid try: electedrep = ElectedrepMaster.objects.get(pk=electedrepid) except Exception: raise APIError('Electedrep id ' + electedrepid + ' not found', 404) self.getSummaryData(electedrep, self.reportInfo) # Get list of schools associated with that electedrep active_schools = electedrep.schools() # Get aggregate data for schools with that electedrep for the current # academic year electedrepData = self.get_aggregations(active_schools, academic_year) electedrepData = self.check_values(electedrepData) # get information about the parent self.parentInfo = self.get_parent_info(electedrep) # get the counts of students/gender/teacher/school self.get_counts(electedrepData, active_schools, academic_year)
def check_role(self): if not self.is_superuser: if self.role is None: raise APIError({'details': 'User role is not set'}, status.HTTP_409_CONFLICT) if self.role.group == "CAMPUS" and self.is_admin: raise APIError( {'details': 'Invalid role for institute account.'}, status.HTTP_409_CONFLICT) if self.role.group == "INSTITUTE" and not self.is_admin: raise APIError({'details': 'Invalid role for campus account.'}, status.HTTP_409_CONFLICT)
def get_report_comparison(self, boundaryid): year = self.request.GET.get('year', settings.DEFAULT_ACADEMIC_YEAR) try: academic_year = AcademicYear.objects.get(name=year) except AcademicYear.DoesNotExist: raise APIError( 'Academic year is not valid.\ It should be in the form of 2011-2012.', 404) try: boundary = Boundary.objects.get(pk=boundaryid) except Exception: raise APIError('Boundary not found', 404) active_schools = boundary.schools() boundaryData = self.get_aggregations(active_schools, academic_year) boundaryData = self.check_values(boundaryData) self.get_comparison_data(boundary, active_schools, academic_year, year)
def validate_campus_and_institute(self): if self.is_superuser: self.institute = None self.campus = None elif self.role.group == "CAMPUS": if self.campus is None: raise APIError({'details': 'Campus is not set.'}, status_code=status.HTTP_409_CONFLICT) if self.campus.institute.id != self.institute.id: raise APIError({'details': 'Campus, Institute ID mismatch.'}, status_code=status.HTTP_409_CONFLICT) elif self.role.group == "INSTITUTE": self.campus = None if self.institute is None: raise APIError( {'details': 'Institute not set for institute account.'}, status_code=status.HTTP_409_CONFLICT)
def get_electedrep_info(self, electedrepid): year = self.request.GET.get('year', settings.DEFAULT_ACADEMIC_YEAR) try: academic_year = AcademicYear.objects.get(name=year) except AcademicYear.DoesNotExist: raise APIError('Academic year is not valid.\ It should be in the form of 2011-2012.', 404) self.reportInfo["academic_year"] = year try: electedrep = ElectedrepMaster.objects.get(pk=electedrepid) except Exception: raise APIError('ElectedRep id ' + electedrepid+' not found', 404) self.getSummaryData(electedrep, self.reportInfo) self.reportInfo["neighbour_info"] = [] if electedrep.neighbours: self.getNeighbours(electedrep.neighbours.split('|'), electedrep.const_ward_type, self.reportInfo) self.getParentData(electedrep, self.reportInfo)
def get_report_comparison(self, electedrepid): year = self.request.GET.get('year', settings.DEFAULT_ACADEMIC_YEAR) try: academic_year = AcademicYear.objects.get(name=year) except AcademicYear.DoesNotExist: raise APIError( 'Academic year is not valid.\ It should be in the form of 2011-2012.', 404) try: electedrep = ElectedrepMaster.objects.get(pk=electedrepid) except Exception: raise APIError('ElectedRep id not found', 404) active_schools = electedrep.schools() electedrepData = self.get_aggregations(active_schools, academic_year) electedrepData = self.check_values(electedrepData) self.get_comparison_data(electedrep, active_schools, academic_year, year)
def get_boundary_info(self, boundaryid): year = self.request.GET.get('year', settings.DEFAULT_ACADEMIC_YEAR) try: academic_year = AcademicYear.objects.get(name=year) except AcademicYear.DoesNotExist: raise APIError('Academic year is not valid.\ It should be in the form of 2011-2012.', 404) self.reportInfo["academic_year"] = year try: boundary = Boundary.objects.get(pk=boundaryid) except Exception: raise APIError('Boundary not found', 404) self.get_boundary_summary_data(boundary, self.reportInfo) if boundary.get_admin_level() == 1: self.reportInfo["neighbours"] = [] boundaries = self.getDistrictNeighbours(boundary) for comparisonboundary in boundaries: self.reportInfo["neighbours"].append({ "dise": comparisonboundary.dise_slug, "type": "district"})
def authenticate(self, request, **kwargs): email = request.data.get('email', '') password = request.data.get('password', '') try: user = UserOperations().get_by_email(email=email) if user.check_password(password): if user.is_active: return user except ObjectDoesNotExist: pass raise APIError("Invalid email or password.", status_code=403)
def getNeighbours(self, neighbours, elect_type, reportInfo): for neighbour in neighbours: try: electedrep = ElectedrepMaster.objects.filter( elec_comm_code=neighbour, const_ward_type=elect_type) except Exception: raise APIError( 'ElectedRep neighbour id (' + neighbour + ') not found', 404) neighbour = {} for rep in electedrep: neighbour["commision_code"] = rep.elec_comm_code neighbour["name"] = rep.const_ward_name.lower() neighbour["type"] = rep.const_ward_type.lower() neighbour["elected_party"] = rep.current_elected_party.lower() neighbour["elected_rep"] = rep.current_elected_rep.lower() neighbour["dise"] = rep.dise_slug reportInfo["neighbour_info"].append(neighbour)
def get_neighbour_comparison(self, academic_year, electedrep): comparisonData = [] if electedrep.neighbours: neighbours = electedrep.neighbours.split('|') for neighbour in neighbours: try: reps = ElectedrepMaster.objects.filter( elec_comm_code=neighbour, const_ward_type=electedrep.const_ward_type) except Exception: raise APIError( 'ElectedRep neighbour id ' + neighbour + 'not found', 404) for rep in reps: comparisonData.append( self.fillComparison(rep, academic_year)) comparisonData.append(self.fillComparison(electedrep, academic_year)) for data in comparisonData: data["school_perc"] = round( data["school_count"] * 100 / float(self.totalschools), 2) return comparisonData
def assert_query(query_data, key: str, raise_exc=True): value = query_data.get(key, None) if value is None and raise_exc is True: raise APIError('required query parameter ' + key + ' not found.', status_code=status.HTTP_400_BAD_REQUEST) return value
def get(self, request, id=None): boundary_id = id year = request.GET.get('year', settings.DEFAULT_ACADEMIC_YEAR) source = request.GET.get('source', None) try: academic_year = AcademicYear.objects.get(name=year) except AcademicYear.DoesNotExist: raise APIError( 'Academic year is not valid. It should be in the form of 2011-2012.', 404) try: boundary = Boundary.objects.get(pk=boundary_id) except Exception: raise APIError('Boundary not found', 404) active_schools = boundary.schools() agg = self.get_aggregations(active_schools, academic_year) agg['boundary'] = BoundaryWithGrandparentSerializer(boundary, context={ 'request': request }).data # Getting the Anganwadi infrastructure data from the # StoryDetail view. if source: start_date = self.request.QUERY_PARAMS.get('from', None) end_date = self.request.QUERY_PARAMS.get('to', None) date = Date() if start_date: sane = date.check_date_sanity(start_date) if not sane: raise APIException( "Please enter `from` in the format YYYY-MM-DD") else: start_date = date.get_datetime(start_date) if end_date: sane = date.check_date_sanity(end_date) if not sane: raise APIException( "Please enter `to` in the format YYYY-MM-DD") else: end_date = date.get_datetime(end_date) schools_with_anganwadi_stories = active_schools.filter( story__group__source__name='anganwadi', ).distinct('id') stories = Story.objects.filter( school__in=schools_with_anganwadi_stories, group__source__name='anganwadi', ).order_by('school', '-date_of_visit').distinct('school') if start_date: stories = stories.filter(date_of_visit__gte=start_date) if end_date: stories = stories.filter(date_of_visit__lte=end_date) data = get_que_and_ans(stories, 'anganwadi', 'PreSchool', None) agg['infrastructure'] = data # Not using serializer because we need to do the aggregations here # and return the results directly return Response(agg)