From fc42ad33244523b1afaa3bbc02c8ce2f1d5a0f6c Mon Sep 17 00:00:00 2001 From: "753.network" Date: Thu, 6 Jun 2024 01:37:46 -0400 Subject: [PATCH 1/2] Add autolist_package_ids to SchemaCommunity The Community Schema now has an autolist_package_ids property, which allows for creating listings for specified packages in the community when the schema is synced. --- .../0029_packagelisting_is_auto_imported.py | 18 ++++++++++++++++++ .../community/models/package_listing.py | 1 + django/thunderstore/schema_import/schema.py | 1 + django/thunderstore/schema_import/sync.py | 13 +++++++++++++ 4 files changed, 33 insertions(+) create mode 100644 django/thunderstore/community/migrations/0029_packagelisting_is_auto_imported.py diff --git a/django/thunderstore/community/migrations/0029_packagelisting_is_auto_imported.py b/django/thunderstore/community/migrations/0029_packagelisting_is_auto_imported.py new file mode 100644 index 000000000..61c8bb53d --- /dev/null +++ b/django/thunderstore/community/migrations/0029_packagelisting_is_auto_imported.py @@ -0,0 +1,18 @@ +# Generated by Django 3.1.7 on 2024-06-05 18:40 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("community", "0028_add_cover_image_fields"), + ] + + operations = [ + migrations.AddField( + model_name="packagelisting", + name="is_auto_imported", + field=models.BooleanField(default=False), + ), + ] diff --git a/django/thunderstore/community/models/package_listing.py b/django/thunderstore/community/models/package_listing.py index 03c97f29b..e0b7a3263 100644 --- a/django/thunderstore/community/models/package_listing.py +++ b/django/thunderstore/community/models/package_listing.py @@ -85,6 +85,7 @@ class PackageListing(TimestampMixin, AdminLinkMixin, models.Model): blank=True, ) has_nsfw_content = models.BooleanField(default=False) + is_auto_imported = models.BooleanField(default=False) class Meta: constraints = [ diff --git a/django/thunderstore/schema_import/schema.py b/django/thunderstore/schema_import/schema.py index 766e4fe6e..da708b724 100644 --- a/django/thunderstore/schema_import/schema.py +++ b/django/thunderstore/schema_import/schema.py @@ -19,6 +19,7 @@ class SchemaCommunity(BaseModel): sections: Dict[str, SchemaThunderstoreSection] discord_url: Optional[str] = Field(alias="discordUrl") wiki_url: Optional[str] = Field(alias="wikiUrl") + autolist_package_ids: Optional[List[str]] = Field(alias="autolistPackageIds") class SchemaGameMeta(BaseModel): diff --git a/django/thunderstore/schema_import/sync.py b/django/thunderstore/schema_import/sync.py index 8f3072c63..c088338c9 100644 --- a/django/thunderstore/schema_import/sync.py +++ b/django/thunderstore/schema_import/sync.py @@ -5,10 +5,12 @@ from thunderstore.community.models import ( Community, PackageCategory, + PackageListing, PackageListingSection, ) from thunderstore.core.utils import ExceptionLogger from thunderstore.repository.models import PackageInstaller +from thunderstore.repository.package_reference import PackageReference from thunderstore.schema_import.schema import ( Schema, SchemaCommunity, @@ -46,6 +48,17 @@ def import_community(identifier: str, schema: SchemaCommunity): community.wiki_url = schema.wiki_url community.save() + if schema.autolist_package_ids: + for package_id in schema.autolist_package_ids: + with ExceptionLogger(continue_on_error=True): + package = PackageReference.parse(package_id).package + if package.get_package_listing(community) is None: + PackageListing.objects.create( + package=package, + community=community, + is_auto_imported=True, + ) + for k, v in schema.categories.items(): if not ( category := PackageCategory.objects.filter( From e3d3f8e9f13d2f2efd4e6bc07e0b85e59a7d63fa Mon Sep 17 00:00:00 2001 From: Oksamies Date: Tue, 3 Sep 2024 13:50:09 +0300 Subject: [PATCH 2/2] Add test case for autoimporting package listings Add a test case which ensures the ecosystem schema autolisted package IDs field is imported correctly --- .../schema_import/tests/test_sync.py | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/django/thunderstore/schema_import/tests/test_sync.py b/django/thunderstore/schema_import/tests/test_sync.py index 6d62bdf34..abd29ade9 100644 --- a/django/thunderstore/schema_import/tests/test_sync.py +++ b/django/thunderstore/schema_import/tests/test_sync.py @@ -5,10 +5,16 @@ PackageCategory, PackageListingSection, ) -from thunderstore.repository.models import PackageInstaller -from thunderstore.schema_import.schema import Schema, SchemaPackageInstaller +from thunderstore.community.models.package_listing import PackageListing +from thunderstore.repository.models import Package, PackageInstaller +from thunderstore.schema_import.schema import ( + Schema, + SchemaCommunity, + SchemaPackageInstaller, +) from thunderstore.schema_import.sync import ( get_slogan_from_display_name, + import_schema_communities, import_schema_package_installers, ) from thunderstore.schema_import.tasks import sync_ecosystem_schema @@ -30,12 +36,14 @@ def test_schema_sync(): assert Community.objects.count() == 0 assert PackageCategory.objects.count() == 0 assert PackageListingSection.objects.count() == 0 + assert PackageListing.objects.filter(is_auto_imported=True).count() == 0 sync_ecosystem_schema.delay() com_count = Community.objects.count() cat_count = PackageCategory.objects.count() sec_count = PackageListingSection.objects.count() + assert com_count > 0 assert cat_count > 0 assert sec_count > 0 @@ -65,3 +73,23 @@ def test_import_schema_installers(): import_schema_package_installers(schema) assert PackageInstaller.objects.count() == 1 assert PackageInstaller.objects.first().identifier == "foo" + + +@pytest.mark.django_db +def test_import_autolisted_packages(active_package: Package): + schema = Schema( + schemaVersion="0.0.1", + games=dict(), + communities={ + "test": SchemaCommunity( + displayName="Test community", + categories=dict(), + sections=dict(), + autolistPackageIds=[active_package.full_package_name], + ), + }, + packageInstallers=dict(), + ) + assert PackageListing.objects.filter(is_auto_imported=True).count() == 0 + import_schema_communities(schema) + assert PackageListing.objects.filter(is_auto_imported=True).count() == 1