Skip to content

Commit

Permalink
Refactor logic #95
Browse files Browse the repository at this point in the history
Signed-off-by: Jono Yang <[email protected]>
  • Loading branch information
JonoYang committed Jul 19, 2023
1 parent 2068602 commit f81802a
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions minecode/model_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,13 @@ def merge_or_create_package(scanned_package, visit_level):
# Check to see if we have a package with the same purl, so we can use
# that package_set value
# TODO: Consider adding this logic to the Package queryset manager
existing_related_package = Package.objects.filter(
existing_related_packages = Package.objects.filter(
type=scanned_package.type,
namespace=scanned_package.namespace,
name=scanned_package.name,
version=scanned_package.version,
).first()
)
existing_related_package = existing_related_packages.first()
package_content = scanned_package.extra_data.get('package_content')

package_data = dict(
Expand Down Expand Up @@ -315,24 +316,21 @@ def merge_or_create_package(scanned_package, visit_level):
if existing_related_package:
related_package_sets_count = existing_related_package.package_sets.count()
if (
(
existing_related_package.package_content != PackageContentType.BINARY
related_package_sets_count == 0
or (
related_package_sets_count > 0
and created_package.package_content == PackageContentType.BINARY
)
or related_package_sets_count == 0
):
# Binary packages can only be part of one set
package_set = PackageSet.objects.create()
package_set.add_to_package_set(existing_related_package)
package_set.add_to_package_set(created_package)
elif related_package_sets_count > 0:
if created_package.package_content != PackageContentType.BINARY:
for package_set in existing_related_package.package_sets.all():
package_set.add_to_package_set(created_package)
else:
# Binary packages can only be part of one set
package_set = PackageSet.objects.create()
package_set.add_to_package_set(existing_related_package)
# Binary packages can only be part of one set
package_set = PackageSet.objects.create()
package_set.add_to_package_set(existing_related_package)
package_set.add_to_package_set(created_package)
elif (
related_package_sets_count > 0
and created_package.package_content != PackageContentType.BINARY
):
for package_set in existing_related_package.package_sets.all():
package_set.add_to_package_set(created_package)

for party in scanned_package.parties:
Expand Down

0 comments on commit f81802a

Please sign in to comment.