1

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"

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178

1 Answers1

2

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.

Will McCutchen
  • 13,047
  • 3
  • 44
  • 43