def login(request, **values): session_store = MySQLSessionStore() sid = request.cookies.get('hccook') if sid: #log.debug("Found session cookie sid: " + sid) if session_store.session_valid(sid): ##log.debug("Trying session login") user_id = session_store.get_userid(sid) return login_success(user_id,sid = sid, send_cookie = False) if 'email' in request.args and 'pass' in request.args: #log.debug("Trying credentials login:"******"Email exists") user_id = check_login(email, password) if user_id: ##log.debug("Found user id") request.session = session_store.session_new("",user_id) return login_success(user_id, sid = request.session.sid, send_cookie = True) return login_fail()
def insertFacebookUser(request): from MySQLSessionStore import MySQLSessionStore session_store = MySQLSessionStore() token = request.form.get('token') print request.form fid = face.valid_face(token) if not fid: return sid = request.form.get('sid') fname = request.form.get('fname') sname = request.form.get('sname') lon = request.form.get('lon') lat = request.form.get('lat') starting_year = request.form.get('starting_year') car = request.form.get('car') if car == 'true': car = True else: car = False #sqlusr = "******" + sid + "," + "\"" + fname + "\"," + "\"" + sname + "\",point(" + lat + "," + lon + ")," + starting_year + "," + car + ")" sqlusr = "******" #sqlfb = "insert into facebook_user values((select user_id from user where firstname=\"" + fname + "\" and surname=\"" + sname + "\"), \"" + fbid + "\")" sqlfb = "insert into facebook_user values(%s, %s)" db = sql.getdb() success = False try: cursor = db.cursor() cursor.execute(sqlusr, (sid, fname, sname, lat, lon, starting_year,car )) user_id = cursor.lastrowid print user_id print fid cursor.execute(sqlfb, (user_id, fid)) db.commit() success = True except Exception as ex: print ex db.rollback() success = False if success: user_id = session_store.get_userid_from_face(fid) import datetime request.session = session_store.session_new("",user_id) response = Response("",mimetype='text/plain') response.set_cookie('hccook', value=request.session.sid, max_age=3600*24*4, expires=datetime.datetime.utcnow() + datetime.timedelta(days=4)) return response else: return Response('{test:"test"}')
def is_logged_in(request): session_store = MySQLSessionStore() sid = request.cookies.get('hccook') print "sid" print sid if sid and session_store.session_valid(sid): request.session = session_store.get(sid) request.user_id = session_store.get_userid(sid) return True return False
def fbUserId(request): import face from MySQLSessionStore import MySQLSessionStore session_store = MySQLSessionStore() cursor = sql.getCursor() token = request.args.get('token') fid = face.valid_face(token) if fid == None: return Response('[{"user_id":-200}]', mimetype='text/plain') user_id = session_store.get_userid_from_face(fid) if not user_id: response = Response('[{"user_id": -100, "study_id": null, "firstname": "null", "surname": "null", "latlon": "null", "institution_name": "null", "campus_name": "null", "department_name": "null", "name_of_study": "null", "starting_year": null, "car": null, "gcm_id":null}]',mimetype='text/plain') return response rowarray = [] cursor.execute("SELECT user_id, user.study_id, firstname, surname, AsText(latlon) as latlon, institution.institution_name, campus.campus_name, department.department_name, name_of_study, starting_year, car, gcm_id FROM user INNER JOIN study ON user.study_id = study.study_id INNER JOIN campus ON study.campus_id = campus.campus_id ""INNER JOIN department ON study.department_id = department.department_id INNER JOIN institution ON department.institution_id = institution.institution_id WHERE user_id = (SELECT user_id FROM facebook_user WHERE facebook_id=%s)",(fid)) rows = cursor.fetchall() for row in rows: c = collections.OrderedDict() c['user_id'] = row[0] c['study_id'] = row[1] c['firstname'] = str(row[2]) c['surname'] = str(row[3]) c['latlon'] = str(row[4]) c['institution_name'] = str(row[5]) c['campus_name'] = str(row[6]) c['department_name'] = str(row[7]) c['name_of_study'] = str(row[8]) c['starting_year'] = row[9] c['car'] = row[10] c['gcm_id'] = row[11] rowarray.append(c) j = json.dumps(rowarray, ensure_ascii=False) response = Response(j, mimetype='text/plain') sid = request.cookies.get('hccook') if not (sid and session_store.session_valid(sid)): import datetime request.session = session_store.session_new("",user_id) response.set_cookie('hccook', value=request.session.sid, max_age=3600*24*4, expires=datetime.datetime.utcnow() + datetime.timedelta(days=4)) return response