'Update field\'s width and height fields, if defined.
This method is hooked up to model\'s post_init signal to update
dimensions after instantiating a model instance.  However, dimensions
won\'t be updated if the dimensions fields are already populated.  This
avoids unnecessary recalculation when loading an object from the
database.
Dimensions can be forced to update with force=True, which is how
ImageFileDescriptor.__set__ calls this method.'

def update_dimension_fields(self, instance, force=False, *args, **kwargs):

    has_dimension_fields = (self.width_field or self.height_field)
    if ((not has_dimension_fields) or (self.attname not in instance.__dict__)):
        return
    file = getattr(instance, self.attname)
    if ((not file) and (not force)):
        return
    dimension_fields_filled = (not ((self.width_field and (not getattr(instance, self.width_field))) or (self.height_field and (not getattr(instance, self.height_field)))))
    if (dimension_fields_filled and (not force)):
        return
    if file:
        width = file.width
        height = file.height
    else:
        width = None
        height = None
    if self.width_field:
        setattr(instance, self.width_field, width)
    if self.height_field:
        setattr(instance, self.height_field, height)
