Exemple #1
0
 def __init__(self, config):
     self.log = logging.getLogger("RESTfulWebServiceResponseHandler")
     self.config = config
     self.messageBoardController = MessageBoardController(config)
     self.deptInfoController = DeptInfoController(config)
     self.buildingController = BuildingController(config)
Exemple #2
0
class RESTfulWebServiceResponseHandler(object):
    def __init__(self, config):
        self.log = logging.getLogger("RESTfulWebServiceResponseHandler")
        self.config = config
        self.messageBoardController = MessageBoardController(config)
        self.deptInfoController = DeptInfoController(config)
        self.buildingController = BuildingController(config)

    def index(self):
        self.log.info("index start")
        f = open("index.html", "r")
        data = f.read()

        response = Response(response=data, status="200 OK", mimetype="text/html", content_type="text/html")
        f.close()
        return response

    def calendar(self):
        self.log.info("calendar")
        f = open("calendar.html", "r")
        data = f.read()
        response = Response(response=data, status="200 OK", content_type="text/html")
        f.close()
        return response

    def getDept(self):
        self.log.info("get dept start")
        result = self.deptInfoController.getDept()
        response = Response(
            response=json.dumps(result), status="200 OK", mimetype="application/json", content_type="text/plain"
        )
        return response

    def getMessageByMsgId(self, msgId):
        try:
            result = self.messageBoardController.getMessageByMsgId(msgId)
        except Exception as e:
            self.log.error("error: %s", e.args)
            return e.args
        response = Response(
            response=json.dumps(result), status="200 OK", mimetype="application/json", content_type="text/plain"
        )
        return response

    def getMessage(self, start_date, end_date, untid="", deptid="", base=0, offset=100, func=None):
        try:
            start_date = dateStringToDate(start_date)
            end_date = dateStringToDate(end_date)
            data = func(start_date, end_date, untid, deptid)
            if base > len(data):
                base = len(data) - 1
            if base + offset >= len(data):
                data = data[base:]
                offset = len(data)
            else:
                data = data[base : base + offset]
        except Exception as e:
            self.log.error("error: %s", e.args)
            return e.args
        self.log.info("base: %d offset: %d", base, offset)

        response = Response(
            response=json.dumps(data, sort_keys=True),
            status="200 OK",
            mimetype="application/json",
            content_type="text/plain",
        )
        return response

    def getMessageCount(self, start_date, end_date, untid="", deptid="", func=None):
        try:
            start_date = dateStringToDate(start_date)
            end_date = dateStringToDate(end_date)
            result = func(start_date, end_date, untid, deptid)
        except Exception as e:
            self.log.error("error: %s", e.args)
            return e.args
        response = Response(
            response=json.dumps(result, sort_keys=True),
            status="200 OK",
            mimetype="application/json",
            content_type="text/plain",
        )
        return response

    def getMessageOfNews(self, start_date, end_date, untid="", deptid="", base=0, offset=100):
        self.log.info(
            "get message with news: %s ~ %s and untid: %s, deptid: %s and base:" "%d offset:%d",
            start_date,
            end_date,
            untid,
            deptid,
            base,
            offset,
        )
        response = self.getMessage(
            start_date, end_date, untid, deptid, base, offset, self.messageBoardController.getMessageOfNews
        )
        return response

    def getMessageCountOfNews(self, start_date, end_date, untid="", deptid=""):
        self.log.info("get message with news: %s ~ %s and untid: %s, deptid: %s", start_date, end_date, untid, deptid)
        response = self.getMessageCount(
            start_date, end_date, untid, deptid, self.messageBoardController.getMessageCountOfNews
        )
        return response

    def getMessageOfActivity(self, start_date, end_date, untid="", deptid="", base=0, offset=100):
        self.log.info(
            "get message with activity: %s ~ %s and untid: %s, deptid: %s and " "base:%d offset:%d",
            start_date,
            end_date,
            untid,
            deptid,
            base,
            offset,
        )
        response = self.getMessage(
            start_date, end_date, untid, deptid, base, offset, self.messageBoardController.getMessageOfActivity
        )
        return response

    def getMessageCountOfActivity(self, start_date, end_date, untid="", deptid=""):
        self.log.info(
            "get message with activity: %s ~ %s and untid: %s, deptid: %s", start_date, end_date, untid, deptid
        )
        response = self.getMessageCount(
            start_date, end_date, untid, deptid, self.messageBoardController.getMessageCountOfActivity
        )
        return response

    def getMessageOfDiscussion(self, start_date, end_date, untid="", deptid="", base=0, offset=100):
        self.log.info(
            "get message with discussion: %s ~ %s and untid: %s, deptid: %s " "and base:%d offset:%d",
            start_date,
            end_date,
            untid,
            deptid,
            base,
            offset,
        )
        response = self.getMessage(
            start_date, end_date, untid, deptid, base, offset, self.messageBoardController.getMessageOfDiscussion
        )
        return response

    def getMessageCountOfDiscussion(self, start_date, end_date, untid="", deptid=""):
        self.log.info(
            "get message with discussion: %s ~ %s and untid: %s, deptid: %s ", start_date, end_date, untid, deptid
        )
        response = self.getMessageCount(
            start_date, end_date, untid, deptid, self.messageBoardController.getMessageCountOfDiscussion
        )
        return response

    def getMessageOfLibrary(self, start_date, end_date, untid="LB", deptid="", base=0, offset=100):
        self.log.info(
            "get message with Library: %s ~ %s and untid: %s, deptid: %s " "and base:%d offset:%d",
            start_date,
            end_date,
            untid,
            deptid,
            base,
            offset,
        )
        response = self.getMessage(
            start_date, end_date, "LB", "", base, offset, self.messageBoardController.getMessageOfLibrary
        )
        return response

    def getMessageCountOfLibrary(self, start_date, end_date, untid="LB", deptid=""):
        self.log.info(
            "get message with Library: %s ~ %s and untid: %s, deptid: %s ", start_date, end_date, untid, deptid
        )
        response = self.getMessageCount(
            start_date, end_date, "LB", "", self.messageBoardController.getMessageCountOfLibrary
        )
        return response

    def getBuilding(self):
        self.log.info("get building")
        result = self.buildingController.getBuilding()
        response = Response(
            response=json.dumps(result), status="200 OK", mimetype="application/json", content_type="text/plain"
        )
        return response