0

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?

4
  • 2
    try adding a print to your init function and make sure User is the class you think it is Commented Dec 9, 2015 at 0:27
  • Did you import the User class from wherever it was defined? Commented Dec 9, 2015 at 0:29
  • @NicoleWhite Hello Nicole! Seeing your reply was a very pleasant surprise :). I am actually using your neo4j-flask tutorial as a base for my practice, but I'm tweaking your code to run on my own database because I think that makes learning more interesting (hope you don't mind). Your reply made my day. And yes I imported the User class correctly. Respect! Commented Dec 9, 2015 at 0:48
  • Of course! That's the reason I built it. :) I suggest you follow what @JoranBeasley said and print something within the init function. Commented Dec 9, 2015 at 1:11

1 Answer 1

1

I looked into this thanks to some similar questions here on stack overflow. I think when you create the user instance with

User(username).add_restaurant(name, cuisine, location)

The user is not properly instantiated. Try with:

else:
    user = user(username)
    user.add_restaurant(name, cuisine, location)

Let us know if this is of any help.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.