Beispiel #1
0
def get_lila_response(event, context):
    try:
        parameters = utilities.get_parameters(event, [PARAM_LILA_REQUEST, PARAM_USER_ID], [])
        user_id = parameters[PARAM_USER_ID]
        lila_request = parameters[PARAM_LILA_REQUEST]

        # Appel de l'API ML avec retour de l'intention, du score, de l'espèce.
        parameters = {"q" : lila_request}
        response = requests.get("http://todoo.xyz:5000/", params = parameters).json()[0]

        intention = response["results"][0]
        score = response["results"][1]
        species = response["plant"]

        # Score inférieur à 10%
        if score < 0.1:
            return utilities.generate_http_response({"Response": "Je n'ai pas compris votre question... Pourriez-vous reformuler ?"}), 200

        # Si l'espèce est nulle
        if species is None:
            return utilities.generate_http_response({"Response": "De quelle espèce parlez-vous ?"}), 200

        try:
            response = SWITCHER[intention](species)
        except ClientError as error:
            raise error

        return utilities.generate_http_response({"Response": response}), 200

    except (ClientError, utilities.MissingParameterException) as error:
        return utilities.handle_error(error)
Beispiel #2
0
def get_user_plant_infos(event, context):
    """Returns the infos of a user's plant"""
    try:
        parameters = utilities.get_parameters(event, [PARAM_USER_ID, PARAM_USER_PLANT_ID], [])
        user_id = parameters[PARAM_USER_ID]
        user_plant_id = parameters[PARAM_USER_PLANT_ID]

        item = db_dealer.get_item(db_dealer.USER_PLANT_TABLE, user_plant_id, "userId", user_id, [])

        user_plant ={
            "id": item["id"]["S"],
            "plantId": item["plantId"]["S"],
            "userId": item["userId"]["S"],
            "nickname": "NULL" if "NULL" in item["nickname"] else item["nickname"]["S"],
            "location": "NULL" if "NULL" in item["location"] else item["location"]["S"],
            "temperature": "NULL" if "NULL" in item["temperature"] else item["temperature"]["S"],
            "sunExpo": "NULL" if "NULL" in item["sunExpo"] else item["sunExpo"]["S"],
            "shared": item["shared"]["BOOL"],
            "picUrl": item["picUrl"]["S"],
            "species": item["species"]["S"]
        }
        return utilities.generate_http_response(user_plant), 200

    except ClientError as error:
        return utilities.handle_error(error)
Beispiel #3
0
def get_shared_plants(event, context):
    """Returns shared plants of given plantID"""
    try:
        parameters = utilities.get_parameters(event, [PARAM_PLANT_ID, PARAM_USER_ID], [])
        plant_id = parameters[PARAM_PLANT_ID]
        user_id = parameters[PARAM_USER_ID]
        items = db_dealer.get_all_items(db_dealer.USER_PLANT_TABLE)

        plants = []
        for item in items:
            if item["plantId"]["S"] == plant_id and item["shared"]["BOOL"] and item["userId"]["S"] != user_id:
                plant = {
                    "id": item["id"]["S"],
                    "picUrl": item["picUrl"]["S"],
                    "plantId": item["plantId"]["S"],
                    "userId": item["userId"]["S"],
                    "nickname": "NULL" if "NULL" in item["nickname"] else item["nickname"]["S"],
                    "location": "NULL" if "NULL" in item["location"] else item["location"]["S"],
                    "temperature": "NULL" if "NULL" in item["temperature"] else item["temperature"]["S"],
                    "sunExpo": "NULL" if "NULL" in item["sunExpo"] else item["sunExpo"]["S"],
                    "species": item["species"]["S"]
                }
                plants.append(plant)

        return utilities.generate_http_response(plants), 200

    except (ClientError, utilities.MissingParameterException) as error:
        return utilities.handle_error(error)
Beispiel #4
0
def get_plant_infos(event, context):
    """Gives infos on plant with plantId"""
    try:
        parameters = utilities.get_parameters(event, [PARAM_PLANT_ID], [])
        plant_id = parameters[PARAM_PLANT_ID]

        item = db_dealer.get_item(db_dealer.PLANT_TABLE, plant_id, "", "", [])

        response = {
            "species":
            item["species"]["S"],
            "picUrl":
            item["picUrl"]["S"],
            "description":
            item["description"]["S"],
            "latinName":
            item["latinName"]["S"],
            "family":
            item["family"]["S"],
            "type":
            item["type"]["S"] if "type" in item else "NULL",
            "vegetation":
            item["vegetation"]["S"] if "vegetation" in item else "NULL",
            "height":
            item["height"]["S"] if "height" in item else "NULL",
            "width":
            item["width"]["S"] if "width" in item else "NULL",
            "careLevel":
            item["careLevel"]["S"],
            "waterNeed":
            item["waterNeed"]["S"],
            "growth":
            item["growth"]["S"],
            "coldResistance":
            item["coldResistance"]["S"],
            "soilType":
            item["soilType"]["S"] if "soilType" in item else "NULL",
            "sunNeed":
            item["sunNeed"]["S"],
            "indoorUse":
            item["indoorUse"]["S"] if "indoorUse" in item else "NULL",
            "outdoorUse":
            item["outdoorUse"]["S"] if "outdoorUse" in item else "NULL",
            "plantationMonths":
            item["plantationMonths"]["SS"]
            if "plantationMonths" in item else [],
            "pest":
            item["pest"]["S"] if "pest" in item else "NULL",
            "ecologicalTips":
            item["ecologicalTips"]["S"]
            if "ecologicalTips" in item else "NULL",
            "history":
            item["history"]["S"] if "history" in item else "NULL"
        }

        return utilities.generate_http_response(response), 200

    except (ClientError, utilities.MissingParameterException) as error:
        return utilities.handle_error(error)
Beispiel #5
0
def get_random_infos(event, context):
    """Returns a random info"""
    try:
        lines = open('./common/anecdotes.txt').read().splitlines()
        line = random.choice(lines)
        return utilities.generate_http_response({"Info": line}), 200

    except ClientError as error:
        raise error
Beispiel #6
0
def insert_user_plant(event, context):
    "Insert plant for the specified user into the database"
    try:
        parameters = utilities.get_parameters(
            event, [PARAM_USER_ID, PARAM_SPECIES], [
                PARAM_PLANT_NICKNAME, PARAM_PLANT_LOCATION, PARAM_PLANT_TEMP,
                PARAM_PLANT_SUNEXPO, PARAM_PLANT_SHARED
            ])
        species = parameters[PARAM_SPECIES]

        plant_id, picUrl = supported_plants.get_plant_infos(species)

        if plant_id:

            if not parameters[PARAM_PLANT_NICKNAME]:
                parameters[PARAM_PLANT_NICKNAME] = None

            if not parameters[PARAM_PLANT_LOCATION]:
                parameters[PARAM_PLANT_LOCATION] = None

            if not parameters[PARAM_PLANT_TEMP]:
                parameters[PARAM_PLANT_TEMP] = None

            if not parameters[PARAM_PLANT_SUNEXPO]:
                parameters[PARAM_PLANT_SUNEXPO] = None

            parameters[PARAM_PLANT_SHARED] = (
                parameters[PARAM_PLANT_SHARED].lower() == "true")

            parameters["plantId"] = plant_id
            parameters["picUrl"] = picUrl

            user_plant_id = db_dealer.insert_item(db_dealer.USER_PLANT_TABLE,
                                                  parameters)

            response = {"userPlantId": user_plant_id, "plantId": plant_id}

            return utilities.generate_http_response(response), 200

        return utilities.generate_http_response(
            {"Message": "Web-scrapping nécessaire"}), 501

    except (ClientError, utilities.MissingParameterException) as error:
        return utilities.handle_error(error)
Beispiel #7
0
def get_supported_plants(event, context):
    """Returns the list of the supported plants"""
    try:
        supported_plants = db_dealer.get_all_items(
            db_dealer.SUPPORTED_PLANT_TABLE)
        for plant in supported_plants:
            plant["species"] = plant["species"]["S"]
            plant.pop("websiteUrl")

        return utilities.generate_http_response(supported_plants), 200

    except ClientError as error:
        return utilities.handle_error(error)
Beispiel #8
0
def get_notifications(event, context):
    """Returns notification for reporting"""
    try:
        parameters = utilities.get_parameters(event, [PARAM_USER_ID], [])
        user_id = parameters[PARAM_USER_ID]
        items = db_dealer.list_items(db_dealer.USER_PLANT_TABLE, "userId", user_id)
        notifs = []
        for item in items:
            report_item = db_dealer.get_item(db_dealer.REPORTING_TABLE, item["userPlantId"]["S"], "date", date.today(), [])
            if not report_item:
                notifs.append(item["species"]["S"])
        return utilities.generate_http_response(notifs), 200

    except (ClientError, utilities.MissingParameterException) as error:
        return utilities.handle_error(error)
Beispiel #9
0
def delete_user_plant(event, context):
    """Delete plant at the specified user plant ID"""

    try:
        parameters = utilities.get_parameters(
            event, [PARAM_USER_ID, PARAM_USER_PLANT_ID], [])
        user_id = parameters[PARAM_USER_ID]
        user_plant_id = parameters[PARAM_USER_PLANT_ID]

        db_dealer.delete_item(db_dealer.USER_PLANT_TABLE, user_plant_id,
                              "userId", user_id)

        return utilities.generate_http_response(
            {"Message": "Successfully deleted"}), 200

    except (ClientError, utilities.MissingParameterException) as error:
        return utilities.handle_error(error)
Beispiel #10
0
def update_plant(event, context):
    """Updates the given user plant"""
    try:
        parameters = utilities.get_parameters(event, [
            PARAM_USER_PLANT_ID, PARAM_USER_ID, PARAM_PLANT_NICKNAME,
            PARAM_PLANT_LOCATION, PARAM_PLANT_TEMP, PARAM_PLANT_SUNEXPO,
            PARAM_PLANT_SHARED
        ], [])

        parameters[PARAM_PLANT_SHARED] = (
            parameters[PARAM_PLANT_SHARED].lower() == "true")

        db_dealer.update_item(db_dealer.USER_PLANT_TABLE, parameters)

        return utilities.generate_http_response({"Message":
                                                 "Update success"}), 200

    except (ClientError, utilities.MissingParameterException) as error:
        return utilities.handle_error(error)
Beispiel #11
0
def get_list_plants_user(event, context):
    "Get list of plants for the specified user"
    try:
        parameters = utilities.get_parameters(event, [PARAM_USER_ID], [])
        user_id = parameters[PARAM_USER_ID]
        items = db_dealer.list_items(db_dealer.USER_PLANT_TABLE, "userId",
                                     user_id)

        plants = []
        for item in items:
            plant = {
                "id":
                item["id"]["S"],
                "plantId":
                item["plantId"]["S"],
                "userId":
                item["userId"]["S"],
                "nickname":
                "NULL"
                if "NULL" in item["nickname"] else item["nickname"]["S"],
                "location":
                "NULL"
                if "NULL" in item["location"] else item["location"]["S"],
                "temperature":
                "NULL"
                if "NULL" in item["temperature"] else item["temperature"]["S"],
                "sunExpo":
                "NULL" if "NULL" in item["sunExpo"] else item["sunExpo"]["S"],
                "shared":
                item["shared"]["BOOL"],
                "picUrl":
                item["picUrl"]["S"],
                "species":
                item["species"]["S"]
            }
            plants.append(plant)

        return utilities.generate_http_response(plants), 200

    except (ClientError, utilities.MissingParameterException) as error:
        return utilities.handle_error(error)
Beispiel #12
0
def get_reportings(event, context):
    """Returns the 6 last reportings for a given userPlant"""
    try:
        parameters = utilities.get_parameters(event, [PARAM_USER_PLANT_ID], [])
        user_plant_id = parameters[PARAM_USER_PLANT_ID]

        items = db_dealer.list_items(db_dealer.REPORTING_TABLE, "userPlantId",
                                     user_plant_id)
        items.sort(
            reverse=True,
            key=lambda dict: datetime.strptime(dict["date"]["S"], "%Y-%m-%d"))

        if len(items) > 6:
            items = items[:6]

        reports = []
        for item in items[::-1]:
            report = {
                "date":
                item["date"]["S"],
                "water":
                item["water"]["BOOL"],
                "prune":
                item["prune"]["BOOL"],
                "repotting":
                item["repotting"]["BOOL"],
                "harvest":
                item["harvest"]["BOOL"],
                "comment":
                "NULL" if "NULL" in item["comment"] else item["comment"]["S"]
            }
            reports.append(report)

        return utilities.generate_http_response(reports), 200

    except (ClientError, utilities.MissingParameterException) as error:
        return utilities.handle_error(error)
Beispiel #13
0
def add_reporting(event, context):
    """Adds a reporting in the database"""
    try:
        parameters = utilities.get_parameters(event, [PARAM_USER_PLANT_ID], [
            PARAM_WATER, PARAM_PRUNE, PARAM_REPOTTING, PARAM_HARVEST,
            PARAM_COMMENT
        ])
        parameters["date"] = str(date.today())

        parameters[PARAM_WATER] = (parameters[PARAM_WATER].lower() == "true")
        parameters[PARAM_PRUNE] = (parameters[PARAM_PRUNE].lower() == "true")
        parameters[PARAM_REPOTTING] = (
            parameters[PARAM_REPOTTING].lower() == "true")
        parameters[PARAM_HARVEST] = (
            parameters[PARAM_HARVEST].lower() == "true")

        if not parameters[PARAM_COMMENT]:
            parameters[PARAM_COMMENT] = None

        db_dealer.insert_item(db_dealer.REPORTING_TABLE, parameters)
        return utilities.generate_http_response({"Message": "Success"}), 200

    except (ClientError, utilities.MissingParameterException) as error:
        return utilities.handle_error(error)