Example #1
0
def service_areas():
    redis = DARedis()
    result = redis.get('lsc_service_areas')
    if result is None:
        #sys.stderr.write('service_areas: calling arcgis.\n')
        r = requests.get(
            'https://services3.arcgis.com/n7h3cEoHTyNCwjCf/ArcGIS/rest/services/BasicFieldServiceAreas_GrantCycle/FeatureServer/0/query?where=OBJECTID%3E%3D0&objectIds=&time=&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&resultType=none&distance=0.0&units=esriSRUnit_Meter&returnGeodetic=false&outFields=*&returnGeometry=false&returnCentroid=false&multipatchOption=xyFootprint&maxAllowableOffset=&geometryPrecision=&outSR=&datumTransformation=&applyVCSProjection=false&returnIdsOnly=false&returnUniqueIdsOnly=false&returnCountOnly=false&returnExtentOnly=false&returnQueryGeometry=false&returnDistinctValues=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=&having=&resultOffset=&resultRecordCount=&returnZ=false&returnM=false&returnExceededLimitFeatures=true&quantizationParameters=&sqlFormat=none&f=pjson&token='
        )
        if r.status_code != 200:
            redis.set('lsc_service_areas', '{}')
            sys.stderr.write(
                'load_service_areas: got error code {} from ArcGIS.  Response: {}\n'
                .format(r.status_code, r.text))
        else:
            try:
                the_dict = r.json()
                assert 'features' in the_dict
                assert len(the_dict['features']) > 0
                redis.set('lsc_service_areas', r.text)
            except Exception as the_err:
                redis.set('lsc_service_areas', '{}')
                sys.stderr.write(
                    'load_service_areas: got invalid response from server: {}\n'
                    .format(text_type(the_err)))
        redis.expire('lsc_service_areas', 60 * 60 * 24 * 7)
        result = redis.get('lsc_service_areas')
    return json.loads(result.decode())
Example #2
0
 def get_credentials(self):
     r = DARedis()
     r_key = 'da:' + self.appname + ':status:user:'******'code' in self.url_args and 'state' in self.url_args:
             r.delete(r_key)
             if self.url_args['state'] != stored_state.decode():
                 raise Exception("State did not match")
             flow = self._get_flow()
             credentials = flow.step2_exchange(self.url_args['code'])
             storage = RedisCredStorage(self.appname)
             storage.put(credentials)
             del self.url_args['code']
             del self.url_args['state']
         else:
             message("Please wait.",
                     "You are in the process of authenticating.")
     storage = RedisCredStorage(self.appname)
     credentials = storage.get()
     if not credentials or credentials.invalid:
         state_string = random_string(16)
         pipe = r.pipeline()
         pipe.set(r_key, state_string)
         pipe.expire(r_key, 60)
         pipe.execute()
         flow = self._get_flow()
         uri = flow.step1_get_authorize_url(state=state_string)
         if 'state' in self.url_args:
             del self.url_args['state']
         if 'code' in self.url_args:
             del self.url_args['code']
         response(url=uri)
     return credentials
Example #3
0
class RedisCredStorage(oauth2client.client.Storage):
    def __init__(self, app):
        self.r = DARedis()
        self.key = 'da:' + app + ':user:'******'da:' + app + ':lock:user:' + user_info().email

    def acquire_lock(self):
        pipe = self.r.pipeline()
        pipe.set(self.lockkey, 1)
        pipe.expire(self.lockkey, 5)
        pipe.execute()

    def release_lock(self):
        self.r.delete(self.lockkey)

    def locked_get(self):
        json_creds = self.r.get(self.key)
        creds = None
        if json_creds is not None:
            json_creds = json_creds.decode()
            try:
                creds = oauth2client.client.Credentials.new_from_json(
                    json_creds)
            except:
                log("RedisCredStorage: could not read credentials from " +
                    str(json_creds))
        return creds

    def locked_put(self, credentials):
        self.r.set(self.key, credentials.to_json())

    def locked_delete(self):
        self.r.delete(self.key)
Example #4
0
class RedisCredStorage(oauth2client.client.Storage):
    def __init__(self, key, lock, expires):
        self.r = DARedis()
        self.key = key
        self.lockkey = lock
        self.expires = expires
    def acquire_lock(self):
        pipe = self.r.pipeline()
        pipe.set(self.lockkey, 1)
        pipe.expire(self.lockkey, 5)
        pipe.execute()
    def release_lock(self):
        self.r.delete(self.lockkey)
    def locked_get(self):
        json_creds = self.r.get(self.key)
        creds = None
        if json_creds is not None:
            self.r.expire(self.key, self.expires)
            json_creds = json_creds.decode()
            try:
                creds = oauth2client.client.Credentials.new_from_json(json_creds)
            except:
                log("RedisCredStorage: could not read credentials from " + str(json_creds))
        return creds
    def locked_put(self, credentials):
        if self.expires:
            pipe = self.r.pipeline()
            pipe.set(self.key, credentials.to_json())
            pipe.expire(self.key, self.expires)
            pipe.execute()
        else:
            self.r.set(self.key, credentials.to_json())
    def locked_delete(self):
        self.r.delete(self.key)
 def fetch_philadox_info(self):
     r = DARedis()
     while r.get('using_philadox') is not None:
         time.sleep(5)
     pipe = r.pipeline()
     pipe.set('using_philadox', 1)
     pipe.expire('using_philadox', 120)
     pipe.execute()
     tdir = tempfile.mkdtemp()
     info = urllib.quote(json.dumps([self.address['number'], self.address['direction'], self.address['street'], tdir, get_config('philadox username'), get_config('philadox password')]))
     step = ['casperjs', DAStaticFile(filename='eagleweb.js').path(), info]
     result = subprocess.call(step)
     r.delete('using_philadox')
     if result != 0:
         raise Exception("Failed to fetch Philadox information")
     outfiles = []
     for pdf_file in sorted([f for f in os.listdir(tdir) if f.endswith('.pdf')]):
         new_file = DAFile()
         new_file.set_random_instance_name()
         new_file.initialize(filename=pdf_file)
         new_file.copy_into(os.path.join(tdir, pdf_file))
         new_file.retrieve()
         new_file.commit()
         outfiles.append(new_file)
     self.philadox_files = outfiles
Example #6
0
 def get_credentials(self):
     self._setup()
     r = DARedis()
     r_key = self._get_redis_key()
     stored_state = r.get(r_key)
     if stored_state is not None and stored_state.decode() == 'None':
         stored_state = None
     if stored_state is not None:
         if 'code' in self.url_args and 'state' in self.url_args:
             r.delete(r_key)
             if self.url_args['state'] != stored_state.decode():
                 raise Exception("State did not match.  " +
                                 repr(self.url_args['state']) + " vs " +
                                 repr(stored_state.decode()) +
                                 " where r_key is " + repr(r_key))
             flow = self._get_flow()
             credentials = flow.step2_exchange(self.url_args['code'])
             storage = self._get_redis_cred_storage()
             storage.put(credentials)
             del self.url_args['code']
             del self.url_args['state']
         else:
             message("Please wait.",
                     "You are in the process of authenticating.",
                     dead_end=True)
     storage = self._get_redis_cred_storage()
     credentials = storage.get()
     if not credentials or credentials.invalid:
         state_string = safeid(user_info().filename + '^' +
                               random_string(8))
         pipe = r.pipeline()
         pipe.set(r_key, state_string)
         pipe.expire(r_key, 300)
         pipe.execute()
         flow = self._get_flow()
         uri = flow.step1_get_authorize_url(state=state_string)
         if 'state' in self.url_args:
             del self.url_args['state']
         if 'code' in self.url_args:
             del self.url_args['code']
         response(url=uri)
     return credentials