예제 #1
0
 def insert_assessments(self):
     count = 0
     questions = []
     date = Date()
     for row in self.csv_files["assessment"]:
         if count == 0:
             count += 1
             continue
         count += 1
         institution_id = row[3].strip()
         print(count)
         institution = Institution.objects.get(pk=institution_id)
         date_of_visit = date.get_datetime(row[5].strip())
         date_of_visit = timezone.make_aware(
             date_of_visit, timezone.get_current_timezone())
         is_verified = True
         questiongroup = QuestionGroup.objects.get(pk=30)
         status = Status.objects.get(pk='IA')
         answergroup = AnswerGroup_Institution.objects.create(
             institution=institution,
             date_of_visit=date_of_visit,
             is_verified=True,
             questiongroup=questiongroup,
             status=status)
         order = 1
         for column in range(7, 33):
             question = Question.objects.get(
                 questiongroup_questions__questiongroup_id=30,
                 questiongroup_questions__sequence=order)
             answer = AnswerInstitution.objects.create(
                 answergroup=answergroup,
                 question=question,
                 answer=row[column])
             order += 1
예제 #2
0
 def get_reportdate(self, report_date):
     date = Date()
     sane = date.check_date_sanity(report_date)
     if not sane:
         print """
         Error:
         Wrong --from format. Expected YYYY-MM-DD
         """
         print self.help
         return
     else:
         report_date = date.get_datetime(report_date)
     return (report_date)
예제 #3
0
 def get_reportdate(self, report_date):
     date = Date()
     sane = date.check_date_sanity(report_date)
     if not sane:
         print( """
         Error:
         Wrong --from format. Expected YYYY-MM-DD
         """)
         print (self.help)
         return
     else:
         report_date = date.get_datetime(report_date)
     return (report_date)
예제 #4
0
 def importPDF(self, file_path, filename):
     filepath_split = file_path.split('/')        
     filename_split = filename.rstrip('.pdf').split("_")
     school_id = filename_split[1]
     language = filepath_split[4] #6
     school = School.objects.get(pk=school_id)
     mr = MeetingReport()
     mr.school = school
     mr.language = language
     generated = Date()
     mr.generated_at = generated.get_datetime(filepath_split[2]+'-'+filepath_split[3]+'-1')
     #fil = open(file_path)
     #django_file = File(fil)
     #mr.pdf.save(file_path, django_file, save=True)
     mr.pdf.name = file_path.split('/', 1)[1]
     mr.save()
예제 #5
0
 def importPDF(self, file_path, filename):
     filepath_split = file_path.split('/')
     filename_split = filename.rstrip('.pdf').split("_")
     school_id = filename_split[1]
     language = filepath_split[4]  #6
     school = School.objects.get(pk=school_id)
     mr = MeetingReport()
     mr.school = school
     mr.language = language
     generated = Date()
     mr.generated_at = generated.get_datetime(filepath_split[2] + '-' +
                                              filepath_split[3] + '-1')
     #fil = open(file_path)
     #django_file = File(fil)
     #mr.pdf.save(file_path, django_file, save=True)
     mr.pdf.name = file_path.split('/', 1)[1]
     mr.save()
예제 #6
0
    def get_dates(self, duration, start_date, end_date):
        today = datetime.now()
        if duration:
            if duration == 'weekly':
                days = 7
            elif duration == 'monthly':
                days = 30
            else: #daily
                days = 1
            start_date = today - timedelta(days=int(days))
            end_date = today

        elif (start_date and end_date):
            date = Date()
            sane = date.check_date_sanity(start_date)
            if not sane:
                print """
                Error:
                Wrong --from format. Expected YYYY-MM-DD
                """
                print self.help
                return
            else:
                start_date = date.get_datetime(start_date)

            sane = date.check_date_sanity(end_date)
            if not sane:
                print """
                Error:
                Wrong --to format. Expected YYYY-MM-DD
                """
                print self.help
                return
            else:
                end_date = date.get_datetime(end_date)
        else:
            raise Exception(
                "Please specify --duration as 'monthly' or 'weekly' OR --from and --to"
            )

        return (start_date, end_date)
예제 #7
0
    def get_dates(self, duration, start_date, end_date):
        today = datetime.now()
        if duration:
            if duration == 'weekly':
                days = 7
            elif duration == 'monthly':
                days = 30
            else: #daily
                days = 1
            start_date = today - timedelta(days=int(days))
            end_date = today

        elif (start_date and end_date):
            date = Date()
            sane = date.check_date_sanity(start_date)
            if not sane:
                print ("""
                Error:
                Wrong --from format. Expected YYYY-MM-DD
                """)
                print (self.help)
                return
            else:
                start_date = date.get_datetime(start_date)

            sane = date.check_date_sanity(end_date)
            if not sane:
                print ("""
                Error:
                Wrong --to format. Expected YYYY-MM-DD
                """)
                print (self.help)
                return
            else:
                end_date = date.get_datetime(end_date)
        else:
            raise Exception(
                "Please specify --duration as 'monthly' or 'weekly' OR --from and --to"
            )

        return (start_date, end_date)
예제 #8
0
    def get(self, request):
        source = self.request.QUERY_PARAMS.get('source', None)
        versions = self.request.QUERY_PARAMS.getlist('version', None)
        admin1_id = self.request.QUERY_PARAMS.get('admin1', None)
        admin2_id = self.request.QUERY_PARAMS.get('admin2', None)
        admin3_id = self.request.QUERY_PARAMS.get('admin3', None)
        school_id = self.request.QUERY_PARAMS.get('school_id', None)
        mp_id = self.request.QUERY_PARAMS.get('mp_id', None)
        mla_id = self.request.QUERY_PARAMS.get('mla_id', None)
        start_date = self.request.QUERY_PARAMS.get('from', None)
        end_date = self.request.QUERY_PARAMS.get('to', None)
        school_type = self.request.QUERY_PARAMS.get(
            'school_type', 'Primary School')
        response_type = self.request.QUERY_PARAMS.get(
            'response_type', 'call_volume')

        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)

        response_json = {}

        stories_qset = Story.objects.filter(
            school__admin3__type__name=school_type)

        if source:
            stories_qset = stories_qset.filter(
                group__source__name=source)

        if versions:
            versions = map(int, versions)
            stories_qset = stories_qset.filter(
                group__version__in=versions)

        if admin1_id:
            stories_qset = stories_qset.filter(
                school__schooldetails__admin1__id=admin1_id)

        if admin2_id:
            stories_qset = stories_qset.filter(
                school__schooldetails__admin2__id=admin2_id)

        if admin3_id:
            stories_qset = stories_qset.filter(
                school__schooldetails__admin3__id=admin3_id)

        if school_id:
            stories_qset = stories_qset.filter(
                school=school_id)

        if mp_id:
            stories_qset = stories_qset.filter(
                school__electedrep__mp_const__id=mp_id)

        if mla_id:
            stories_qset = stories_qset.filter(
                school__electedrep__mla_const__id=mla_id)

        if start_date:
            stories_qset = stories_qset.filter(
                date_of_visit__gte=start_date)

        if end_date:
            stories_qset = stories_qset.filter(
                date_of_visit__lte=end_date)

        story_dates = stories_qset.values_list('date_of_visit', flat=True)
        months = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split()

        if response_type == 'call_volume':
            response_json['volumes'] = self.get_call_volume(story_dates, months)
        elif response_type == 'gka-class':
            response_json['volumes'] = self.get_gka_class_volume(stories_qset, months)
        else:
            response_json = {}

        return Response(response_json)
예제 #9
0
    def get(self, request):
        admin1_id = self.request.QUERY_PARAMS.get('admin1', None)
        admin2_id = self.request.QUERY_PARAMS.get('admin2', None)
        admin3_id = self.request.QUERY_PARAMS.get('admin3', None)
        school_id = self.request.QUERY_PARAMS.get('school_id', None)
        mp_id = self.request.QUERY_PARAMS.get('mp_id', None)
        mla_id = self.request.QUERY_PARAMS.get('mla_id', None)
        start_date = self.request.QUERY_PARAMS.get('from', None)
        end_date = self.request.QUERY_PARAMS.get('to', None)
        school_type = self.request.QUERY_PARAMS.get(
            'school_type', 'Primary School')

        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)

        response_json = {}

        stories_qset = Story.objects.filter(
            school__admin3__type__name=school_type)

        if admin1_id:
            stories_qset = stories_qset.filter(
                school__schooldetails__admin1__id=admin1_id)

        if admin2_id:
            stories_qset = stories_qset.filter(
                school__schooldetails__admin2__id=admin2_id)

        if admin3_id:
            stories_qset = stories_qset.filter(
                school__schooldetails__admin3__id=admin3_id)

        if school_id:
            stories_qset = stories_qset.filter(
                school=school_id)

        if mp_id:
            stories_qset = stories_qset.filter(
                school__electedrep__mp_const__id=mp_id)

        if mla_id:
            stories_qset = stories_qset.filter(
                school__electedrep__mla_const__id=mla_id)

        if start_date:
            stories_qset = stories_qset.filter(
                date_of_visit__gte=start_date)

        if end_date:
            stories_qset = stories_qset.filter(
                date_of_visit__lte=end_date)

        story_dates = stories_qset.values_list('date_of_visit', flat=True)

        json = {}
        for date in story_dates:
            if date.year in json:
                json[date.year].append(date.month)
            else:
                json[date.year] = []
                json[date.year].append(date.month)

        per_month_json = {}
        for year in json:
            per_month_json[year] = dict(Counter(
                [calendar.month_abbr[date] for date in json[year]])
            )

        ordered_per_month_json = {}
        months = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split()
        for year in per_month_json:
            ordered_per_month_json[year] = OrderedDict()
            for month in months:
                ordered_per_month_json[year][month] = per_month_json[year].get(month, 0)

        response_json['volumes'] = ordered_per_month_json
        return Response(response_json)
예제 #10
0
    def get(self, request, boundary=None):
        source = self.request.QUERY_PARAMS.get('source', None)
        admin1_id = self.request.QUERY_PARAMS.get('admin1', None)
        admin2_id = self.request.QUERY_PARAMS.get('admin2', None)
        admin3_id = self.request.QUERY_PARAMS.get('admin3', None)
        school_id = self.request.QUERY_PARAMS.get('school_id', None)
        mp_id = self.request.QUERY_PARAMS.get('mp_id', None)
        mla_id = self.request.QUERY_PARAMS.get('mla_id', None)
        start_date = self.request.QUERY_PARAMS.get('from', None)
        end_date = self.request.QUERY_PARAMS.get('to', None)
        school_type = self.request.QUERY_PARAMS.get(
            'school_type', 'Primary School')

        # This boundary variable and check is for the time
        # when this endpoint is being called from within the
        # BoundarySchoolAggView
        if boundary:
            boundary_type = boundary.hierarchy.name
            if boundary_type == u'district':
                admin1_id = boundary.id
            elif boundary_type in [u'block', u'project']:
                admin2_id = boundary.id
            else:
                admin3_id = boundary.id

        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)

        stories = Story.objects.all()

        if source:
            stories = stories.filter(group__source__name=source)

        if school_type:
            stories = stories.filter(school__admin3__type__name=school_type)

        if admin1_id:
            stories = stories.filter(
                school__schooldetails__admin1__id=admin1_id
            )

        if admin2_id:
            stories = stories.filter(
                school__schooldetails__admin2__id=admin2_id
            )

        if admin3_id:
            stories = stories.filter(
                school__schooldetails__admin3__id=admin3_id
            )

        if school_id:
            stories = stories.filter(school__id=school_id)

        if mp_id:
            stories_qset = stories_qset.filter(
                school__electedrep__mp_const__id=mp_id)

        if mla_id:
            stories_qset = stories_qset.filter(
                school__electedrep__mla_const__id=mla_id)

        if start_date:
            stories = stories.filter(date_of_visit__gte=start_date)

        if end_date:
            stories = stories.filter(date_of_visit__lte=end_date)

        response_json = {}

        # Sources and filters
        if source:
            response_json[source] = self.get_que_and_ans(
                stories, source, school_type)
        else:
            sources = Source.objects.all().values_list('name', flat=True)
            for source in sources:
                response_json[source] = self.get_que_and_ans(
                    stories, source, school_type)

        return Response(response_json)
예제 #11
0
    def handle(self, *args, **options):
        duration = options.get('duration', None)
        start_date = options.get('from', None)
        end_date = options.get('to', None)
        emails = options.get('emails', None)
        fc_report = options.get('fc_report', None)

        today = datetime.now().date()
        if duration:
            if duration == 'weekly':
                days = 7
            elif duration == 'monthly':
                days = 30
            start_date = today - timedelta(days=int(days))
            end_date = today

        elif (start_date and end_date):
            date = Date()
            sane = date.check_date_sanity(start_date)
            if not sane:
                print """
                Error:
                Wrong --from format. Expected YYYY-MM-DD
                """
                print self.help
                return
            else:
                start_date = date.get_datetime(start_date)

            sane = date.check_date_sanity(end_date)
            if not sane:
                print """
                Error:
                Wrong --to format. Expected YYYY-MM-DD
                """
                print self.help
                return
            else:
                end_date = date.get_datetime(end_date)
        else:
            raise Exception(
                "Please specify --duration as 'monthly' or 'weekly' OR --from and --to"
            )

        if not emails:
            raise Exception(
                "Please specify --emails as a list of comma separated emails")
        emails = emails.split(",")

        report_dir = settings.PROJECT_ROOT + "/gka-reports/"

        bfc = Group.objects.get(name="BFC")
        crp = Group.objects.get(name="CRP")
        bfc_users = bfc.user_set.all()
        crp_users = crp.user_set.all()

        states = State.objects.filter(date_of_visit__gte=start_date,
                                      date_of_visit__lte=end_date)
        valid_states = states.filter(is_invalid=False)

        date = datetime.now().date().strftime("%d_%b_%Y")
        csv_file = report_dir + date + '.csv'
        csv = open(csv_file, "w")

        lines = []

        # Overall count
        heading = "OVERALL COUNT"
        lines.extend([heading, "\n"])

        columns = "Total SMS received, No. of invalid SMS, % of invalid SMS, No. valid SMS received from BFC, No. valid SMS received from CRP, No. of schools with unique valid SMS"
        lines.extend([columns])

        total_sms_received = states.count()
        number_of_invalid_sms = states.filter(is_invalid=True).count()
        percentage_of_invalid_sms = (float(number_of_invalid_sms) /
                                     float(total_sms_received)) * 100.0
        number_of_valid_smses_from_bfc = valid_states.filter(
            user__in=bfc_users).count()
        number_of_valid_smses_from_crp = valid_states.filter(
            user__in=crp_users).count()
        number_of_schools_with_unique_valid_sms = states.filter(
            is_invalid=False).order_by().distinct('school_id').count()

        values = [
            str(total_sms_received),
            str(number_of_invalid_sms),
            str(percentage_of_invalid_sms),
            str(number_of_valid_smses_from_bfc),
            str(number_of_valid_smses_from_crp),
            str(number_of_schools_with_unique_valid_sms)
        ]

        values = ",".join(values)
        lines.extend([values, "\n"])

        # Invalid SMS error classification
        heading = "INVALID SMS ERROR CLASSIFICATION"
        lines.extend([heading, "\n"])

        columns = "Error type, Count"
        lines.extend([columns])

        errors = states.filter(is_invalid=True).values_list('comments',
                                                            flat=True)
        errors_dict = {}
        for error in errors:
            # Let's make certain errors more concise. Refer to 'get_message'
            # in utils.py for all possible messages.
            if error:
                if 'Expected' in error:
                    error = 'Formatting error'
                if 'registered' in error:
                    error = 'Not registered'
                if 'que.no' in error:
                    error = 'Entry error for a specific question'
                if 'School' in error:
                    error = 'School ID error'
                if 'Logical' in error:
                    error = 'Logical error'
                if 'accepted' in error:
                    # We have to do this because all State are by default
                    # invalid when created. Since we only process SMSes at
                    # 8.30PM in the night, the SMSes that came in that day
                    # morning will show as invalid.
                    continue
            if error in errors_dict:
                errors_dict[error] += 1
            else:
                errors_dict[error] = 1

        for error, count in errors_dict.iteritems():
            values = [str(error), str(count)]
            values = ",".join(values)
            lines.extend([values])
        lines.extend(["\n"])

        # District Level performance
        heading = "DISTRICT LEVEL PERFORMANCE"
        lines.extend([heading, "\n"])

        columns = ("District,"
                   "Total SMS received,"
                   "No. SMS from BFC,"
                   "No. SMS from CRP,"
                   "Invalid SMS Count,"
                   "BFC invalid SMS count,"
                   "CRP invalid SMS count,"
                   "No. of unique schools with invalid SMS")
        lines.extend([columns])

        school_ids = State.objects.all().values_list('school_id', flat=True)
        district_ids = School.objects.filter(id__in=school_ids).values_list(
            'admin3__parent__parent',
            flat=True).order_by().distinct('admin3__parent__parent')
        districts = Boundary.objects.filter(id__in=district_ids).exclude(
            id__in=EXCLUDED_DISTRICT_IDS)

        district_dict = {}
        for district in districts:
            school_ids = district.schools().values_list('id', flat=True)
            smses = states.filter(school_id__in=school_ids)
            smses_received = smses.count()
            district_dict[district.id] = smses_received
        district_dict_list = sorted(district_dict.items(),
                                    key=operator.itemgetter(1),
                                    reverse=True)

        for district_id, smses_count in district_dict_list:
            district = Boundary.objects.get(id=district_id)
            school_ids = district.schools().values_list('id', flat=True)
            smses = states.filter(school_id__in=school_ids)
            smses_received = smses.count()
            smses_from_bfc = smses.filter(user__in=bfc_users).count()
            smses_from_crp = smses.filter(user__in=crp_users).count()
            invalid_smses = smses.filter(is_invalid=True).count()
            invalid_smses_from_bfc = smses.filter(is_invalid=True,
                                                  user__in=bfc_users).count()
            invalid_smses_from_crp = smses.filter(is_invalid=True,
                                                  user__in=crp_users).count()
            schools_with_invalid_smses = smses.filter(
                is_invalid=True).order_by().distinct('school_id').count()

            values = [
                str(district.name),
                str(smses_received),
                str(smses_from_bfc),
                str(smses_from_crp),
                str(invalid_smses),
                str(invalid_smses_from_bfc),
                str(invalid_smses_from_crp),
                str(schools_with_invalid_smses),
            ]

            values = ",".join(values)
            lines.extend([values])

        lines.extend(["\n"])
        # Block Level performance
        heading = "BLOCK LEVEL PERFORMANCE"
        lines.extend([heading, "\n"])

        columns = ("Block,"
                   "District,"
                   "Total SMS received,"
                   "No. SMS from BFC,"
                   "No. SMS from CRP,"
                   "Invalid SMS Count,"
                   "BFC invalid SMS count,"
                   "CRP invalid SMS count,"
                   "No. of unique schools with invalid SMS")
        lines.extend([columns])

        school_ids = State.objects.all().values_list('school_id', flat=True)
        block_ids = School.objects.filter(id__in=school_ids).values_list(
            'admin3__parent', flat=True).order_by().distinct('admin3__parent')
        blocks = Boundary.objects.filter(id__in=block_ids).exclude(
            id__in=EXCLUDED_BLOCK_IDS)

        block_dict = {}
        for block in blocks:
            school_ids = block.schools().values_list('id', flat=True)
            smses = states.filter(school_id__in=school_ids)
            smses_received = smses.count()
            block_dict[block.id] = smses_received
        block_dict_list = sorted(block_dict.items(),
                                 key=operator.itemgetter(1),
                                 reverse=True)

        for block_id, smses_count in block_dict_list:
            block = Boundary.objects.get(id=block_id)
            school_ids = block.schools().values_list('id', flat=True)
            smses = states.filter(school_id__in=school_ids)
            smses_received = smses.count()
            smses_from_bfc = smses.filter(user__in=bfc_users).count()
            smses_from_crp = smses.filter(user__in=crp_users).count()
            invalid_smses = smses.filter(is_invalid=True).count()
            invalid_smses_from_bfc = smses.filter(is_invalid=True,
                                                  user__in=bfc_users).count()
            invalid_smses_from_crp = smses.filter(is_invalid=True,
                                                  user__in=crp_users).count()
            schools_with_invalid_smses = smses.filter(
                is_invalid=True).order_by().distinct('school_id').count()

            values = [
                str(block.name),
                str(block.parent.name),
                str(smses_received),
                str(smses_from_bfc),
                str(smses_from_crp),
                str(invalid_smses),
                str(invalid_smses_from_bfc),
                str(invalid_smses_from_crp),
                str(schools_with_invalid_smses),
            ]

            values = ",".join(values)
            lines.extend([values])

        lines.extend(["\n"])

        # Top 5 valid SMS contributors:
        heading = "TOP 5 VALID SMS CONTRIBUTORS"
        lines.extend([heading, "\n"])

        columns = ("Name,"
                   "Mobile number,"
                   "Districts,"
                   "Blocks,"
                   "Clusters,"
                   "Group,"
                   "Valid SMS count,"
                   "SMS count")
        lines.extend([columns])

        users = User.objects.filter(state__in=valid_states).annotate(
            sms_count=Count('state')).order_by('-sms_count')[:5]

        for user in users:
            name = user.get_full_name()
            mobile_number = user.mobile_no
            school_ids = user.state_set.filter(
                id__in=valid_states).values_list('school_id', flat=True)
            clusters = School.objects.filter(id__in=school_ids).values_list(
                'admin3__name', flat=True).order_by().distinct('admin3__name')
            blocks = School.objects.filter(id__in=school_ids).values_list(
                'admin3__parent__name',
                flat=True).order_by().distinct('admin3__parent__name')
            districts = School.objects.filter(id__in=school_ids).values_list(
                'admin3__parent__parent__name',
                flat=True).order_by().distinct('admin3__parent__parent__name')

            try:
                group = user.groups.get().name
            except:
                group = ''

            valid_smses = states.filter(user=user, is_invalid=False).count()
            total_smses = states.filter(user=user).count()

            values = [
                str(name),
                str(mobile_number),
                str("-".join(districts)),
                str("-".join(blocks)),
                str("-".join(clusters)),
                str(group),
                str(valid_smses),
                str(total_smses)
            ]

            values = ",".join(values)
            lines.extend([values])

        lines.extend(["\n"])

        # Top 5 blocks with valid SMS
        heading = "TOP 5 BLOCKS WITH VALID SMS"
        lines.extend([heading, "\n"])

        columns = ("Block Name," "District Name," "Number of Valid SMS,")
        lines.extend([columns])

        school_ids = valid_states.values_list('school_id', flat=True)
        block_ids = School.objects.filter(id__in=school_ids).values_list(
            'admin3__parent', flat=True).order_by().distinct('admin3__parent')
        blocks = Boundary.objects.filter(id__in=block_ids)
        block_sms_dict = {}
        for block in blocks:
            school_ids = block.schools().values_list('id', flat=True)
            smses = valid_states.filter(school_id__in=school_ids).count()
            block_sms_dict[block.id] = smses

        block_sms_list = sorted(block_sms_dict.items(),
                                key=operator.itemgetter(1))
        for i in list(reversed(block_sms_list))[:5]:
            block = Boundary.objects.get(id=i[0])
            values = [
                str(block.name),
                str(block.parent.name),
                str(i[1]),
            ]

            values = ",".join(values)
            lines.extend([values])

        lines.extend(["\n"])

        if fc_report == 'True':

            # Weekly BFC error report
            heading = "BFC ERROR REPORT"
            lines.extend([heading, "\n"])

            columns = ("Group,"
                       "Name,"
                       "Telephone,"
                       "District,"
                       "Block,"
                       "Cluster,"
                       "SMSes sent,"
                       "Number of Invalid SMS,"
                       "Top 3 error classification with counts,"
                       "Number of schools with SMS,"
                       "No. of unique schools with SMS")
            lines.extend([columns])

            user_dict = {}
            for user in bfc_users:
                user_smses_count = user.state_set.filter(id__in=states).count()
                user_dict[user.id] = user_smses_count
            user_dict_list = sorted(user_dict.items(),
                                    key=operator.itemgetter(1),
                                    reverse=True)

            for user_id, user_smses_count in user_dict_list:
                group = "BFC"
                user = User.objects.get(id=user_id)
                user_smses = user.state_set.filter(id__in=states)
                name = user.get_full_name()
                telephone = user.mobile_no
                school_ids = user_smses.values_list('school_id', flat=True)
                clusters = School.objects.filter(
                    id__in=school_ids).values_list(
                        'admin3__name',
                        flat=True).order_by().distinct('admin3__name')
                blocks = School.objects.filter(id__in=school_ids).values_list(
                    'admin3__parent__name',
                    flat=True).order_by().distinct('admin3__parent__name')
                districts = School.objects.filter(
                    id__in=school_ids).values_list(
                        'admin3__parent__parent__name',
                        flat=True).order_by().distinct(
                            'admin3__parent__parent__name')
                smses_sent = user_smses.count()
                invalid_smses_count = user_smses.filter(
                    is_invalid=True).count()
                errors = user_smses.filter(is_invalid=True).values_list(
                    'comments', flat=True)
                errors_dict = {}
                for error in errors:
                    if error:
                        if 'Expected' in error:
                            error = 'Formatting error'
                        if 'registered' in error:
                            error = 'Not registered'
                        if 'que.no' in error:
                            error = 'Entry error for a specific question'
                        if 'School' in error:
                            error = 'School ID error'
                        if 'Logical' in error:
                            error = 'Logical error'
                        if 'accepted' in error:
                            # We have to do this because all State are by default
                            # invalid when created. Since we only process SMSes at
                            # 8.30PM in the night, the SMSes that came in that day
                            # morning will show as invalid.
                            continue
                    if error in errors_dict:
                        errors_dict[error] += 1
                    else:
                        errors_dict[error] = 1
                errors_dict = sorted(errors_dict.items(),
                                     key=operator.itemgetter(1))
                top_3_errors = list((reversed(errors_dict)))[:3]
                schools_with_sms = user_smses.values_list('school_id',
                                                          flat=True).count()
                unique_schools_with_sms = user_smses.values_list(
                    'school_id',
                    flat=True).order_by().distinct('school_id').count()
                values = [
                    str(group),
                    str(name),
                    str(telephone),
                    str("-".join(districts)),
                    str("-".join(blocks)),
                    str("-".join(clusters)),
                    str(smses_sent),
                    str(invalid_smses_count),
                    str(top_3_errors).replace(',', '-'),
                    str(schools_with_sms),
                    str(unique_schools_with_sms)
                ]

                values = ",".join(values)
                lines.extend([values])

            lines.extend(["\n"])

            # Weekly CRP error report
            heading = "CRP ERROR REPORT"
            lines.extend([heading, "\n"])

            columns = ("Group,"
                       "Name,"
                       "Telephone,"
                       "District,"
                       "Block,"
                       "Cluster,"
                       "SMSes sent,"
                       "Number of Invalid SMS,"
                       "Top 3 error classification with counts,"
                       "Number of schools with SMS,"
                       "No. of unique schools with SMS")
            lines.extend([columns])

            user_dict = {}
            for user in crp_users:
                user_smses_count = user.state_set.filter(id__in=states).count()
                user_dict[user.id] = user_smses_count
            user_dict_list = sorted(user_dict.items(),
                                    key=operator.itemgetter(1),
                                    reverse=True)

            for user_id, user_smses_count in user_dict_list:
                group = "CRP"
                user = User.objects.get(id=user_id)
                user_smses = user.state_set.filter(id__in=states)
                name = user.get_full_name()
                telephone = user.mobile_no
                school_ids = user_smses.values_list('school_id', flat=True)
                clusters = School.objects.filter(
                    id__in=school_ids).values_list(
                        'admin3__name',
                        flat=True).order_by().distinct('admin3__name')
                blocks = School.objects.filter(id__in=school_ids).values_list(
                    'admin3__parent__name',
                    flat=True).order_by().distinct('admin3__parent__name')
                districts = School.objects.filter(
                    id__in=school_ids).values_list(
                        'admin3__parent__parent__name',
                        flat=True).order_by().distinct(
                            'admin3__parent__parent__name')
                smses_sent = user_smses.count()
                invalid_smses_count = user_smses.filter(
                    is_invalid=True).count()
                errors = user_smses.filter(is_invalid=True).values_list(
                    'comments', flat=True)
                errors_dict = {}
                for error in errors:
                    if error:
                        if 'Expected' in error:
                            error = 'Formatting error'
                        if 'registered' in error:
                            error = 'Not registered'
                        if 'que.no' in error:
                            error = 'Entry error for a specific question'
                        if 'School' in error:
                            error = 'School ID error'
                        if 'Logical' in error:
                            error = 'Logical error'
                        if 'accepted' in error:
                            # We have to do this because all State are by default
                            # invalid when created. Since we only process SMSes at
                            # 8.30PM in the night, the SMSes that came in that day
                            # morning will show as invalid.
                            continue

                    if error in errors_dict:
                        errors_dict[error] += 1
                    else:
                        errors_dict[error] = 1
                errors_dict = sorted(errors_dict.items(),
                                     key=operator.itemgetter(1))
                top_3_errors = list((reversed(errors_dict)))[:3]
                schools_with_sms = user_smses.values_list('school_id',
                                                          flat=True).count()
                unique_schools_with_sms = user_smses.values_list(
                    'school_id',
                    flat=True).order_by().distinct('school_id').count()
                values = [
                    str(group),
                    str(name),
                    str(telephone),
                    str("-".join(districts)),
                    str("-".join(blocks)),
                    str("-".join(clusters)),
                    str(smses_sent),
                    str(invalid_smses_count),
                    str(top_3_errors).replace(',', '-'),
                    str(schools_with_sms),
                    str(unique_schools_with_sms)
                ]

                values = ",".join(values)
                lines.extend([values])

            lines.extend(["\n"])

        for line in lines:
            csv.write(line + "\n")

        date_range = start_date.strftime("%d/%m/%Y") + " to " + today.strftime(
            "%d/%m/%Y")
        subject = 'GKA SMS Report for ' + date_range
        from_email = settings.EMAIL_DEFAULT_FROM
        to_emails = emails
        msg = EmailMultiAlternatives(subject, "Please view attachment",
                                     from_email, to_emails)
        msg.attach_alternative("<b>Please View attachement</b>", "text/html")
        msg.attach_file(csv_file)
        msg.send()
예제 #12
0
    def get(self, request):
        survey = self.request.QUERY_PARAMS.get('survey', None)
        source = self.request.QUERY_PARAMS.get('source', None)
        versions = self.request.QUERY_PARAMS.getlist('version', None)
        admin1_id = self.request.QUERY_PARAMS.get('admin1', None)
        admin2_id = self.request.QUERY_PARAMS.get('admin2', None)
        admin3_id = self.request.QUERY_PARAMS.get('admin3', None)
        school_id = self.request.QUERY_PARAMS.get('school_id', None)
        mp_id = self.request.QUERY_PARAMS.get('mp_id', None)
        mla_id = self.request.QUERY_PARAMS.get('mla_id', None)
        start_date = self.request.QUERY_PARAMS.get('from', None)
        end_date = self.request.QUERY_PARAMS.get('to', None)
        school_type = self.request.QUERY_PARAMS.get(
            'school_type', 'Primary School')
        response_type = self.request.QUERY_PARAMS.get(
            'response_type', 'call_volume')

        if 'Primary' in school_type:
            school_type = 'Primary School'

        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)

        response_json = {}
        response_json['user_groups'] = {}

        school_type = BoundaryType.objects.get(name=school_type)
        stories_qset = Story.objects.select_related(
            'school', 'user'
        ).filter(
            school__admin3__type=school_type
        )
        assessments_qset = AssessmentsV2.objects.all()

        if survey:
            stories_qset = stories_qset.filter(
                group__survey__name=survey)

        if source:
            stories_qset = stories_qset.filter(
                group__source__name=source)

        if versions:
            versions = map(int, versions)
            stories_qset = stories_qset.filter(
                group__version__in=versions)

        if admin1_id:
            stories_qset = stories_qset.filter(
                school__schooldetails__admin1__id=admin1_id)
            boundary = Boundary.objects.get(id=admin1_id)
            assessments_qset = assessments_qset.filter(
                student_uid__district=boundary.name
            )

        if admin2_id:
            stories_qset = stories_qset.filter(
                school__schooldetails__admin2__id=admin2_id)
            boundary = Boundary.objects.get(id=admin2_id)
            assessments_qset = assessments_qset.filter(
                student_uid__block=boundary.name
            )

        if admin3_id:
            stories_qset = stories_qset.filter(
                school__schooldetails__admin3__id=admin3_id)
            boundary = Boundary.objects.get(id=admin3_id)
            assessments_qset = assessments_qset.filter(
                student_uid__cluster=boundary.name
            )

        if school_id:
            stories_qset = stories_qset.filter(
                school=school_id)
            assessments_qset = assessments_qset.filter(
                student_uid__school_code=school_id
            )

        if mp_id:
            stories_qset = stories_qset.filter(
                school__electedrep__mp_const__id=mp_id)

        if mla_id:
            stories_qset = stories_qset.filter(
                school__electedrep__mla_const__id=mla_id)

        if start_date:
            stories_qset = stories_qset.filter(
                date_of_visit__gte=start_date)
            assessments_qset = assessments_qset.filter(
                assessed_ts__gte=start_date,
            )

        if end_date:
            stories_qset = stories_qset.filter(
                date_of_visit__lte=end_date)
            assessments_qset = assessments_qset.filter(
                assessed_ts__lte=end_date,
            )

        if response_type == 'call_volume':
            dates = stories_qset.values_list('date_of_visit', flat=True).order_by()
            groups = Group.objects.all()
            for group in groups:
                response_json['user_groups'][group.name] = stories_qset.filter(
                    user__in=group.user_set.all()
                ).count()
        else:
            dates = assessments_qset.values_list('assessed_ts', flat=True)
            response_json['user_groups'] = {}

        response_json['volumes'] = self.get_call_volume(dates)

        return Response(response_json)
예제 #13
0
    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)
예제 #14
0
    def handle(self, *args, **options):
        start_date = options.get('from')
        end_date = options.get('to')
        days = options.get('days')
        emails = options.get('emails')

        if not emails:
            print """
            Error:
            --emails parameter not specificed.
            """
            print self.help
            return
        elif days:
            end_date = datetime.today()
            start_date = end_date - timedelta(days=int(days))
        elif start_date or end_date:
            if not (start_date and end_date):
                print """
                Error:
                Please specify both --from and --to parameters.
                """
                print self.help
                return

            date = Date()
            if start_date:
                sane = date.check_date_sanity(start_date)
                if not sane:
                      print """
                      Error:
                      Wrong --from format. Expected YYYY-MM-DD
                      """
                      print self.help
                      return
                else:
                    start_date = date.get_datetime(start_date)

            if end_date:
                sane = date.check_date_sanity(end_date)
                if not sane:
                    print """
                    Error:
                    Wrong --to format. Expected YYYY-MM-DD
                    """
                    print self.help
                    return
                else:
                    end_date = date.get_datetime(end_date)
        else:
            print self.help
            return

        emails = emails.split(",")

        districts = []
        gka_district_ids = set(
            Story.objects.filter(
                group__source__name="sms"
            ).values_list(
                'school__admin3__parent__parent__id',
                flat=True
            )
        )

        for district_id in gka_district_ids:

            district = Boundary.objects.get(id=district_id)
            admin1_json = { 'name': district.name, 'id': district.id}
            admin1_json['sms'] = self.get_story_meta(district.id,'district',start_date, end_date)
            admin1_json['details'] = self.get_story_details(district.id,'district',start_date, end_date)
            admin1_json['blocks'] = []
            #print admin1_json
            blocks = (Boundary.objects.all_active().filter(
                parent_id=district_id,
                type=district.type
            ).select_related('boundarycoord__coord', 'type__name',
                            'hierarchy__name'))
            for block in blocks:
                admin2_json = { 'name': block.name, 'id': block.id}
                admin2_json['sms'] = self.get_story_meta(block.id,'block', start_date, end_date)
                admin2_json['details'] = self.get_story_details(block.id,'block', start_date, end_date)
                #if(int(admin2_json['sms']['stories']) > 0):
                admin1_json['blocks'].append(admin2_json)
            districts.append(admin1_json)

        for each in districts:
            blks = self.transform_data(each)
            for blk in blks:
                self.make_pdf(blk,start_date,end_date,blk[0][1],emails)
예제 #15
0
    def get(self, request):
        source = self.request.QUERY_PARAMS.get('source', None)
        versions = self.request.QUERY_PARAMS.getlist('version', None)
        admin1_id = self.request.QUERY_PARAMS.get('admin1', None)
        admin2_id = self.request.QUERY_PARAMS.get('admin2', None)
        admin3_id = self.request.QUERY_PARAMS.get('admin3', None)
        school_id = self.request.QUERY_PARAMS.get('school_id', None)
        mp_id = self.request.QUERY_PARAMS.get('mp_id', None)
        mla_id = self.request.QUERY_PARAMS.get('mla_id', None)
        start_date = self.request.QUERY_PARAMS.get('from', None)
        end_date = self.request.QUERY_PARAMS.get('to', None)
        school_type = self.request.QUERY_PARAMS.get(
            'school_type', 'Primary School')

        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)

        stories = Story.objects.all()

        if source:
            stories = stories.filter(group__source__name=source)

        if versions:
            versions = map(int, versions)
            stories = stories.filter(group__version__in=versions)

        if school_type:
            stories = stories.filter(school__admin3__type__name=school_type)

        if admin1_id:
            stories = stories.filter(
                school__schooldetails__admin1__id=admin1_id
            )

        if admin2_id:
            stories = stories.filter(
                school__schooldetails__admin2__id=admin2_id
            )

        if admin3_id:
            stories = stories.filter(
                school__schooldetails__admin3__id=admin3_id
            )

        if school_id:
            stories = stories.filter(school__id=school_id)

        if mp_id:
            stories = stories.filter(
                school__electedrep__mp_const__id=mp_id)

        if mla_id:
            stories = stories.filter(
                school__electedrep__mla_const__id=mla_id)

        if start_date:
            stories = stories.filter(date_of_visit__gte=start_date)

        if end_date:
            stories = stories.filter(date_of_visit__lte=end_date)

        response_json = {}

        # Sources and filters
        if source:
            response_json[source] = get_que_and_ans(
                stories, source, school_type, versions)
        else:
            sources = Source.objects.all().values_list('name', flat=True)
            for source in sources:
                response_json[source] = get_que_and_ans(
                    stories, source, school_type, versions)

        return Response(response_json)
예제 #16
0
    def get(self, request):
        gka_comparison = self.request.QUERY_PARAMS.get('gka_comparison', None)
        survey = self.request.QUERY_PARAMS.get('survey', None)
        source = self.request.QUERY_PARAMS.get('source', None)
        versions = self.request.QUERY_PARAMS.getlist('version', None)
        admin1_id = self.request.QUERY_PARAMS.get('admin1', None)
        admin2_id = self.request.QUERY_PARAMS.get('admin2', None)
        admin3_id = self.request.QUERY_PARAMS.get('admin3', None)
        school_id = self.request.QUERY_PARAMS.get('school_id', None)
        mp_id = self.request.QUERY_PARAMS.get('mp_id', None)
        mla_id = self.request.QUERY_PARAMS.get('mla_id', None)
        start_date = self.request.QUERY_PARAMS.get('from', None)
        end_date = self.request.QUERY_PARAMS.get('to', None)
        school_type = self.request.QUERY_PARAMS.get(
            'school_type', 'Primary School')

        if 'Primary' in school_type:
            school_type = 'Primary School'

        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)

        chosen_boundary = None
        chosen_school = None

        stories = Story.objects.select_related('school').order_by().all().values('id')

        if survey:
            stories = stories.filter(group__survey__name=survey)
        
        if source:
            stories = stories.filter(group__source__name=source)

        if versions:
            versions = map(int, versions)
            stories = stories.filter(group__version__in=versions)

        if school_type:
            stories = stories.filter(school__admin3__type__name=school_type)

        if admin1_id:
            stories = stories.filter(
                school__schooldetails__admin1__id=admin1_id
            )
            boundary = Boundary.objects.get(id=admin1_id)
            chosen_boundary = boundary

        if admin2_id:
            stories = stories.filter(
                school__schooldetails__admin2__id=admin2_id
            )
            boundary = Boundary.objects.get(id=admin2_id)
            chosen_boundary = boundary

        if admin3_id:
            stories = stories.filter(
                school__schooldetails__admin3__id=admin3_id
            )
            boundary = Boundary.objects.get(id=admin3_id)
            chosen_boundary = boundary

        if school_id:
            stories = stories.filter(school__id=school_id)
            school = School.objects.get(id=school_id)
            chosen_school = school

        if mp_id:
            stories = stories.filter(
                school__electedrep__mp_const__id=mp_id)

        if mla_id:
            stories = stories.filter(
                school__electedrep__mla_const__id=mla_id)

        if start_date:
            stories = stories.filter(date_of_visit__gte=start_date)

        if end_date:
            stories = stories.filter(date_of_visit__lte=end_date)

        response_json = {}
        
        if gka_comparison:
            gka = GKA(start_date, end_date)
            response_json = gka.generate_report(
                chosen_boundary, chosen_school)
        elif survey == "GP Contest":
            gp_contest = GPContest()
            response_json = gp_contest.generate_report(stories)
        # Sources and filters
        elif source:
            response_json[source] = get_que_and_ans(
                stories, source, school_type, versions)
        else:
            sources = Source.objects.all().values_list('name', flat=True)
            for source in sources:
                response_json[source] = get_que_and_ans(
                    stories, source, school_type, versions)

        return Response(response_json)
예제 #17
0
    def get(self, request):
        source = self.request.QUERY_PARAMS.get('source', None)
        versions = self.request.QUERY_PARAMS.getlist('version', None)
        admin1_id = self.request.QUERY_PARAMS.get('admin1', None)
        admin2_id = self.request.QUERY_PARAMS.get('admin2', None)
        admin3_id = self.request.QUERY_PARAMS.get('admin3', None)
        school_id = self.request.QUERY_PARAMS.get('school_id', None)
        mp_id = self.request.QUERY_PARAMS.get('mp_id', None)
        mla_id = self.request.QUERY_PARAMS.get('mla_id', None)
        start_date = self.request.QUERY_PARAMS.get('from', None)
        end_date = self.request.QUERY_PARAMS.get('to', None)
        school_type = self.request.QUERY_PARAMS.get(
            'school_type', 'Primary School')
        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)

        school_qset = School.objects.filter(
            admin3__type__name=school_type, status=2)
        stories_qset = Story.objects.filter(
            school__admin3__type__name=school_type)

        if admin1_id:
            school_qset = school_qset.filter(
                schooldetails__admin1__id=admin1_id)
            stories_qset = stories_qset.filter(
                school__schooldetails__admin1__id=admin1_id)

        if admin2_id:
            school_qset = school_qset.filter(
                schooldetails__admin2__id=admin2_id)
            stories_qset = stories_qset.filter(
                school__schooldetails__admin2__id=admin2_id)

        if admin3_id:
            school_qset = school_qset.filter(
                schooldetails__admin3__id=admin3_id)
            stories_qset = stories_qset.filter(
                school__schooldetails__admin3__id=admin3_id)

        if school_id:
            school_qset = school_qset.filter(id=school_id)
            stories_qset = stories_qset.filter(
                school=school_id)

        if mp_id:
            school_qset = school_qset.filter(
                electedrep__mp_const__id=mp_id)
            stories_qset = stories_qset.filter(
                school__electedrep__mp_const__id=mp_id)

        if mla_id:
            school_qset = school_qset.filter(
                electedrep__mla_const__id=mla_id)
            stories_qset = stories_qset.filter(
                school__electedrep__mla_const__id=mla_id)

        if start_date:
            #school_qset = school_qset.filter(
            #    story__date_of_visit__gte=start_date)
            stories_qset = stories_qset.filter(
                date_of_visit__gte=start_date)

        if end_date:
            # school_qset = school_qset.filter(
            #     story__date_of_visit__lte=end_date)
            stories_qset = stories_qset.filter(
                date_of_visit__lte=end_date)

        response_json = {}

        response_json['total'] = {}
        response_json['total']['schools'] = school_qset.count()
        response_json['total']['stories'] = stories_qset.count()
        response_json['total']['schools_with_stories'] = stories_qset.distinct('school').count()

        if source:
            stories_qset = self.source_filter(
                source,
                stories_qset
            )

            if versions:
                versions = map(int, versions)
                stories_qset = stories_qset.filter(
                    group__version__in=versions
                )

            response_json[source] = self.get_json(
                source,
                stories_qset,
            )
        else:
            sources = Source.objects.all().values_list('name', flat=True)
            for source in sources:
                stories = self.source_filter(
                    source,
                    stories_qset
                )
                response_json[source] = self.get_json(
                    source,
                    stories,
                )

        response_json['respondents'] = self.get_respondents(stories_qset)

        return Response(response_json)
예제 #18
0
    def get(self, request):
        survey = self.request.QUERY_PARAMS.get('survey', None)
        source = self.request.QUERY_PARAMS.get('source', None)
        versions = self.request.QUERY_PARAMS.getlist('version', None)
        admin1_id = self.request.QUERY_PARAMS.get('admin1', None)
        admin2_id = self.request.QUERY_PARAMS.get('admin2', None)
        admin3_id = self.request.QUERY_PARAMS.get('admin3', None)
        school_id = self.request.QUERY_PARAMS.get('school_id', None)
        mp_id = self.request.QUERY_PARAMS.get('mp_id', None)
        mla_id = self.request.QUERY_PARAMS.get('mla_id', None)
        start_date = self.request.QUERY_PARAMS.get('from', None)
        end_date = self.request.QUERY_PARAMS.get('to', None)
        school_type = self.request.QUERY_PARAMS.get(
            'school_type', 'Primary School')
        top_summary = self.request.QUERY_PARAMS.get('top_summary', None)
        date = Date()

        if 'Primary' in school_type:
            school_type = 'Primary School'

        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)

        school_type = BoundaryType.objects.get(name=school_type)
        school_qset = School.objects.filter(
            admin3__type=school_type, status=2).order_by().values('id')
        stories_qset = Story.objects.select_related('school').filter(
            school__in=school_qset).order_by().values('id')

        if admin1_id:
            school_qset = school_qset.filter(
                schooldetails__admin1__id=admin1_id)
            stories_qset = stories_qset.filter(
                school__schooldetails__admin1__id=admin1_id)

        if admin2_id:
            school_qset = school_qset.filter(
                schooldetails__admin2__id=admin2_id)
            stories_qset = stories_qset.filter(
                school__schooldetails__admin2__id=admin2_id)

        if admin3_id:
            school_qset = school_qset.filter(
                schooldetails__admin3__id=admin3_id)
            stories_qset = stories_qset.filter(
                school__schooldetails__admin3__id=admin3_id)

        if school_id:
            school_qset = school_qset.filter(id=school_id)
            stories_qset = stories_qset.filter(
                school=school_id)

        if mp_id:
            school_qset = school_qset.filter(
                electedrep__mp_const__id=mp_id)
            stories_qset = stories_qset.filter(
                school__electedrep__mp_const__id=mp_id)

        if mla_id:
            school_qset = school_qset.filter(
                electedrep__mla_const__id=mla_id)
            stories_qset = stories_qset.filter(
                school__electedrep__mla_const__id=mla_id)

        if start_date:
            stories_qset = stories_qset.filter(
                date_of_visit__gte=start_date)

        if end_date:
            stories_qset = stories_qset.filter(
                date_of_visit__lte=end_date)

        response_json = {}

        total_schools = school_qset.count()

        response_json['total'] = {}
        response_json['total']['schools'] = total_schools
        response_json['total']['stories'] = stories_qset.count()
        response_json['total']['schools_with_stories'] = stories_qset.distinct('school').count()

        if top_summary:
            response_json['top_summary'] = self.get_total_summary(total_schools, school_qset, admin1_id)
            return Response(response_json)

        if survey:
            stories_qset = stories_qset.filter(
                group__survey__name=survey
            )
    
        if source:
            stories_qset = self.source_filter(
                source,
                stories_qset
            )

            unique_schools_counts_per_source = [
                {
                    'group__source__name':source,
                    'school_count':stories_qset.aggregate(
                        Count('school', distinct=True)
                    )['school__count']
                }
            ]

            if versions:
                versions = map(int, versions)
                stories_qset = stories_qset.filter(
                    group__version__in=versions
                )

            response_json[source] = self.get_json(
                source,
                stories_qset,
                unique_schools_counts_per_source,
            )
        else:
            sources = Source.objects.all().values_list('name', flat=True)
            
            unique_schools_counts_per_source = stories_qset.values(
                'group__source__name'
            ).order_by(
            ).annotate(
                school_count=Count(
                    'school',
                    distinct=True
                )
            )

            for source in sources:
                stories = self.source_filter(
                    source,
                    stories_qset
                )
                response_json[source] = self.get_json(
                    source,
                    stories,
                    unique_schools_counts_per_source,
                )

        response_json['respondents'] = self.get_respondents(stories_qset)
        response_json['users'] = self.get_users(stories_qset)

        return Response(response_json)
예제 #19
0
    def handle(self, *args, **options):
        start_date = options.get('from')
        end_date = options.get('to')
        days = options.get('days')
        emails = options.get('emails')

        if not emails:
            print """
            Error:
            --emails parameter not specificed.
            """
            print self.help
            return
        elif days:
            end_date = datetime.today()
            start_date = end_date - timedelta(days=int(days))
        elif start_date or end_date:
            if not (start_date and end_date):
                print """
                Error:
                Please specify both --from and --to parameters.
                """
                print self.help
                return

            date = Date()
            if start_date:
                sane = date.check_date_sanity(start_date)
                if not sane:
                    print """
                      Error:
                      Wrong --from format. Expected YYYY-MM-DD
                      """
                    print self.help
                    return
                else:
                    start_date = date.get_datetime(start_date)

            if end_date:
                sane = date.check_date_sanity(end_date)
                if not sane:
                    print """
                    Error:
                    Wrong --to format. Expected YYYY-MM-DD
                    """
                    print self.help
                    return
                else:
                    end_date = date.get_datetime(end_date)
        else:
            print self.help
            return

        emails = emails.split(",")

        districts = []
        gka_district_ids = set(
            Story.objects.filter(group__source__name="sms").values_list(
                'school__admin3__parent__parent__id', flat=True))

        for district_id in gka_district_ids:

            district = Boundary.objects.get(id=district_id)
            admin1_json = {'name': district.name, 'id': district.id}
            admin1_json['sms'] = self.get_story_meta(district.id, 'district',
                                                     start_date, end_date)
            admin1_json['details'] = self.get_story_details(
                district.id, 'district', start_date, end_date)
            admin1_json['blocks'] = []
            #print admin1_json
            blocks = (Boundary.objects.all_active().filter(
                parent_id=district_id,
                type=district.type).select_related('boundarycoord__coord',
                                                   'type__name',
                                                   'hierarchy__name'))
            for block in blocks:
                admin2_json = {'name': block.name, 'id': block.id}
                admin2_json['sms'] = self.get_story_meta(
                    block.id, 'block', start_date, end_date)
                admin2_json['details'] = self.get_story_details(
                    block.id, 'block', start_date, end_date)
                #if(int(admin2_json['sms']['stories']) > 0):
                admin1_json['blocks'].append(admin2_json)
            districts.append(admin1_json)

        for each in districts:
            blks = self.transform_data(each)
            for blk in blks:
                self.make_pdf(blk, start_date, end_date, blk[0][1], emails)
예제 #20
0
    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)