I am trying to call a function on button click using flask. Here is a snippet of my code to make my question clearer.
class User:
def __init__(self, username):
self.username = username
def find(self):
user = graph.find_one("User", "username", self.username)
return user
def add_restaurant(self, name, cuisine, location):
user=self.find()
restaurant = Node("Restaurant", id=str(uuid.uuid4()),name=name)
graph.create(restaurant)
graph.create(rel(user,"LIKES", restaurant))
rest_type = Node("Cuisine", cuisine=cuisine)
graph.create(rest_type)
graph.create(rel(restaurant,"SERVES", rest_type))
loc = Node("Location", location=location)
graph.create(loc)
graph.create(rel(restaurant, "IN", loc))
The add_restaurant function is supposed to create 3 nodes and their corresponding relationships in Neo4j graph database, and it works fine.
But, when I call this function from my Flask file, I get an
AttributeError: User instance has no attribute 'add_restaurant'
This is the function in the Flask file
@app.route('/add_restaurant', methods=['POST'])
def add_restaurant():
name = request.form['name1']
cuisine = request.form['cuisine1']
location = request.form['location1']
username = session.get('username')
if not name or not cuisine or not location:
if not name:
flash('You must enter a restaurant name')
if not cuisine:
flash('What cuisine does the restaurant belong to')
if not location:
flash('You must enter a location')
else:
User(username).add_restaurant(name, cuisine, location) #Error comes from here.
return redirect(url_for('index'))
name, cuisine, and location are generated from text fields. What am I doing wrong to get this error?