示例#1
0
文件: forms.py 项目: malthe/cvs
    def parse(self, keyword=None):
        roles = HealthRole.objects.all()
        by_keyword = dict((role.keyword, role) for role in roles)
        matches = difflib.get_close_matches(keyword.upper(), by_keyword)

        if not matches:
            raise FormatError(
                u"Did not understand the keyword: '%s'." % keyword)

        keyword = matches[0]
        result = {
            'role': by_keyword[keyword.upper()],
            }

        try:
            code = u"".join(pico.digits())
        except:
            raise FormatError(u"Expected an HMIS facility code (got: %s)." %
                             "".join(remaining()))

        try:
            facility = result['facility'] = Facility.objects.filter(code=code).get()
        except Facility.DoesNotExist:
            raise FormatError(u"No such HMIS facility code: %s." % code)

        whitespace()
        optional(pico.separator, None)

        # optionally provide a sub-village group
        name = "".join(remaining()).strip()
        if name:
            # get all (name, location) pairs of all child nodes of
            # groups that report to this facility
            policies = {}
            policy = None
            group = None

            for policy in facility.policies.all().select_related():
                policies[policy.group.name.upper()] = policy
                for descendant in policy.group.get_descendants():

                    try:
                        group_name = descendant.name.upper()
                        policies[group_name] = descendant.reporting_policy
                    except ReportingPolicy.DoesNotExist:
                        pass

            matches = difflib.get_close_matches(name.upper(), policies)
            if matches:
                name = matches[0]
                group = policies[name].group
            elif policy is not None:
                group = policy.group.add_child(
                    slug="added_by_user", name='"%s"' % name)
                facility.policies.create(group=group)

            result['group'] = group

        return result
示例#2
0
文件: forms.py 项目: djangosms/core
    def parse(cls):
        result = {}

        @tri
        def ident():
            pico.hash()
            commit()
            return many1(any_token)

        @tri
        def number():
            return many1(partial(one_of, string.digits + ' -+()'))

        ident = optional(partial(choice, ident, number), None)
        if ident is not None:
            result['ident'] = re.sub('[ \-+()]', '', "".join(ident))
        else:
            name = optional(tri(pico.name), None)
            if name is not None:
                result['name'] = name

        whitespace()
        if peek() and not result:
            raise FormatError(
                "We did not understand: %s." % "".join(remaining()))

        return result
示例#3
0
    def parse(cls):
        one_of('+')
        caseless_string('echo')
        whitespace()

        return {
            'echo': "".join(remaining())
            }
示例#4
0
文件: forms.py 项目: malthe/cvs
    def parse(cls):
        result = {}

        try:
            result['name'] = pico.name()
        except:
            raise FormatError(
                "Expected name (got: %s)." % "".join(remaining()))

        try:
            many1(partial(one_of, ' ,;'))
            result['sex'] = pico.one_of_strings(
                'male', 'female', 'm', 'f')[0].upper()
        except:
            raise FormatError(
                "Expected the infant's gender "
                "(\"male\", \"female\", or simply \"m\" or \"f\"), "
                "but received instead: %s." % "".join(remaining()))

        try:
            many1(partial(one_of, ' ,;'))
            words = pico.name().lower()
        except:
            raise FormatError(
                "Expected a location; "
                "either \"home\", \"clinic\" or \"facility\" "
                "(got: %s)." % "".join(remaining()))

        for word in words.split():
            matches = difflib.get_close_matches(
                word, ('home', 'clinic', 'facility'))
            if matches:
                result['place'] = matches[0].upper()
                break
        else:
            raise FormatError(
                "Did not understand the location: %s." % words)

        return result
示例#5
0
文件: forms.py 项目: malthe/cvs
    def parse(cls):
        result = {}

        try:
            identifiers = optional(tri(pico.ids), None)
            if identifiers:
                result['ids'] = [id.upper() for id in identifiers]
            else:
                result['name'] = pico.name()
        except:
            raise FormatError(
                "Expected a name, or a patient's health or tracking ID "
                "(got: %s)." % "".join(remaining()))

        if 'name' in result:
            try:
                many1(partial(one_of, ' ,;'))
                result['sex'] = pico.one_of_strings(
                    'male', 'female', 'm', 'f')[0].upper()
            except:
                raise FormatError(
                    "Expected the infant's gender "
                    "(\"male\", \"female\", or simply \"m\" or \"f\"), "
                    "but received instead: %s." % "".join(remaining()))
            try:
                pico.separator()
            except:
                raise FormatError("Expected age or birthdate of patient.")

            try:
                result['age'] = choice(*map(tri, (pico.date, pico.timedelta)))
            except:
                raise FormatError("Expected age or birthdate of patient, but "
                                 "received %s." % "".join(remaining()))

        return result
示例#6
0
    def parse(cls):
        one_of('+')
        caseless_string('epi')

        aggregates = {}

        if whitespace():
            while peek():
                try:
                    code = "".join(pico.one_of_strings(*(
                        tuple(cls.TOKENS) + tuple(cls.ALIAS))))
                    code = code.upper()
                except:
                    raise FormatError(
                        "Expected an epidemiological indicator "
                        "such as TB or MA (got: %s)." % \
                        "".join(remaining()))

                # rewrite alias
                code = cls.ALIAS.get(code, code)

                if code in aggregates:
                    raise FormatError("Duplicate value for %s." % code)

                whitespace1()

                try:
                    minus = optional(partial(one_of, '-'), '')
                    value = int("".join([minus]+pico.digits()))
                except:
                    raise FormatError("Expected a value for %s." % code)

                if value < 0:
                    raise FormatError("Got %d for %s. You must "
                                      "report a positive value." % (
                        value, cls.TOKENS[code].lower()))

                aggregates[code] = value
                many(partial(one_of, ' ,;.'))

        return {
            'aggregates': aggregates
            }
示例#7
0
    def parse(cls):
        result = {}

        prefix = optional(tri(identifier), None)
        if prefix is not None:
            result['patient_id'] = "".join(prefix)
            whitespace()

        one_of('+')
        caseless_string('muac')

        if prefix is None:
            try:
                whitespace1()
                part = optional(tri(identifier), None)
                if part is not None:
                    result['patient_id'] = "".join(part)
                else:
                    result['name'] = name()
            except:
                raise FormatError("Expected a patient id or name.")

        if 'name' in result:
            try:
                separator()
                result['sex'] = one_of('MmFf').upper()
            except:
                raise FormatError("Expected either M or F to indicate the patient's gender.")

            try:
                separator()
            except:
                raise FormatError("Expected age or birthdate of patient.")

            try:
                result['age'] = choice(*map(tri, (date, timedelta)))
            except:
                received, stop = many_until(any_token, comma)
                raise FormatError("Expected age or birthdate of patient, but "
                                 "received %s." % "".join(received))
        try:
            if prefix is None:
                separator()
            else:
                whitespace1()

            reading = choice(
                partial(one_of_strings, 'red', 'green', 'yellow', 'r', 'g', 'y'),
                digits)

            try:
                reading = int("".join(reading))
            except:
                reading = reading[0].upper()
            else:
                whitespace()
                unit = optional(partial(one_of_strings, 'mm', 'cm'), None)
                if unit is None:
                    reading = cls.get_reading_in_mm(reading)
                elif "".join(unit) == 'cm':
                    reading = reading * 10
            result['reading'] = reading
        except:
            raise FormatError(
                "Expected MUAC reading (either green, yellow or red), but "
                "received %s." % "".join(remaining()))

        if optional(separator, None):
            result['tags'] = tags()

        return result
示例#8
0
文件: forms.py 项目: malthe/cvs
    def parse(self, health_id=None):
        result = {}

        if health_id is None:
            try:
                part = optional(tri(pico.identifier), None)
                if part is not None:
                    health_id = "".join(part)
                else:
                    result['name'] = pico.name()
            except:
                raise FormatError("Expected a patient id or name.")

        if 'name' in result:
            try:
                pico.separator()
                result['sex'] = pico.one_of_strings(
                    'male', 'female', 'm', 'f')[0].upper()
            except:
                raise FormatError(
                    "Expected either M or F " \
                    "to indicate the patient's gender (got: %s)." %
                    "".join(remaining()))

            try:
                pico.separator()
            except:
                raise FormatError("Expected age or birthdate of patient.")

            try:
                result['age'] = choice(*map(tri, (pico.date, pico.timedelta)))
            except:
                raise FormatError("Expected age or birthdate of patient, but "
                                 "received %s." % "".join(
                                      remaining()).split(',')[0])

        if health_id is not None:
            result['health_id'] = health_id

        try:
            whitespace()
            optional(pico.separator, None)
            reading = choice(
                partial(pico.one_of_strings,
                        'red', 'green', 'yellow', 'r', 'g', 'y'), pico.digits)

            try:
                reading = int("".join(reading))
            except:
                result['category'] = reading[0].upper()
            else:
                whitespace()
                unit = optional(partial(pico.one_of_strings, 'mm', 'cm'), None)
                if unit is None:
                    reading = self.get_reading_in_mm(reading)
                elif "".join(unit) == 'cm':
                    reading = reading * 10
                result['reading'] = reading
        except:
            raise FormatError(
                "Expected MUAC reading (either green, yellow or red), but "
                "received %s." % "".join(remaining()))

        if optional(partial(choice, tri(pico.separator), tri(whitespace)), None):
            if optional(partial(
                pico.one_of_strings, 'oedema', 'odema', 'oe'), None):
                result['oedema'] = True
            elif peek():
                raise FormatError(
                    "Specify \"oedema\"  or \"oe\" if the patient shows "
                    "signs of oedema, otherwise leave empty (got: %s)." % \
                    "".join(remaining()))

        return result
示例#9
0
文件: forms.py 项目: malthe/cvs
    def parse(cls, keyword=None, keywords=None):
        if keywords is None:
            keywords = cls.KEYWORDS

        slug = keywords[keyword.lower()]
        kind = ReportKind.objects.get(slug=slug)

        observations = {}
        result = {
            'observations': observations,
            'kind': kind,
            }

        total = "".join(optional(pico.digits, ()))
        if total:
            result['total'] = int(total)
            many1(partial(one_of, ' ,;'))

        kinds = ObservationKind.objects.filter(slug__startswith="%s_" % slug).all()
        observation_kinds = dict((kind.slug, kind) for kind in kinds)
        codes = [kind.abbr for kind in kinds if kind.abbr]

        # we allow both the observation kinds and any aliases
        allowed_codes = tuple(codes) + tuple(cls.ALIASES)

        while peek():
            # look up observation kinds that double as user input
            # for the aggregate codes
            try:
                code = "".join(pico.one_of_strings(*allowed_codes)).lower()
            except:
                raise FormatError(
                    "Expected an indicator code "
                    "such as %s (got: %s)." % (
                        " or ".join(map(unicode.upper, codes[:2])),
                        "".join(remaining()).strip() or u"nothing"))

            # rewrite alias if required, then look up kind
            munged= "%s_%s" % (slug, code)
            munged = cls.ALIASES.get(munged, munged)
            kind = observation_kinds[munged]

            # guard against duplicate entries
            if kind.slug in observations:
                raise FormatError("Duplicate value for %s." % code)

            whitespace()

            try:
                minus = optional(partial(one_of, '-'), '')
                value = int("".join([minus]+pico.digits()))
            except:
                raise FormatError("Expected a value for %s." % code)

            if value < 0:
                raise FormatError("Got %d for %s. You must "
                                  "report a positive value." % (
                    value, kind.name))

            observations[kind.slug] = value
            many(partial(one_of, ' ,;.'))

        return result
示例#10
0
 def parse(cls):
     one_of('+')
     caseless_string('bad')
     from django.core.exceptions import ImproperlyConfigured
     raise ImproperlyConfigured("".join(remaining()))
示例#11
0
文件: forms.py 项目: djangosms/core
 def parse(self):
     return {
         'text': "".join(remaining())
         }