def get(cls, id):
        """Gets one activity from database based on given id.
            Fails if there is no activity with that id.

        Args:
            id (int): The identifier of the maintenance activity to be retrieved.

        Returns:
            dict of (str, any): Jsonified activity or error message.
        """
        try:
            activity = MaintenanceActivityModel.find_by_id(id)
        except Exception as e:
            return {"error": str(e)}, 500

        if not activity:
            return {"message": "Activity not found"}, 404
        return activity.json(), 200
    def delete(cls, id):
        """Deletes one activity from database based on given id.
            Fails if there is no activity with that id.

        Args:
            id (int): The identifier of the activity to be deleted.

        Returns:
            dict of (str, any): Confirmation or error message.
        """
        try:
            activity = MaintenanceActivityModel.find_by_id(id)
            if not activity:
                return {"message": "Activity not found"}, 404
            activity.delete_from_db()

        except Exception as e:
            return {"error": str(e)}, 500

        return {"message": "Activity deleted"}, 200
    def put(cls, id):
        """Edits one activity in the database based on given id.
            Fails if there is no activity with that id.

        Args:
            id (int): The identifier of the activity to be edited.
            workspace_notes (str): Body param indicating the new workspace notes.

        Returns:
            dict of (str, any): Jsonified activity or error message.
        """
        data = cls._activity_parser.parse_args()
        try:
            activity = MaintenanceActivityModel.find_by_id(id)

            if not activity:
                return {"message": "Activity not found"}, 404

            activity.update_and_save(data)
        except Exception as e:
            return {"error": str(e)}, 500
        return activity.json(), 200
Exemplo n.º 4
0
    def can_do_activity(self, activity_id, week_day, start_time):
        """Checks if a new activity can be inserted in the user's schedule given his time left in his DailyAgenda

        Raises:
            RoleError: If the user's role is not 'maintainer'
            InvalidAgendaError: If the user does not have enough time to perform the maintenance activities

        Args:
            activity_id (int): The id for the new activity that has to be inserted
            start_time (int): The hour that the activity has to be scheduled to
            week_day (str): The day of the week (i.e.: monday, tuesday, ...)

        Returns:
            (bool, str): A tuple that contains a boolean that tells if the answer is insertable, and a string
            that tells a message about the obtained response 
        """
        activity = MaintenanceActivityModel.find_by_id(activity_id)
        if not activity:
            return False, "Activity not found"
        daily_agenda = self.DailyAgenda(
            self, activity.week, week_day, exclude=activity_id)
        return daily_agenda.is_activity_insertable(activity_id, start_time)
    def get(cls, username):
        """Gets the public representation of the DailyAgenda for a user with given username based on the week associated with
        the activity with given activity_id and the given week_day.
        Fails if the user's role is not 'maintainer'

        Args:
            username (str): A valid username
            activity_id (int): Body param indicating the identifier of the maintenance activity to be assigned.
            week_day (str): Body param indicating the day of the week (i.e.: monday, tuesday, ...)


        Returns:
            dict of (str, any): Json representing the user's availabilities in minutes classified by hour or an error message.
        """
        agenda = None
        try:
            user: UserModel = UserModel.find_by_username(username)
            if not user:
                return {"message": "User not found"}, 404

            if user.role != "maintainer":
                return {
                    "message":
                    "User role for user with given username is not 'maintainer'"
                }, 400

            data = cls._activity_parser.parse_args()
            activity = MaintenanceActivityModel.find_by_id(data["activity_id"])
            if not activity:
                return {"message": "Activity not found"}, 404

            agenda = user.get_daily_agenda(activity.week,
                                           data["week_day"],
                                           exclude=data["activity_id"])
        except Exception as e:
            return {"error": str(e)}, 500

        return agenda.json(), 200
    def put(cls, id):
        """Assigns a MaintenanceActivity to an user with role 'maintainer'.
        Fails if the maintainer role is not 'maintainer'.

        Args:
            id (int): The identifier of the maintenance activity to be assigned.
            maintainer_username (str): Body param indicating the maintainer's username
            week_day (str): Body param indicating the day of the week (i.e.: monday, tuesday, ...)
            start_time (int): Body param indicating the hour that the activity has to be scheduled to

        Returns:
            dict of (str, str): Jsonified success or error message.
        """
        data = cls._activity_parser.parse_args()

        try:
            activity = MaintenanceActivityModel.find_by_id(id)
            if not activity:
                return {"message": "Activity not found"}, 404

            user = UserModel.find_by_username(data["maintainer_username"])
            if not user:
                return {"message": "User not found"}, 404

            if user.role != "maintainer":
                return {"message": "User role for user with given username is not 'maintainer'"}, 400

            is_doable, reason = user.can_do_activity(
                id, data["week_day"], data["start_time"])
            if not is_doable:
                return {"message": reason}, 400

            activity.update_and_save(data)

        except Exception as e:
            return {"error": str(e)}, 500

        return {"message": "Activity assigned successfully"}, 200
Exemplo n.º 7
0
        def is_activity_insertable(self, activity_id, start_time):
            """Checks if a new activity can be inserted in the user's schedule given his time left in the DailyAgenda

            Args:
                activity_id (int): The id for the new activity that has to be inserted
                start_time (int): The hour that the activity has to be scheduled to

            Returns:
                (bool, str): A tuple that contains a boolean that tells if the answer is insertable, and a string
                that tells a message about the obtained response 
            """
            if start_time < self.user.work_start_hour or start_time > self.user.work_start_hour + self.user.work_hours:
                return False, "Invalid start_time"
            activity = MaintenanceActivityModel.find_by_id(activity_id)
            if not activity:
                return False, "Activity not found"

            activity.start_time = start_time
            try:
                self._calculate_agenda_dictionary(append=activity)
                return True, "Ok"
            except InvalidAgendaError as e:
                return False, e.message
    def get(cls, activity_id):
        """Gets a paginated list of Maintainers weekly availability, along with its metadata.
        For every user it returns aswell the user itself, the user's skill compliance (expressed as a fraction) 
        with the next activity that the planner wants to assign and the week in which the
        activity has to be assigned

        Args:
            activity_id (int): The identifier of the maintenance activity to be assigned.
            current_page (int, optional): Body param indicating the requested page. Defaults to 1.
            page_size (int, optional): Body param indicating the page size. Defaults to 10.

        Returns:
            dict of (str, any): Json of rows and meta or an error message. Rows is the list of paginated users' informations; meta is its metadata.
        """

        activity = MaintenanceActivityModel.find_by_id(activity_id)
        if not activity:
            return {"message": "Activity not found"}, 404

        data = cls._activity_parser.parse_args()
        rows, meta = UserModel.find_some_maintainers(**data)

        return {
            "rows": [{
                "user":
                user.json(),
                "skill_compliance":
                "3/5",
                "weekly_percentage_availability":
                user.get_weekly_percentage_availability(
                    activity.week, exclude=activity_id).json(),
                "week":
                activity.week
            } for user in rows],
            "meta":
            meta
        }, 200