def test_dao(db): dao=BookDao(db) book1 ={} book1['book_titile']= 'introduce to tornado' book1['addr']='4-2-7' book2 = {'book_title':'learning python', 'author':'liu dw','publisher':'bca','description':'this is a book about basic knowledge of python', 'addr':'4-2-7', 'price':15.21} book3 = {'book_title':'Restful Web Service', 'author':'yunjianfei','publisher':'bca','description':'this is a book about restful web service', 'addr':'4-2-34', 'price':34.14} book4 = {'author':'aimer yin', 'publisher':'baihualou', 'description':' kanxinqing', 'addr':'7-8-2','price':34.2} #ret = dao.insert_by_dict(book3) #print 'insert book : %s, ret: %s' % (str(book3),str(ret)) #h = dao.get_by_bid(1) #print 'get by id, ret: %s'% str(h) #ret = dao.get_by_title('kim') #print 'get by title ,ret: %s' % str(ret) #ret = dao.del_by_bid(14) ret=dao.update_by_bid(17,book4) print ret
def initialize(self): self.db = self.application.db self.bookdao = BookDao(self.db) self.Resp = Resp()
class BookHandler(tornado.web.RequestHandler): def initialize(self): self.db = self.application.db self.bookdao = BookDao(self.db) self.Resp = Resp() def get(self): book_id = self.get_argument('book_id', None) book_id = tools.to_int(book_id) book_title = self.get_argument('book_title', None) book_title = tools.strip_string(book_title) author = self.get_argument('author', None) author = tools.strip_string(author) publisher = self.get_argument('publisher', None) publisher = tools.strip_string(publisher) resp = None if book_id != None: logging.info("in GET method! Get book by bookid: '%s'", str(book_id)) book = self.bookdao.get_by_bid(book_id) resp = book elif book_title != None: logging.info("in GET method! Get book by title: '%s'", str(book_title)) book = self.bookdao.get_by_title(book_title) resp = book elif author != None: logging.info("in GET method! Get book by author: '%s'", str(author)) book = self.bookdao.get_by_author(author) resp = book elif publisher != None: logging.info("in GET method! Get book by publisher: '%s'", str(publisher)) book = self.bookdao.get_by_publisher(publisher) resp = book else: logging.info("in GET method! Get All book") books = self.bookdao.get_allbook resp = books logging.info("Query result: %s", str(resp)) if resp == None: logging.error("There is no record!") resp = self.Resp.make_response(code=RespCode.NO_RECORD) self.write(resp) return resp = self.Resp.make_response(code=RespCode.SUCCESS, content=resp) self.write(resp) def post(self): data = self.request.body h = json.loads(data) logging.info("in insert method. receive data:%s", str(data)) book_title = h.get('book_title', None) book_title = tools.strip_string(book_title) if book_title == None: logging.error("there is no parameter 'book_title'!") resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, para='book_title') self.write(resp) return addr = tools.strip_string(h.get('addr', None)) if addr == None: logging.error("there is no parameter 'addr'!") resp = self.Resp.make_response(code=ReaspCode.NO_PARAMETER, para='addr') self.write(resp) return author = tools.strip_string(h.get('author', None)) price = h.get('price', None) publisher = tools.strip_string(h.get('publisher', None)) description = tools.strip_string(h.get('description', None)) logging.debug("check parameter complete, ready to save in db") book = {} book['book_title'] = book_title book['author'] = author book['publisher'] = publisher book['description'] = description book['addr'] = addr book['price'] = price ret = self.bookdao.insert_by_dict(book) if ret == None: err_str = "error oncurred when insert into table 'book'" logging.error(err_str) resp = self.Resp.make_response(code=RespCode.DB_ERROR, err_str=err_str) self.write(resp) return logging.info('save book object successed! the book: %s', str(book)) book['book_id'] = ret resp = self.Resp.make_response(code=RespCode.SUCCESS, para="add book", content=book) self.write(resp) def delete(self): book_id = self.get_argument('book_id', None) book_id = tools.strip_string(book_id) if book_id == None: logging.error("there is no parameter 'book_id'!") resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, para='book_id') self.write(resp) return logging.info(" in delete method! delete book by book_id:'%s'.", str(book_id)) ret = self.bookdao.del_by_bid(book_id) err_str = "error oucurred when delete book by id: '%s'" % book_id if ret == None: logging.error(err_str) resp = self.Resp.make_response(code=RespCode.INVALID_PARAMETER, err_str=err_str) self.write(resp) return logging.info("delete book object successed!") resp = self.Resp.make_response(code=RespCode.SUCCESS, para="delete book") self.write(resp) def put(self): data = self.request.body h = json.loads(data) logging.info("in update method! receive data: %s", str(data)) book_id = h.get('book_id', None) book_id = tools.strip_string(book_id) if book_id == None: logging.error("there is no parameter 'book_id'!") resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, para='book_id') self.write(resp) return if_exist = self.bookdao.if_exist(book_id) if if_exist == False: logging.error("the book doesn't existed!") resp = self.Resp.make_response(code=RespCode.INVALID_PARAMETER, para='book_id') self.write(resp) return h1 = {} for k, v in h.items(): if v != None: h1[k] = v ret = self.bookdao.update_by_bid(book_id, h1) resp = self.Resp.make_response(code=RespCode.SUCCESS, para='update book') self.write(resp)
def initialize(self): self.db = self.application.db self.bookdao=BookDao(self.db) self.readerdao=ReaderDao(self.db) self.borrowdao=BorrowDao(self.db) self.Resp=Resp()
class UserHandler(tornado.web.RequestHandler): def initialize(self): self.db = self.application.db self.bookdao=BookDao(self.db) self.readerdao=ReaderDao(self.db) self.borrowdao=BorrowDao(self.db) self.Resp=Resp() def search(self): se=self.get_argument('book',None) se=tools.strip_string(se) resp = [] if se != None: logging.info("in search method! get search input:'%s'",str(se)) ret=self.bookdao.get_by_title(se) resp.extend(ret) ret=self.bookdao.get_by_auther(se) resp.extend(ret) ret=self.bookdao.get_by_publisher(se) resp.extend(ret) logging.info('query result: %s', str(resp)) else: logging.error("there is no input!") resp=self.Resp.make_response(code=RespCode.NO_PARAMETER,pare= 'input') self.write(resp) return if resp == None or len(resp) == 0: logging.error('there is no record!') resp = self.Resp.make_response(code=RespCode.No_RECORD) self.write(resp) resp = self.Resp.make_response(code=RespCode.SUCCESS, content=resp) self.write(resp) def getbybid(self): bid=self.get_argument('book_id',None) bid=tools.strip_string(bid) resp=None if bid != None: logging.info("bookdetail! bookid:'%s'",str(bid)) ret=self.bookdao.get_by_bid(bid) resp=ret h=self.borrowdao.if_exist(bid) if h==True: resp['onsheet']='no' else: resp['onsheet']='yes' logging.info('query result: %s', str(resp)) else: logging.error("there is no input!") resp=self.Resp.make_response(code=RespCode.NO_PARAMETER,pare= 'bid') self.write(resp) return if resp == None or len(resp) == 0: logging.error('there is no record!') resp = self.Resp.make_response(code=RespCode.No_RECORD) self.write(resp) resp = self.Resp.make_response(code=RespCode.SUCCESS, content=resp) self.write(resp) def searchbyrid(self): rid = self.get_argument('readerid',None) rid = tools.strip_string(rid) resp=None if readerid != None: logging.info("in searchbyrid method! get reader by readerid:'%s'",str(rid)) ret= self.readerdao.get_by_rid(rid) resp=ret books=self.borrowdao.get_by_readerid(rid) resp["books"]=books logging.info('query result: %s', str(resp)) if resp == None or len(resp) == 0: logging.error('there is no record!') resp = self.Resp.make_response(code=RespCode.No_RECORD) self.write(resp) resp = self.Resp.make_response(code=RespCode.SUCCESS, content=resp) self.write(resp) def renew(self): bookid=self.get_argument('bookid',None) bookid=self.tools.strip_string(bookid) if bookid == None: logging.error("there is no input!") resp=self.Resp.make_response(code=RespCode.NO_PARAMETER,pare='bid') self.write(resp) return logging.info('renew book id:%s',str(bookid)) ret=self.borrowdao.update_renew(bookid) if ret: #####add a month### re=self.borrowdao.get_by_bookid(bookid) restamp=re['returndate'] datearray=datetime.datetime.utcfromtimestamp(restamp) re1=datearray+datetime.timedelta(days=31) re1stamp=int(time.mktime(re1.timetuple())) ret=update_returndate(re1stamp,bookid) logging.info("renew success! bookid:'%s'",str(bookid)) else: logging.error("Fail! already done before.") resp = self.Resp.make_response(code=RespCode.HAS_EXISTED) self.write(resp) resp = self.Resp.make_response(code=RespCode.SUCCESS) self.write(resp)
class BorrowHandler(tornado.web.RequestHandler): def initialize(self): self.db = self.application.db self.bookdao=BookDao(self.db) self.readerdao=ReaderDao(self.db) self.borrowdao=BorrowDao(self.db) self.Resp=Resp() def borrow(self): bookid=self.get_argument('bookid',None) readerid=self.get_argument('readerid',None) bookid=tools.strip_string(bookid) readerid=tools.strip_string(readerid) resp=None if bookid ==None or readerid ==None: logging.error("there is no parament bookid or readerid!") resp = self.Resp.make_response(code=RespCode.NO_PARAMETER,para='id') self.write(resp) return '''check readerid bookid exist and borrow exist ...''' borrow={} borrow['bookid']=bookid borrow['readerid']=readerid nowstamp=int(time.time()) datearray=datetime.datetime.utcfromtimestamp(nowstamp) re=datearray+datetime.timedelta(days=31) restamp=int(time.mktime(re.timetuple())) borrow['borrowdate']=nowstamp borrow['returndate']=restamp ret = self.borrowdao.insert_by_dict(borrow) if ret == None: logging.error("error oucurred when insert into table 'borrow'") resp =self.Resp.make_response(code=RespCode.DB_ERROE) self.write(resp) return else: logging.info("borrow successed! The book: %s", str(bookid)) borrow['brw_id'] = ret resp = self.Resp.make_response(code=RespCode.SUCCESS, content=borrow) self.write(resp) def returnbook(self): bookid=self.get_argument('bookid',None) bookid=tools.strip_string(bookid) resp=None if bookid ==None: logging.error("there is no parament bookid!") resp = self.Resp.make_response(code=RespCode.NO_PARAMETER,para='id') self.write(resp) return now=datetime.datetime.now() borrow=self.borrowdao.get_by_bookid(bookid) re=datetime.datetime.utcfromtimestamp(borrow['returndate']) delta=re-now day=getattr(delta,'days') if day < 0: punish=0.5*abs(day) logging.error("this book is delay and pay for %s yuan!",str(punish)) resp = self.Resp.make_response(code=RespCode.RETURN,para='punish') self.write(resp) logging.info("delete borrow book %s",str(bookid)) ret=self.borrowdao.del_by_bookid(bookid) logging.info("delete successed!") resp = self.Resp.make_response(code=RespCode.SUCCESS) self.write(resp) def renew(self): bookid=self.get_argument('bookid',None) bookid=self.tools.strip_string(bookid) if bookid == None: logging.error("there is no input!") resp=self.Resp.make_response(code=RespCode.NO_PARAMETER,pare= 'input') self.write(resp) return logging.info('renew book id:%s',str(bookid)) ret=self.borrowdao.update_renew(bookid) # if ret: # logging.info("renew success! bookid:'%s'",str(bookid) # else: # logging.error("Fail! already done before.") # resp = self.Resp.make_response(code=RespCode.HAS_EXISTED) # self.write(resp) # #add a month re=self.borrowdao.get_by_bookid(bookid) restamp=re['returndate'] datearray=datetime.datetime.utcfromtimestamp(restamp) re1=datearray+datetime.timedelta(days=31) re1stamp=int(time.mktime(re1.timetuple())) ret=update_returndate(re1stamp,bookid) resp = self.Resp.make_response(code=RespCode.SUCCESS) self.write(resp) def getbybid(self): bid=self.get_argument('book_id',None) bid=tools.strip_string(bid) resp=None if bid != None: logging.info("borrowdetail! bookid:'%s'",str(bid)) ret=self.borrowdao.get_by_bid(bid) resp=ret logging.info('query result: %s', str(resp)) else: logging.error("there is no input!") resp=self.Resp.make_response(code=RespCode.NO_PARAMETER,pare= 'bid') self.write(resp) return if resp == None or len(resp) == 0: logging.error('there is no record!') resp = self.Resp.make_response(code=RespCode.No_RECORD) self.write(resp) resp = self.Resp.make_response(code=RespCode.SUCCESS, content=resp) self.write(resp) def lost(self): bid=self.get_argument('book_id',None) bid=tools.strip_string(bid) if bid != None: h=self.bookdao.get_by_bid(bid) price=h['price'] logging.info("lost bookid:'%s',pay for $%s",str(bid),str(price)) ret=self.bookdao.del_by_bid(bid) else: logging.error("there is no input!") resp=self.Resp.make_response(code=RespCode.NO_PARAMETER,pare= 'bid') self.write(resp) return resp = self.Resp.make_response(code=RespCode.SUCCESS) self.write(resp) def clearlist(self): pass
class BookHandler(tornado.web.RequestHandler): def initialize(self): self.db = self.application.db self.bookdao=BookDao(self.db) self.readerdao=ReaderDao(self.db) self.borrowdao=BorrowDao(self.db) self.Resp=Resp() def insert(self): data = self.request.body h = json.loads(data) logging.error("in insert method. receive data:%s", str(data)) book_title = h.get('book-title',None) book_title = tools.strip_string(book_title) if book_title == None: logging.error("there is no parameter 'book_title'!") resp = self.Resp.make_response(code=RespCode.NO_PARAMETER,para='book_title') self.write(resp) return addr=tools.strip_string(h.get(addr,None)) if addr == None: logging.error("there is no parameter 'addr'!") resp = self.Reap.make_response(code = RespCode.NO_PARAMETER,para='addr') self.write(resp) return author=tools.strip_string(h.get('author','')) publisher=tools.strip_string(h.get('publisher','')) description=tools.strip_string(h.get('description','')) price=float(h.get('price',0)) logging.debug("check parameters complete, ready to save in db") book={} book['book_title']=book_title book['author']=author book['publiser']=publisher book['description']=description book['addr']=addr book['price']=price ret = self.bookdao.insert_by_dict(book) if ret == None: err_str="error oucurred when insert into table 'book'" logging.error(err_str) resp = self.Resp.make_response(code = RespCode.DB_ERROR,err_str=err_str) self.write(resp) return logging.info('save book object successed! the book: %s', str(book)) book['book_id']=ret resp = self.Resp.make_response(code=RespCode.SUCCESS,content=book) self.write(resp) def update(self): data=self.request.body h = json.loads(data) logging.info("in update method! receive data: %s", str(data)) book_id=h.get('book_id',None) book_id=tools.strip_string(book_id) book_title=tools.strip_string(h.get('book_title',None)) if book_id ==None: logging.error("there is no parameter 'book_id'!") resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, para='book_id') self.write(resp) return if_exist=self.bookdao.if_exist(book_id) if if_exist == False: logging.error("the book doesn't existed!") resp=self.Resp.make_response(code=RespCode.NO_RECORD, para='book_id') self.write(resp) return ret=self.bookdao.update_by_bid(book_id,h) resp=self.Resp.make_response(code=RespCode.SUCCESS) self.write(resp) # get=self.bookdao.get_by_bid(book_id) # book={} # book['addr']=tools.strip_string(h.get(addr,get['addr'])) # book['author']=tools.strip_string(h.get('author',get['author'])) # book['publisher']=tools.strip_string(h.get('publisher',get['pubulisher'])) # book['description']=tools.strip_string(h.get('description',get['description'])) # book['price']=float(h.get('price',get['price'])) # ret=self.bookdao.update_by_bid(book_id,book) # ret always be 0,so can not classify it, it is problem def getbybid(self): book_id=self.get_argument('book_id',None) book_id=tools.strip_string(book_id) if book_id ==None: logging.error("there is no parameter 'book_id'!") resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, para='book_id') self.write(resp) return logging.info("in getbybid method. get book by id: '%s'",str(book_id)) ret=self.bookdao.get_by_bid(book_id) resp =ret if resp == None or len(resp)== 0: logging.error("there is no record!") resp = self.Resp.make_response(code=RespCode.NO_RECORD) self.write(resp) return resp = self.Resp.make_response(code=RespCode.SUCCESS,content=resp) self.write(resp) def getall(self): logging.info("in get all book method :") ret=self.bookdao.get_allbook() resp =ret if resp == None or len(resp)== 0: logging.error("there is something wrong!") resp = self.Resp.make_response(code=RespCode.DB_ERROR) self.write(resp) return resp = self.Resp.make_response(code=RespCode.SUCCESS,content=resp) self.write(resp) def delbybid(self): book_id=self.get_argument('book_id',None) book_id=tools.strip_string(book_id) if book_id == None: logging.error("there is no parameter 'book_id'!") resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, para='book_id') self.write(resp) return logging.info("in delete method! delete book by book_id:'%s'.",str(book_id)) ret=self.bookdao.del_by_bid(bid) ####same problem as before , db.excute() return 0 whether is succuss or fail### ''' if ret == None: resp= self.Resp.make_response(code = RespCode. ''' logging.info("delete book object successed!") resp = self.Resp.make_response(code=RespCode.SUCCESS) self.write(resp)
def initialize(self): self.db = self.application.db self.bookdao = BookDao(self.db) self.readerdao = ReaderDao(self.db) self.borrowdao = BorrowDao(self.db) self.Resp = Resp()
class BorrowHandler(tornado.web.RequestHandler): def initialize(self): self.db = self.application.db self.bookdao = BookDao(self.db) self.readerdao = ReaderDao(self.db) self.borrowdao = BorrowDao(self.db) self.Resp = Resp() def borrow(self): bookid = self.get_argument('bookid', None) readerid = self.get_argument('readerid', None) bookid = tools.strip_string(bookid) readerid = tools.strip_string(readerid) resp = None if bookid == None or readerid == None: logging.error("there is no parament bookid or readerid!") resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, para='id') self.write(resp) return '''check readerid bookid exist and borrow exist ...''' borrow = {} borrow['bookid'] = bookid borrow['readerid'] = readerid nowstamp = int(time.time()) datearray = datetime.datetime.utcfromtimestamp(nowstamp) re = datearray + datetime.timedelta(days=31) restamp = int(time.mktime(re.timetuple())) borrow['borrowdate'] = nowstamp borrow['returndate'] = restamp ret = self.borrowdao.insert_by_dict(borrow) if ret == None: logging.error("error oucurred when insert into table 'borrow'") resp = self.Resp.make_response(code=RespCode.DB_ERROE) self.write(resp) return else: logging.info("borrow successed! The book: %s", str(bookid)) borrow['brw_id'] = ret resp = self.Resp.make_response(code=RespCode.SUCCESS, content=borrow) self.write(resp) def returnbook(self): bookid = self.get_argument('bookid', None) bookid = tools.strip_string(bookid) resp = None if bookid == None: logging.error("there is no parament bookid!") resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, para='id') self.write(resp) return now = datetime.datetime.now() borrow = self.borrowdao.get_by_bookid(bookid) re = datetime.datetime.utcfromtimestamp(borrow['returndate']) delta = re - now day = getattr(delta, 'days') if day < 0: punish = 0.5 * abs(day) logging.error("this book is delay and pay for %s yuan!", str(punish)) resp = self.Resp.make_response(code=RespCode.RETURN, para='punish') self.write(resp) logging.info("delete borrow book %s", str(bookid)) ret = self.borrowdao.del_by_bookid(bookid) logging.info("delete successed!") resp = self.Resp.make_response(code=RespCode.SUCCESS) self.write(resp) def renew(self): bookid = self.get_argument('bookid', None) bookid = self.tools.strip_string(bookid) if bookid == None: logging.error("there is no input!") resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, pare='input') self.write(resp) return logging.info('renew book id:%s', str(bookid)) ret = self.borrowdao.update_renew(bookid) # if ret: # logging.info("renew success! bookid:'%s'",str(bookid) # else: # logging.error("Fail! already done before.") # resp = self.Resp.make_response(code=RespCode.HAS_EXISTED) # self.write(resp) # #add a month re = self.borrowdao.get_by_bookid(bookid) restamp = re['returndate'] datearray = datetime.datetime.utcfromtimestamp(restamp) re1 = datearray + datetime.timedelta(days=31) re1stamp = int(time.mktime(re1.timetuple())) ret = update_returndate(re1stamp, bookid) resp = self.Resp.make_response(code=RespCode.SUCCESS) self.write(resp) def getbybid(self): bid = self.get_argument('book_id', None) bid = tools.strip_string(bid) resp = None if bid != None: logging.info("borrowdetail! bookid:'%s'", str(bid)) ret = self.borrowdao.get_by_bid(bid) resp = ret logging.info('query result: %s', str(resp)) else: logging.error("there is no input!") resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, pare='bid') self.write(resp) return if resp == None or len(resp) == 0: logging.error('there is no record!') resp = self.Resp.make_response(code=RespCode.No_RECORD) self.write(resp) resp = self.Resp.make_response(code=RespCode.SUCCESS, content=resp) self.write(resp) def lost(self): bid = self.get_argument('book_id', None) bid = tools.strip_string(bid) if bid != None: h = self.bookdao.get_by_bid(bid) price = h['price'] logging.info("lost bookid:'%s',pay for $%s", str(bid), str(price)) ret = self.bookdao.del_by_bid(bid) else: logging.error("there is no input!") resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, pare='bid') self.write(resp) return resp = self.Resp.make_response(code=RespCode.SUCCESS) self.write(resp) def clearlist(self): pass
class UserHandler(tornado.web.RequestHandler): def initialize(self): self.db = self.application.db self.bookdao = BookDao(self.db) self.readerdao = ReaderDao(self.db) self.borrowdao = BorrowDao(self.db) self.Resp = Resp() def search(self): se = self.get_argument('book', None) se = tools.strip_string(se) resp = [] if se != None: logging.info("in search method! get search input:'%s'", str(se)) ret = self.bookdao.get_by_title(se) resp.extend(ret) ret = self.bookdao.get_by_auther(se) resp.extend(ret) ret = self.bookdao.get_by_publisher(se) resp.extend(ret) logging.info('query result: %s', str(resp)) else: logging.error("there is no input!") resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, pare='input') self.write(resp) return if resp == None or len(resp) == 0: logging.error('there is no record!') resp = self.Resp.make_response(code=RespCode.No_RECORD) self.write(resp) resp = self.Resp.make_response(code=RespCode.SUCCESS, content=resp) self.write(resp) def getbybid(self): bid = self.get_argument('book_id', None) bid = tools.strip_string(bid) resp = None if bid != None: logging.info("bookdetail! bookid:'%s'", str(bid)) ret = self.bookdao.get_by_bid(bid) resp = ret h = self.borrowdao.if_exist(bid) if h == True: resp['onsheet'] = 'no' else: resp['onsheet'] = 'yes' logging.info('query result: %s', str(resp)) else: logging.error("there is no input!") resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, pare='bid') self.write(resp) return if resp == None or len(resp) == 0: logging.error('there is no record!') resp = self.Resp.make_response(code=RespCode.No_RECORD) self.write(resp) resp = self.Resp.make_response(code=RespCode.SUCCESS, content=resp) self.write(resp) def searchbyrid(self): rid = self.get_argument('readerid', None) rid = tools.strip_string(rid) resp = None if readerid != None: logging.info("in searchbyrid method! get reader by readerid:'%s'", str(rid)) ret = self.readerdao.get_by_rid(rid) resp = ret books = self.borrowdao.get_by_readerid(rid) resp["books"] = books logging.info('query result: %s', str(resp)) if resp == None or len(resp) == 0: logging.error('there is no record!') resp = self.Resp.make_response(code=RespCode.No_RECORD) self.write(resp) resp = self.Resp.make_response(code=RespCode.SUCCESS, content=resp) self.write(resp) def renew(self): bookid = self.get_argument('bookid', None) bookid = self.tools.strip_string(bookid) if bookid == None: logging.error("there is no input!") resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, pare='bid') self.write(resp) return logging.info('renew book id:%s', str(bookid)) ret = self.borrowdao.update_renew(bookid) if ret: #####add a month### re = self.borrowdao.get_by_bookid(bookid) restamp = re['returndate'] datearray = datetime.datetime.utcfromtimestamp(restamp) re1 = datearray + datetime.timedelta(days=31) re1stamp = int(time.mktime(re1.timetuple())) ret = update_returndate(re1stamp, bookid) logging.info("renew success! bookid:'%s'", str(bookid)) else: logging.error("Fail! already done before.") resp = self.Resp.make_response(code=RespCode.HAS_EXISTED) self.write(resp) resp = self.Resp.make_response(code=RespCode.SUCCESS) self.write(resp)
class BookHandler(tornado.web.RequestHandler): def initialize(self): self.db = self.application.db self.bookdao = BookDao(self.db) self.readerdao = ReaderDao(self.db) self.borrowdao = BorrowDao(self.db) self.Resp = Resp() def insert(self): data = self.request.body h = json.loads(data) logging.error("in insert method. receive data:%s", str(data)) book_title = h.get('book-title', None) book_title = tools.strip_string(book_title) if book_title == None: logging.error("there is no parameter 'book_title'!") resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, para='book_title') self.write(resp) return addr = tools.strip_string(h.get(addr, None)) if addr == None: logging.error("there is no parameter 'addr'!") resp = self.Reap.make_response(code=RespCode.NO_PARAMETER, para='addr') self.write(resp) return author = tools.strip_string(h.get('author', '')) publisher = tools.strip_string(h.get('publisher', '')) description = tools.strip_string(h.get('description', '')) price = float(h.get('price', 0)) logging.debug("check parameters complete, ready to save in db") book = {} book['book_title'] = book_title book['author'] = author book['publiser'] = publisher book['description'] = description book['addr'] = addr book['price'] = price ret = self.bookdao.insert_by_dict(book) if ret == None: err_str = "error oucurred when insert into table 'book'" logging.error(err_str) resp = self.Resp.make_response(code=RespCode.DB_ERROR, err_str=err_str) self.write(resp) return logging.info('save book object successed! the book: %s', str(book)) book['book_id'] = ret resp = self.Resp.make_response(code=RespCode.SUCCESS, content=book) self.write(resp) def update(self): data = self.request.body h = json.loads(data) logging.info("in update method! receive data: %s", str(data)) book_id = h.get('book_id', None) book_id = tools.strip_string(book_id) book_title = tools.strip_string(h.get('book_title', None)) if book_id == None: logging.error("there is no parameter 'book_id'!") resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, para='book_id') self.write(resp) return if_exist = self.bookdao.if_exist(book_id) if if_exist == False: logging.error("the book doesn't existed!") resp = self.Resp.make_response(code=RespCode.NO_RECORD, para='book_id') self.write(resp) return ret = self.bookdao.update_by_bid(book_id, h) resp = self.Resp.make_response(code=RespCode.SUCCESS) self.write(resp) # get=self.bookdao.get_by_bid(book_id) # book={} # book['addr']=tools.strip_string(h.get(addr,get['addr'])) # book['author']=tools.strip_string(h.get('author',get['author'])) # book['publisher']=tools.strip_string(h.get('publisher',get['pubulisher'])) # book['description']=tools.strip_string(h.get('description',get['description'])) # book['price']=float(h.get('price',get['price'])) # ret=self.bookdao.update_by_bid(book_id,book) # ret always be 0,so can not classify it, it is problem def getbybid(self): book_id = self.get_argument('book_id', None) book_id = tools.strip_string(book_id) if book_id == None: logging.error("there is no parameter 'book_id'!") resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, para='book_id') self.write(resp) return logging.info("in getbybid method. get book by id: '%s'", str(book_id)) ret = self.bookdao.get_by_bid(book_id) resp = ret if resp == None or len(resp) == 0: logging.error("there is no record!") resp = self.Resp.make_response(code=RespCode.NO_RECORD) self.write(resp) return resp = self.Resp.make_response(code=RespCode.SUCCESS, content=resp) self.write(resp) def getall(self): logging.info("in get all book method :") ret = self.bookdao.get_allbook() resp = ret if resp == None or len(resp) == 0: logging.error("there is something wrong!") resp = self.Resp.make_response(code=RespCode.DB_ERROR) self.write(resp) return resp = self.Resp.make_response(code=RespCode.SUCCESS, content=resp) self.write(resp) def delbybid(self): book_id = self.get_argument('book_id', None) book_id = tools.strip_string(book_id) if book_id == None: logging.error("there is no parameter 'book_id'!") resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, para='book_id') self.write(resp) return logging.info("in delete method! delete book by book_id:'%s'.", str(book_id)) ret = self.bookdao.del_by_bid(bid) ####same problem as before , db.excute() return 0 whether is succuss or fail### ''' if ret == None: resp= self.Resp.make_response(code = RespCode. ''' logging.info("delete book object successed!") resp = self.Resp.make_response(code=RespCode.SUCCESS) self.write(resp)