Esempio n. 1
0
 def testAddTask(self):
     task = Task(mock_task_data)
     tasks = [
         mock_task_data,
         mock_task_data.copy(),
         mock_task_data.copy()
     ]
     task_list = TaskList(tasks)
     task_list.add_task(mock_task_data)
     result = task_list.tasks[-1]
     self.assertEqual(result, task)
Esempio n. 2
0
class TaskLedgerSystem(Observable):
    def __init__(self):
        super().__init__()
        self.task_list = TaskList()
        self.auth_service = AuthService()
        self.task_service = TaskService()
        self.task_validator = TaskValidator()
        self.user = None
        self.token = None
        self.loading = False

    def _notify_observers(self):
        for observer in self._observers:
            observer.update_data()

    def set_current_user(self, user: Dict):
        self.user = user

    def set_current_token(self, token: str):
        self.token = token

    def get_task_list(self):
        return self.task_list

    def set_task_list(self, task_list: TaskList):
        self.task_list = task_list
        self._notify_observers()

    def login(self, username, password) -> bool:

        response = AuthService.login(username, password)

        if response:
            self.set_current_user(response['user'])
            self.set_current_token(response['key'])
            task_list = TaskService.list_task(self.user["id"])
            self.set_task_list(task_list)
            return True
        return False

    def is_authenticated(self) -> bool:
        return self.token is not None

    def register(self, username, password1, password2) -> Dict:
        response = AuthService.register(username, password1, password2)

        if len(response) == 0:
            return {}
        else:
            return response

    def create_task(self, details: Dict) -> bool:
        details.update({'user': self.user['id']})
        is_valid = self.task_validator.validate(details)
        if is_valid:
            new_task = self.task_service.create_task(details)
            self.task_list.add_task(new_task)
            self._notify_observers()
            return True
        return False

    def update_task(self, task_id: int, details: Dict):
        details.update({'user': self.user['id']})
        is_valid = self.task_validator.validate(details)

        if is_valid:
            updated_task = TaskService.update_task(task_id, details)
            self.task_list.update_task(task_id, updated_task)
            self._notify_observers()
            return True
        return False

    def delete_task(self, task_id):
        deleted_task = self.task_list.delete_task(task_id)

        is_delete_success = False

        if deleted_task is not None:
            is_delete_success = TaskService.delete_task(task_id)

        if is_delete_success:
            self._notify_observers()
            return True

        return False

    def set_loading(self, loading_status):
        self.loading = loading_status

    def is_busy(self, date: QDate) -> bool:
        return self.task_list.is_busy(date)