From a85a862c18e2f026a4de62d8d76ecf5247f8a279 Mon Sep 17 00:00:00 2001 From: KasenJ <1129390987@qq.com> Date: Wed, 9 Jul 2014 12:59:39 +0800 Subject: [PATCH 01/66] =?UTF-8?q?registerHandler=20v.1=20&=20updatedatabas?= =?UTF-8?q?es=20by=E6=B1=9F=E5=98=89=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/dbapi.py | 35 +++++++++++++++++++++++++++++++++ code/handler/RegisterHandler.py | 21 ++++++++++++++++++-- code/index.html | 2 +- db.sql | 3 +-- 4 files changed, 56 insertions(+), 5 deletions(-) diff --git a/code/dbapi.py b/code/dbapi.py index 2ecf9ec..ec09359 100644 --- a/code/dbapi.py +++ b/code/dbapi.py @@ -56,5 +56,40 @@ def getEventsByUserName(self,username): return [] return self.getEventsByUserId(user["userid"]) + #check if cardid exist + #exist return dict + #not exist return none + def getInfoBycardid(self,cardid): + cursor=self.db.cursor(cursorclass=MySQLdb.cursors.DictCursor) + sql="select * from info where cardid=%s" + param=(cardid,) + cursor.execute(sql,param) + result=cursor.fetchone() + cursor.close() + return result + + #register a new user + #pre condiction:no user.name,info.cardid duplicate + #after : insert new user,new info + def register(self,content): + cursor = self.db.cursor() + + sql = "insert into user(name,kind,password) values(%s,%s,%s)" + param = (content["username"],content["kind"],content["password"]) + cursor.execute(sql,param) + self.db.commit() + + cursor.execute('SELECT LAST_INSERT_ID()') + result=cursor.fetchone() + print result[0] + + sql = "insert into info(id,cardid,name,sex,age,address,illness,credit,score) values(%s,%s,%s,%s,%s,%s,%s,%s,%s)" + param = (result[0],content["cardid"],content["realname"],content["sex"],content["age"],content["address"],content["illness"],0,0) + cursor.execute(sql,param) + self.db.commit() + + cursor.close() + return + def __del__(self): self.db.close() diff --git a/code/handler/RegisterHandler.py b/code/handler/RegisterHandler.py index ca1f57f..c139935 100644 --- a/code/handler/RegisterHandler.py +++ b/code/handler/RegisterHandler.py @@ -1,7 +1,24 @@ import tornado.ioloop import tornado.web import tornado.httpserver -import os,dbapi +import os,sys,json +sys.path.append("..") +import dbapi class RegisterHandler(tornado.web.RequestHandler): def post(self): - self.write("RegisterHandler") + #content = self.get_argument("content") + content = '{"username": "ooo","password": 111111,"kind": 1, "cardid":"42434q" ,"realname":"hiii","sex":1,"age":41, "address":"iii","illness":"hijiiii"}' + j = json.loads(content) + if(self.application.dbapi.getUserByUserName(j['username']) is not None): + self.write("{'state':1}") + print "username exist" + return + if(self.application.dbapi.getInfoBycardid(j['cardid']) is not None): + self.write("{'state':2}") + print "cardid exist" + return + + self.application.dbapi.register(j) + self.write("{'state':3}") + print("Register") + return diff --git a/code/index.html b/code/index.html index daae199..d10ae67 100644 --- a/code/index.html +++ b/code/index.html @@ -4,7 +4,7 @@