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 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