def login():

    error = None
    if not request.form['username']:
        error = 'username is null'
        return render_template('login.html', error = error)
    if not request.form['password']:
        error = 'password is null'
        return render_template('login.html', error = error)


    username = request.form['username'].encode('utf8')
    password = request.form['password'].encode('utf8')
    user=None
    if username == "admin":
        user = accessControl.verify(username, password)
    else:
        er = accessControl._valid_login_format(username,password)
        if er:
            return render_template('login.html', error = er[0])

        user = accessControl.verify(username, password)


    if user is None:
        error = 'username and password does not match'
        return render_template('login.html', error = error)
    else:
        if user.type == 1:
            session['type'] = 1
            session['username'] = username
            session['fname'] = user.fname
            return redirect(url_for('index.show_network',username=username))
        if user.status == 0:
            error = 'User account is locked'
            return render_template('login.html', error = error)
        session['type'] = 0
        session['username'] = username
        session['fname'] = user.fname
        if not user.questions:
            return render_template('select.html',questions = global_v.fixed_questions)
        else:
              ques=user.questions
              list=[ques.sq1,ques.sq2,ques.sq3]
              #  get count from webpage , send list[index_list[count]]
              # correct answer =  verifyQues(index_list[count], session['username'])
              return render_template('validate.html', questions = list, check = 1)
def change_password():
    a = request.args.get('a', 0, type = str)
    b = request.args.get('b', 0, type = str)
    username = session['username']
    print username,a,b
    re = accessControl.verify(username, a)
    msg = None
    if re:
        msg = accessControl.change_pwd(username, b)
        if msg == None:
            msg = 'success'
    else:
        msg = 'Old password wrong!'
    print msg
    return jsonify(result = msg)
from models import accessControl
from models import netModel
from models import mydb
import json
from models import Node




u = 'ew'.encode('utf8')
p = 'dsa'.encode('utf8')
re = accessControl.verify(u, p)
print re


# u = User('s','d','ds')
#
# p = Pattern(2,u)
#
# print p.connect.name

mydb.insert_db('update ')
print accessControl._valid_login_format('spike1390', '2cs744')
# l = [Node(21,4,3), Node(1,2,5), Node(2,4,2)]
# l.sort()
# for u in l:
#     print u.index
# re = accessControl.verify('spikewang', 'cs744')
# print re.name