Skip to content

Commit 16ef842

Browse files
authored
Merge pull request #155 from kurusugawa-computer/add-get_rejected_count
utils.pyに`get_rejected_count`を追加する。
2 parents 2109da4 + 6673aec commit 16ef842

File tree

4 files changed

+140
-7
lines changed

4 files changed

+140
-7
lines changed

annofabapi/__version__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.30.0'
1+
__version__ = '0.30.1'

annofabapi/utils.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import requests
1616
from requests.structures import CaseInsensitiveDict
1717

18-
from annofabapi.models import Task, TaskHistory, TaskPhase
18+
from annofabapi.models import Task, TaskHistory, TaskHistoryShort, TaskPhase
1919

2020

2121
def _raise_for_status(response: requests.Response) -> None:
@@ -273,6 +273,32 @@ def get_task_history_index_skipped_inspection(task_history_list: List[TaskHistor
273273
return index_list
274274

275275

276+
def get_number_of_rejections(task_histories: List[TaskHistoryShort], phase: TaskPhase, phase_stage: int = 1) -> int:
277+
"""
278+
タスク履歴から、指定されたタスクフェーズでの差し戻し回数を取得する。
279+
280+
Args:
281+
task_histories: タスク履歴
282+
phase: どのフェーズで差し戻されたか(TaskPhase.INSPECTIONかTaskPhase.ACCEPTANCE)
283+
phase_stage: どのフェーズステージで差し戻されたか。デフォルトは1。
284+
285+
Returns:
286+
差し戻し回数
287+
"""
288+
if phase not in [TaskPhase.INSPECTION, TaskPhase.ACCEPTANCE]:
289+
raise ValueError("引数'phase'には、'TaskPhase.INSPECTION'か'TaskPhase.ACCEPTANCE'を指定してください。")
290+
291+
rejections_by_phase = 0
292+
for i, history in enumerate(task_histories):
293+
if not (history["phase"] == phase.value and history["phase_stage"] == phase_stage and history["worked"]):
294+
continue
295+
296+
if i + 1 < len(task_histories) and task_histories[i + 1]["phase"] == TaskPhase.ANNOTATION.value:
297+
rejections_by_phase += 1
298+
299+
return rejections_by_phase
300+
301+
276302
def can_put_annotation(task: Task, my_account_id: str) -> bool:
277303
"""
278304
対象タスクが、`put_annotation` APIで、アノテーションを更新できる状態かどうか。

generate/generate.sh

+1-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ usage_exit() {
1010
}
1111

1212
FLAG_DOWNLOAD=false
13-
FLAG_DOCKER_PULL=false
13+
1414

1515
if [ $# -gt 0 ]; then
1616
for OPT in "$@"
@@ -41,9 +41,6 @@ fi
4141
SCRIPT_DIR=$(cd $(dirname $0); pwd)
4242
pushd ${SCRIPT_DIR}
4343

44-
if "${FLAG_DOCKER_PULL}"; then
45-
docker pull openapitools/openapi-generator-cli
46-
fi
4744

4845
# swagger.yamlを修正したいときがあるので、
4946
if "${FLAG_DOWNLOAD}"; then

tests/test_local_utils.py

+111-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
# tz_jst = datetime.timezone(datetime.timedelta(hours=9))
55
# assert to_iso8601_extension(d, tz_jst) == "2019-10-08T16:20:08.241+09:00"
66

7-
from annofabapi.utils import get_task_history_index_skipped_acceptance, get_task_history_index_skipped_inspection
7+
from annofabapi.models import TaskPhase
8+
from annofabapi.utils import (get_number_of_rejections, get_task_history_index_skipped_acceptance,
9+
get_task_history_index_skipped_inspection)
810

911

1012
class TestTaskHistoryUtils:
@@ -236,3 +238,111 @@ def test_get_task_history_index_skipped_inspection_検査2回_検査1回目で
236238
actual = get_task_history_index_skipped_inspection(task_history_list)
237239
expected = []
238240
assert all([a == b for a, b in zip(actual, expected)])
241+
242+
def test_get_number_of_rejections_教師付1回目(self):
243+
task_history_short_list = [{
244+
"account_id": self.ACCOUNT_ID,
245+
"phase": "annotation",
246+
"phase_stage": 1,
247+
"worked": True
248+
}]
249+
250+
actual = get_number_of_rejections(task_history_short_list, TaskPhase.ACCEPTANCE)
251+
assert actual == 0
252+
253+
def test_get_number_of_rejections_受入で1回差戻(self):
254+
task_history_short_list = [
255+
{
256+
"account_id": self.ACCOUNT_ID,
257+
'phase': 'annotation',
258+
'phase_stage': 1,
259+
'worked': True
260+
},
261+
{
262+
"account_id": self.ACCOUNT_ID,
263+
'phase': 'acceptance',
264+
'phase_stage': 1,
265+
'worked': True
266+
},
267+
{
268+
"account_id": self.ACCOUNT_ID,
269+
'phase': 'annotation',
270+
'phase_stage': 1,
271+
'worked': True
272+
},
273+
]
274+
275+
actual = get_number_of_rejections(task_history_short_list, TaskPhase.ACCEPTANCE)
276+
assert actual == 1
277+
278+
def test_get_number_of_rejections_検査で1回差戻(self):
279+
task_history_short_list = [
280+
{
281+
"account_id": self.ACCOUNT_ID,
282+
'phase': 'annotation',
283+
'phase_stage': 1,
284+
'worked': True
285+
},
286+
{
287+
"account_id": self.ACCOUNT_ID,
288+
'phase': 'inspection',
289+
'phase_stage': 1,
290+
'worked': True
291+
},
292+
{
293+
"account_id": self.ACCOUNT_ID,
294+
'phase': 'annotation',
295+
'phase_stage': 1,
296+
'worked': True
297+
},
298+
]
299+
300+
actual = get_number_of_rejections(task_history_short_list, TaskPhase.INSPECTION)
301+
assert actual == 1
302+
303+
def test_get_number_of_rejections_検査と受入で1回差戻(self):
304+
task_history_short_list = [{
305+
"account_id": self.ACCOUNT_ID,
306+
"phase": "annotation",
307+
"phase_stage": 1,
308+
"worked": True
309+
}, {
310+
"account_id": self.ACCOUNT_ID,
311+
"phase": "inspection",
312+
"phase_stage": 1,
313+
"worked": True
314+
}, {
315+
"account_id": self.ACCOUNT_ID,
316+
"phase": "annotation",
317+
"phase_stage": 1,
318+
"worked": True
319+
}, {
320+
"account_id": self.ACCOUNT_ID,
321+
"phase": "inspection",
322+
"phase_stage": 1,
323+
"worked": True
324+
}, {
325+
"account_id": self.ACCOUNT_ID,
326+
"phase": "acceptance",
327+
"phase_stage": 1,
328+
"worked": True
329+
}, {
330+
"account_id": self.ACCOUNT_ID,
331+
"phase": "annotation",
332+
"phase_stage": 1,
333+
"worked": True
334+
}, {
335+
"account_id": "00589ed0-dd63-40db-abb2-dfe5e13c8299",
336+
"phase": "inspection",
337+
"phase_stage": 1,
338+
"worked": True
339+
}, {
340+
"account_id": self.ACCOUNT_ID,
341+
"phase": "acceptance",
342+
"phase_stage": 1,
343+
"worked": True
344+
}]
345+
actual_inspection = get_number_of_rejections(task_history_short_list, TaskPhase.INSPECTION)
346+
assert actual_inspection == 1
347+
actual_acceptance = get_number_of_rejections(task_history_short_list, TaskPhase.ACCEPTANCE)
348+
assert actual_acceptance == 1

0 commit comments

Comments
 (0)