Beispiel #1
0
    def handle_register(self, message, msg_text):
        connection, unused = getConnectionAndReporter(message,
                                                      self.ALLOWED_ROLE_CODES)

        text = msg_text.strip()

        if text == u'':
            message.respond(self.HELP_MESSAGES[u'register'])
            return

        try:
            location_code, role_code, full_name = grammar(text).register()
        except parsley.ParseError:
            message.respond(self.ERROR_MESSAGES[u'invalid_message'] %
                            {u'text': message.text})
            return

        location = Location.get_by_code(location_code)
        if location is None:
            message.respond(self.ERROR_MESSAGES[u'invalid_location'] % {
                u'location_code': location_code,
                u'text': message.text
            })
            return

        role = Role.get_by_code(role_code)
        if role is None or role.code.lower() not in self.ALLOWED_ROLE_CODES:
            message.respond(self.ERROR_MESSAGES[u'invalid_role'] % {
                u'role_code': role_code,
                u'text': message.text
            })
            return

        kwargs = {u'location': location, u'role': role}
        kwargs[u'alias'], kwargs[u'first_name'], kwargs[
            u'last_name'] = Reporter.parse_name(full_name)
        rep = Reporter(**kwargs)

        if Reporter.exists(rep, connection):
            message.respond(
                self.RESPONSE_MESSAGES[u'already_registered'] % {
                    u'name': rep.first_name,
                    u'role': rep.role.name,
                    u'location': rep.location.name,
                    u'location_type': rep.location.type.name
                })
            return

        rep.save()
        connection.reporters.add(rep)

        message.respond(
            self.RESPONSE_MESSAGES[u'register'] % {
                u'name': rep.first_name,
                u'role': rep.role.code,
                u'location': rep.location.name,
                u'location_type': rep.location.type.name
            })
Beispiel #2
0
    def handle_report(self, message, msg_text):
        connection, reporter = getConnectionAndReporter(
            message, self.ALLOWED_ROLE_CODES)
        text = msg_text.strip()

        if text == u'':
            message.respond(self.HELP_MESSAGES[u'report'])
            return

        if reporter is None:
            message.respond(self.ERROR_MESSAGES[u'not_registered'])
            return

        try:
            location_code, pairs = text.split(None, 1)
            pairs = grammar(pairs).report_list()
        except (ValueError, parsley.ParseError):
            message.respond(self.ERROR_MESSAGES[u'invalid_message'] %
                            {u'text': message.text})
            return

        location = Location.get_by_code(location_code)
        if location is None:
            message.respond(self.ERROR_MESSAGES[u'invalid_location'] % {
                u'location_code': location_code,
                u'text': message.text
            })
            return

        amounts = []
        commodities = []
        for code, amount in pairs:
            result = process.extractOne(code, commodity_codes, score_cutoff=50)

            if result is None:
                continue

            comm = result[0]
            amounts.append(amount)
            commodities.append(comm.upper())

            Report.objects.create(reporter=reporter,
                                  time=now(),
                                  connection=connection,
                                  location=location,
                                  commodity=comm,
                                  immunized=amount)

        response_pairs = u', '.join(u'{}={}'.format(a, b)
                                    for a, b in zip(commodities, amounts))
        message.respond(
            self.RESPONSE_MESSAGES[u'report'] % {
                u'location': location.name,
                u'location_type': location.type.name,
                u'pairs': response_pairs,
                u'name': reporter.first_name
            })
Beispiel #3
0
    def handle_shortage(self, message, msg_text):
        connection, reporter = getConnectionAndReporter(
            message, self.ALLOWED_ROLE_CODES)
        text = msg_text.strip()

        if text == u'':
            message.respond(self.HELP_MESSAGES[u'shortage'])
            return

        if reporter is None:
            message.respond(self.ERROR_MESSAGES[u'not_registered'])
            return

        try:
            location_code, codes = text.split(None, 1)
            codes = grammar(codes).shortage_list()
        except (ValueError, parsley.ParseError):
            message.respond(self.ERROR_MESSAGES[u'invalid_message'] %
                            {u'text': message.text})
            return

        location = Location.get_by_code(location_code)
        if location is None:
            message.respond(self.ERROR_MESSAGES[u'invalid_location'] % {
                u'location_code': location_code,
                u'text': message.text
            })
            return

        results = [
            process.extractOne(c, codes, score_cutoff=50) for c in codes
        ]
        commodity = None

        for result in results:
            if result is None:
                continue

            comm = result[0]

            if commodity is None:
                commodity = comm

            Shortage.objects.create(time=now(),
                                    commodity=comm,
                                    reporter=reporter,
                                    location=location,
                                    connection=connection)

        message.respond(
            self.RESPONSE_MESSAGES[u'shortage'] % {
                u'location': location.name,
                u'location_type': location.type.name,
                u'commodity': commodity.upper()
            })
Beispiel #4
0
    def handle_nc(self, message, msg_text):
        connection, reporter = getConnectionAndReporter(
            message, self.ALLOWED_ROLE_CODES)

        text = msg_text.strip()

        if text == u'':
            message.respond(self.HELP_MESSAGES[u'nc'])
            return

        if reporter is None:
            message.respond(self.ERROR_MESSAGES[u'not_registered'])
            return

        try:
            location_code, reason_code, cases = grammar(text).noncompliance()
        except parsley.ParseError:
            message.respond(self.ERROR_MESSAGES[u'invalid_message'] %
                            {u'text': message.text})
            return

        location = Location.get_by_code(location_code)
        if location is None:
            message.respond(self.ERROR_MESSAGES[u'invalid_location'] % {
                u'location_code': location_code,
                u'text': message.text
            })
            return

        if reason_code not in reason_codes:
            message.respond(self.ERROR_MESSAGES[u'invalid_reason'] % {
                u'reason_code': reason_code,
                u'text': message.text
            })

        report = NonCompliance.objects.create(reporter=reporter,
                                              location=location,
                                              reason=reason_code,
                                              cases=cases,
                                              time=now(),
                                              connection=connection)

        message.respond(
            self.RESPONSE_MESSAGES[u'nc'] % {
                u'location': location.name,
                u'reason': report.get_reason_display(),
                u'cases': cases,
                u'location_type': location.type.name
            })
Beispiel #5
0
    def handle_report(self, message, message_text):
        connection, reporter = getConnectionAndReporter(
            message, self.ALLOWED_ROLE_CODES)
        text = message_text.strip().upper()

        if text == u'':
            message.respond(HELP_MESSAGES[u'report'])
            return

        if reporter is None:
            message.respond(ERROR_MESSAGES[u'not_registered'])
            return

        try:
            entries = report_grammar(text).report()
        except parsley.ParseError:
            message.respond(ERROR_MESSAGES[u'invalid_message'] %
                            {u'text': message.text})
            return

        location = reporter.location

        report_data = {k: 0 for k in FIELD_MAP}
        report_data.update(dict(entries))

        report_date = classify_date(date.today())
        try:
            report = DeathReport.objects.get(date=report_date,
                                             location=location)
        except DeathReport.DoesNotExist:
            report = DeathReport(location=location,
                                 date=report_date,
                                 connection=connection,
                                 reporter=reporter)

        report.data.update(report_data)
        report.save()

        message.respond(
            RESPONSE_MESSAGES[u'report'] % {
                u'location': location.name,
                u'location_type': location.type.name,
                u'name': reporter.first_name,
                u'date': report.date.strftime(u'%d-%m-%Y')
            })
Beispiel #6
0
    def register(self, message, location_code, role, name=''):
        conn, unused = getConnectionAndReporter(message, ALLOWED_ROLE_CODES)

        data = {}
        try:
            data['location'] = Location.objects.get(code=location_code,
                                                    type__name=u'RC',
                                                    active=True)
            data['role'] = Role.objects.get(code__iexact=role)
            data['alias'], data['first_name'], data[
                'last_name'] = Reporter.parse_name(name.strip())
            rep = Reporter(**data)

            if Reporter.exists(rep, conn):
                message.respond(self.response_messages['already_registered'] %
                                dict(name=rep.first_name,
                                     role=rep.role,
                                     location_name=rep.location))
                return True

            rep.save()
            conn.reporters.add(rep)

            message.respond(self.response_messages['registered'] %
                            dict(name=rep.first_name,
                                 role=rep.role,
                                 location_name=rep.location,
                                 location_type=rep.location.type))
        except Role.DoesNotExist:
            message.respond(self.error_messages['invalid_role'] %
                            dict(role_code=role, text=message.text))
        except Location.DoesNotExist:
            message.respond(
                self.error_messages['invalid_location'] %
                dict(location_code=location_code, text=message.text))

        return True
Beispiel #7
0
    def report(self, message, gender_data):
        connection, reporter = getConnectionAndReporter(
            message, ALLOWED_ROLE_CODES)

        report = dict(gender_data)
        for gender in ['m', 'f']:
            report[gender] = map(lambda i: int(i), report[gender]) + (
                [0] *
                (4 - len(report[gender]))) if report[gender] else [0, 0, 0, 0]

        try:
            if reporter is None:
                message.respond(self.error_messages['unauthorized_reporter'])
                return True

            if not reporter.role.code.lower() in ALLOWED_ROLE_CODES:
                message.respond(self.error_messages['unauthorized_role'])
                return True

            location = reporter.location
            if not location.active:
                message.respond(self.response_messages['inactive_location'])
                return True

            # store the report
            try:
                br = BirthRegistration.objects.get(connection=connection,
                                                   reporter=reporter,
                                                   location=location,
                                                   time=message.datetime)
            except BirthRegistration.DoesNotExist:
                br = BirthRegistration()
                br.connection = connection
                br.reporter = reporter
                br.location = location
                br.time = message.datetime

            br.girls_below1 = report['f'][0]
            br.girls_1to4 = report['f'][1]
            br.girls_5to9 = report['f'][2]
            br.girls_10to18 = report['f'][3]
            br.boys_below1 = report['m'][0]
            br.boys_1to4 = report['m'][1]
            br.boys_5to9 = report['m'][2]
            br.boys_10to18 = report['m'][3]

            br.save()

            # respond adequately
            message.respond(self.response_messages['report'] %
                            dict(location=location.name,
                                 date=message.datetime.strftime('%d/%m/%Y'),
                                 girls_below1=br.girls_below1,
                                 girls_1to4=br.girls_1to4,
                                 girls_5to9=br.girls_5to9,
                                 girls_10to18=br.girls_10to18,
                                 boys_below1=br.boys_below1,
                                 boys_1to4=br.boys_1to4,
                                 boys_5to9=br.boys_5to9,
                                 boys_10to18=br.boys_10to18))
        except Exception as e:
            logger.debug(e)

        return True