def modify_contact(self, account, contact_uuid, struct): ''' Modify contact ''' # riak search index search_index = 'sparta_index' # riak bucket type bucket_type = 'sparta' # riak bucket name bucket_name = 'leads' # solr query query = 'uuid_register:{0}'.format(contact_uuid.rstrip('/')) # filter query filter_query = 'account_register:{0}'.format(account) # search query url url = "https://iofun.io/search/query/{0}?wt=json&q={1}&fq={2}".format( search_index, query, filter_query ) # pretty please, ignore this list of fields from database. IGNORE_ME = ["_yz_id","_yz_rk","_yz_rt","_yz_rb","checked","keywords"] got_response = [] update_complete = False def handle_request(response): ''' Request Async Handler ''' if response.error: logging.error(response.error) got_response.append({'error':True, 'message': response.error}) else: got_response.append(json.loads(response.body)) try: http_client.fetch( url, callback=handle_request ) while len(got_response) == 0: # don't be careless with the time. yield gen.sleep(0.0010) response_doc = got_response[0].get('response')['docs'][0] riak_key = str(response_doc['_yz_rk']) bucket = self.kvalue.bucket_type(bucket_type).bucket('{0}'.format(bucket_name)) bucket.set_properties({'search_index': search_index}) contact = Map(bucket, riak_key) for key in struct: if key not in IGNORE_ME: contact.registers['{0}'.format(key)].assign(str(struct.get(key))) contact.update() update_complete = True except Exception, e: logging.exception(e) raise gen.Return(e)
def modify_remove(self, account, team_uuid, struct): ''' Modify remove ''' # riak search index search_index = 'mango_team_index' # riak bucket type bucket_type = 'mango_team' # riak bucket name bucket_name = 'teams' # solr query query = 'uuid_register:{0}'.format(team_uuid.rstrip('/')) # filter query filter_query = 'account_register:{0}'.format(account.decode('utf-8')) # search query url url = "https://{0}/search/query/{1}?wt=json&q={2}&fq={3}".format( self.solr, search_index, query, filter_query ) # pretty please, ignore this list of fields from database. IGNORE_ME = ("_yz_id","_yz_rk","_yz_rt","_yz_rb","checked","keywords") # got callback response? got_response = [] # yours truly message = {'update_complete':False} def handle_request(response): ''' Request Async Handler ''' if response.error: logging.error(response.error) got_response.append({'error':True, 'message': response.error}) else: got_response.append(json.loads(response.body)) try: http_client.fetch( url, callback=handle_request ) while len(got_response) == 0: # Please, don't be careless with the time. yield gen.sleep(0.0010) response = got_response[0].get('response')['docs'][0] riak_key = str(response['_yz_rk']) bucket = self.kvalue.bucket_type(bucket_type).bucket('{0}'.format(bucket_name)) bucket.set_properties({'search_index': search_index}) team = Map(bucket, riak_key) for key in struct: if key not in IGNORE_ME: if type(struct.get(key)) == list: team.reload() old_value = team.registers['{0}'.format(key)].value if old_value: old_list = json.loads(old_value.replace("'",'"')) new_list = [x for x in old_list if x not in struct.get(key)] team.registers['{0}'.format(key)].assign(str(new_list)) team.update() message['update_complete'] = True else: message['update_complete'] = False except Exception as error: logging.exception(error) return message.get('update_complete', False)
def modify_remove(self, account, task_uuid, struct): ''' Modify remove ''' # riak search index search_index = 'mango_task_index' # riak bucket type bucket_type = 'mango_task' # riak bucket name bucket_name = 'tasks' # solr query query = 'uuid_register:{0}'.format(task_uuid.rstrip('/')) # filter query filter_query = 'account_register:{0}'.format(account.decode('utf-8')) # search query url url = "https://{0}/search/query/{1}?wt=json&q={2}&fq={3}".format( self.solr, search_index, query, filter_query) # pretty please, ignore this list of fields from database. # if you want to include something in here, remember the _register. # example: labels_register. IGNORE_ME = ("_yz_id", "_yz_rk", "_yz_rt", "_yz_rb") # got callback response? got_response = [] # yours truly message = {'update_complete': False} def handle_request(response): ''' Request Async Handler ''' if response.error: logging.error(response.error) got_response.append({'error': True, 'message': response.error}) else: got_response.append(json.loads(response.body)) try: http_client.fetch(url, callback=handle_request) while len(got_response) == 0: # Please, don't be careless with the time. yield gen.sleep(0.0010) response = got_response[0].get('response')['docs'][0] riak_key = str(response['_yz_rk']) bucket = self.kvalue.bucket_type(bucket_type).bucket( '{0}'.format(bucket_name)) bucket.set_properties({'search_index': search_index}) task = Map(bucket, riak_key) for key in struct: if key not in IGNORE_ME: if type(struct.get(key)) == list: task.reload() old_value = task.registers['{0}'.format(key)].value if old_value: old_list = json.loads(old_value.replace("'", '"')) new_list = [ x for x in old_list if x not in struct.get(key) ] task.registers['{0}'.format(key)].assign( str(new_list)) task.update() message['update_complete'] = True else: message['update_complete'] = False except Exception as error: logging.exception(error) return message.get('update_complete', False)
def modify_file(self, account, file_uuid, struct): ''' Modify file ''' # riak search index search_index = 'sloth_file_index' # riak bucket type bucket_type = 'sloth_file' # riak bucket name bucket_name = 'files' # solr query query = 'uuid_register:{0}'.format(file_uuid.rstrip('/')) # filter query filter_query = 'account_register:{0}'.format(account.decode('utf-8')) # search query url url = "https://{0}/search/query/{1}?wt=json&q={2}&fq={3}".format( self.solr, search_index, query, filter_query ) logging.warning(url) # pretty please, ignore this list of fields from database. IGNORE_ME = ("_yz_id","_yz_rk","_yz_rt","_yz_rb","checked","keywords") # got callback response? got_response = [] # yours truly message = {'update_complete':False} def handle_request(response): ''' Request Async Handler ''' if response.error: logging.error(response.error) got_response.append({'error':True, 'message': response.error}) else: got_response.append(json.loads(response.body)) try: http_client.fetch( url, callback=handle_request ) while len(got_response) == 0: # don't be careless with the time. yield gen.sleep(0.0010) response = got_response[0].get('response')['docs'][0] riak_key = str(response['_yz_rk']) bucket = self.kvalue.bucket_type(bucket_type).bucket('{0}'.format(bucket_name)) bucket.set_properties({'search_index': search_index}) file = Map(bucket, riak_key) for key in struct: if key not in IGNORE_ME: if type(struct.get(key)) == list: file.reload() old_value = file.registers['{0}'.format(key)].value if old_value: old_list = json.loads(old_value.replace("'",'"')) for thing in struct.get(key): old_list.append(thing) file.registers['{0}'.format(key)].assign(str(old_list)) else: new_list = [] for thing in struct.get(key): new_list.append(thing) file.registers['{0}'.format(key)].assign(str(new_list)) else: file.registers['{0}'.format(key)].assign(str(struct.get(key))) file.update() update_complete = True message['update_complete'] = True except Exception as error: logging.exception(error) return message.get('update_complete', False)