예제 #1
0
 def load(self):
     """
     Load cases from persistant storage.
     """
     the_redis = DARedis()
     cases = the_redis.get_data(self.user_cases_key) or {}
     self.cases = cases
예제 #2
0
파일: oauth.py 프로젝트: xkra/docassemble
 def delete_credentials(self):
     """Deletes the stored credentials."""
     self._setup()
     r = DARedis()
     r.delete(self._get_redis_key())
     storage = self._get_redis_cred_storage()
     storage.locked_delete()
 def read(self) -> dict:
     """
     Read the list of counties from file storage.
     """
     the_redis = DARedis()
     result = the_redis.get_data(STORE)
     return result
예제 #4
0
파일: oauth.py 프로젝트: xkra/docassemble
 def _get_random_unique_id(self):
     r = DARedis()
     tries = 10
     while tries > 0:
         key = random_alphanumeric(32)
         if r.setnx('da:' + self.appname + ':status:uniqueid:' + key, 'None'):
             r.expire('da:' + self.appname + ':status:uniqueid:' + key, 300)
             return key
         tries -= 1
     raise Exception("DAOAuth: unable to set a random unique id")
 def read(self) -> dict:
     """
     Read the list of counties from local storage.
     """
     result = None
     try:
         the_redis = DARedis()
         result = the_redis.get_data(STORE)
     except Exception as e:
         logmessage(f"Unable to read cached list of courts: {str(e)}")
     return result
예제 #6
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)
    def save(self, courts: dict, courts_by_county: dict) -> bool:
        """
        Persist the list of courts to local storage.

        Args:
            courts (dict): Dict of courts indexed by court id
            courts_by_county (dict): Dict where index is County and value is list of courts
        Returns:
            (bool): True if successful, otherwise False
        """
        try:
            the_redis = DARedis()
            the_redis.set_data(STORE, cache_record(courts, courts_by_county))
            return True
        except Exception as e:
            logmessage(f"Unable to cache list of courts: {str(e)}")
        return False
예제 #8
0
    def save(self, case) -> bool:
        """
        Save the revised case list. If a case is provided, then add it to the
        case list before saving. If no case is provided, then save the case list
        without doing anything else. (This is probably called by a delete operation.)

        Args:
            case (Case): Case to add to list or None
        Returns:
            (bool): True is successful save, otherwise raises exception.
        """
        if case:
            key = case_key(case)
            case.key = key
            self.cases[key] = case
        the_redis = DARedis()
        the_redis.set_data(self.user_cases_key, self.cases)
        return True
예제 #9
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
예제 #10
0
 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
예제 #11
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
예제 #12
0
 def __init__(self, app):
     self.r = DARedis()
     self.key = 'da:' + app + ':user:'******'da:' + app + ':lock:user:' + user_info().email
예제 #13
0
 def del_cases(self):
     logmessage(f"del_cases(): " +
                "Deleting all cases for user = {self.user_id}")
     the_redis = DARedis()
     the_redis.set_data(self.user_cases_key, None)
     return
예제 #14
0
from docassemble.base.util import log, DARedis  #, Individual, #, DAObject

redis = DARedis()


def amend_signer(data, key, value):
    if 'action_key' in data:
        party_id = data['party_id']
        action_data = redis.get_data(data['action_key'])

        # set party key
        action_data['parties'][party_id][key] = value
        redis.set_data(data['action_key'], action_data)
        return action_data['parties'][party_id]
    else:
        return None


def get_signer(data):

    #if 'action_key' in url_args:
    #  data = url_args
    #
    ## Will this error?
    #elif action_argument('action_key'):
    #  data = {
    #    #'parent_interview_data_id': action_argument('parent_interview_data_id'),
    #    'action_key': action_argument('action_key'),
    #    'party_id': action_argument('party_id')
    #  }
예제 #15
0
파일: oauth.py 프로젝트: xkra/docassemble
 def __init__(self, key, lock, expires):
     self.r = DARedis()
     self.key = key
     self.lockkey = lock
     self.expires = expires
def potential_panelists() -> Iterable[Tuple[str, datetime]]:
    red = DARedis()
    return [(item, datetime.fromtimestamp(score)) for item, score in
            red.zrange(redis_panel_emails_key, 0, -1, withscores=True)]
 def save(self, courts, clerks):
     """
     Persist the directory info to file storage.
     """
     the_redis = DARedis()
     the_redis.set_data(STORE, cache_record(courts, clerks))
def add_panel_participant(email: str):
    """Adds this email to a list of potential participants for a qualitative research panel.
    Adds the email and when they responded, as we shouldn't link their feedback to their identity at all.
    """
    red = DARedis()
    red.zadd(redis_panel_emails_key, {email: datetime.now().timestamp()})
예제 #19
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())
def restore_ls_fields(redis_secret, redis_key):
  r = DARedis()
  encrypted_object_string = r.get_data(redis_key)
  return decrypt_object(encrypted_object_string, redis_secret)
def save_ls_fields(ls_variables, redis_secret, redis_key, expire=30):
  r = DARedis()
  encrypted_vars = encrypt_object(ls_variables, redis_secret)
  r.set_data(redis_key, encrypted_vars, expire=expire)
 def save(self, counties):
     """
     Persist the list of counties to file storage.
     """
     the_redis = DARedis()
     the_redis.set_data(STORE, counties)
예제 #23
0
 def delete_credentials(self):
     """Deletes the stored credentials."""
     r = DARedis()
     r.delete('da:' + self.appname + ':status:user:' + user_info().email)
     storage = RedisCredStorage(self.appname)
     storage.locked_delete()
예제 #24
0
def me():
    the_redis = DARedis()
    key = ME_KEY.format(__user_id())
    me = the_redis.get_data(key)
    return me
예제 #25
0
파일: oauth.py 프로젝트: xkra/docassemble
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)
예제 #26
0
def save_me(about_me):
    the_redis = DARedis()
    key = ME_KEY.format(__user_id())
    the_redis.set_data(key, about_me)
    return True