Problems when connecting to Appfog's SQL service from Flask

71 Views Asked by At

I'm currently trying to get my Flask app working with Appfog. I'm having 500 error every time I'm trying to do something with my DB. My connection code looks like:

services = json.loads(os.environ.get("VCAP_SERVICES"))
pg = services["postgresql-9.1"][0]["credentials"]

app.config.update({
  'SQLALCHEMY_DATABASE_URI': "postgresql://%s:%s@%s:%i/%s" % \
    (pg["username"], pg["password"], pg["host"], pg["port"], pg["name"])
})

# I've tried MySQL too

Model:

class User(db.Model):
  id = db.Column(db.Integer, primary_key=True)
  username = db.Column(db.String(40), unique=True)
  passhash = db.Column(db.String(140))

  def __init__(self, username, password):
    self.username = username
    self.passhash = pwd_context.encrypt(password)

  def check_password(self, password):
    return pwd_context.verify(password, self.passhash) # from passlib

  # Flask-Login properties
  def is_authenticated(self): return True
  def is_active(self): return True
  def is_anonymous(self): return False
  def get_id(self): return unicode(self.id)

And some example route that utilizes DB:

@app.route("/login", methods=['GET', 'POST'])
def login():
  if current_user.is_authenticated():
    logout_user()
  if request.method == 'POST':
    username = request.form.get('username')
    password = request.form.get('password')
    user = User.query.filter_by(username=username).first()
    if not user or not user.check_password(password):
      flash('Wrong username or password.')
      return render_template('login.html')
    else:
      login_user(user)
      return redirect(request.args.get("next") or "/")
  else:
    return render_template('login.html')

This works with sqlite database on localhost. So, am I doing something wrong?

0

There are 0 best solutions below