Parcourir la source

Allow to delete tiles

Baptiste Jonglez il y a 10 ans
Parent
commit
52e979ef64
2 fichiers modifiés avec 71 ajouts et 4 suppressions
  1. 12 4
      panorama/models.py
  2. 59 0
      panorama/utils.py

+ 12 - 4
panorama/models.py

@@ -13,6 +13,7 @@ from django.core.urlresolvers import reverse
 from django.utils.encoding import python_2_unicode_compatible
 
 from .tasks import generate_tiles
+from .utils import makedirs, path_exists
 
 
 EARTH_RADIUS = 6371009
@@ -131,11 +132,18 @@ class Panorama(ReferencePoint):
         return os.path.join(settings.MEDIA_URL, settings.PANORAMA_TILES_DIR,
                             str(self.pk))
 
+    def delete_tiles(self):
+        """Delete all tiles and the tiles dir"""
+        # If the directory doesn't exist, do nothing
+        if not path_exists(self.tiles_dir()):
+            return
+        # Delete all tiles
+        for filename in os.listdir(self.tiles_dir()):
+            os.unlink(os.path.join(self.tiles_dir(), filename))
+        os.rmdir(self.tiles_dir())
+
     def generate_tiles(self):
-        try:
-            os.makedirs(self.tiles_dir())
-        except OSError:
-            pass
+        makedirs(self.tiles_dir(), exist_ok=True)
         generate_tiles.delay(self.image.path, self.tiles_dir())
 
     def get_absolute_url(self):

+ 59 - 0
panorama/utils.py

@@ -0,0 +1,59 @@
+# -*- coding: utf-8 -*-
+
+"""
+OS-related utils for python2/python3 compatibility
+"""
+
+from __future__ import unicode_literals, division, print_function
+
+import sys
+import os
+
+
+if sys.version_info.major >= 3:
+    # Python 3
+    def path_exists(path):
+        """Returns True if the given file or directory exists.  May raise
+        exceptions, for instance if you don't have permission to stat()
+        the given path.
+        """
+        try:
+            os.stat(path)
+            return True
+        except FileNotFoundError:
+            return False
+
+    def makedirs(name, mode=0o777, exist_ok=False):
+        """No-op wrapper around os.makedirs()"""
+        return os.makedirs(name, mode=mode, exist_ok=exist_ok)
+
+else:
+    # Python 2
+    def path_exists(path):
+        """Returns True if the given file or directory exists.  May raise
+        exceptions, for instance if you don't have permission to stat()
+        the given path.
+        """
+        try:
+            os.stat(path)
+            return True
+        except OSError as e:
+            if e.errno == os.errno.ENOENT:
+                return False
+            else:
+                raise
+
+    def makedirs(name, mode=0o777, exist_ok=False):
+        """Wrapper around os.makedirs() to support the python3-style "exist_ok"
+        argument.
+        """
+        if not exist_ok:
+            return os.makedirs(name, mode=mode)
+        # Emulate exist_ok behaviour
+        try:
+            res = os.makedirs(name, mode=mode)
+        except OSError as e:
+            if e.errno == os.errno.EEXIST:
+                return res
+            else:
+                raise