def send(request): if not validate_login.is_logged_in(request): return validate_login.failed_login() try: db = sql.getdb() cursor = db.cursor() user_id_receiver = request.form.get('user_id_receiver') message = request.form.get('message') ##log.debug(request.form) #cursor.execute("insert into message(user_id_sender, user_id_receiver, message, sent) values(" + user_id_sender + "," + user_id_receiver + "," + "\"" + message + "\"" + ", current_timestamp)" ) cursor.execute("insert into message(user_id_sender, user_id_receiver, message, sent) values(%s,%s,%s,current_timestamp)",(request.user_id, user_id_receiver, message)) db.commit() #cursor.execute("select gcm_id from user where user_id = %s", (user_id_receiver)) cursor.execute("select firstname, surname from user where user_id = %s", (request.user_id)) row = cursor.fetchone() firstname = row[0] surname = row[1] cursor.execute("select gcm_id from user where user_id = %s", (user_id_receiver)) row = cursor.fetchone() receiver = row[0] db.close() send_gcm(firstname, surname, receiver, message, request.user_id) return Response(message, mimetype='text/plain', status=200) except Exception as e: ##log.exception("Send exception: ") return Response('{test:test}',status=400)
def deleteuser(request): if not validate_login.is_logged_in(request): return validate_login.failed_login() print "Deleting user" uid = request.args.get('uid') print uid print request.user_id if uid and int(uid) == request.user_id: print "Starting delete" db = sql.getdb() try: cursor = db.cursor() cursor.execute("delete from message where user_id_sender = %s or user_id_receiver = %s", (request.user_id, request.user_id)) cursor.execute("delete from facebook_user where user_id = %s", (request.user_id)) cursor.execute("delete from email_user where user_id = %s", (request.user_id)) cursor.execute("delete from user where user_id = %s", (request.user_id)) cursor.execute("delete from session where user_id = %s", (request.user_id)) db.commit() except: print "Delete failed" db.rollback()
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 test(): db = sql.getdb() cursor = db.cursor() user_id_receiver = 36 message = "test message" #cursor.execute("insert into message(user_id_sender, user_id_receiver, message, sent) values(" + user_id_sender + "," + user_id_receiver + "," + "\"" + message + "\"" + ", current_timestamp)" ) cursor.execute("insert into message(user_id_sender, user_id_receiver, message, sent) values(%s,%s,%s,current_timestamp)",(35, user_id_receiver, message)) db.commit() import os send_gcm("test", "test", "APA91bGisBzMoj-n6cy7TKkyK-smGuR0N_mX3rFBYsEOqaS5beam0UMcsaFbscdfdICfar1tb8YjcrX9ZBg2goxaYtDHigxK6LTOuaLO2dNUGo29fq_H7yR-_59GOm4R3IySsDDax5VFTcDxv-n7hENHaO1pIsFsWA","test message",35)
def read(request): if not validate_login.is_logged_in(request): return validate_login.failed_login() db = sql.getdb() cursor = db.cursor() user_id_sender = request.form.get('user_id_sender') #cursor.execute("update message set `read`=current_timestamp where user_id_sender=" + user_id_sender + " and user_id_receiver=" + user_id_receiver + " and `read` is NULL") cursor.execute("update message set `read`=current_timestamp where user_id_sender=%s and user_id_receiver=%s and `read` is NULL", (user_id_sender, request.user_id)) cursor.close() db.commit() db.close() return Response('{test:"test"}')
def reggcm(request): if not validate_login.is_logged_in(request): return validate_login.failed_login() gcmId = request.form.get('gcmId') gcmVersion = request.form.get('gcmVersion') if not (gcmId and gcmVersion): return db = sql.getdb() cursor = db.cursor() cursor.execute("update user set gcm_id = %s, gcm_version = %s where user_id = %s", (gcmId,gcmVersion, request.user_id)) db.commit()
def send_gcm(firstname, surname, receiver, message, sender_id): import gcm from gcm import GCM g = GCM(gcm_key) #log.debug("Gcm_key: " + gcm_key) data = {'message':message, 'sender_firstname':firstname,'sender_surname':surname, 'sender_id':str(sender_id)} try: #log.debug("Sending: ") #log.debug(data) #log.debug("Receiver: " + receiver) g.plaintext_request(registration_id=receiver,data=data) except gcm.gcm.GCMInvalidRegistrationException: #log.exception("Gcm failed: ") db = sql.getdb() cursor = db.cursor() cursor.execute("update user set gcm_id = null where gcm_id = %s", (receiver)) db.commit() db.close()
def updateAddress(request): if not validate_login.is_logged_in(request): return validate_login.failed_login() ##log.debug(request.form) lat = request.form.get('lat') lon = request.form.get('lon') if lat and lon: try: db = sql.getdb() cursor = db.cursor() cursor.execute("UPDATE user set latlon = POINT(%s,%s) where user_id = %s", (lat,lon,request.user_id)) db.commit() except Exception as e: ##log.exception("Exception:") db.rollback() return Response('{test:test}',status=400) return Response('{test:test}',status=200) return Response('{test:test}',status=400)
def insertEmailUser(request): """ User table sid = form.getfirst("sid", "") fname = form.getfirst("fname", "") sname = form.getfirst("sname", "") lon = form.getfirst("lon", "") lat = form.getfirst("lat", "") car = form.getfirst("car", "") if car == 'true': car = True else: car = False starting_year = form.getfirst("starting_year", "") #Email table email = form.getfirst("email", "") pw = form.getfirst("pw", "")""" 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') car = request.form.get('car') if car == 'true': car = True else: car = False starting_year = request.form.get('starting_year') email = request.form.get('email') if not email.endswith('@hiof.no'): ##log.debug("Wrong email") return Response('{test:"test"}', status=400) ##log.debug("Args:") ##log.debug(request.form) pw = request.form.get('pw').encode('utf-8') try: hpw = bcrypt.hashpw(pw,bcrypt.gensalt()) except Exception as e: #log.exception("Exception: ") return Response('{test:"test"}', status=400) ##log.debug("Hash: " + hpw) db = sql.getdb() db.autocommit(False) cursor = db.cursor() #sqlusr = "******" + sid + "," + "\"" + fname + "\"," + "\"" + sname + "\",point(" + lat + "," + lon + ")," + car + "," + starting_year +")" sqlusr = "******" #sqlemail = "insert into email_user values((select user_id from user where firstname=\"" + fname + "\" and surname=\"" + sname + "\"), \"" + email + "\", \"" + pw + "\")" sqlemail = "insert into email_user values(%s, %s, %s)" try: cursor.execute(sqlusr, (sid,fname,sname,lat,lon,car,starting_year)) user_id = cursor.lastrowid cursor.execute(sqlemail, (user_id,email, hpw)) db.commit() #log.debug("Registration success") return Response('{test:"test"}',status=200) except Exception as ex: #log.exception("Registration failed") db.rollback() return Response('{test:"test"}',status=400)
def delete_old_sessions(self, user_id): db = sql.getdb() cursor = db.cursor() cursor.execute("delete from session where user_id = %s", (user_id)) db.commit()
def save(self, sid, user_id): db = sql.getdb() cursor = db.cursor() cursor.execute("insert into session (session_id, user_id, created) values(%s,%s,now())",(sid, user_id)) db.commit()