コード例 #1
0
ファイル: subjects.py プロジェクト: arut-ji/task-ledger
 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
コード例 #2
0
 def testInitializationWithJson(self):
     tasks = [
         mock_task_data,
         mock_task_data.copy(),
         mock_task_data.copy()
     ]
     task_list = TaskList(tasks)
     size = task_list.count()
     self.assertEqual(size, 3)
コード例 #3
0
 def testGetTask(self):
     task = Task(mock_task_data)
     tasks = [
         mock_task_data,
         mock_task_data.copy(),
         mock_task_data.copy()
     ]
     task_list = TaskList(tasks)
     result = task_list.get_task(task.id)
     self.assertEqual(result, task)
コード例 #4
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)
コード例 #5
0
 def testCountTask(self):
     task = Task(mock_task_data)
     mock_task_data_2 = mock_task_data.copy()
     mock_task_data_3 = mock_task_data.copy()
     mock_task_data_2['id'] = 1
     mock_task_data_3['id'] = 2
     tasks = [
         mock_task_data,
         mock_task_data_2,
         mock_task_data_3
     ]
     task_list = TaskList(tasks)
     self.assertEqual(task_list.count(), 3)
コード例 #6
0
 def testDeleteTask(self):
     task = Task(mock_task_data)
     mock_task_data_2 = mock_task_data.copy()
     mock_task_data_3 = mock_task_data.copy()
     mock_task_data_2['id'] = 1
     mock_task_data_3['id'] = 2
     tasks = [
         mock_task_data,
         mock_task_data_2,
         mock_task_data_3
     ]
     task_list = TaskList(tasks)
     task_list.delete_task(1)
     self.assertEqual(task_list.get_task(1), None)
コード例 #7
0
    def testUpdateTask(self):
        task = Task(mock_task_data)
        mock_task_data_2 = mock_task_data.copy()
        mock_task_data_3 = mock_task_data.copy()
        mock_task_data_2['id'] = 1
        mock_task_data_3['id'] = 2
        tasks = [
            mock_task_data,
            mock_task_data_2,
            mock_task_data_3
        ]
        task_list = TaskList(tasks)
        new_data = mock_task_data.copy()
        new_data['topic'] = "hello world"
        result = task_list.update_task(task.id, new_data)

        self.assertEqual(result.topic, "hello world")
        self.assertEqual(result, task_list.get_task(task.id))
コード例 #8
0
 def testInitializationWithEmptyTask(self):
     tasks = TaskList()
     size = tasks.count()
     self.assertEqual(size, 0)
コード例 #9
0
 def list_task(user_id):
     api_endpoint = USER_TASKS_API.format(str(user_id))
     response = requests.get(url=api_endpoint)
     if response.status_code == 200:
         return TaskList(response.json())
     return None
コード例 #10
0
ファイル: subjects.py プロジェクト: arut-ji/task-ledger
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)