Skip to content

Commit

Permalink
Feat(Safe Apps): admin chains with names
Browse files Browse the repository at this point in the history
  • Loading branch information
katspaugh committed Sep 19, 2024
1 parent 9209f3a commit 8339b66
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/safe_apps/admin.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
from typing import Any

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):
chain_ids = forms.ModelMultipleChoiceField(
queryset=Chain.objects.all(), widget=forms.SelectMultiple, required=False
)

class Meta:
model = SafeApp
fields = "__all__"

def clean_chain_ids(self):
"""
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 8339b66

Please sign in to comment.