Exemplo n.º 1
0
    def update_shelters(self, admin_key):
        self.result['meta']['method_name'] = '/tools/update_shelters'

        ## check admin key
        #if self.config.ADMIN_KEY != admin_key:
        #    self.result['meta']['status'] = 'Authentication failed'
        #    return self.result

        resource = Resource(self.db, self.config)
        openhmis = OpenHMIS(self.db, self.config)

        shelters = openhmis.get_shelters()
        counter = 0
        bad = 0
        shelters_total = len(shelters)
        for s in shelters:
            if counter >= shelters_total: break
            #print counter
            counter = counter + 1

            if resource.check_if_shelter_exists(s['ProgramName']) is False:

                shelter_info = openhmis.get_program_info_by_id(s['ProgramKey'])
                pprint(shelter_info)
                agency_name = shelter_info[0]['AgencyName']
                contact_name = shelter_info[0]['ContactName']
                contact_phone = shelter_info[0]['ContactPhone']
                program_address_full = shelter_info[0]['ProgramAddressFull']
                program_address_street = shelter_info[0]['ProgramAddress']
                program_address_city = shelter_info[0]['ProgramCity']
                program_address_zip = shelter_info[0]['ProgramZip']
                program_key = shelter_info[0]['ProgramKey']
                program_name = shelter_info[0]['ProgramName']
                program_type = shelter_info[0]['ProgramType']
                site_geo_code = shelter_info[0]['SiteGeocode']
                target_pop_a_name = shelter_info[0]['TargetPopAName']
                units_available = shelter_info[0]['UnitsAvailable']
                units_occupied = shelter_info[0]['UnitsOccupied']
                units_total = shelter_info[0]['UnitsTotal']
                update_time_stamp = shelter_info[0]['UpdateTimeStamp']

                if program_address_full == 'NULL' or program_address_full == 'null' or program_address_full is None:
                    if program_address_street != 'NULL' or program_address_street != 'null' or program_address_street is not None:
                        if program_address_city != 'NULL' or program_address_city != 'null' or program_address_city is not None:
                            if program_address_zip != 'NULL' or program_address_zip != 'null' or program_address_zip is not None:
                                program_address_full = '%s %s %s' % (
                                    program_address_street,
                                    program_address_city, program_address_zip)

                if program_address_full == 'NULL' or program_address_full == 'null' or program_address_full is None or program_address_full == 'None None None':
                    print 'skipping - bad address: %s' % (program_name)
                    self.db.rejectedPrograms.insert({
                        "program_key":
                        s['ProgramKey'],
                        "programName":
                        program_name,
                        "Address":
                        program_address_full
                    })
                    continue

                print 'normalizing data'
                ## normalize data

                try:
                    numbers = re.findall(r'\d+', contact_phone)
                    phone = '(%s) %s-%s' % (numbers[0], numbers[1], numbers[2])
                except:
                    phone = contact_phone

## Mod starts  program_address_city program_address_zip program_address_zip
                try:
                    time.sleep(1)  ## too avoid google maps api rate limit
                    geo_data = self.lookup_geocode_data(program_address_full)
                    if geo_data is False:
                        if program_address_zip == 'NULL' or program_address_zip == 'null' or program_address_zip is None:
                            print 'no zip'
                            self.db.rejectedPrograms.insert({
                                "program_key":
                                s['ProgramKey'],
                                "programName":
                                program_name,
                                "Address":
                                program_address_full
                            })
                            bad = bad + 1
                            continue
                        print 'trying /shelter name/ zip for: %s' % (
                            program_name)
                        address_full = '%s %s %s' % (program_name,
                                                     program_address_zip, "US")
                        geo_data = self.lookup_geocode_partial_data(
                            address_full)
                        if geo_data is False:
                            print 'save program info to rejected table'
                            bad = bad + 1
                            self.db.rejectedPrograms.insert({
                                "program_key":
                                s['ProgramKey'],
                                "programName":
                                program_name,
                                "Address":
                                program_address_full
                            })
                            continue
                        self.db.updatedAddress.insert({
                            "program_key":
                            s['ProgramKey'],
                            "programName":
                            program_name,
                            "Address":
                            program_address_full
                        })

                    print "Recording info"
                    lat = geo_data['lat']
                    lng = geo_data['lng']
                    if geo_data['full_address'] is not None:
                        full_address = geo_data['full_address']
                    else:
                        full_address = program_address_full

                    if geo_data['street_name'] is not None:
                        street_1 = geo_data['street_name']
                    else:
                        street_1 = program_address_street

                    street_2 = ''
                    if geo_data['city'] is not None:
                        city = geo_data['city']
                    else:
                        city = program_address_city

                    #if geo_data[''] is not None:
                    state = geo_data['state']
                    zipcode = program_address_zip

                except Exception as e:
                    print 'Error:', e
                    continue

#Mod ends

                print 'inserting to db: %s' % (program_name)

                resource_type = 'shelter'
                va_status = 0
                name_1 = program_name
                name_2 = agency_name
                url = ''
                hours = 'Call for hours'
                notes = ''
                hmis_program = ''
                beds = int(units_available)
                if beds is None: beds = 0
                beds_occupied = int(units_occupied)
                beds_total = int(units_total)
                contact_name = contact_name
                admin_code = self.generate_admin_code()

                ## update db
                resource.new_shelter(admin_key, admin_code, resource_type,
                                     va_status, name_1, name_2, full_address,
                                     street_1, street_2, city, state, zipcode,
                                     lat, lng, phone, url, hours, notes, beds,
                                     beds_occupied, beds_total, hmis_program,
                                     contact_name, program_key, program_type,
                                     target_pop_a_name, update_time_stamp)
            else:
                'print updating program_key'
                ## update program_key
                db_result = self.db.resource.update(
                    {'name_1': s['ProgramName']},
                    {'$set': {
                        'program_key': s['ProgramKey']
                    }},
                    upsert=False)

                ## update bed availability data
                db_result = self.db.resource.find_one(
                    {'program_key': s['ProgramKey']})
                current_time = int(time.time())
                last_update_time = db_result['last_updated']
                time_difference = current_time - last_update_time

                ## if within 24 hours -- use ours, do nothing
                ## if within 6 hours -- use ours, update openhmis  (http://openciss.appspot.com/bed_units/occupied/set?id=567&val=30)
                ## if over 24 hours (last resort) -- use openhmis data

                if time_difference > 86400:
                    ## update bed from openhmis
                    shelter_info = openhmis.get_program_info_by_id(
                        s['ProgramKey'])
                    units_available = int(shelter_info[0]['UnitsAvailable'])
                    units_occupied = int(shelter_info[0]['UnitsOccupied'])
                    units_total = int(shelter_info[0]['UnitsTotal'])
                    update_time_stamp = shelter_info[0]['UpdateTimeStamp']

                    db_result = self.db.resource.update(
                        {'program_key': s['ProgramKey']},
                        {'$set': {
                            'beds': units_available
                        }},
                        upsert=False)

                    db_result = self.db.resource.update(
                        {'program_key': s['ProgramKey']},
                        {'$set': {
                            'beds_occupied': units_occupied
                        }},
                        upsert=False)

                    db_result = self.db.resource.update(
                        {'program_key': s['ProgramKey']},
                        {'$set': {
                            'beds_total': units_total
                        }},
                        upsert=False)

                    ## update last update time
                    db_result = self.db.resource.update(
                        {'program_key': s['ProgramKey']},
                        {'$set': {
                            'last_updated': int(time.time())
                        }},
                        upsert=False)

                if time_difference < 21600:
                    ## send update to openhmis
                    openhmis.update_bed_units_occupied(
                        s['ProgramKey'], db_result['beds_occupied'])

        print bad
        self.result['meta']['status'] = 'OK'
        return self.result