Database Reference
In-Depth Information
The add_content route and add_content method are shown in Listing 9-29. When a new status update is created,
in addition to its graph id, the add_content method also generates a contentId, which performs using the uuid1
method.
The add_content method also make the status the CURRENTPOST . Determine whether a previous CURRENTPOST
exists and, if one does, change its relationship type to NEXTPOST . In addition, the tags connected to the status update
are merged into the graph and connected to the status update via the HAS relationship type.
Listing 9-29. add_content Route and add_content Method for a Status Update
# add status update
@route('/posts/add', method='POST')
def add_content():
# get json from the request
content = request.json
#save the status update
content=Content().add_content(graph_db, request.get_cookie(graphstoryUserAuthKey), content)
# set response type
response.content_type = 'application/json'
# return the saved content
return dumps(content)
# add a status update
def add_content(self, graph_db, username, content):
tagstr=self.trim_content_tags(content["tagstr"])
tags = tagstr.split(",")
ts = time.time()
query = neo4j.CypherQuery(graph_db,
" MATCH (user { username: {u}}) " +
" CREATE UNIQUE (user)-[:CURRENTPOST]->(newLP:Content { title:{title}, " +
" url:{url}, tagstr:{tagstr}, timestamp:{timestamp}, contentId:{contentId} }) " +
" WITH user, newLP" +
" FOREACH (tagName in {tags} | " +
" MERGE (t:Tag {wordPhrase:tagName}) " +
" MERGE (newLP)-[:HAS]->(t) " +
" )" +
" WITH user, newLP " +
" OPTIONAL MATCH (newLP)<-[:CURRENTPOST]-(user)-[oldRel:CURRENTPOST]->(oldLP)" +
" DELETE oldRel " +
" CREATE (newLP)-[:NEXTPOST]->(oldLP) " +
" RETURN newLP.contentId as contentId, newLP.title as title, newLP.tagstr as tagstr, " +
" newLP.timestamp as timestamp, newLP.url as url, {u} as username, true as owner ")
params = {"u": username, "title": contentItem["title"].strip(),
"url": contentItem["url"].strip(),
"tagstr":tagstr, "timestamp":ts,"contentId": uuid.uuid1(), "tags":tags}
 
Search WWH ::




Custom Search