Ejemplo n.º 1
0
    def save(self, name, content, save=True):
        # if the _committed attribute (from the super-class) is True, there is no new image to upload
        # that means we are either just changing coordinates (in which case name = coordinates) or not changing anything at all (use self.coords_csv)
        if self._committed:
            coords_csv = self.coords_csv if hasattr(self, 'coords_csv') else name
            stashed_filepath = getattr(self.instance, self.field.name + '_stashed_name').split(IMAGE_FIELD_DELIMITER)[0]
            compound_name = IMAGE_FIELD_DELIMITER.join([stashed_filepath, coords_csv])

            # get imagekit spec field names --> spec files
            spec_dict = dict(zip(self.instance._ik.spec_fields, self.instance._ik.spec_files))

            # invalidate imagekit spec files
            if self.field.invalidate_on_save:
                self.name = self.filename if hasattr(self, 'filename') else stashed_filepath
                self.coords_csv = self.coords_csv if hasattr(self, 'coords_csv') else coords_csv
                for spec_name in self.field.invalidate_on_save:
                    if spec_name in spec_dict:
                        spec = spec_dict.get(spec_name)
                        spec.delete()
                        spec.invalidate()
        
        else:
            # call the super's save() which will save to storage with the proper filename
            # explicitly set the "save" arg to False so that model isn't saved (we don't want to save with actual filename)
            storage_name = self.field.generate_filename(self.instance, self.filename)
            super(CroppableImageFieldFile, self).save(storage_name, content, save=False)

            # create compound filename to save to the model in the database
            # must be done *after* the call to the super class in case the storage backend changes the filename (e.g. duplicates)
            actual_filepath = getattr(self.instance, self.field.name).name
            compound_name = IMAGE_FIELD_DELIMITER.join([actual_filepath, self.coords_csv])

        # update name on both self & model instance to compound_name which is what we want to save to the database
        setattr(self.instance, self.field.name, compound_name) 
        self.name = compound_name

        # if we were supposed to save the model, now we should actually do it since the filename was reverted above
        # (the below 2 lines are also the last 2 lines in the super.save() from Django)
        if save:
            self.instance.save()
Ejemplo n.º 2
0
    def compress(self, data_list):
        if data_list:
            # if the widget returns False as opposed to None, the "clear" checkbox was checked (this is a Django thing)
            if data_list[0] is False:
                return False

            image_file = data_list[0]
            coords_csv = data_list[1]

            # an image won't exist here if the image field is being cleared by the user
            if image_file:
                if IMAGE_FIELD_DELIMITER in image_file.name:
                    raise forms.ValidationError('You cannot upload image files containing the image field delimiter: \"' + \
                                                IMAGE_FIELD_DELIMITER + '\".  You can configure this with the \
                                                IMAGE_FIELD_DELIMITER setting')

                image_file.name = IMAGE_FIELD_DELIMITER.join([image_file.name, coords_csv])
            
                return image_file

            if coords_csv:
                return coords_csv

        return None