mirror of
https://github.com/almet/copanier.git
synced 2025-04-28 11:32:38 +02:00
Throw an exception when duplicates are found
This commit is contained in:
parent
2b0459e94a
commit
2f2344d3b3
1 changed files with 9 additions and 0 deletions
|
@ -1,6 +1,7 @@
|
|||
import inspect
|
||||
import threading
|
||||
import uuid
|
||||
from collections import Counter
|
||||
from datetime import datetime, timedelta
|
||||
from dataclasses import dataclass, field, asdict
|
||||
from pathlib import Path
|
||||
|
@ -426,6 +427,14 @@ class Delivery(PersistedBase):
|
|||
if not path.exists():
|
||||
raise DoesNotExist
|
||||
data = yaml.safe_load(path.read_text())
|
||||
# Get the most common product key.
|
||||
# If we have multiple keys with the same name
|
||||
# dedupe them by appending something to the newest of them.
|
||||
if 'products' in data:
|
||||
counter = Counter([i['ref'] for i in data['products']])
|
||||
most_common = counter.most_common(1)[0]
|
||||
if most_common[1] > 1:
|
||||
raise Exception(f'Duplicate product keys for "{most_common[0]}"')
|
||||
# Tolerate extra fields (but we'll lose them if instance is persisted)
|
||||
data = {k: v for k, v in data.items() if k in cls.__dataclass_fields__}
|
||||
delivery = cls(**data)
|
||||
|
|
Loading…
Reference in a new issue