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?

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
ramez
  • 321
  • 3
  • 22
  • 2
    try adding a print to your init function and make sure `User` is the class you think it is – Joran Beasley Dec 09 '15 at 00:27
  • Did you import the User class from wherever it was defined? – Nicole White Dec 09 '15 at 00: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! – ramez Dec 09 '15 at 00:48
  • Of course! That's the reason I built it. :) I suggest you follow what @JoranBeasley said and print something within the __init__ function. – Nicole White Dec 09 '15 at 01:11

1 Answers1

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.

Community
  • 1
  • 1
ciacicode
  • 708
  • 1
  • 5
  • 13