def get_random_scan(category_key: str) -> Scan: """Fetch random scan for labeling. :param category_key: unique key identifying category :return: Scan Metadata object """ user = get_current_user() category = ScanCategoriesRepository.get_category_by_key(category_key) try: scan = ScansRepository.get_random_scan(category, user) if not scan: raise NotFoundException( 'Could not find any Scan for this category!') return scan except NoResultFound: raise NotFoundException('Could not find any Scan for this category!')
def get_task_for_key(task_key: str) -> Task: """Fetch Task for given key. :return: Task """ try: return TasksRepository.get_task_by_key(task_key) except NoResultFound: raise NotFoundException('Did not found task for {} key!'.format(task_key))
def get_label(label_id: LabelID) -> Label: """Fetch Label from database for given Label ID. :param label_id: ID of a Label which should be returned """ try: return LabelsRepository.get_label_by_id(label_id) except NoResultFound: raise NotFoundException('Label "{}" not found.'.format(label_id))
def skip_scan(scan_id: ScanID) -> bool: """Increases skip count of Scan with given scan_id. :param scan_id: ID of a Scan which should be returned :return: boolean information whether the Scan was skipped or not """ if not ScansRepository.increase_skip_count_of_a_scan(scan_id): raise NotFoundException('Scan "{}" not found.'.format(scan_id)) return True
def get_random_label() -> Label: """Fetch random label that has the NOT_VERIFIED status. :return: dictionary with details about label """ try: return LabelsRepository.get_random_label( LabelVerificationStatus.NOT_VERIFIED) except NoResultFound: raise NotFoundException('No Labels not found.')
def get_scan(scan_id: ScanID) -> Scan: """Return Scan for given scan_id. :param scan_id: ID of a Scan which should be returned :return: Scan object """ try: return ScansRepository.get_scan_by_id(scan_id) except NoResultFound: raise NotFoundException('Scan "{}" not found.'.format(scan_id))
def get_action_details(action_id: ActionID) -> Action: """Fetch details about Action. :param action_id: ID of an Action that should be returned :return: Action for given ID """ try: return ActionsRepository.get_action_by_id(action_id) except NoResultFound: raise NotFoundException('Action "{}" not found.'.format(action_id))
def get_random_label() -> Label: """Fetch random label that has the NOT_VERIFIED status. :return: random Label object from database """ try: return LabelsRepository.get_random_label( LabelVerificationStatus.NOT_VERIFIED) except NoResultFound: raise NotFoundException('No Labels found.')
def change_label_status(label_id: LabelID, status: LabelVerificationStatus) -> Label: """Change status of the label. :param label_id: ID of a label for which the status should be changed :param status: new Label Verification Status that should be set """ try: label = LabelsRepository.get_label_by_id(label_id) except NoResultFound: raise NotFoundException('Label "{}" not found.'.format(label_id)) label.update_status(status) return label
def add_action_response(action_id: ActionID, response: Dict) -> ActionResponse: """Add new Response for given Action. :param action_id: ID of an Action for which this Response should be added :param response: dictionary Response for given Action :return: ActionResponse database object """ try: return ActionsRepository.add_action_response(action_id, response) except NoResultFound: raise NotFoundException('Action "{}" not found.'.format(action_id)) except UnsupportedActionException: raise InvalidArgumentsException( 'Action does not support returning Respose.') except InvalidResponseException: raise InvalidArgumentsException( 'Your answers does not match keys in Survey.')
def add_label(scan_id: ScanID, elements: List[Dict], files: Dict[str, bytes], labeling_time: LabelingTime) -> Label: """Add label to given scan. :param scan_id: ID of a given scan :param elements: List of JSONs describing elements for a single label :param files: mapping of uploaded files (name and content) :param labeling_time: time in seconds that user spent on labeling :return: Label object """ user = get_current_user() try: label = LabelsRepository.add_new_label(scan_id, user, labeling_time) except IntegrityError: raise NotFoundException('Could not find Scan for that id!') for element in elements: add_label_element(element, label.id, files) return label
def get_random_scan(task_key: str) -> Scan: """Fetch random scan from specified Task for labeling. :param task_key: unique key identifying task :return: Scan Metadata object """ task = TasksRepository.get_task_by_key(task_key) if not task: raise InvalidArgumentsException('Task key {} is invalid!'.format(task_key)) user = get_current_user() scan = ScansRepository.get_random_scan(task, user) if not scan: raise NotFoundException('Could not find any Scan for this task!') predefined_label = LabelsRepository.get_predefined_label_for_scan_in_task(scan, task) if predefined_label: scan.predefined_label_id = predefined_label.id return scan
def add_label(scan_id: ScanID, task_key: str, elements: List[Dict], # pylint: disable-msg=too-many-arguments files: Dict[str, bytes], labeling_time: LabelingTime, comment: str = None, is_predefined: bool = False) -> Label: """Add label to given scan. :param scan_id: ID of a given scan :param task_key: Key of Task :param elements: List of JSONs describing elements for a single label :param files: mapping of uploaded files (name and content) :param labeling_time: time in seconds that user spent on labeling :param comment: (optional) comment describing a label :param is_predefined: (optional) mark such Label as predefined to show on Labeling Page :return: Label object """ user = get_current_user() try: label = LabelsRepository.add_new_label(scan_id, task_key, user, labeling_time, comment, is_predefined) except IntegrityError: raise NotFoundException('Could not find Scan for that id!') for element in elements: add_label_element(element, label.id, files) return label
def _get_label_tag(tag_key: str) -> LabelTag: """Return Label Tag based on Tag's key or raise an exception in case if not found.""" try: return LabelTagRepository.get_label_tag_by_key(tag_key) except NoResultFound: raise NotFoundException('Could not find any Label Tag for that key!')