コード例 #1
0
def build_json_value(org, cf, value):
    json_value = {"text": value.string_value}

    if value.datetime_value is not None:
        localized = timezone.localtime(value.datetime_value, org.timezone)
        json_value["datetime"] = localized.isoformat()

    if value.decimal_value is not None:
        json_value["number"] = format_number(value.decimal_value)

    if value.location_value is not None:
        loc = value.location_value

        if loc.level == 1:
            json_value["state"] = loc.path

        elif loc.level == 2:
            json_value["district"] = loc.path
            json_value["state"] = strip_last_path(loc.path)

        elif loc.level == 3:
            json_value["ward"] = loc.path
            json_value["district"] = strip_last_path(loc.path)
            json_value["state"] = strip_last_path(json_value["district"])

    return {str(cf.uuid): json_value}
コード例 #2
0
def build_json_value(org, cf, value):
    json_value = {'text': value.string_value}

    if value.datetime_value is not None:
        localized = timezone.localtime(value.datetime_value, org.timezone)
        json_value['datetime'] = localized.isoformat()

    if value.decimal_value is not None:
        json_value['number'] = format_number(value.decimal_value)

    if value.location_value is not None:
        loc = value.location_value

        if loc.level == 1:
            json_value['state'] = loc.path

        elif loc.level == 2:
            json_value['district'] = loc.path
            json_value['state'] = strip_last_path(loc.path)

        elif loc.level == 3:
            json_value['ward'] = loc.path
            json_value['district'] = strip_last_path(loc.path)
            json_value['state'] = strip_last_path(json_value['district'])

    return {six.text_type(cf.uuid): json_value}
コード例 #3
0
ファイル: mailroom.py プロジェクト: tybritten/rapidpro
def serialize_field_value(contact, field, value):
    org = contact.org

    # parse as all value data types
    str_value = str(value)[:640]
    dt_value = org.parse_datetime(value)
    num_value = org.parse_number(value)
    loc_value = None

    # for locations, if it has a '>' then it is explicit, look it up that way
    if AdminBoundary.PATH_SEPARATOR in str_value:
        loc_value = contact.org.parse_location_path(str_value)

    # otherwise, try to parse it as a name at the appropriate level
    else:
        if field.value_type == ContactField.TYPE_WARD:
            district_field = ContactField.get_location_field(org, ContactField.TYPE_DISTRICT)
            district_value = contact.get_field_value(district_field)
            if district_value:
                loc_value = org.parse_location(str_value, AdminBoundary.LEVEL_WARD, district_value)

        elif field.value_type == ContactField.TYPE_DISTRICT:
            state_field = ContactField.get_location_field(org, ContactField.TYPE_STATE)
            if state_field:
                state_value = contact.get_field_value(state_field)
                if state_value:
                    loc_value = org.parse_location(str_value, AdminBoundary.LEVEL_DISTRICT, state_value)

        elif field.value_type == ContactField.TYPE_STATE:
            loc_value = org.parse_location(str_value, AdminBoundary.LEVEL_STATE)

        if loc_value is not None and len(loc_value) > 0:
            loc_value = loc_value[0]
        else:
            loc_value = None

    # all fields have a text value
    field_dict = {"text": str_value}

    # set all the other fields that have a non-zero value
    if dt_value is not None:
        field_dict["datetime"] = timezone.localtime(dt_value, org.timezone).isoformat()

    if num_value is not None:
        field_dict["number"] = format_number(num_value)

    if loc_value:
        if loc_value.level == AdminBoundary.LEVEL_STATE:
            field_dict["state"] = loc_value.path
        elif loc_value.level == AdminBoundary.LEVEL_DISTRICT:
            field_dict["district"] = loc_value.path
            field_dict["state"] = AdminBoundary.strip_last_path(loc_value.path)
        elif loc_value.level == AdminBoundary.LEVEL_WARD:
            field_dict["ward"] = loc_value.path
            field_dict["district"] = AdminBoundary.strip_last_path(loc_value.path)
            field_dict["state"] = AdminBoundary.strip_last_path(field_dict["district"])

    return field_dict