"""Reusable image gallery.

"""


#other apps have models defining certain image sizes.
#It should up be easy for them to do this alongside their model
#code.  But this is an 'import-time' activity that requires the
#database to be fully up and running.  


from django.db.models.signals import post_syncdb
#from rldjango.apps import imadj



#mechanism to ensure sizes are created.


_sizes = {}


def defineSize(id, width, height, format):
    """Other apps can call this to define their sizes. Image app ensures
    they exist when database is initialised, but won't stop you changing them.
    
    """
    _sizes[id] = [width, height, format]


def _ensureSizesExist(sender, **kwargs):
    from models import Size
    for id, (width, height, format) in _sizes.items():
        size, created = Size.objects.get_or_create(id=id,
                                                   width=width,
                                                   height=height,
                                                   width2=width,
                                                   height2=height,
                                                   format=format)
        del _sizes[id]  #avoids it being called for every table
        if created:
            print 'created size', id

post_syncdb.connect(_ensureSizesExist)

