def add_attribute_portrait(uid, attribute_name, attribute_value, submit_user): status = False # identify the user exist # identify the attribute exist # identify the attribute exist in user_portrait # add attribute in user_portrait # submit user should has power to change???without try: user_result = es.get(index=user_index_name, doc_type=user_index_type, id=uid)['_source'] except: return 'no user' try: attribute_result = es.get(index=attribute_index_name, doc_type=attribute_index_type, id=attribute_name)['_source'] except: return 'no attribute' attribute_value_list = attribute_result['attribute_value'].split('&') if attribute_value not in attribute_value_list: return 'no attribute value' attribute_name = "tag-" + attribute_name if attribute_name in user_result: return 'attribute exist' add_attribute_dict = {attribute_name: attribute_value} es.update(index=user_index_name, doc_type=user_index_type, id=uid, body={'doc':add_attribute_dict}) status = True return status
def delete_attribute(attribute_name): status = False try: result = es.get(index=attribute_index_name, doc_type=attribute_index_type, id=attribute_name)['_source'] except Exception, e: raise e return status
def get_attribute_value(attribute_name): attribute_value_list = [] try: attribute_result = es.get(index=attribute_index_name, doc_type=attribute_index_type, id=attribute_name)['_source'] except: return 'no attribute' print 'attribute_result:', attribute_result attribute_value_string = attribute_result['attribute_value'] attribute_value_list = attribute_value_string.split('&') return attribute_value_list
def change_attribute_portrait(uid, attribute_name, attribute_value, submit_user): status = False #identify the user exist #identify the attribute exist #identify the attribute value exist #identify the submit_user have been admitted----without try: user_exist = es.get(index=user_index_name, doc_type=user_index_type, id=uid)['_source'] except: return 'no user' try: attribute_result = es.get(index=attribute_index_name, doc_type=attribute_index_type, id=attribute_name)['_source'] except: return 'no attribute' value_list = attribute_result['attribute_value'].split('&') if attribute_value not in value_list: return 'no attribute value' attribute_name = "tag-" + attribute_name change_attribute_dict = {attribute_name: attribute_value} es.update(index=user_index_name, doc_type=user_index_type, id=uid, body={'doc': change_attribute_dict}) status = True return status
def add_tag2group(uid_list, attribute_name, attribute_value): status = False #identify the attribute exist #for uid in uid_list #identify the attribute not in this user #add tag to this user try: attribute_exist = es.get(index=attribute_index_name, doc_type=attribute_index_type, id=attribute_name)['_source'] except: return 'no attribute' attribute_exist_value_list = attribute_exist['attribute_value'].split('&') if attribute_value not in attribute_exist_value_list: return 'no attribute value' for uid in uid_list: try: user_exist = es.get(index=user_index_name, doc_type=user_index_type, id=uid)['_source'] except: user_exist = {} attribute_name = "tag-" + attribute_name if user_exist and attribute_name not in user_exist: add_attribute_dict = {attribute_name: attribute_value} es.update(index=user_index_name, doc_type=user_index_type, id=uid, body={'doc':add_attribute_dict}) status = True return status
def get_user_attribute_name(uid): result = [] user_result = es.get(index=user_index_name, doc_type=user_index_type, \ id=uid) print 'user_result:', user_result try: source = user_result['_source'] except: source = {} for key in source: if "tag-" in key: temp_list = key.split("-") key = "-".join(temp_list[1:]) result.append(key) return result
def change_attribute(attribute_name, value, user, state): status = False # identify the attribute_name is in ES - custom attribute try: result = es.get(index=attribute_index_name, doc_type=attribute_index_type, id=attribute_name)['_source'] except: result = None return status value_list = '&'.join(value.split(',')) result['attribute_name'] = attribute_name result['attribute_value'] = value_list result['user'] = user now_ts = time.time() now_date = ts2datetime(now_ts) result['date'] = now_date es.index(index=attribute_index_name, doc_type=attribute_index_type, id=attribute_name ,body=result) status = True return status
def get_group_tag(group_name): result = {} order_result = [] #get group task uid list #get user tag #statistic tag try: group_task_result = es.get(index=group_index_name, doc_type=group_index_type, id=group_name) except: return 'no group task' try: uid_list = group_task_result['_source']['uid_list'] except: return 'no user' try: user_result = es.mget(index=user_index_name, doc_type=user_index_type, body={'ids': uid_list})['docs'] except Exception, e: raise e
def delete_attribute_portrait(uid, attribute_name, submit_user): status = False #identify the user exist #identify the attribute value exist in es_user_portrait #identify the submit_user have been admitted---without try: user_exist = es.get(index=user_index_name, doc_type=user_index_type, id=uid)['_source'] except: return 'no user' attribute_name = "tag-"+attribute_name if attribute_name not in user_exist: return 'user have no attribtue' try: del_attribute_value = user_exist.pop(attribute_name) es.index(index=user_index_name, doc_type=user_index_type, id=uid, body=user_exist) status = True except Exception, e: raise e