How to generate a choicelist from all ImageSpecs

150 views Asked by At

I want to generate a choicelist for all specs that inherit from imagekit.specs.ImageSpec.

The idea is to allow users of the admin interface to select an ImageSpec to add to a picture.

i.e:

class Display(ImageSpec):
    pre_cache = True
    increment_count = True
    processors = [ResizeDisplay,]

class SingleDisplay(ImageSpec):
    pre_cache = True
    increment_count = True
    processors = [SingleDisplayResize]

class Reflection(ImageSpec):
    increment_count = True
    processors = [ResizeDisplay, ReflectionProcessor]

class SingleDisplayReflection(ImageSpec):
    increment_count = True
    processors = [SingleDisplayResize, ReflectionProcessor]

results in a drop-down list "Display, Singledisplay, Reflection, Singledisplayreflection"

1

There are 1 answers

1
Will McCutchen On BEST ANSWER

Well, something like the following will get you a list of all the ImageSpec subclasses defined in the file:

def subclassfilter(x, baseclass):
    return x is not baseclass and isinstance(x, type) and issubclass(x, baseclass)

subclasses = [c for c in locals().values() if subclassfilter(c, ImageSpec)]

You could then generate the choices list from the __name__ attribute of each class in the subclasses list.