def as_dto(self): """ Creates a Task DTO suitable for transmitting via the API :param task_id: Task ID in scope :param project_id: Project ID in scope :return: JSON serializable Task DTO """ task_history = [] for action in self.task_history: if action.action_text is None: continue # Don't return any history without action text history = TaskHistoryDTO() history.action = action.action history.action_text = action.action_text history.action_date = action.action_date history.action_by = action.actioned_by.username if action.actioned_by else None task_history.append(history) task_dto = TaskDTO() task_dto.task_id = self.id task_dto.project_id = self.project_id task_dto.task_status = TaskStatus(self.task_status).name task_dto.lock_holder = self.lock_holder.username if self.lock_holder else None task_dto.task_history = task_history return task_dto
def as_dto_with_instructions(self, preferred_locale: str = 'en') -> TaskDTO: """ Get dto with any task instructions """ task_history = [] for action in self.task_history: history = TaskHistoryDTO() history.action = action.action history.action_text = action.action_text history.action_date = action.action_date history.action_by = action.actioned_by.username if action.actioned_by else None task_history.append(history) task_dto = TaskDTO() task_dto.task_id = self.id task_dto.project_id = self.project_id task_dto.task_status = TaskStatus(self.task_status).name task_dto.lock_holder = self.lock_holder.username if self.lock_holder else None task_dto.task_history = task_history task_dto.auto_unlock_seconds = Task.auto_unlock_delta().total_seconds() per_task_instructions = self.get_per_task_instructions(preferred_locale) # If we don't have instructions in preferred locale try again for default locale task_dto.per_task_instructions = per_task_instructions if per_task_instructions else self.get_per_task_instructions( self.projects.default_locale) return task_dto