forked from retoor/devplacepy
Zo doen wijdat in oost
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections import Counter
|
||||
|
||||
from devplacepy.schemas.sync import SyncBatchOut, SyncRecordResultOut
|
||||
from devplacepy.services.sync.processors import PROCESSORS
|
||||
from devplacepy.services.sync.records import SyncBatchForm
|
||||
from devplacepy.services.sync.spec import STATUSES
|
||||
|
||||
|
||||
class BatchRunner:
|
||||
def __init__(self, kind: str, *, request=None):
|
||||
self.kind = kind
|
||||
self.request = request
|
||||
|
||||
def run(self, data: SyncBatchForm) -> SyncBatchOut:
|
||||
processor = PROCESSORS[self.kind](
|
||||
data.origin, dry_run=data.dry_run, request=self.request
|
||||
)
|
||||
results = [
|
||||
SyncRecordResultOut(**vars(processor.process(index, raw)))
|
||||
for index, raw in enumerate(data.records)
|
||||
]
|
||||
tally = Counter(result.status for result in results)
|
||||
return SyncBatchOut(
|
||||
origin=data.origin,
|
||||
kind=self.kind,
|
||||
dry_run=data.dry_run,
|
||||
total=len(results),
|
||||
counts={status: tally.get(status, 0) for status in STATUSES},
|
||||
results=results,
|
||||
)
|
||||
Reference in New Issue
Block a user