Database Reference
In-Depth Information
User Edit Route
The graphstory application contains a route with the path /user/edit , which takes the JSON object argument. The
User object is converted from a JSON string and returns a User object as JSON . The response could be used to update
the form elements, but because the values are already set within the form there is no need to update the values. In
this case, the application uses the JSON response to let the user know if the update succeeded or not via a standard
JavaScript alert message (Listing 9-21).
Listing 9-21. user_edit Route
@route('/user/edit', method='PUT')
def user_edit():
User().update_user(graph_db, request.get_cookie(graphstoryUserAuthKey),
request.json["firstname"], request.json["lastname"])
response.content_type = 'application/json'
return {"msg": "ok"}
User Update Method
To complete the update, the controller layer calls the update_user method in User class. Because the object being
passed into the update method did nothing more than modify the first and last name of an existing entity, you can use
the SET clause via Cypher to update the properties in the graph, as shown in Listing 9-22.
This Cypher statement also makes use of the MATCH clause to retrieve the User node. You could also complete this
feature by executing a find or using the get_user_by_username method, and then updating the first and last name via
the update_properties method of the py2neo Node class.
Listing 9-22. The update_user Method in the User Class
def update_user(self, graph_db, username, firstname, lastname):
query = neo4j.CypherQuery(graph_db,
"MATCH (user:User {username:{u}} ) " +
"SET user.firstname = {fn}, user.lastname = {ln}")
params = {"u": username, "fn": firstname, "ln": lastname}
result = query.execute(**params)
return result
Connecting Users
A common feature in social media applications is to allow users to connect to each other through an explicit
relationship. In the sample application, you will use the directed relationship type called FOLLOWS . By going to the
“Friends” page within the social graph section, you can see the list of the users the current user is following, search for
new friends to follow, add them and remove friends the current user is following.
The user management section of graphstory.py contains each of the routes to control the flow for these features,
specifically the routes that cover friends , search_by_username , follow and unfollow .
To display the list of the users the current user is following, the friends route, showing in Listing 9-23, in the
graphstory application calls the following method in User class. The following method in User class, also shown in
Listing 9-23, creates a list of users by matching the current user's username with directed relationship FOLLOWS on the
variable user .
 
Search WWH ::




Custom Search