-
Notifications
You must be signed in to change notification settings - Fork 10
feat: Add repo name and service id to django admin repo search #591
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: main
Are you sure you want to change the base?
Changes from all commits
3e8f261
01d5856
ac9554d
4db8f39
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 |
|---|---|---|
|
|
@@ -135,7 +135,11 @@ class Meta: | |
| class RepositoryAdmin(AdminMixin, admin.ModelAdmin): | ||
| inlines = [RepositoryTokenInline] | ||
| list_display = ("name", "service_id", "author") | ||
| search_fields = ("author__username__exact",) | ||
| search_fields = ( | ||
| "name", | ||
| "author__username__exact", | ||
| "service_id__exact", | ||
| ) | ||
| show_full_result_count = False | ||
| autocomplete_fields = ("bot",) | ||
| form = RepositoryAdminForm | ||
|
|
@@ -172,21 +176,24 @@ def get_search_results( | |
| search_term: str, | ||
| ) -> tuple[QuerySet[Repository], bool]: | ||
| """ | ||
| Search for repositories by name or repoid. | ||
| Search for repositories by name, service_id, author username, or repoid. | ||
| https://docs.djangoproject.com/en/5.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_search_results | ||
| """ | ||
| # Default search is by author username (defined in `search_fields`) | ||
| # Default search is by name, author username, and service_id (defined in `search_fields`) | ||
| queryset, may_have_duplicates = super().get_search_results( | ||
| request, | ||
| queryset, | ||
| search_term, | ||
| ) | ||
| # Also search by repoid if the search term is numeric | ||
| try: | ||
| search_term_as_int = int(search_term) | ||
| except ValueError: | ||
| pass | ||
| else: | ||
| queryset |= self.model.objects.filter(repoid=search_term_as_int) | ||
| # avoid N+1 queries for foreign key author | ||
| queryset = queryset.select_related("author") | ||
|
Comment on lines
+188
to
+196
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. Bug: Applying 🔍 Detailed AnalysisThe code applies 💡 Suggested FixApply 🤖 Prompt for AI AgentDid we get this right? 👍 / 👎 to inform future reviews. |
||
| return queryset, may_have_duplicates | ||
|
|
||
| def has_add_permission(self, _, obj=None): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # Generated by Django 4.2.25 on 2025-12-04 01:26 | ||
|
|
||
| import django.contrib.postgres.indexes | ||
| import django.db.models.functions.text | ||
| from django.db import migrations | ||
|
|
||
| from shared.django_apps.migration_utils import RiskyAddIndex | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| """ | ||
| BEGIN; | ||
| -- | ||
| -- Create index repos_name_trgm_idx on OpClass(Upper(F(name)), name=gin_trgm_ops) on model repository | ||
| -- | ||
| CREATE INDEX "repos_name_trgm_idx" ON "repos" USING gin ((UPPER("name")) gin_trgm_ops); | ||
| COMMIT; | ||
| """ | ||
|
|
||
| dependencies = [ | ||
| ("core", "0076_increment_version"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| RiskyAddIndex( | ||
| model_name="repository", | ||
| index=django.contrib.postgres.indexes.GinIndex( | ||
| django.contrib.postgres.indexes.OpClass( | ||
| django.db.models.functions.text.Upper("name"), name="gin_trgm_ops" | ||
| ), | ||
| name="repos_name_trgm_idx", | ||
| ), | ||
| ), | ||
| ] |
This comment was marked as outdated.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.