from typing import List from models import ArtistMomentType from models.prs_purchase_order import PRSPurchaseOrder from models.territories import Territory from models.artist_moment import ArtistMoment from workers.prs.repository import PRSImportRepository from utils.json_encoder import DataclassDictionaryEncoder as Encoder from artists_moments.schemas import MomentMetadata, MomentEditableFields class MomentMapper: repository = PRSImportRepository() def __init__( self, purchase_order: PRSPurchaseOrder, moment_type_id: int, artist_id: int, artist_external_id: str, territories: List[Territory] ): self.purchase_order = purchase_order self.moment_type_id = moment_type_id self.artist_id = artist_id self.artist_external_id = artist_external_id self.territories = territories def build(self): m_type = self.repository.get_moment_type(self.moment_type_id) key = self.__build_name(m_type) artist_moment = self.repository.get_moment_by_external_key(key) or ArtistMoment() artist_moment.purchase_order = self.purchase_order artist_moment.name = m_type.name artist_moment.date = self.purchase_order.created_date artist_moment.type_id = m_type.id artist_moment.notes = self.purchase_order.description artist_moment.artist_id = self.artist_id artist_moment.external_key = key artist_moment.metainfo = self.assign_editable_fields() artist_moment.territories = self.territories return artist_moment def __build_name(self, moment_type: ArtistMomentType): created_at = self.purchase_order.created_date.strftime('%Y-%m-%d') return f"{self.artist_external_id}-{moment_type.name}-{moment_type.id}-{created_at}" def assign_editable_fields(self): return Encoder.convert_to_dict( MomentMetadata(fields=MomentEditableFields(date=False, artist=False, type=False, category=False)) )