{"action":"created","issue":{"url":"https://api.github.com/repos/jdriscoll/django-photologue/issues/147","labels_url":"https://api.github.com/repos/jdriscoll/django-photologue/issues/147/labels{/name}","comments_url":"https://api.github.com/repos/jdriscoll/django-photologue/issues/147/comments","events_url":"https://api.github.com/repos/jdriscoll/django-photologue/issues/147/events","html_url":"https://github.com/jdriscoll/django-photologue/issues/147","id":124571648,"number":147,"title":"Changing \"Photo\" image leaves extra files on server","user":{"login":"gjstein","id":10986158,"avatar_url":"https://avatars.githubusercontent.com/u/10986158?v=3","gravatar_id":"","url":"https://api.github.com/users/gjstein","html_url":"https://github.com/gjstein","followers_url":"https://api.github.com/users/gjstein/followers","following_url":"https://api.github.com/users/gjstein/following{/other_user}","gists_url":"https://api.github.com/users/gjstein/gists{/gist_id}","starred_url":"https://api.github.com/users/gjstein/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gjstein/subscriptions","organizations_url":"https://api.github.com/users/gjstein/orgs","repos_url":"https://api.github.com/users/gjstein/repos","events_url":"https://api.github.com/users/gjstein/events{/privacy}","received_events_url":"https://api.github.com/users/gjstein/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2016-01-02T02:32:24Z","updated_at":"2016-01-05T20:59:39Z","closed_at":"2016-01-05T20:59:39Z","body":"Changing the `image` property of the `ImageModel` class from the admin interface leaves a number of files on the server. When the `save` function is called, it relies on a call to `self.clear_cache()` to delete the images from the server. However, value of `self.image` is changes before `save` is called, which means that it is looking for the images in the wrong place. Rather, it should be using the value of `self._old_image.name` for calculations.\r\n\r\nThere is no-doubt a more elegant way to factor the code, but I solved the problem by modifying a portion of the `save` function in the `ImageModel` class:\r\n\r\n```python\r\n    def save(self, *args, **kwargs):\r\n\r\n        image_has_changed = False\r\n        if self._get_pk_val() and (self._old_image != self.image):\r\n            image_has_changed = True\r\n\r\n            # ==== BEGIN CHANGES ====\r\n\r\n            # Delete base image\r\n            self._old_image.storage.delete(self._old_image.name)\r\n\r\n            # Get components of filenames\r\n            cache_path = os.path.join(os.path.dirname(self._old_image.name), \"cache\")\r\n            old_filename = os.path.basename(force_text(self._old_image.name))\r\n            base, ext = os.path.splitext(old_filename)\r\n            cache = PhotoSizeCache()\r\n\r\n            # Loop through file sizes\r\n            for photosize in cache.sizes.values():\r\n                # Get filename & delete files\r\n                size_name = photosize.name\r\n                filename = os.path.join(cache_path,''.join([base, '_', size_name, ext]))\r\n                if self._old_image.storage.exists(filename):\r\n                    self._old_image.storage.delete(filename)\r\n\r\n            # ==== END CHANGES ====\r\n\r\n        if self.date_taken is None or image_has_changed:\r\n            # Attempt to get the date the photo was taken from the EXIF data.\r\n            try:\r\n                exif_date = self.EXIF(self.image.file).get('EXIF DateTimeOriginal', None)\r\n                if exif_date is not None:\r\n                    d, t = str.split(exif_date.values)\r\n                    year, month, day = d.split(':')\r\n                    hour, minute, second = t.split(':')\r\n                    self.date_taken = datetime(int(year), int(month), int(day),\r\n                                               int(hour), int(minute), int(second))\r\n            except:\r\n                logger.error('Failed to read EXIF DateTimeOriginal', exc_info=True)\r\n        super(ImageModel, self).save(*args, **kwargs)\r\n        self.pre_cache()\r\n```"},"comment":{"url":"https://api.github.com/repos/jdriscoll/django-photologue/issues/comments/169132395","html_url":"https://github.com/jdriscoll/django-photologue/issues/147#issuecomment-169132395","issue_url":"https://api.github.com/repos/jdriscoll/django-photologue/issues/147","id":169132395,"user":{"login":"richardbarran","id":94111,"avatar_url":"https://avatars.githubusercontent.com/u/94111?v=3","gravatar_id":"","url":"https://api.github.com/users/richardbarran","html_url":"https://github.com/richardbarran","followers_url":"https://api.github.com/users/richardbarran/followers","following_url":"https://api.github.com/users/richardbarran/following{/other_user}","gists_url":"https://api.github.com/users/richardbarran/gists{/gist_id}","starred_url":"https://api.github.com/users/richardbarran/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/richardbarran/subscriptions","organizations_url":"https://api.github.com/users/richardbarran/orgs","repos_url":"https://api.github.com/users/richardbarran/repos","events_url":"https://api.github.com/users/richardbarran/events{/privacy}","received_events_url":"https://api.github.com/users/richardbarran/received_events","type":"User","site_admin":false},"created_at":"2016-01-05T20:59:39Z","updated_at":"2016-01-05T20:59:39Z","body":"I've just committed some code that should fix this - thanks for your help!"}}