Skip to content

Commit

Permalink
Fix package name case-insensitivity
Browse files Browse the repository at this point in the history
Prevent newly uploaded packages from having the same name but in a
different case.
  • Loading branch information
x753 committed Sep 23, 2024
1 parent aba4310 commit 60a4d01
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
10 changes: 9 additions & 1 deletion django/thunderstore/repository/package_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
PackageVersionField,
StrictCharField,
)
from thunderstore.repository.utils import does_contain_package, has_duplicate_packages
from thunderstore.repository.utils import (
does_contain_package,
has_different_case,
has_duplicate_packages,
)


class PackageInstallerSerializer(serializers.Serializer):
Expand Down Expand Up @@ -96,6 +100,10 @@ def validate(self, data):
)
if does_contain_package(result["dependencies"], reference):
raise ValidationError("Package depending on itself is not allowed")
if has_different_case(reference.without_version):
raise ValidationError(
"A similar package already exists with different capitalization"
)
return result

def update(self, instance, validated_data):
Expand Down
12 changes: 12 additions & 0 deletions django/thunderstore/repository/package_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,15 @@ def exists(self) -> bool:
:rtype: bool
"""
return self.queryset.exists()

@cached_property
def exists_in_any_case(self) -> bool:
"""
Check if a package with a name with different capitalization exists in the db
:return: True if a different case package exists, False otherwise
:rtype: bool
"""
return Package.objects.filter(
namespace=self.namespace, name__iexact=self.name
).exists()
8 changes: 8 additions & 0 deletions django/thunderstore/repository/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ def has_duplicate_packages(packages: List["PackageReference"]) -> bool:
return False


def has_different_case(reference: "PackageReference") -> bool:
if reference.exists:
return False
elif reference.exists_in_any_case:
return True
return False


def unpack_serializer_errors(field, errors, error_dict=None):
if error_dict is None:
error_dict = {}
Expand Down

0 comments on commit 60a4d01

Please sign in to comment.