예제 #1
0
    def getBuildingsByKeyword(self, offset, limit, keyword):
        """
        Returns a list of buildings taht match a given searchstring
        Uses :func:`~app.DAOs.BuildingDAO.BuildingDAO.searchBuildingsByKeyword`

        :param keyword: The keyword to search for.
        :type keyword: str
        :param offset: Number of results to skip from top of list.
        :type offset: int
        :param limit: Number of results to return. Default = 20.
        :type limit: int
        :return JSON: A list of buildings that match the given keyword
        """

        dao = BuildingDAO()
        keyword = keyword
        result = []
        alphanumeric_filter = filter(str.isalnum, keyword)
        keyword = "".join(alphanumeric_filter)
        print(keyword)
        response = dao.searchBuildingsByKeyword(keyword=keyword,
                                                offset=offset,
                                                limit=limit)

        for building in response:
            result.append(_buildBuildingResponse(building_tuple=building))

        return jsonify({"buildings": result})
예제 #2
0
    def addFullBuilding(self, json, uid):
        """
        Handler method to verify that all necessary JSON keys are
        present before creating a new building.
        Uses :func:`~app.DAOs.BuildingDAO.BuildingDAO.addFullBuilding`

        :param json: Contains the necessary keys to create a building from UPRM Portal data
            ["edificioid", "nomoficial", "blddenom", "codigoold", "bldtype", "attributes"]
        :type json: JSON
        :param uid: User ID
        :type uid: int
        :return: JSON
        """
        if not isinstance(uid, int) or uid < 0:
            return jsonify(Error="Invalid uid: " + str(uid))
        for key in ADD_BUILDING_KEYS:
            if key not in json:
                return jsonify(Error='Missing key in JSON: ' + str(key)), 404
        try:
            building_results = BuildingDAO().addFullBuilding(
                building_json=json, uid=uid)
        except ValueError as e:
            return jsonify(Error=str(e)), 400
        except KeyError as e:
            return jsonify(Error=str(e)), 400
        return jsonify(Result=str(building_results)), 201
예제 #3
0
    def getAllBuildingsSegmented(self, offset, limit=20):
        """
        Return all Building entries in the database, segmented.
        Uses :func:`~app.DAOs.BuildingDAO.BuildingDAO.getAllBuildingsSegmented` as well as
        :func:`~app.handlers.BuildingHandler._buildBuildingResponse`

        :param offset: Number of results to skip from top of list.
        :type offset: int
        :param limit: Number of results to return. Default = 20.
        :type limit: int
        :return JSON: containing all tags. Error JSON otherwise.
        """
        dao = BuildingDAO()
        buildings = dao.getAllBuildingsSegmented(offset=offset, limit=limit)
        result = []
        for row in buildings:
            result.append(_buildBuildingResponse(row))
        return jsonify({"buildings": result})
예제 #4
0
 def getCoreBuildingByID(self, bid, no_json=False):
     """
     Return the building entry belonging to the specified bid.
     Uses :func:`~app.DAOs.BuildingDAO.BuildingDAO.getBuildingByID` as well as
     :func:`~app.handlers.BuildingHandler._buildCoreBuildingResponse`
    
     :param bid: building ID.
     :type bid: int
     :param no_json: states if the response should be returned as JSON or not. Default=False
     :type no_json: bool
     :return JSON: containing room information. Error JSON otherwise.
     """
     dao = BuildingDAO()
     building = dao.getBuildingByID(bid=bid)
     if not building:
         return jsonify(Error='building does not exist: bid=' +
                        str(bid)), 404
     else:
         response = _buildCoreBuildingResponse(building_tuple=building)
         if no_json:
             return response
         return jsonify(response)
예제 #5
0
    def getAllBuildings(self, no_json=False):
        """
        Return all Building entries in the database.
        Uses :func:`~app.DAOs.BuildingDAO.BuildingDAO.getAllBuildings` as well as
        :func:`~app.handlers.BuildingHandler._buildBuildingResponse`

        :param  no_json: states if the response should be returned as JSON or not. Default=False
        :type no_json: bool
        :return JSON: containing all tags. Error JSON otherwise.
        """
        dao = BuildingDAO()
        buildings = dao.getAllBuildings()
        if not buildings:
            return jsonify(
                Error='Could not find any buildings in system.'), 404
        else:
            building_list = []
            for row in buildings:
                building_list.append(
                    _buildBuildingResponse(building_tuple=row))
            response = {"buildings": building_list}
            if no_json:
                return response
            return jsonify(response)
예제 #6
0
def _getDistinctFloorNumbersByBuildingID(bid):
    """
    Private Method to build building dictionary to be JSONified.

     Uses :func:`~app.DAOs.BuildingDAO.BuildingDAO.getDistinctFloorNumbersByBuildingID`

    :param building_tuple: response tuple from SQL query
    :returns Dict: Building information
    """
    floors = BuildingDAO().getDistinctFloorNumbersByBuildingID(bid=bid)
    floor_array = []
    if not floors:
        pass
    else:
        for floor in floors:
            floor_array.append(floor[0])
    return floor_array