@@ -692,29 +692,8 @@ def import_package(self, package_data):
692692 # Vulnerabilities are fetched post import.
693693 package_data .pop ("affected_by_vulnerabilities" , None )
694694
695- unique_together_lookups = {
696- field : package_data .get (field , "" ) for field in self .unique_together_fields
697- }
698-
699- # Check if the Package already exists in the local Dataspace
700- try :
701- package = Package .objects .scope (self .user .dataspace ).get (** unique_together_lookups )
702- self .existing ["package" ].append (str (package ))
703- except ObjectDoesNotExist :
704- package = None
705-
706- # Check if the Package already exists in the reference Dataspace
707- reference_dataspace = Dataspace .objects .get_reference ()
708- user_dataspace = self .user .dataspace
709- if not package and user_dataspace != reference_dataspace :
710- qs = Package .objects .scope (reference_dataspace ).filter (** unique_together_lookups )
711- if qs .exists ():
712- reference_object = qs .first ()
713- try :
714- package = copy_object (reference_object , user_dataspace , self .user , update = False )
715- self .created ["package" ].append (str (package ))
716- except IntegrityError as error :
717- self .errors ["package" ].append (str (error ))
695+ # Check if the package already exists to prevent duplication.
696+ package = self .look_for_existing_package (package_data )
718697
719698 if license_expression := package_data .get ("declared_license_expression" ):
720699 license_expression = str (self .licensing .dedup (license_expression ))
@@ -786,3 +765,31 @@ def import_dependency(self, dependency_data):
786765 return
787766
788767 self .created ["dependency" ].append (str (dependency .dependency_uid ))
768+
769+ def look_for_existing_package (self , package_data ):
770+ package = None
771+ unique_together_lookups = {
772+ field : package_data .get (field , "" ) for field in self .unique_together_fields
773+ }
774+
775+ # Check if the Package already exists in the local Dataspace
776+ try :
777+ package = Package .objects .scope (self .user .dataspace ).get (** unique_together_lookups )
778+ self .existing ["package" ].append (str (package ))
779+ except ObjectDoesNotExist :
780+ package = None
781+
782+ # Check if the Package already exists in the reference Dataspace
783+ reference_dataspace = Dataspace .objects .get_reference ()
784+ user_dataspace = self .user .dataspace
785+ if not package and user_dataspace != reference_dataspace :
786+ qs = Package .objects .scope (reference_dataspace ).filter (** unique_together_lookups )
787+ if qs .exists ():
788+ reference_object = qs .first ()
789+ try :
790+ package = copy_object (reference_object , user_dataspace , self .user , update = False )
791+ self .created ["package" ].append (str (package ))
792+ except IntegrityError as error :
793+ self .errors ["package" ].append (str (error ))
794+
795+ return package
0 commit comments