Database Reference
In-Depth Information
Listing 9-15.
The Sign-Up Route
@route('/signup/add', method='POST')
def signup():
username = request.forms.get('username').strip().lower()
# make sure username was passed
if username:
# check if username exists
user = User().get_user_by_username(graph_db, username)
if user:
# user found, show message
return template('public/templates/home/index.html', layout=homelayout, title="Home",
error='The username ' + username + ' already exists. Please use a
different username.')
else:
# save user
User().save_user(graph_db, username)
redirect("/msg?u=" + username)
# otherwise send back
else:
return template('public/templates/home/index.html', layout=homelayout,
title="Home", error="Please enter a username.")
Adding a User
In each part of the five graph areas covered in this chapter, the domain object has a corresponding service class to
manage the persistence operations within the database. In this case, the
User
class covers the management of the
application's user nodes, using a mix of py2neo convenience methods and executing Cypher queries.
To save a node and label it as a User, the
save_user
method, shown in Listing 9-16, makes use of the
create
method by passing in the username param and value. Once the node is created, the
add_labels
method applies the
User label.
Listing 9-16.
The
save_user
Method in the
User
Class
def save_user(self, graph_db, username):
# create user
newuser, = graph_db.create({"username": username})
# add the label
newuser.add_labels("User")
return newuser
Login
This section reviews the login process for the sample application. To execute the login process, you will also use the
login route as well as
User
class. Before reviewing the controller and service layer, take a quick look at the front-end
code for the login.