Exemplo n.º 1
0
def upload_resources(filename, skip=0, limit=None):
    """Upload  from a CSV file."""
    # Use sys.stdout.write so resources can be printed nicely and succinctly
    import sys

    date_converter = lambda s: datetime.strptime(s, '%Y-%m-%d')
    bool_converter = lambda s: s == "true"
    resource_schema = facility_schema['fields']
    
    convert_map = {
		'integer': int,
		'float': float,
		'datetime': date_converter,
		'boolean': bool_converter
    }

    convert = {}
    
    for k, v in resource_schema.items():
		field_type = v.get('type')
		if convert_map.has_key(field_type):
			convert[k] = convert_map[field_type]

    def print_flush(msg):
        sys.stdout.write(msg)
        sys.stdout.flush()

    facility_code = facility_schema['facility_code']
    print_every = 1000
    print_flush("Adding resources. Please be patient.")

    with open(filename) as f:
        reader = DictReader(f)
        for i in range(skip):
            reader.next()
        for i, d in enumerate(reader):
            actual_index = i + skip + 2
            do_print = actual_index % print_every == 0
            try:
                d = dict((k, convert.get(k, str)(v)) for k, v in d.items() if v)
                coords = [d.pop('longitude', None), d.pop('latitude', None)]
                if coords[0] and coords[1]:
                    d['location'] = {'type': 'Point', 'coordinates': coords}
                d['facility_code'] = facility_code
                if not check(add_document(facility_schema['endpoint'], d), 201, False):
                    raise Exception()
                if do_print:
                    print_flush(".")

            except Exception as e:
                print "Error adding resource", e
                pprint(d)
                exit()

            if limit and i >= limit:
                break
    # Create a 2dsphere index on the location field for geospatial queries
	app.data.driver.db['resources'].create_index([('location', '2dsphere')])
    print "Resources uploaded!"
Exemplo n.º 2
0
def upload_waterpoints(filename, skip=0, limit=None):
    """Upload waterpoints from a CSV file."""
    date_converter = lambda s: datetime.strptime(s, '%Y-%m-%d')
    bool_converter = lambda s: s == "true"

    status_map = {
        "non functional": "not functional",
        "functional needs repair": "needs repair"
    }

    status_converter = lambda s: status_map.get(s.lower(), s.lower())

    convert = {
        'gid': int,
        'object_id': int,
        'valid_from': date_converter,
        'valid_to': date_converter,
        'amount_tsh': float,
        'breakdown_year': int,
        'date_recorded': date_converter,
        'gps_height': float,
        'latitude': float,
        'longitude': float,
        'num_private': int,
        'region_code': int,
        'district_code': int,
        'population': int,
        'public_meeting': bool_converter,
        'construction_year': int,
        'status_group': status_converter
    }

    facility_code = "wpf001"

    with open(filename) as f:
        reader = DictReader(f)
        for i in range(skip):
            reader.next()
        for i, d in enumerate(reader):
            print "Adding line", i + skip + 2

            try:
                d = dict((k, convert.get(k, str)(v)) for k, v in d.items() if v)
                coords = [d.pop('longitude'), d.pop('latitude')]
                d['location'] = {'type': 'Point', 'coordinates': coords}
                d['facility_code'] = facility_code
                if not check(add_document('waterpoints', d)):
                    raise Exception()

            except Exception as e:
                print "Error adding waterpoint", e
                pprint(d)
                exit()

            if limit and i >= limit:
                break
    # Create a 2dsphere index on the location field for geospatial queries
    app.data.driver.db['facilities'].create_index([('location', '2dsphere')])
Exemplo n.º 3
0
def create_request(wp, status):
    """Create an example request reporting a broken waterpoint"""
    r = {
        "service_code": "wps001",
        "attribute": {
            "waterpoint_id": wp,
            "status": status
        }
    }
    check(add_document("requests", r))
Exemplo n.º 4
0
def sms_register_user(mobile,
                      is_registered,
                      network,
                      expect_return=False,
                      expect_both=True):
    if is_registered == 0:
        # Do user registration
        subscriber = {
            'facility_code': 'sub001',
            'issue_code': 'REGISTER',
            'issue_date': datetime.now(),
            'issue_phone_number': mobile,
            'issue_status': '1',
            'issue_status_group': 'complete',
            'issue_keyword': 'REGISTRATION',
            'issue_description':
            'This user has been successfully registered with the system.',
            '_id': mobile
        }

        if check_response(add_document('subscribers', subscriber)):
            info = "Thank you. You have successfully registered on our system with number %s." % mobile

            if expect_return is False:
                sms_send_text(mobile, info, network)
                sms_send_menu(mobile, network)
                if expect_both is True:
                    return '{"status":"SUCCESS","info":"%s"}' % info
            else:
                sms_send_menu(mobile, network)
                return '{"status":"SUCCESS","info":"%s"}' % info
        else:

            info = "Sorry we have failed to register you on our system with number %s." % mobile
            if expect_return is False:
                sms_send_text(mobile, info, network)
                if expect_both is True:
                    return '{"status":"ERROR","info":"%s"}' % info
            else:
                return '{"status":"ERROR","info":"%s"}' % info

    else:
        info = "You have already registered on the system with number %s." % mobile
        if expect_return is False:
            sms_send_text(mobile, info, network)
            sms_send_menu(mobile, network)
            if expect_both is True:
                return '{"status":"IDLE","info":"%s"}' % info
        else:
            sms_send_menu(mobile, network)
            return '{"status":"IDLE","info":"%s"}' % info
Exemplo n.º 5
0
def upload_waterpoints(filename):
    """Upload waterpoints from a gzipped CSV file."""
    convert = {
        'date_recorded': lambda s: datetime.strptime(s, '%m/%d/%Y'),
        'population': int,
        'construction_year': lambda s: datetime.strptime(s, '%Y'),
        'breakdown_year': lambda s: datetime.strptime(s, '%Y'),
        'amount_tsh': float,
        'gps_height': float,
        'latitude': float,
        'longitude': float,
    }
    with gzip.open(filename) as f:
        for d in DictReader(f):
            d = dict((k, convert.get(k, str)(v)) for k, v in d.items() if v)
            d['facility_code'] = 'wp001'
            check(add_document('waterpoints', d))
Exemplo n.º 6
0
def upload_waterpoints(filename, skip=0, limit=None):
    """Upload waterpoints from a gzipped CSV file."""
    convert = {
        'date_recorded': lambda s: datetime.strptime(s, '%m/%d/%Y'),
        'population': int,
        'construction_year': lambda s: datetime.strptime(s, '%Y'),
        'breakdown_year': lambda s: datetime.strptime(s, '%Y'),
        'amount_tsh': float,
        'gps_height': float,
        'latitude': float,
        'longitude': float,
    }
    with gzip.open(filename) as f:
        reader = DictReader(f)
        for i in range(skip):
            reader.next()
        for i, d in enumerate(reader):
            d = dict((k, convert.get(k, str)(v)) for k, v in d.items() if v)
            d['facility_code'] = 'wpf001'
            check(add_document('waterpoints', d))
            if limit and i >= limit:
                break
Exemplo n.º 7
0
def upload_waterpoints(filename, skip=0, limit=None):
    """Upload waterpoints from a gzipped CSV file."""
    convert = {
        'date_recorded': lambda s: datetime.strptime(s, '%m/%d/%Y'),
        'population': int,
        'construction_year': lambda s: datetime.strptime(s, '%Y'),
        'breakdown_year': lambda s: datetime.strptime(s, '%Y'),
        'amount_tsh': float,
        'gps_height': float,
        'latitude': float,
        'longitude': float,
    }
    with gzip.open(filename) as f:
        reader = DictReader(f)
        for i in range(skip):
            reader.next()
        for i, d in enumerate(reader):
            d = dict((k, convert.get(k, str)(v)) for k, v in d.items() if v)
            d['facility_code'] = 'wpf001'
            check(add_document('waterpoints', d))
            if limit and i >= limit:
                break
Exemplo n.º 8
0
def upload_waterpoints(filename, skip=0, limit=None):
    """Upload waterpoints from a CSV file."""
    # Use sys.stdout.write so waterpoints can be printed nicely and succinctly
    import sys

    date_converter = lambda s: datetime.strptime(s, '%Y-%m-%d')
    bool_converter = lambda s: s == "true"

    status_map = {
        "non functional": "not functional",
        "functional needs repair": "needs repair"
    }

    status_converter = lambda s: status_map.get(s.lower(), s.lower())

    convert = {
        'gid': int,
        'object_id': int,
        'valid_from': date_converter,
        'valid_to': date_converter,
        'amount_tsh': float,
        'breakdown_year': int,
        'date_recorded': date_converter,
        'gps_height': float,
        'latitude': float,
        'longitude': float,
        'num_private': int,
        'region_code': int,
        'district_code': int,
        'population': int,
        'public_meeting': bool_converter,
        'construction_year': int,
        'status_group': status_converter
    }

    def print_flush(msg):
        sys.stdout.write(msg)
        sys.stdout.flush()

    facility_code = "wpf001"
    print_every = 1000
    print_flush("Adding waterpoints. Please be patient.")

    with open(filename) as f:
        reader = DictReader(f)
        for i in range(skip):
            reader.next()
        for i, d in enumerate(reader):
            actual_index = i + skip + 2
            do_print = actual_index % print_every == 0
            try:
                d = dict((k, convert.get(k, str)(v)) for k, v in d.items() if v)
                coords = [d.pop('longitude'), d.pop('latitude')]
                d['location'] = {'type': 'Point', 'coordinates': coords}
                d['facility_code'] = facility_code
                if not check(add_document('waterpoints', d), 201, False):
                    raise Exception()
                if do_print:
                    print_flush(".")

            except Exception as e:
                print "Error adding waterpoint", e
                pprint(d)
                exit()

            if limit and i >= limit:
                break
    # Create a 2dsphere index on the location field for geospatial queries
    app.data.driver.db['resources'].ensure_index([('location', '2dsphere')])
    print "Waterpoints uploaded!"
Exemplo n.º 9
0
def create_service():
    """Create service for waterpoints."""
    check(add_document('services', service_schema))
Exemplo n.º 10
0
def create_facility():
    """Create facility for waterpoints."""
    check(add_document('facilities', facility_schema))
Exemplo n.º 11
0
def create_request(wp, status):
    """Create an example request reporting a broken waterpoint"""
    r = {"service_code": "wps001",
         "attribute": {"waterpoint_id": wp,
                       "status": status}}
    check(add_document("requests", r))
Exemplo n.º 12
0
def create_facilities():
    """Create facilities for vehicles and freight movements."""
    check(add_document('facilities', vehicle_facility))
    check(add_document('facilities', freight_movement_facility))
Exemplo n.º 13
0
def create_service():
    """Register project service."""
    check(add_document('services', service_schema))
Exemplo n.º 14
0
def create_facility():
    """Register project facility."""
    check(add_document('facilities', facility_schema))
Exemplo n.º 15
0
def sms_flow():
    if 'text' in request.args and 'mobile' in request.args and 'network' in request.args:
        text = request.args.get('text')
        mobile = request.args.get('mobile')
        mobile = mobile.encode('utf8')
        network = request.args.get('network')
        keyword = None

        # Check if user is registered
        is_registered_cursor = sms_db.resources.find({
            '_id': mobile,
            'facility_code': 'sub001'
        })
        is_registered = is_registered_cursor.count()

        # Get keyword from received text string
        find_keyword = str.strip(text.encode('utf8'))

        if re.match(r'^REGISTER$', find_keyword, re.M | re.I) is not None:
            return sms_register_user(mobile, is_registered, network)

        elif re.match(r'^MENU$', find_keyword, re.M | re.I) is not None:
            if is_registered == 1:
                menu = sms_send_menu(mobile, network)
                return '{"status":"SUCCESS","info":"%s"}' % menu
            else:
                prompt = sms_send_register(mobile, network)
                return '{"status":"ERROR","info":"%s"}' % prompt

        elif re.match(r'^IS([1-9]+[0-9]*)$', find_keyword,
                      re.M | re.I) is not None:
            if is_registered == 1:
                matches = re.match(r'^IS([1-9]+[0-9]*)$', find_keyword,
                                   re.M | re.I)
                id_code = int(matches.group(1))
                issue_cursor = sms_db.resources.find({
                    'facility_code': 'trd001',
                    '_id': id_code
                })

                issue_status_group = "pending"

                if issue_cursor.count() > 0:
                    for issue in issue_cursor:
                        issue_status_group = issue['issue_status_group']

                info = "Sorry...we could not determine the status of your issue with code IS%s" % id_code
                issue_status_group = str.strip(
                    issue_status_group.encode('utf8'))
                if re.match(r'pending', issue_status_group, re.M
                            | re.I) is not None and issue_cursor.count() > 0:
                    info = "Your issue with code IS%s is PENDING. Please continue to follow up to know the status." % id_code
                elif re.match(r'complete', issue_status_group, re.M
                              | re.I) is not None and issue_cursor.count() > 0:
                    info = "Your issue with code IS%s has been RESOLVED" % id_code

                sms_send_text(mobile, info, network)
                return '{"status":"IDLE","info":"%s"}' % info
            else:
                prompt = sms_send_register(mobile, network)
                return '{"status":"ERROR","info":"%s"}' % prompt

        elif re.match(r'^([1-9]+[0-9]*) .*', find_keyword,
                      re.M | re.I | re.S) is not None:
            if is_registered == 1:
                matches = re.match(r'^([1-9]+[0-9]*) .*', find_keyword,
                                   re.M | re.I | re.S)
                issue_keyword = matches.group(1)
                index_cursor = sms_db.facilities.find({
                    'facility_code': 'trd001'
                }).sort('_id', -1).limit(1)
                if index_cursor.count(with_limit_and_skip=True) == 1:
                    for facility in index_cursor:
                        keywords = facility['keywords']

                        if len(keywords) >= int(issue_keyword):
                            issue_keyword = keywords[int(issue_keyword) - 1]
                        else:
                            info = "Reported issue choice out of bound. Please choose issue index within appropriate bounds."
                            sms_send_text(mobile, info, network)
                            sms_send_menu(mobile, network)
                            return '{"status":"ERROR","info":"%s"}' % info

                issue_code = 1

                latest_issue_code_cursor = sms_db.resources.find({
                    'facility_code':
                    'trd001'
                }).sort('_id', -1).limit(1)
                latest_issue_code_count = latest_issue_code_cursor.count(
                    with_limit_and_skip=True)

                if latest_issue_code_count > 0:
                    for issue in latest_issue_code_cursor:
                        if issue_code <= issue['_id']:
                            issue_code = issue['_id']

                    issue_code = issue_code + 1

                id_code = issue_code
                issue_code = "IS" + str(issue_code)
                report = {
                    'facility_code': 'trd001',
                    'issue_date': datetime.now(),
                    'issue_phone_number': mobile,
                    'issue_status_group': 'pending',
                    'issue_status': text,
                    'issue_code': issue_code,
                    'issue_keyword': issue_keyword,
                    '_id': id_code
                }

                if check_response(add_document('traders', report)):
                    short_code = "1040"
                    if re.match(r'^AIRTELMW$', network,
                                re.M | re.I) is not None:
                        short_code = "104"

                    info = "Thank you for sending us your report to follow up on your report send %s to %s" % (
                        issue_code, short_code)
                    sms_send_text(mobile, info, network)
                    return '{"status":"SUCCESS","info":"%s"}' % info

                info = "Sorry...the system could not process your report, please try this again another time."
                sms_send_text(mobile, info, network)
                return '{"status":"ERROR","info":"%s"}' % info
            else:
                prompt = sms_send_register(mobile, network)
                return '{"status":"ERROR","info":"%s"}' % prompt
        else:
            if is_registered == 1:
                info = "Sorry...we are unable to process your report please retry by following instructions appropriately."
                sms_send_text(mobile, info, network)
                sms_send_menu(mobile, network)
                return '{"status":"ERROR","info":"%s"}' % info
            else:
                prompt = sms_send_register(mobile, network)
                return '{"status":"ERROR","info":"%s"}' % prompt
    else:
        return '{"status":"ERROR","info":"Missing key parameters/arguments in HTTP request."}'