def survey():
    if request.method == 'POST':
        logging.info("form data = " + str(request.form))
        questionOptionDict = [{
            key: request.form[key]
        } for key in request.form.keys()]
        logging.info("form data dict: " + str(questionOptionDict))
        db = sddb()
        db.save_results(questionOptionDict)
        db.close()
        return redirect('/login')
    elif request.method == 'GET':
        sid = request.args.get('sid', '')
        if sid:
            db = sddb()
            survey = db.get_survey(int(sid))
            questions = db.get_survey_questions(sid)
            db.close()
            logging.info(str(questions))
            return render_template('survey.pt',
                                   logged_in=False,
                                   surveyName=survey['sname'],
                                   questions=questions,
                                   preview=False)
        else:
            return redirect('/login')
def surveys():
    db = sddb()
    surveys = db.get_surveys(session['uid'])
    db.close()
    return render_template('surveys.pt',
                           logged_in=True,
                           user=session['uname'],
                           surveys=surveys)
def delete():
    try:
        sid = request.args.get('sid', '')
        if sid:
            db = sddb()
            db.remove_survey(sid)
            db.close()
    except Exception as e:
        logging.error("Could not remove survey %s description %s" % (sid, str(e)))
    return redirect('/surveys')
def delete():
    try:
        sid = request.args.get('sid', '')
        if sid:
            db = sddb()
            db.remove_survey(sid)
            db.close()
    except Exception as e:
        logging.error("Could not remove survey %s description %s" %
                      (sid, str(e)))
    return redirect('/surveys')
def survey():
    if request.method =='POST':
        logging.info("form data = " + str(request.form))
        questionOptionDict = [{key:request.form[key]} for key in request.form.keys()]
        logging.info("form data dict: " + str(questionOptionDict))
        db = sddb()
        db.save_results(questionOptionDict)
        db.close()
        return redirect('/login')
    elif request.method == 'GET':
        sid = request.args.get('sid','')
        if sid:
            db= sddb()
            survey = db.get_survey(int(sid))
            questions = db.get_survey_questions(sid)
            db.close()
            logging.info(str(questions))
            return render_template('survey.pt', logged_in = False,
                        surveyName = survey['sname'], questions = questions, preview=False)
        else:
            return redirect('/login')
def save():

    try:
        data = json.loads(request.data)
        db = sddb()
        db.save_survey(session['uid'], data)
        db.close()

    except Exception as e:
        logging.error(str(e))

    return "success"
def save():

    try:
        data = json.loads(request.data)
        db = sddb()
        db.save_survey(session['uid'], data)
        db.close()

    except Exception as e:
        logging.error(str(e))

    return "success"
def results():

    if request.method == 'GET':
        sid = request.args.get('sid','')
        if sid:
            db= sddb()
            survey = db.get_survey(int(sid))
            questions = db.get_survey_questions(sid)
            db.close()
            logging.info(str(questions))
            return render_template('result.pt', logged_in = True,user = session['uname'],
                        surveyName = survey['sname'], questions = questions, preview=True)
        else:
            return redirect('/login')
def login():
    error = None
    if request.method =='POST':
        #logic to authenticate
        db = sddb()
        user = db.login_user(request.form['uname'], request.form['password'])
        db.close()
        if user:
            session['logged_in'] = True
            session['uid']= user['id']
            session['uname']=user['uname']
            return redirect('/surveys')
        else:
            error = "Invalid Credentials. Please try again."

    return render_template('login.pt', logged_in=False, error=error)
def login():
    error = None
    if request.method == 'POST':
        #logic to authenticate
        db = sddb()
        user = db.login_user(request.form['uname'], request.form['password'])
        db.close()
        if user:
            session['logged_in'] = True
            session['uid'] = user['id']
            session['uname'] = user['uname']
            return redirect('/surveys')
        else:
            error = "Invalid Credentials. Please try again."

    return render_template('login.pt', logged_in=False, error=error)
def results():

    if request.method == 'GET':
        sid = request.args.get('sid', '')
        if sid:
            db = sddb()
            survey = db.get_survey(int(sid))
            questions = db.get_survey_questions(sid)
            db.close()
            logging.info(str(questions))
            return render_template('result.pt',
                                   logged_in=True,
                                   user=session['uname'],
                                   surveyName=survey['sname'],
                                   questions=questions,
                                   preview=True)
        else:
            return redirect('/login')
def surveys():
    db = sddb()
    surveys = db.get_surveys(session['uid'])
    db.close()
    return render_template('surveys.pt', logged_in=True, user = session['uname'],surveys= surveys)