Skip to content

Commit

Permalink
Use lazy function to get choices and initial in form
Browse files Browse the repository at this point in the history
  • Loading branch information
medihack committed Feb 26, 2024
1 parent fc725b4 commit a02660c
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions radis/search/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,30 @@
from django import forms
from django.db.models import F, Func
from django.urls import reverse
from django.utils.functional import lazy

from radis.reports.models import Report
from radis.search.layouts import QueryInput, RangeSlider

from .site import search_providers

SEARCH_PROVIDER_CHOICES = sorted(
[(provider.name, provider.name) for provider in search_providers.values()]
)

def get_search_providers():
return sorted([(provider.name, provider.name) for provider in search_providers.values()])


def get_initial_provider():
return get_search_providers()[0][0]


class SearchForm(forms.Form):
# Query fields
query = forms.CharField(required=False, label=False)
provider = forms.ChoiceField(
required=False,
choices=SEARCH_PROVIDER_CHOICES,
# TODO: in Django 5 choices and initial can be passed a function directly
choices=lazy(get_search_providers, tuple)(),
initial=lazy(get_initial_provider, str)(),
label=False,
)
# Filter fields
Expand Down Expand Up @@ -64,9 +71,6 @@ class SearchForm(forms.Form):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

if SEARCH_PROVIDER_CHOICES:
self.fields["provider"].initial = SEARCH_PROVIDER_CHOICES[0][0]

# TODO: put an index to modalities_in_study
# https://stackoverflow.com/a/4059785/166229
modalities = (
Expand Down

0 comments on commit a02660c

Please sign in to comment.