Skip to content

Commit

Permalink
Merge pull request #1039 from thunderstore-io/autolist-default-packages
Browse files Browse the repository at this point in the history
Add autolist_package_ids to SchemaCommunity
  • Loading branch information
MythicManiac committed Sep 5, 2024
2 parents d363679 + e3d3f8e commit 39e4f1c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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),
),
]
1 change: 1 addition & 0 deletions django/thunderstore/community/models/package_listing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
1 change: 1 addition & 0 deletions django/thunderstore/schema_import/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
13 changes: 13 additions & 0 deletions django/thunderstore/schema_import/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
32 changes: 30 additions & 2 deletions django/thunderstore/schema_import/tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 39e4f1c

Please sign in to comment.