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_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 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 unlike(user_id, entity_id): entity = nomagic._get_entity_by_id(entity_id) likes = entity.get("likes", []) if user_id in likes: likes.remove(user_id) entity["likes"] = likes nomagic._update_entity_by_id(entity_id, entity) return likes
def like(user_id, entity_id): entity = nomagic._get_entity_by_id(entity_id) likes = entity.get("likes", []) if user_id not in likes: likes.append(user_id) entity["likes"] = likes nomagic._update_entity_by_id(entity_id, entity) return likes
def disconnect_entities(entity_id1, entity_connection_name1, entity_id2, entity_connection_name2): entity1, entity2 = nomagic._get_entities_by_ids([entity_id1, entity_id2]) entity_data1, entity_data2 = entity1[1], entity2[1] entity_connection = set(entity_data1.get(entity_connection_name1, [])) if entity_id2 in entity_connection: entity_connection.remove(entity_id2) entity_data1[entity_connection_name1] = list(entity_connection) nomagic._update_entity_by_id(entity_id1, entity_data1) entity_connection = set(entity_data2.get(entity_connection_name2, [])) if entity_id1 in entity_connection: entity_connection.remove(entity_id1) entity_data2[entity_connection_name2] = list(entity_connection) nomagic._update_entity_by_id(entity_id2, entity_data2)
def disconnect_index_and_entity(table_index, index_name, index_connection_name, entity_id, entity_connection_name): index = conn.get("SELECT * FROM %s WHERE name = %s" % (table_index, "%s"), index_name) index_data = nomagic._unpack(index["data"] or "{}") index_connection = set(index_data.get(index_connection_name, [])) if entity_id in index_connection: index_connection.remove(entity_id) index_data[index_connection_name] = list(index_connection) index_data_updated = nomagic._pack(index_data) conn.execute("UPDATE %s SET data = %s WHERE name = %s" % (table_index, "%s", "%s"), index_data_updated, index_name) entity = nomagic._get_entity_by_id(entity_id) entity_connection = set(entity.get(entity_connection_name, [])) if index_name in entity_connection: entity_connection.remove(index_name) entity[entity_connection_name] = list(entity_connection) nomagic._update_entity_by_id(entity_id, entity)
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 update_entity(cache, entity_id, entity): cache.unset(entity_id) nomagic._update_entity_by_id(entity_id, entity)