Skip to content

Commit

Permalink
Feat(Safe Apps): admin chains with names (#1234)
Browse files Browse the repository at this point in the history
* Feat(Safe Apps): admin chains with names

* Types
  • Loading branch information
katspaugh authored Sep 20, 2024
1 parent 9209f3a commit 04dbd17
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/safe_apps/admin.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
from typing import Any
from typing import Any, List

from django import forms
from django.contrib import admin
from django.db.models import Model, QuerySet

from chains.models import Chain

from .models import Client, Feature, Provider, SafeApp, SocialProfile, Tag


# Custom form for SafeApp to use Chain model in a multi-select field
class SafeAppForm(forms.ModelForm[SafeApp]):
chain_ids = forms.ModelMultipleChoiceField(
queryset=Chain.objects.all(), widget=forms.SelectMultiple, required=True
)

class Meta:
model = SafeApp
fields = "__all__"

def clean_chain_ids(self) -> List[int]:
"""
Override clean_chain_ids to store the selected Chain IDs as a list of integers.
"""
chain_ids = self.cleaned_data["chain_ids"]
return [chain.id for chain in chain_ids]


class ChainIdFilter(admin.SimpleListFilter):
title = "Chains"
parameter_name = "chain_ids"
Expand Down Expand Up @@ -43,6 +64,7 @@ class SocialProfileInline(admin.TabularInline[Model, Model]):

@admin.register(SafeApp)
class SafeAppAdmin(admin.ModelAdmin[SafeApp]):
form = SafeAppForm # Use the custom form for SafeApp
list_display = ("name", "url", "chain_ids", "listed")
list_filter = (ChainIdFilter,)
search_fields = ("name", "url")
Expand Down

0 comments on commit 04dbd17

Please sign in to comment.