-
Notifications
You must be signed in to change notification settings - Fork 30
Add category and section populators; enhance dependency handling #1241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| from typing import List | ||
|
|
||
| from thunderstore.community.models import PackageCategory | ||
| from thunderstore.core.management.commands.content.base import ( | ||
| ContentPopulator, | ||
| ContentPopulatorContext, | ||
| ) | ||
| from thunderstore.utils.iterators import print_progress | ||
|
|
||
|
|
||
| class CategoryPopulator(ContentPopulator): | ||
| categories = [ | ||
| "Mods", | ||
| "Modpacks", | ||
| "Tools", | ||
| "Libraries", | ||
| "Misc", | ||
| "Audio", | ||
| "BepInEx", | ||
| "MelonLoader", | ||
| "Suits", | ||
| "Boombox Music", | ||
| "TV Videos", | ||
| "Posters", | ||
| "Equipment", | ||
| "Items", | ||
| "Monsters", | ||
| "Moons", | ||
| "Interiors", | ||
| "Furniture", | ||
| "Vehicles", | ||
| "Client-side", | ||
| "Server-side", | ||
| "Cosmetics", | ||
| "Asset Replacements", | ||
| "Translations", | ||
| "Emotes", | ||
| "Weather", | ||
| "Hazards", | ||
| "Bug Fixes", | ||
| "Performance", | ||
| "Tweaks & Quality Of Life", | ||
| ] | ||
|
|
||
| def populate(self, context: ContentPopulatorContext) -> None: | ||
| print("Populating package categories...") | ||
| for community in print_progress(context.communities, len(context.communities)): | ||
| for name in self.categories: | ||
| slug = name.lower().replace(" ", "-").replace("&", "").replace("--", "-") | ||
| PackageCategory.objects.get_or_create( | ||
| community=community, | ||
| name=name, | ||
| defaults={"slug": slug}, | ||
| ) | ||
|
|
||
| def clear(self) -> None: | ||
| print("Deleting existing package categories...") | ||
| PackageCategory.objects.all().delete() | ||
|
Comment on lines
+1
to
+58
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Normalize line endings to pass pre-commit CI indicates mixed line endings in this file, and pre-commit is mutating it. 🧰 Tools🪛 Flake8 (7.3.0)[error] 1-1: 'typing.List' imported but unused (F401) 🪛 GitHub Actions: Build & Test[error] 1-1: Mixed line endings detected and file was modified by pre-commit hook (mixed-line-ending). 🪛 Ruff (0.15.6)[warning] 12-43: Mutable default value for class attribute (RUF012) 🤖 Prompt for AI Agents |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import random | ||
| from django.db.models import signals | ||
|
|
||
| from thunderstore.community.models import PackageListing | ||
| from thunderstore.community.models import PackageCategory, PackageListing | ||
| from thunderstore.core.management.commands.content.base import ( | ||
| ContentPopulator, | ||
| ContentPopulatorContext, | ||
|
|
@@ -19,11 +20,23 @@ def populate(self, context: ContentPopulatorContext) -> None: | |
| PackageListing.post_delete, sender=PackageListing | ||
| ) | ||
|
|
||
| community_categories = {} | ||
| for community in context.communities: | ||
| community_categories[community.id] = list( | ||
| PackageCategory.objects.filter(community=community) | ||
| ) | ||
|
|
||
| for i, package in print_progress( | ||
| enumerate(context.packages), len(context.packages) | ||
| ): | ||
| for community in context.communities: | ||
| package.get_or_create_package_listing(community) | ||
| listing = package.get_or_create_package_listing(community) | ||
|
|
||
| cats = community_categories.get(community.id, []) | ||
| if cats: | ||
| count = random.choices([1, 2, 3, 4, 5], weights=[40, 30, 15, 10, 5])[0] | ||
| chosen = random.sample(cats, min(count, len(cats))) | ||
| listing.categories.set(chosen) | ||
|
Comment on lines
+35
to
+39
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clear stale categories when a community has no available categories On reruns, Line 36 skips assignment when 🧰 Tools🪛 Ruff (0.15.6)[error] 37-37: Standard pseudo-random generators are not suitable for cryptographic purposes (S311) 🤖 Prompt for AI Agents |
||
|
|
||
| # Re-enabling previously disabled signals | ||
| signals.post_save.connect(PackageListing.post_save, sender=PackageListing) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| from thunderstore.community.models import PackageCategory, PackageListingSection | ||
| from thunderstore.core.management.commands.content.base import ( | ||
| ContentPopulator, | ||
| ContentPopulatorContext, | ||
| ) | ||
| from thunderstore.utils.iterators import print_progress | ||
|
|
||
|
|
||
| class SectionPopulator(ContentPopulator): | ||
| def populate(self, context: ContentPopulatorContext) -> None: | ||
| print("Populating package listing sections...") | ||
| for community in print_progress(context.communities, len(context.communities)): | ||
|
|
||
| def get_cat(slug): | ||
| return PackageCategory.objects.get(community=community, slug=slug) | ||
|
|
||
| # Define sections | ||
| # Note: Higher priority value means the section is shown first (descending sort). | ||
| sections_data = [ | ||
| { | ||
| "name": "Mods", | ||
| "slug": "mods", | ||
| "priority": 100, | ||
| "exclude": ["modpacks", "asset-replacements"], | ||
| "require": [], | ||
| }, | ||
| { | ||
| "name": "Asset Replacements", | ||
| "slug": "asset-replacements", | ||
| "priority": 90, | ||
| "exclude": ["modpacks"], | ||
| "require": ["asset-replacements"], | ||
| }, | ||
| { | ||
| "name": "Tools", | ||
| "slug": "tools", | ||
| "priority": 85, | ||
| "exclude": ["modpacks"], | ||
| "require": ["tools"], | ||
| }, | ||
| { | ||
| "name": "APIs & Libraries", | ||
| "slug": "libraries", | ||
| "priority": 80, | ||
| "exclude": ["modpacks"], | ||
| "require": ["libraries"], | ||
| }, | ||
| { | ||
| "name": "Modpacks", | ||
| "slug": "modpacks", | ||
| "priority": 70, | ||
| "exclude": [], | ||
| "require": ["modpacks"], | ||
| }, | ||
| ] | ||
|
|
||
| for s_data in sections_data: | ||
| section, created = PackageListingSection.objects.get_or_create( | ||
| community=community, | ||
| slug=s_data["slug"], | ||
| defaults={ | ||
| "name": s_data["name"], | ||
| "priority": s_data["priority"], | ||
| }, | ||
| ) | ||
|
|
||
| if created: | ||
| for ex_slug in s_data["exclude"]: | ||
| try: | ||
| cat = get_cat(ex_slug) | ||
| section.exclude_categories.add(cat) | ||
| except PackageCategory.DoesNotExist: | ||
| print(f"Warning: Category {ex_slug} not found for section {s_data['slug']}") | ||
|
|
||
| for req_slug in s_data["require"]: | ||
| try: | ||
| cat = get_cat(req_slug) | ||
| section.require_categories.add(cat) | ||
| except PackageCategory.DoesNotExist: | ||
| print(f"Warning: Category {req_slug} not found for section {s_data['slug']}") | ||
|
Comment on lines
+57
to
+80
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Section definitions are not synced for existing rows Because updates are gated behind Suggested fix- section, created = PackageListingSection.objects.get_or_create(
+ section, _created = PackageListingSection.objects.get_or_create(
community=community,
slug=s_data["slug"],
defaults={
"name": s_data["name"],
"priority": s_data["priority"],
},
)
-
- if created:
- for ex_slug in s_data["exclude"]:
- try:
- cat = get_cat(ex_slug)
- section.exclude_categories.add(cat)
- except PackageCategory.DoesNotExist:
- print(f"Warning: Category {ex_slug} not found for section {s_data['slug']}")
-
- for req_slug in s_data["require"]:
- try:
- cat = get_cat(req_slug)
- section.require_categories.add(cat)
- except PackageCategory.DoesNotExist:
- print(f"Warning: Category {req_slug} not found for section {s_data['slug']}")
+ section.name = s_data["name"]
+ section.priority = s_data["priority"]
+ section.save(update_fields=["name", "priority"])
+
+ exclude_cats = []
+ for ex_slug in s_data["exclude"]:
+ try:
+ exclude_cats.append(get_cat(ex_slug))
+ except PackageCategory.DoesNotExist:
+ print(f"Warning: Category {ex_slug} not found for section {s_data['slug']}")
+
+ require_cats = []
+ for req_slug in s_data["require"]:
+ try:
+ require_cats.append(get_cat(req_slug))
+ except PackageCategory.DoesNotExist:
+ print(f"Warning: Category {req_slug} not found for section {s_data['slug']}")
+
+ section.exclude_categories.set(exclude_cats)
+ section.require_categories.set(require_cats)🤖 Prompt for AI Agents |
||
|
|
||
| def clear(self) -> None: | ||
| print("Deleting existing package listing sections...") | ||
| PackageListingSection.objects.all().delete() | ||
|
Comment on lines
+1
to
+84
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Normalize line endings in this file Pipeline shows mixed line endings and pre-commit auto-modifies this file. 🧰 Tools🪛 GitHub Actions: Build & Test[error] 1-1: Mixed line endings detected and file was modified by pre-commit hook (mixed-line-ending). 🪛 Ruff (0.15.6)[warning] 15-15: Function definition does not bind loop variable (B023) 🤖 Prompt for AI Agents |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove unused import
ListLine 1 imports
Listbut nothing in this file uses it (F401).Suggested fix
📝 Committable suggestion
🧰 Tools
🪛 Flake8 (7.3.0)
[error] 1-1: 'typing.List' imported but unused
(F401)
🪛 GitHub Actions: Build & Test
[error] 1-1: Mixed line endings detected and file was modified by pre-commit hook (mixed-line-ending).
🤖 Prompt for AI Agents