Exemplo n.º 1
0
    def __init__(
            self,
            uid,
            creator_uid,
            tid,
            # Replace later with the controller
            deadline,
            title,
            pid=None,
            status=Status.IN_PROGRESS,
            priority=Priority.MEDIUM,
            parent=None,
            comment='',
            realization_time=None,
            creation_time=0,
            period=None):
        logger.debug('creating a task...')
        self.childs = []

        self.uid = uid
        self.creator_uid = creator_uid
        self.tid = tid
        self.pid = pid
        self.title = title
        self.status = status
        self.priority = priority
        self.parent = parent
        self.period = period
        self.deadline = deadline
        self.comment = comment
        self.realization_time = realization_time
        self.creation_time = creation_time
Exemplo n.º 2
0
def get_milliseconds(formatted_time):
    """
    This function returns time in milliseconds according to the given formatted
    time
    :param formatted_time: Time in following format of date_format
    (%Y-%m-%d %H:%M:%S)
    :type formatted_time: String
    :return: Int
    """
    logger.debug('passed time %s' % formatted_time)

    try:
        date_time = datetime.strptime(_time_get_formatted(formatted_time),
                                      FORMAT)
        logger.debug('date converted')

        # Note that here we are subtracting 1970's to define them as lower point
        final_time = (date_time - epoch).total_seconds() * 1000 - error
        if final_time < 0:
            raise m_e.InvalidArgumentFormat(m_e.TimeMessages.TIME_EPOCH)

        return int(final_time)

    except ValueError:
        raise m_e.InvalidArgumentFormat(m_e.TimeMessages.TIME_FORMAT)
Exemplo n.º 3
0
    def add_task(self,
                 title,
                 priority=None,
                 status=None,
                 time=None,
                 parent_id=None,
                 comment='',
                 pid=None,
                 receiver_uid=None,
                 period=None):
        """
        This function will add passed task to the database, with the creator and
        executor that named in the config.ini (i.e current logged user)
        :param pid: Project's id
        :param receiver_uid: User's login
        :param comment: Task's comment for some detailed explanation
        :param parent_id: Parent's task id
        :param time: Time in following format: YYYY-MM-DD HH:MM:SS
        :param title: Task's title
        :param priority: Task's priority (enum from Priority)
        :param status: Task's status (enum from Status)
        :param period: Task's period in hours
        :type pid: Int
        :type receiver_uid: String
        :type comment: String
        :type parent_id: Int
        :type time: String
        :type title: String
        :type priority: Int
        :type status: Int
        :type period: String
        :return: None
        """
        if priority is None:
            logger.debug('Default priority has been set')
            priority = Priority.MEDIUM
        elif priority < Priority.MIN or priority > Priority.MAX:
            raise v_e.InvalidPriorityError(
                v_e.PriorityMessages.INVALID_PRIORITY)

        if status is None:
            logger.debug('Default status has been set')
            status = Status.IN_PROGRESS
        elif status < Status.EXPIRED or status > Status.DONE:
            raise v_e.InvalidStatusError(v_e.StatusMessages.INVALID_STATUS)

        if time is not None:
            time = time_candle.model.time_formatter.get_milliseconds(time)

        # if period not is none we convert it to the milliseconds
        if period is not None:
            period = time_candle.model.time_formatter.\
                days_to_milliseconds(period)

        v.check_comment(comment)
        v.check_title(title)

        self.task_logic.add_task(title, priority, status, time, parent_id,
                                 comment, pid, receiver_uid, period)
Exemplo n.º 4
0
    def make_project(cls, obj):
        """
        This function converts some data type to project
        :type obj: type with fields:
        - pid
        - admin uid
        - title
        - description
        :return: Project
        """
        logger.debug('convert storage to model project')

        project = cls(obj.pid, obj.admin, obj.title, obj.description)

        return project
Exemplo n.º 5
0
    def make_message(cls, obj):
        """
        This function converts some data type to user
        :type obj: type with fields:
        - mid
        - content
        - user
        :return: Message
        """
        logger.debug('convert storage to model message')

        message = cls(uid=obj.uid,
                      mid=obj.mid,
                      content=obj.content)

        return message
Exemplo n.º 6
0
    def make_user(cls, obj):
        """
        This function converts some data type to user
        :type obj: type with fields:
        - uid
        - login
        - nickname
        - about
        :return: User
        """
        logger.debug('convert storage to model user')

        user = cls(uid=obj.uid,
                   login=obj.login,
                   nickname=obj.nickname,
                   about=obj.about)

        return user
Exemplo n.º 7
0
def _time_get_formatted(formatted_time):
    """
    This function returns formatted_time, but with the now's date if the date
    is not pointed
    :param formatted_time: string of formatted time
    :type formatted_time: String
    :return: String of format %Y-%m-%d %H:%M:%S to the strptime function
    """
    long_pattern = r'\d{4}-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}:\d{1,2}'
    short_pattern = r'\d{1,2}:\d{1,2}:\d{1,2}'
    if re.match(long_pattern, formatted_time):
        logger.debug('date matched long pattern')
        return formatted_time
    elif re.match(short_pattern, formatted_time):
        logger.debug('date matched short pattern')
        date_string = datetime.strftime(datetime.now(), '%Y-%m-%d')
        return date_string + ' ' + formatted_time
    else:
        logger.warning('the date must be %Y-%m-%d or %Y-%m-%d %H:%M:%S')
        raise m_e.InvalidArgumentFormat(m_e.TimeMessages.TIME_FORMAT)
Exemplo n.º 8
0
    def _update(self, task):
        changed_flag = False
        # make tasks expired if deadline is crossed
        if task.deadline is not None \
                and time_candle.model.\
                time_formatter.time_delta(task.deadline) < 0 \
                and task.status == Status.IN_PROGRESS:
            task.status = Status.EXPIRED
            self.queue.append(
                task.uid, TaskMessages.TASK_EXPIRED.format(task.title))

            self._set_status_expired_to_childs(task)
            logger.debug('tasks status updated')
            changed_flag = True

        # make new deadline to the period task if there is need to do so and if
        # we are not expired our task
        if task.period is not None:
            old_deadline = task.deadline
            # check on expired and maybe change deadline
            if task.status != Status.EXPIRED:
                task.deadline = time_candle.model.time_formatter.\
                    get_next_deadline(task.period, task.deadline)

            # if we are changed it then we are in progress
            if old_deadline != task.deadline:
                task.status = Status.IN_PROGRESS
                logger.debug('status in progress for period task %s', task.tid)
                changed_flag = True

        if changed_flag:
            self.task_adapter.save(task)
            logger.debug('task updated')
Exemplo n.º 9
0
    def make_task(cls, obj):
        """
        This function converts some data type to task
        :type obj: type with fields:
        - receiver uid
        - creator uid
        - other obj project with pid
        - tid
        - deadline_time
        - creation_time
        - realization_time
        - title
        - status
        - priority
        - period
        - other obj parent with tid or None
        - comment
        :return: Task
        """
        logger.debug('convert storage to model task')
        # we can have a None parent, so we have to determine this to take it's
        # id or not
        if obj.parent is None:
            parent_id = None
        else:
            parent_id = obj.parent.tid

        if obj.project is None:
            project_id = None
        else:
            project_id = obj.project.pid

        task = cls(obj.receiver, obj.creator, obj.tid, obj.deadline_time,
                   obj.title, project_id, obj.status, obj.priority, parent_id,
                   obj.comment, obj.realization_time, obj.creation_time,
                   obj.period)

        return task
Exemplo n.º 10
0
    def add_task(self, title, priority, status, deadline_time,
                 parent_id, comment, pid, uid, period):
        # add task to the database

        # This code is also checking is our parent tid exists in the database
        # for logged user. The max func needed to set tasks status and priority
        # not lower then parent's
        if parent_id is not None:
            parent_task = self.task_adapter.get_task_by_id(parent_id)

            if parent_task.status == Status.DONE:
                raise m_e.InvalidStatusError(
                    m_e.StatusMessages.MAKE_PARENT_STATUS_DONE)

            if parent_task.period is not None:
                raise m_e.InvalidParentError(
                    m_e.ParentMessages.PARENT_HAS_PERIOD)

            priority = max(priority, parent_task.priority)

        if status == Status.EXPIRED:
            raise m_e.InvalidStatusError(m_e.StatusMessages.ADD_EXPIRED)

        logger.debug('time in milliseconds %s', deadline_time)
        logger.info('time now (from milliseconds to datetime) %s',
                    time_candle.model.time_formatter.get_datetime(
                        time_candle.model.time_formatter.get_now_milliseconds()
                    ))

        # Check for rights and id's
        if uid is None:
            task_uid = self.uid
        else:
            self.project_adapter.is_user_in_project(uid, pid)
            task_uid = uid

        # check that if we are in the project that we has proper rights
        if pid is not None:
            logger.debug('pid is not none')
            if task_uid != self.uid:
                logger.debug('the receiver is not us')
                if not self.project_adapter.has_rights(pid):
                    raise m_e.InvalidLoginError(
                        m_e.ProjectMessages.DO_NOT_HAVE_RIGHTS)
            else:
                logger.debug('we are the receiver')
                self.project_adapter.is_user_in_project(self.uid, pid)

        # if deadline is not none
        if deadline_time is not None:
            # we checking that deadline is in the future
            if time_candle.model.time_formatter.time_delta(deadline_time) < 0:
                raise m_e.InvalidTimeError(m_e.TimeMessages.TIME_SHIFT)

        # also we must specify deadline if there is a period
        if period is not None and deadline_time is None:
            raise m_e.InvalidTimeError(m_e.TimeMessages.NO_DEADLINE)

        if period is not None and period < 0:
            raise m_e.InvalidTimeError(m_e.TimeMessages.NEGATIVE_PERIOD)

        if status == Status.EXPIRED and deadline_time is None:
            raise m_e.InvalidTimeError(m_e.TimeMessages.NO_DEADLINE)

        if status == Status.DONE:
            realization_time = time_candle.model.time_formatter.\
                get_now_milliseconds()
        else:
            realization_time = None

        task = TaskInstance(uid=task_uid,
                            creator_uid=self.uid,
                            tid=self.task_adapter.last_id() + 1,
                            deadline=deadline_time,
                            title=title,
                            pid=pid,
                            status=status,
                            priority=priority,
                            parent=parent_id,
                            comment=comment,
                            realization_time=realization_time,
                            creation_time=time_candle.model.time_formatter.
                            get_now_milliseconds(),
                            period=period)

        logger.debug('task configured and ready to save , the task id is %s',
                     task.tid)
        self.task_adapter.save(task)

        logger.debug('task added')