Add get_all_cmdsets` helper inspired by work by user trhr in unmerged PR

This commit is contained in:
Griatch 2021-05-21 15:22:35 +02:00
parent 12a251cdcc
commit 73f73f2473
4 changed files with 73 additions and 8 deletions

View file

@ -79,6 +79,15 @@ class ObjectCreateForm(forms.ModelForm):
required=False,
widget=forms.TextInput(attrs={"size": "78"}),
)
# This is not working well because it will not properly allow an empty choice, and will
# also not work well for comma-separated storage without more work. Notably, it's also
# a bit hard to visualize.
# db_cmdset_storage = forms.MultipleChoiceField(
# label="CmdSet",
# required=False,
# choices=adminutils.get_and_load_typeclasses(parent=ObjectDB))
db_location = forms.ModelChoiceField(
ObjectDB.objects.all(),
label="Location",

View file

@ -4,7 +4,7 @@ Helper utils for admin views.
"""
import importlib
from evennia.utils.utils import get_all_typeclasses, inherits_from
from evennia.utils.utils import get_all_typeclasses, inherits_from, get_all_cmdsets
def get_and_load_typeclasses(parent=None, excluded_parents=None):
@ -46,6 +46,34 @@ def get_and_load_typeclasses(parent=None, excluded_parents=None):
"evennia.scripts.models.ScriptDB",
"evennia.comms.models.ChannelDB",)]
# return on form exceptedaccepted by ChoiceField
# return on form excepted by ChoiceField
return [(path, path) for path in tpaths if path]
def get_and_load_cmdsets(parent=None, excluded_parents=None):
"""
Get all cmdsets available or as children based on a parent cmdset. We need
to initialize things here to make sure as much as possible is loaded in the
admin process. This is intended to be used with forms.ChoiceField.
Args:
parent (str, optional): Python-path to the parent cmdset, if any.
excluded_parents (list): A list of cmset-paths to exclude from the result.
Returns:
list: A list of (str, str), the way ChoiceField wants it.
"""
# we must do this to have cmdsets imported and accessible in the inheritance tree.
import evennia
evennia._init()
cmap = get_all_cmdsets(parent)
excluded_parents = excluded_parents or []
cpaths = [path for path in cmap if not any(path == excluded for excluded in excluded_parents)]
cpaths = sorted(cpaths, key=lambda k: (1 if k.startswith("evennia.") else 0, k))
# return on form expected by ChoiceField
return [("", "-")] + [(path, path) for path in cpaths if path]