Database Reference
In-Depth Information
Listing 9-36. Add consumption_add Route and create_user_view_and_return_views Method
# add a product via VIEWED relationship and return VIEWED products
@route('/consumption/add/<productNodeId:int>', method='GET')
def consumption_add(productNodeId):
#save the view and return the full list of views
productTrailAsJson=Product().create_user_view_and_return_views(graph_db, request.cookies
[graphstoryUserAuthKey], productNodeId)
#set the response type
response.content_type = 'application/json'
#return the list of views
return dumps(productTrailAsJson)
# the method to add a user view of a product
def create_user_view_and_return_views(self, graph_db, username, productNodeId):
# create timestamp and string display
ts = time.time()
timestampAsStr = datetime.fromtimestamp(int(ts)).strftime(
'%m/%d/%Y') + " at " + datetime.fromtimestamp(int(ts)).strftime('%I:%M %p')
query = neo4j.CypherQuery(graph_db,
" MATCH (p:Product), (u:User { username:{u} })" +
" WHERE id(p) = {productNodeId}" +
" WITH u,p" +
" MERGE (u)-[r:VIEWED]->(p)" +
" SET r.dateAsStr={timestampAsStr}, r.timestamp={ts}" +
" WITH u " +
" MATCH (u)-[r:VIEWED]->(p)" +
" RETURN p.title as title, r.dateAsStr as dateAsStr" +
" ORDER BY r.timestamp desc")
params = {"productNodeId": productNodeId,"u": username,
"timestampAsStr": timestampAsStr,"ts": ts }
result = query.execute(**params)
result=self.get_product_trail_results_as_json(result)
return result
Filtering Consumption for Users
One practical use of the consumption model is to create a content trail for users, as shown in Figure 9-14 . As a user
clicks on items in the scrolling product stream, the interaction is captured using create_user_view_and_return_
views , which ultimately returns a List of relationship objects of the VIEWED type.
Search WWH ::




Custom Search