Ejemplo n.º 1
0
def change_name_of_file_on_change(sender, **kwargs):
    """
    call to create a filename when saving the changes of a model with the pattern
    ModelName_instance.pk_FieldName_YYYY-MM-DD_HH.MM.SS.ext
    this is done before saving the model
    """
    # Disable signal handler when loading fixtures
    if kwargs.get('raw', False):
        return

    from .models import ProjectUpdate

    if not kwargs.get('created', False):
        instance = kwargs['instance']
        opts = instance._meta
        for f in opts.fields:
            # extend this list of fields if needed to catch other uploads
            if isinstance(f, ImageField):
                img = getattr(instance, f.name)
                # if a new image is uploaded it resides in a InMemoryUploadedFile
                if img:
                    try:
                        if isinstance(img.file, InMemoryUploadedFile):
                            img.name = "%s_%s_%s_%s%s" % (
                                opts.object_name,
                                instance.pk or '',
                                f.name,
                                datetime.now().strftime("%Y-%m-%d_%H.%M.%S"),
                                os.path.splitext(img.name)[1],
                            )
                            # Create thumbnail for use in reports
                            if sender == ProjectUpdate:
                                get_report_thumbnail(img)
                    except Exception:
                        pass
Ejemplo n.º 2
0
def change_name_of_file_on_create(sender, **kwargs):
    """
    call to create a filename when creating a new model instance with the pattern
    ModelName_instance.pk_FieldName_YYYY-MM-DD_HH.MM.SS.ext
    Since we cannot do this until the instance of the model has been saved
    we do it as a post_save signal callback
    """
    # Disable signal handler when loading fixtures
    if kwargs.get('raw', False):
        return

    from .models import ProjectUpdate

    # kwargs['raw'] is True when we're running manage.py loaddata
    if kwargs.get('created', False) and not kwargs.get('raw', False):
        instance = kwargs['instance']
        opts = instance._meta
        for f in opts.fields:
            # extend this list of fields if needed to catch other uploads
            if isinstance(f, ImageField):
                # the actual image sits directly on the instance of the model
                img = getattr(instance, f.name)
                if img:
                    img_name = "%s_%s_%s_%s%s" % (
                        opts.object_name,
                        instance.pk or '',
                        f.name,
                        datetime.now().strftime("%Y-%m-%d_%H.%M.%S"),
                        os.path.splitext(img.name)[1],
                    )
                    save_image(img, img_name, f.name)
                    # Create thumbnail for use in reports
                    if sender == ProjectUpdate:
                        get_report_thumbnail(img)
Ejemplo n.º 3
0
    def handle(self, *args, **options):

        updates = ProjectUpdate.objects.all()

        for update in updates:
            rotate_spinner()
            if update.photo:
                get_report_thumbnail(update.photo)