def create_user(user): email = user["email"] assert not conn.get("SELECT * FROM index_login WHERE login = %s", email) user["type"] = "user" user["name"] = user.get("name", "") user["salt"] = "".join(random.choice(string.ascii_uppercase + string.digits) for x in range(10)) if "password" in user: user["password"] = hashlib.sha1(user["password"] + user["salt"]).hexdigest() user["title"] = "" user["department"] = "" user["locatiion"] = "" user["mobile"] = "" user["tel"] = "" user["about"] = "" user["profile_img"] = "" user["datetime"] = datetime.datetime.now().isoformat() new_id = nomagic._new_key() assert nomagic._node(new_id).execute_rowcount("INSERT INTO entities (id, body) VALUES(%s, %s)", new_id, nomagic._pack(user)) #update indexes: email assert "@" in email assert conn.execute_rowcount("INSERT INTO index_login (login, entity_id) VALUES(%s, %s)", email, new_id) return (new_id, user)
def create_user(user): user["type"] = "user" user["name"] = user.get("name", "") user["salt"] = "".join( random.choice(string.ascii_uppercase + string.digits) for x in range(10)) user["password"] = hashlib.sha1(user["password"] + user["salt"]).hexdigest() user["datetime"] = datetime.datetime.now().isoformat() user["createtime"] = int(time.time()) new_id = nomagic._new_key() while True: new_user = nomagic._get_entity_by_id(new_id) if not new_user: break else: new_id = nomagic._new_key() rowcount = nomagic._node(new_id).execute_rowcount( "INSERT INTO entities (id, body) VALUES(%s, %s)", new_id, nomagic._pack(user)) assert rowcount #update indexes: email # assert "@" in email # rowcount = conn.execute_rowcount("INSERT INTO index_login (login, entity_id) VALUES(%s, %s)", email, new_id) # assert rowcount return (new_id, user)
def update_user(user_id, data): #valid name user = nomagic._get_entity_by_id(user_id) user_json1 = nomagic._pack(user) result = {} if "password0" in data and "password1" in data and data[ "password0"] != data["password1"] and data["password1"] != "": #normal update password with old password0 and new password1 if user["password"] == hashlib.sha1(data["password0"] + user.get("salt", "")).hexdigest(): user["password"] = hashlib.sha1(data["password1"] + user.get("salt", "")).hexdigest() result["password_updated"] = True del data["password0"] del data["password1"] elif "password" in data and data["password"] != "": #force update password user["password"] = hashlib.sha1(data["password"] + user.get("salt", "")).hexdigest() result["password_updated"] = True del data["password"] if user: user.update(data) user_json2 = nomagic._pack(user) if user_json1 != user_json2: assert nomagic._node(user_id).execute_rowcount( "UPDATE entities SET body = %s WHERE id = %s", nomagic._pack(user), nomagic._key(user_id)) BIG_CACHE.unset(user_id) return result
def new_comment(user_id, entity_id, data): entity = nomagic._get_entity_by_id(entity_id) data["type"] = "comment" data["likes"] = [] data["user_id"] = user_id data["activity_id"] = entity_id data["datetime"] = datetime.datetime.now().isoformat() data["comment_ids"] = [] if entity["type"] == "comment": data["activity_id"] = entity.get("activity_id") else: data["activity_id"] = entity_id #content valid assert data.get("content") new_comment_id = nomagic._new_key() assert nomagic._node(new_comment_id).execute_rowcount( "INSERT INTO entities (id, body) VALUES(%s, %s)", new_comment_id, nomagic._pack(data)) comment_ids = entity.get("comment_ids", []) comment_ids.append(new_comment_id) entity["comment_ids"] = comment_ids nomagic._update_entity_by_id(entity_id, entity) return comment_ids, dict(data, id=new_comment_id, like_count=0, like=False, user=nomagic._get_entity_by_id(user_id))
def new_status(user_id, data): now = datetime.datetime.now() data["type"] = "status" data["user_id"] = user_id data["datetime"] = now.isoformat() data["likes"] = [] data["comment_ids"] = [] assert data.get("title") new_id = nomagic._new_key() assert nomagic._node(new_id).execute_rowcount( "INSERT INTO entities (id, body) VALUES(%s, %s)", new_id, nomagic._pack(data)) user = nomagic._get_entity_by_id(user_id) activity = user.get("activity", []) activity.append(new_id) user["activity"] = activity nomagic._update_entity_by_id(user_id, user) data["user"] = user data["like_count"] = 0 data["like"] = False data["comment_count"] = 0 assert conn.execute_rowcount( "INSERT INTO index_posts (user_id, entity_id) VALUES(%s, %s)", user_id, new_id) return new_id, data
def create_user(user): email = user["email"] login = conn.get("SELECT * FROM index_login WHERE login = %s", email) assert not login user["type"] = "user" user["name"] = user.get("name", "") user["salt"] = "".join(random.choice(string.ascii_uppercase + string.digits) for x in range(10)) user["password"] = hashlib.sha1(user["password"] + user["salt"]).hexdigest() user["title"] = "" user["department"] = "" user["locatiion"] = "" user["mobile"] = "" user["tel"] = "" user["about"] = "" user["profile_img"] = "" user["datetime"] = datetime.datetime.now().isoformat() new_id = nomagic._new_key() rowcount = nomagic._node(new_id).execute_rowcount("INSERT INTO entities (id, body) VALUES(%s, %s)", new_id, nomagic._pack(user)) assert rowcount #update indexes: email assert "@" in email rowcount = conn.execute_rowcount("INSERT INTO index_login (login, entity_id) VALUES(%s, %s)", email, new_id) assert rowcount return (new_id, user)
def update_user(user_id, data): #valid name user = nomagic._get_entity_by_id(user_id) user_json1 = nomagic._pack(user) result = {} if "password0" in data and "password1" in data and data["password0"] != data["password1"] and data["password1"] != "": #normal update password with old password0 and new password1 if user["password"] == hashlib.sha1(data["password0"] + user.get("salt", "")).hexdigest(): user["password"] = hashlib.sha1(data["password1"] + user.get("salt", "")).hexdigest() result["password_updated"] = True del data["password0"] del data["password1"] elif "password" in data and data["password"] != "": #force update password user["password"] = hashlib.sha1(data["password"] + user.get("salt", "")).hexdigest() result["password_updated"] = True del data["password"] if user: user.update(data) user_json2 = nomagic._pack(user) if user_json1 != user_json2: assert nomagic._node(user_id).execute_rowcount("UPDATE entities SET body = %s WHERE id = %s", nomagic._pack(user), nomagic._key(user_id)) return result
def new_status(user_id, data): now = datetime.datetime.now() data["type"] = "status" data["user_id"] = user_id data["datetime"] = now.isoformat() data["likes"] = [] data["comment_ids"] = [] assert data.get("title") new_id = nomagic._new_key() assert nomagic._node(new_id).execute_rowcount("INSERT INTO entities (id, body) VALUES(%s, %s)", new_id, nomagic._pack(data)) user = nomagic._get_entity_by_id(user_id) activity = user.get("activity", []) activity.append(new_id) user["activity"] = activity nomagic._update_entity_by_id(user_id, user) data["user"] = user data["like_count"] = 0 data["like"] = False data["comment_count"] = 0 assert conn.execute_rowcount("INSERT INTO index_posts (user_id, entity_id) VALUES(%s, %s)", user_id, new_id) return new_id, data
def new_comment(user_id, entity_id, data): entity = nomagic._get_entity_by_id(entity_id) data["type"] = "comment" data["likes"] = [] data["user_id"] = user_id data["activity_id"] = entity_id data["datetime"] = datetime.datetime.now().isoformat() data["comment_ids"] = [] if entity["type"] == "comment": data["activity_id"] = entity.get("activity_id") else: data["activity_id"] = entity_id #content valid assert data.get("content") new_comment_id = nomagic._new_key() assert nomagic._node(new_comment_id).execute_rowcount("INSERT INTO entities (id, body) VALUES(%s, %s)", new_comment_id, nomagic._pack(data)) comment_ids = entity.get("comment_ids", []) comment_ids.append(new_comment_id) entity["comment_ids"] = comment_ids nomagic._update_entity_by_id(entity_id, entity) return comment_ids, dict(data, id=new_comment_id, like_count=0, like=False, user=nomagic._get_entity_by_id(user_id))
def create_event(event): order["type"] = "event" order["owner"] = order.get("owner", "") order["datetime"] = datetime.datetime.now().isoformat() order["createtime"] = int(time.time()) new_id = nomagic._new_key() while True: new_order = nomagic._get_entity_by_id(new_id) if not new_order: break else: new_id = nomagic._new_key() rowcount = nomagic._node(new_id).execute_rowcount( "INSERT INTO entities (id, body) VALUES(%s, %s)", new_id, nomagic._pack(order)) assert rowcount return (new_id, order)
def update_fileinfo(filehash, user_id, id3info = None, title = None, rev = None): if rev: if not conn.get("SELECT * FROM index_dropbox WHERE rev=%s", rev): assert conn.execute_rowcount("INSERT INTO index_dropbox (rev, entity_id) VALUES (%s, %s)", rev, filehash) fileinfo = nomagic._get_entity_by_id(filehash) if fileinfo: changed = False owners = fileinfo.get("owners", []) if user_id not in owners: owners.append(user_id) fileinfo["owners"] = owners changed = True if id3info and id3info != fileinfo.get("id3info"): fileinfo["id3info"] = id3info changed = True if title and title != fileinfo.get("title"): fileinfo["title"] = title changed = True if changed: nomagic._update_entity_by_id(filehash, fileinfo) else: fileinfo = {"owners":[user_id]} if id3info: fileinfo["id3info"] = id3info if title: fileinfo["title"] = title assert nomagic._node(filehash).execute_rowcount("INSERT INTO entities (id, body) VALUES(%s, %s)",\ filehash, nomagic._pack(fileinfo)) user = nomagic._get_entity_by_id(user_id) if user: files = user.get("files", []) if filehash not in files: files.append(filehash) user["files"] = files nomagic._update_entity_by_id(user_id, user) return fileinfo, user
def create_comment(comment): comment["type"] = "comment" comment["owner"] = comment.get("owner", "") comment["owner_type"] = comment.get("owner_type", "entity") comment["datetime"] = datetime.datetime.now().isoformat() comment["createtime"] = int(time.time()) new_id = nomagic._new_key() while True: new_comment = nomagic._get_entity_by_id(new_id) if not new_comment: break else: new_id = nomagic._new_key() rowcount = nomagic._node(new_id).execute_rowcount( "INSERT INTO entities (id, body) VALUES(%s, %s)", new_id, nomagic._pack(comment)) assert rowcount return (new_id, comment)
def create_chat(chat): chat["type"] = "chat" chat["owner"] = chat.get("owner", "") chat["owner_type"] = chat.get("owner_type", "entity") chat["editors"] = chat.get("editors", []) chat["helpers"] = chat.get("helpers", {}) chat["comment_members"] = chat.get("comment_members", []) chat["notifyers"] = chat.get("notifyers", []) chat["blackers"] = chat.get("blackers", []) chat["datetime"] = datetime.datetime.now().isoformat() chat["createtime"] = int(time.time()) new_id = nomagic._new_key() while True: new_chat = nomagic._get_entity_by_id(new_id) if not new_chat: break else: new_id = nomagic._new_key() rowcount = nomagic._node(new_id).execute_rowcount( "INSERT INTO entities (id, body) VALUES(%s, %s)", new_id, nomagic._pack(chat)) assert rowcount return (new_id, chat)
def update_chat(chat_id, data): rowcount = nomagic._node(chat_id).execute_rowcount( "UPDATE entities SET body = %s WHERE id = %s", nomagic._pack(data), nomagic._key(chat_id)) assert rowcount