Database Reference
In-Depth Information
Listing 9-24. The HTML Code Snippet for Displaying the List of Friends
<div class="col-md-3">
<h3>Current Friends</h3>
<table class="table" id="following">
{{#following}}
<tr><td>{{firstname}} {{lastname}}</td><td><a href="#"
id="{{username}}"
class="removefriend">Remove</a></td></tr>
{{/following}}
{{^following}}
No friends :(
{{/following}}
</table>
</div>
To search for users to follow, the user section of graphstory contains a GET route /searchbyusername and passes
in a username value as part of the path. This route executes the search_by_username_not_following method found
in User class, showing the second section of Listing 9-25. The first part of the WHERE clause in search_by_username_
not_following returns users whose username matches on a wildcard String value. The second part of the WHERE
clause in search_by_username_not_following checks to make sure the users in the MATCH clause are not already
being followed by the current user.
Listing 9-25. The searchbyusername Route and service Method
@route('/searchbyusername/<username>', method='GET')
def search_by_username(username):
# get the users' the current user is following
users = User().search_by_username_not_following(graph_db,
request.get_cookie(graphstoryUserAuthKey),
username)
response.content_type = 'application/json'
# return as json
return dumps({"users": User().users_results_as_array(users)})
# search by user returns users in the network that aren't already being followed
def search_by_username_not_following(self, graph_db, currentusername, username):
username = username.lower() + ".*"
query = neo4j.CypherQuery(graph_db,
" MATCH (n:User), (user { username:{cu}}) " +
" WHERE (n.username =~ {u} AND n <> user) " +
" AND (NOT (user)-[:FOLLOWS]->(n)) " +
" RETURN n.firstname as firstname, n.lastname as lastname,"+
" n.username as username")
params = {"u": username, "cu": currentusername}
result = query.execute(**params)
return result
Search WWH ::




Custom Search