示例#1
0
 def initialize(self):
     self.db = self.application.db
     self.bookdao = BookDao(self.db)
     self.Resp = Resp()
示例#2
0
 def initialize(self):
     self.db = self.application.db
     self.readerdao = ReaderDao(self.db)
     self.Resp = Resp()
示例#3
0
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.host_dao = HostDao(self.db)
     self.Resp = Resp()
     self.HostType = HostType()
示例#5
0
class ReaderHandler(tornado.web.RequestHandler):
    def initialize(self):
        self.db = self.application.db
        self.readerdao = ReaderDao(self.db)
        self.Resp = Resp()

    def post(self):
        data = self.request.body
        h = json.loads(data)

        logging.info("in post method. receive data: %s", str(data))

        reader_name = h.get('reader_name', None)
        reader_name = tools.strip_string(reader_name)
        if reader_name == None:
            logging.error("there is no parameter 'reader_name'!")
            resp = self.Resp.make_response(code=RespCode.NO_PARAMETER,
                                           para='reader_name')
            self.write(resp)
            return

        sex = tools.to_int(h.get('sex', None))
        age = tools.to_int(h.get('age', None))
        email = tools.strip_string(h.get('email', None))

        logging.debug("check parameters complete, ready to save in db")

        reader = {}
        reader['reader_name'] = reader_name
        reader['sex'] = sex
        reader['age'] = age
        reader['email'] = email

        ret = self.readerdao.insert_by_dict(reader)
        if ret == None:
            err_str = "error oucurred when insert into table 'reader'"
            logging.error(err_str)
            resp = self.Resp.make_response(code=RespCode.DB_ERROR,
                                           err_str=err_str)
            self.write(resp)
            return

        logging.info('save reader object successed! the reader: %s',
                     str(reader))

        reader['reader_id'] = ret
        resp = self.Resp.make_response(code=RespCode.SUCCESS, content=reader)
        self.write(resp)

    def delete(self):
        reader_id = self.get_argument('reader_id', None)
        reader_id = tools.strip_string(reader_id)

        if reader_id == None:
            logging.error("there is no parameter 'reader_id'!")
            resp = self.Resp.make_response(code=RespCode.NO_PARAMETER,
                                           para='reader_id')
            self.write(resp)
            return

        logging.info("in delete method! delete reader by reader_id:'%s'.",
                     str(reader_id))
        ret = self.readerdao.del_by_rid(reader_id)

        if ret == None:
            err_str = "error oucurred when delete reader by id: %s" % reader_id
            logging.error(err_str)
            resp = self.Resp.make_response(code=RespCode.INVALID_PARAMETER,
                                           err_str=err_str)
            self.write(resp)
            return

        logging.info("delete reader object successed!")

        resp = self.Resp.make_response(code=RespCode.SUCCESS,
                                       para='delete reader')
        self.write(resp)

    def get(self):
        rid = self.get_argument('reader_id', None)
        rid = tools.to_int(rid)

        if rid == None:
            logging.error("there is no reader_id!")
            resp = self.Resp.make_response(code=RespCode.NO_PARAMETER)
            self.write(resp)
            return

        logging.info("reader detail! readerid:'%s'", str(rid))
        ret = self.readerdao.get_by_rid(rid)
        resp = ret
        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 put(self):
        data = self.request.body
        h = json.loads(data)

        logging.info("in update method! receive data: %s", str(data))

        reader_id = h.get('reader_id', None)
        reader_id = tools.strip_string(reader_id)

        if reader_id == None:
            logging.error("there is no parameter 'reader_id'!")
            resp = self.Resp.make_response(code=RespCode.NO_PARAMETER,
                                           para='reader_id')
            self.write(resp)
            return

        if_exist = self.readerdao.if_exist(reader_id)
        if if_exist == False:
            logging.error("the book doesn't existed!")
            resp = self.Resp.make_response(code=RespCode.INVALID_PARAMETER,
                                           para='READER_id')
            self.write(resp)
            return

        ret = self.readerdao.update_by_rid(reader_id, h)

        reader = self.readerdao.get_by_rid(reader_id)
        logging.info('update reader object successed! the reader: %s',
                     str(reader))

        resp = self.Resp.make_response(code=RespCode.SUCCESS,
                                       content=reader,
                                       pare="update reader")
        self.write(resp)
示例#6
0
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 HostHandler(tornado.web.RequestHandler):
    def initialize(self):
        self.db = self.application.db
        self.host_dao = HostDao(self.db)
        self.Resp = Resp()
        self.HostType = HostType()

    def get(self):
        """
            The GET method is used to get one Host by hostname or host list
        """
        hostname = self.get_argument("hostname", None)
        host_id = self.get_argument("host_id", None)
        hostname = tools.strip_string(hostname)
        host_id = tools.to_int(host_id)

        resp = None
        if hostname is not None:
            logging.info("In GET method! Get host by hostname: '%s'", str(hostname))
            host = self.host_dao.get_by_hostname(hostname)
            resp = host
        elif host_id is not None:
            logging.info("In GET method! Get host by host_id: '%s'", str(host_id))
            host = self.host_dao.get_by_id(host_id)
            resp = host
        else:
            logging.info("In GET method! Get all hosts.")
            hosts = self.host_dao.get_all()
            resp = hosts

        logging.info("Query result: %s", str(resp))

        if resp is 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 post(self):
        """
            The POST method is used to inert one host into 'Host' Table
        """
        data = self.request.body
        h = json.loads(data)

        logging.info("In POST method. Receive data: %s", str(data))

        # start check parameters
        hostname = h.get("hostname", None)
        hostname = tools.strip_string(hostname)
        if hostname is None:
            logging.error("There is no parameter 'hostname'! ")
            resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, para="hostname")
            self.write(resp)
            return

        ip = h.get("ip", None)
        ip = tools.strip_string(ip)
        if ip is None:
            logging.error("There is no parameter 'ip'! ")
            resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, para="ip")
            self.write(resp)
            return

        # check if exist
        if_exist = self.host_dao.if_exist(hostname, ip)
        if if_exist is True:
            logging.error("The host has existed!")
            resp = self.Resp.make_response(code=RespCode.HAS_EXISTED, para="Host")
            self.write(resp)
            return

        host_type = tools.to_int(h.get("host_type", 0))
        if host_type is None or self.HostType.check(host_type) != True:
            logging.error("The value of parameter 'host_type' is invalid!")
            resp = self.Resp.make_response(code=RespCode.INVALID_PARAMETER, para="host_type")
            self.write(resp)
            return

        cpu_count = tools.to_int(h.get("cpu_count", 8))
        memory = tools.to_int(h.get("memory", 8))
        os = h.get("os", "")
        comment = h.get("comment", "")

        worker_num = tools.to_int(h.get("worker_num", 0))
        if worker_num is None or worker_num == 0:
            worker_num = Config.default_worker_num

        logging.debug("Check parameters complete, ready to save in db")

        # save in db
        host = dict()
        host["hostname"] = hostname
        host["host_type"] = host_type
        host["ip"] = ip
        host["cpu_count"] = cpu_count
        host["memory"] = memory
        host["os"] = os
        host["comment"] = comment
        host["worker_num"] = worker_num
        create_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
        host["create_time"] = create_time

        ret = self.host_dao.insert_by_dict(host)
        if ret is None:
            err_str = "Error oucurred when insert into table 'Host'"
            logging.error(err_str)
            resp = self.Resp.make_response(code=RespCode.DB_ERROR, err_str=err_str)
            self.write(resp)
            return

        logging.info("Save host object successed! The host: %s", str(host))

        host["host_id"] = ret
        resp = self.Resp.make_response(code=RespCode.SUCCESS, content=host)
        self.write(resp)

    def put(self):
        """
            The put method is used to modify the Host's worker_num
        """
        data = self.request.body
        h = json.loads(data)

        logging.info("In PUT method! Receive data: %s", str(data))

        # start check parameters
        hostname = h.get("hostname", None)
        host_id = h.get("host_id", None)
        hostname = tools.strip_string(hostname)
        host_id = tools.to_int(host_id)

        worker_num = tools.to_int(h.get("worker_num", 0))
        if worker_num is None or worker_num == 0:
            logging.error("There is no parameter 'worker_num'! ")
            resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, para="worker_num")
            self.write(resp)
            return

        if hostname is not None:
            ret = self.host_dao.update_worker_num_by_hostname(hostname, worker_num)
            err_str = "Error oucurred when Update host by hostname '%s', worker_num: %s" % (hostname, str(worker_num))
            host = self.host_dao.get_by_hostname(hostname)
        elif host_id is not None:
            ret = self.host_dao.update_worker_num_by_id(host_id, worker_num)
            err_str = "Error oucurred when Update host by host_id '%s', worker_num: %s" % (
                str(host_id),
                str(worker_num),
            )
            host = self.host_dao.get_by_id(host_id)
        else:
            logging.error("There is no parameter 'hostname' or 'host_id'! ")
            resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, para="hostname or host_id")
            self.write(resp)
            return

        if ret is None:
            logging.error(err_str)
            resp = self.Resp.make_response(code=RespCode.DB_ERROR, err_str=err_str)
            self.write(resp)
            return

        if host is None:
            logging.info("No record!")
            resp = self.Resp.make_response(code=RespCode.NO_RECORD)
            self.write(resp)
            return

        logging.info("Update host object successed!")
        resp = self.Resp.make_response(code=RespCode.SUCCESS, content=host)
        self.write(resp)

    def delete(self):
        """
            The DELETE method is used to delete Host object
        """
        hostname = self.get_argument("hostname", None)
        host_id = self.get_argument("host_id", None)
        hostname = tools.strip_string(hostname)
        host_id = tools.to_int(host_id)

        if hostname is not None:
            logging.info("In DELETE method! Delete host by hostname: '%s'.", str(hostname))
            ret = self.host_dao.del_by_hostname(hostname)
            err_str = "Error oucurred when Delete host by hostname: '%s'" % hostname
        elif host_id is not None:
            logging.info("In DELETE method! Delete host by host_id: '%s'.", str(host_id))
            ret = self.host_dao.del_by_id(host_id)
            err_str = "Error oucurred when Delete host by host_id: '%s'" % str(host_id)
        else:
            logging.error("There is no parameter 'hostname' or 'host_id'! ")
            resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, para="hostname or host_id")
            self.write(resp)
            return

        if ret is 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 host object successed!")

        resp = self.Resp.make_response(code=RespCode.SUCCESS)
        self.write(resp)
示例#8
0
class ReaderHandler(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)) 

		reader_name = h.get('reader_name',None)
		reader_name = tools.strip_string(reader_name)
		if reader_name == None:
			logging.error("there is no parameter 'reader_name'!")
			resp = self.Resp.make_response(code=RespCode.NO_PARAMETER,para='reader_name')
			self.write(resp)
			return

		sex=tools.to_int(h.get('sex',None))
		age=tools.to_int(h.get('age',None))
		email=tools.strip_string(h.get('email',''))

		logging.debug("check parameters complete, ready to save in db")

		reader={}
		reader['reader_name']=reader_name
		reader['sex']=sex
		reader['age']=age
		reader['email']=email

		ret = self.readerdao.insert_by_dict(reader)
#########insert fail return None?###############
		if ret == None:
			err_str="error oucurred when insert into table 'reader'"
			logging.error(err_str)
			resp = self.Resp.make_response(code = RespCode.DB_ERROR,err_str=err_str)
			self.write(resp)
			return

		logging.info('save reader object successed! the reader: %s', str(reader))

		reader['reader_id']=ret
		resp = self.Resp.make_response(code=RespCode.SUCCESS,content=reader)
		self.write(resp)

	def delbyrid(self):
		reader_id=self.get_argument('reader_id',None)
		reader_id=tools.strip_string(reader_id)
		
		if reader_id == None:
			logging.error("there is no parameter 'reader_id'!")
			resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, para='reader_id')
			self.write(resp)
			return
		
		logging.info("in delete method! delete reader by reader_id:'%s'.",str(reader_id))
		ret=self.readerdao.del_by_rid(bid)

		'''
		if ret == None:
		resp= self.Resp.make_response(code = RespCode.

		'''
		logging.info("delete reader object successed!")

		resp = self.Resp.make_response(code=RespCode.SUCCESS)
		self.write(resp)

	def update(self):
		data=self.request.body
		h = json.loads(data)

		logging.info("in update method! receive data: %s", str(data))

		reader_id=h.get('reader_id',None)
		reader_id=tools.strip_string(reader_id)

		reader_name=tools.strip_string(h.get('reader_name',None))
		if reader_id ==None:
			logging.error("there is no parameter 'reader_id'!")
			resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, para='reader_id')
			self.write(resp)
			return

		if_exist=self.readerdao.if_exist(reader_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.readerdao.update_by_rid(reader_id,h)


#		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 return
		reader=self.readerdao.get_by_rid(reader_id)
		logging.info('update reader object successed! the reader: %s', str(reader))

		resp = self.Resp.make_response(code=RespCode.SUCCESS,content=reader)
		self.write(resp)

	def getbyrid(self):
		rid=self.get_argument('readerid',None)
		rid=tools.strip_string(rid)

		resp=None
		if rid != None:
			logging.info("reader detail! readerid:'%s'",str(rid))
			ret=self.readerdao.get_by_rid(rid)
			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= '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)
示例#9
0
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
示例#10
0
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)
示例#11
0
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)
示例#12
0
 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()
示例#13
0
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
示例#14
0
class ReaderHandler(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))

        reader_name = h.get('reader_name', None)
        reader_name = tools.strip_string(reader_name)
        if reader_name == None:
            logging.error("there is no parameter 'reader_name'!")
            resp = self.Resp.make_response(code=RespCode.NO_PARAMETER,
                                           para='reader_name')
            self.write(resp)
            return

        sex = tools.to_int(h.get('sex', None))
        age = tools.to_int(h.get('age', None))
        email = tools.strip_string(h.get('email', ''))

        logging.debug("check parameters complete, ready to save in db")

        reader = {}
        reader['reader_name'] = reader_name
        reader['sex'] = sex
        reader['age'] = age
        reader['email'] = email

        ret = self.readerdao.insert_by_dict(reader)
        #########insert fail return None?###############
        if ret == None:
            err_str = "error oucurred when insert into table 'reader'"
            logging.error(err_str)
            resp = self.Resp.make_response(code=RespCode.DB_ERROR,
                                           err_str=err_str)
            self.write(resp)
            return

        logging.info('save reader object successed! the reader: %s',
                     str(reader))

        reader['reader_id'] = ret
        resp = self.Resp.make_response(code=RespCode.SUCCESS, content=reader)
        self.write(resp)

    def delbyrid(self):
        reader_id = self.get_argument('reader_id', None)
        reader_id = tools.strip_string(reader_id)

        if reader_id == None:
            logging.error("there is no parameter 'reader_id'!")
            resp = self.Resp.make_response(code=RespCode.NO_PARAMETER,
                                           para='reader_id')
            self.write(resp)
            return

        logging.info("in delete method! delete reader by reader_id:'%s'.",
                     str(reader_id))
        ret = self.readerdao.del_by_rid(bid)
        '''
		if ret == None:
		resp= self.Resp.make_response(code = RespCode.

		'''
        logging.info("delete reader object successed!")

        resp = self.Resp.make_response(code=RespCode.SUCCESS)
        self.write(resp)

    def update(self):
        data = self.request.body
        h = json.loads(data)

        logging.info("in update method! receive data: %s", str(data))

        reader_id = h.get('reader_id', None)
        reader_id = tools.strip_string(reader_id)

        reader_name = tools.strip_string(h.get('reader_name', None))
        if reader_id == None:
            logging.error("there is no parameter 'reader_id'!")
            resp = self.Resp.make_response(code=RespCode.NO_PARAMETER,
                                           para='reader_id')
            self.write(resp)
            return

        if_exist = self.readerdao.if_exist(reader_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.readerdao.update_by_rid(reader_id, h)

        #		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 return
        reader = self.readerdao.get_by_rid(reader_id)
        logging.info('update reader object successed! the reader: %s',
                     str(reader))

        resp = self.Resp.make_response(code=RespCode.SUCCESS, content=reader)
        self.write(resp)

    def getbyrid(self):
        rid = self.get_argument('readerid', None)
        rid = tools.strip_string(rid)

        resp = None
        if rid != None:
            logging.info("reader detail! readerid:'%s'", str(rid))
            ret = self.readerdao.get_by_rid(rid)
            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='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)
示例#15
0
class BorrowHandler(tornado.web.RequestHandler):
    def initialize(self):
        self.db = self.application.db
        self.borrowdao = BorrowDao(self.db)
        self.Resp = Resp()

    def get(self):
        bookid = self.get_argument('bookid', None)
        readerid = self.get_argument('readerid', None)
        bid = tools.strip_string(bookid)
        rid = tools.strip_string(readerid)

        resp = None
        if bid == None and rid == 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

        if bid != None:
            logging.info("borrow detail! bookid:'%s'", str(bid))
            resp = self.borrowdao.get_by_bookid(bid)
            logging.info('query result: %s', str(resp))
        elif rid != None:
            logging.info("borrow detail! readerid:'%s'", str(rid))
            resp = self.borrowdao.get_by_readerid(rid)
            logging.info('query result: %s', str(resp))
        else:
            return

#		bo = tools.to_int(borrow['borrowdate'])
#		re = tools.to_int(borrow['returndate'])
#		bo=datetime.datetime.utcfromstamp(bo)
#		re=	datetime.datetime.utcfromstamp(re)
#		borrow['borrowdate'] = str(bo)
#		borrow['returndate'] = str(re)

        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)

#		_method = self.get_argument('_method', None)
#		if method == 'lost':
#			self.lost()
#
#	def clear(self):
#		pass
#

    def post(self):

        data = self.request.body
        h = json.loads(data)

        logging.info("in  post method. receive data:%s", str(data))
        bookid = h.get('bookid', None)
        bookid = tools.strip_string(bookid)
        readerid = h.get('readerid', None)
        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

        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_ERROR)
            self.write(resp)
            return

        logging.info("borrow successed! The book: %s", str(bookid))
        borrow['brw_id'] = ret
        resp = self.Resp.make_response(code=RespCode.SUCCESS,
                                       content=borrow,
                                       para="borrow")
        self.write(resp)

    def delete(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
        #		days=getattr(delta,'days')
        #		if days < 0:
        #			logging.error("this book is delay for %s days",str(abs(days)))
        #			resp = self.Resp.make_response(code=Resp.DELAY,para=days)
        #			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, para="return")
        self.write(resp)

    def put(self):
        data = self.request.body
        h = json.loads(data)

        bookid = h.get('bookid', None)

        if bookid == None:
            logging.error("there is no input!")
            resp = self.Resp.make_response(code=RespCode.NO_PARAMETER,
                                           pare='bookid')
            self.write(resp)

            return
#sql = "SELECT renew FROM borrow WHERE bookid='%s'"%str(bookid)
#		ret=self.db.get(sql)
#		if ret['renew'] == 0:

#		logging.info('renew book id:%s',str(bookid))
#		ret = self.borrowdao.update_renew(bookid)

#####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 = self.borrowdao.update_returndate(re1stamp, bookid)

        logging.info("renew success! bookid:'%s'", str(bookid))
        resp = self.Resp.make_response(code=RespCode.SUCCESS, para="renew")
        self.write(resp)
示例#16
0
	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()
示例#17
0
 def initialize(self):
     self.db = self.application.db
     self.borrowdao = BorrowDao(self.db)
     self.Resp = Resp()
示例#18
0
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)