diff --git a/app/services/augmented_part_sync.py b/app/services/augmented_part_sync.py index d41be91..a77b198 100644 --- a/app/services/augmented_part_sync.py +++ b/app/services/augmented_part_sync.py @@ -17,7 +17,7 @@ Phase 2 – create entries for new ImportedParts • Type ← first PartType whose name is a substring of the description (longest name wins; skipped if no match) • SubType ← null - • Attachment point ← always named "Occular"; one of: + • Attachment point ← always named "Ocular"; one of: - ISO/UNC thread parsed from TBXThreadAttribute (using the matching ProductMaxThreadDepth mm/in if populated), or - StaticConnection from ProductODmm/ProductODin (whichever @@ -46,7 +46,7 @@ from app.models.part_type import PartType from app.models.unc_thread import UncSeries from app.schemas.augmented_part import AugmentedPartSyncResult -ATTACHMENT_POINT_NAME = "Occular" +ATTACHMENT_POINT_NAME = "Ocular" MM_PER_INCH = 25.4 # e.g. @@ -111,9 +111,7 @@ async def _get_or_create_attachment_point_name(db: AsyncSession, name: str): return apn -async def _add_attachment_point( - db: AsyncSession, ap: AugmentedPart, ip: ImportedPart -) -> None: +async def _add_attachment_point(db: AsyncSession, ap: AugmentedPart, ip: ImportedPart) -> None: """Infer and create an attachment point for a newly-synced AugmentedPart. Preference order: TBXThreadAttribute (ISO/UNC thread) then ProductODmm/in @@ -133,9 +131,7 @@ async def _add_attachment_point( if kind == "iso": depth_mm = _parse_float(ip.ProductMaxThreadDepthmm) - thread = await iso_thread_crud.get_by_values( - db, depth_mm=depth_mm, **fields - ) + thread = await iso_thread_crud.get_by_values(db, depth_mm=depth_mm, **fields) if thread is None: thread = await iso_thread_crud.create(db, depth_mm=depth_mm, **fields) await attachment_point_crud.create( @@ -146,9 +142,7 @@ async def _add_attachment_point( ) elif kind == "unc": depth_in = _parse_float(ip.ProductMaxThreadDepthin) - thread = await unc_thread_crud.get_by_values( - db, depth_in=depth_in, **fields - ) + thread = await unc_thread_crud.get_by_values(db, depth_in=depth_in, **fields) if thread is None: thread = await unc_thread_crud.create(db, depth_in=depth_in, **fields) await attachment_point_crud.create( @@ -246,9 +240,7 @@ async def sync(db: AsyncSession) -> AugmentedPartSyncResult: # ------------------------------------------------------------------ phase 1 # IDs referenced in existing associations - result = await db.execute( - select(augmented_part_imported_parts.c.imported_part_id).distinct() - ) + result = await db.execute(select(augmented_part_imported_parts.c.imported_part_id).distinct()) all_linked_ids: set[str] = {row[0] for row in result.all()} # IDs that still exist in imported_parts @@ -262,9 +254,7 @@ async def sync(db: AsyncSession) -> AugmentedPartSyncResult: # Which AugmentedParts are affected? result = await db.execute( select(augmented_part_imported_parts.c.augmented_part_id).where( - augmented_part_imported_parts.c.imported_part_id.in_( - orphaned_import_ids - ) + augmented_part_imported_parts.c.imported_part_id.in_(orphaned_import_ids) ) ) orphaned_ap_ids: set[int] = {row[0] for row in result.all()} @@ -272,9 +262,7 @@ async def sync(db: AsyncSession) -> AugmentedPartSyncResult: # Remove the stale association rows await db.execute( augmented_part_imported_parts.delete().where( - augmented_part_imported_parts.c.imported_part_id.in_( - orphaned_import_ids - ) + augmented_part_imported_parts.c.imported_part_id.in_(orphaned_import_ids) ) ) await db.flush() @@ -297,9 +285,7 @@ async def sync(db: AsyncSession) -> AugmentedPartSyncResult: # ------------------------------------------------------------------ phase 2 # Re-query linked IDs after phase 1 cleanup - result = await db.execute( - select(augmented_part_imported_parts.c.imported_part_id).distinct() - ) + result = await db.execute(select(augmented_part_imported_parts.c.imported_part_id).distinct()) now_linked_ids: set[str] = {row[0] for row in result.all()} unlinked_ids = current_ids - now_linked_ids @@ -307,9 +293,7 @@ async def sync(db: AsyncSession) -> AugmentedPartSyncResult: skipped = 0 if not unlinked_ids: - return AugmentedPartSyncResult( - created=created, deleted=deleted, skipped=skipped - ) + return AugmentedPartSyncResult(created=created, deleted=deleted, skipped=skipped) result = await db.execute( select(ImportedPart).where(ImportedPart.InventoryID.in_(unlinked_ids)) diff --git a/scripts/seed.py b/scripts/seed.py index 6433bcd..58136ab 100644 --- a/scripts/seed.py +++ b/scripts/seed.py @@ -17,7 +17,7 @@ from app.crud import role as role_crud from app.crud import sub_part_type as sub_part_type_crud from app.crud import user as user_crud -ATTACHMENT_POINT_NAMES = ["Objective", "Occular"] +ATTACHMENT_POINT_NAMES = ["Objective", "Ocular", "Rail"] PART_TYPES = ["Flip Cover", "ARD", "Polarizer", "Adapter Ring", "Scope"] @@ -113,9 +113,7 @@ async def seed() -> None: for name, description in PERMISSIONS: perm = await role_crud.get_permission_by_name(db, name) if perm is None: - perm = await role_crud.create_permission( - db, name=name, description=description - ) + perm = await role_crud.create_permission(db, name=name, description=description) perms[name] = perm # Roles @@ -153,14 +151,10 @@ async def seed() -> None: if pt is not None: for spt_name in spt_names: if ( - await sub_part_type_crud.get_by_name_and_part_type( - db, spt_name, pt.id - ) + await sub_part_type_crud.get_by_name_and_part_type(db, spt_name, pt.id) is None ): - await sub_part_type_crud.create( - db, name=spt_name, part_type_id=pt.id - ) + await sub_part_type_crud.create(db, name=spt_name, part_type_id=pt.id) # Superuser existing = await user_crud.get_by_email(db, settings.first_superuser_email)