Added a comment attribute to image versions.
authorJoel Rosdahl <joel@rosdahl.net>
Fri, 11 Mar 2005 19:38:06 +0000 (19:38 +0000)
committerJoel Rosdahl <joel@rosdahl.net>
Fri, 11 Mar 2005 19:38:06 +0000 (19:38 +0000)
src/cmdline/kofoto
src/lib/kofoto/shelf.py
src/lib/kofoto/shelfupgrade.py
src/test/shelftests.py

index d6a63a3..8d5c105 100755 (executable)
@@ -135,6 +135,8 @@ imageversionCommandsDefinitionList = [
      "Traverse the given paths and print whether each found file is a registered, modified, moved or unregistered image version or a non-image."),
     ("make-primary IMAGEVERSION [IMAGEVERSION ...]",
      "Make an image version the primary version."),
+    ("set-imageversion-comment VALUE IMAGEVERSION [IMAGEVERSION ...]",
+     "Set comment of the given image versions."),
     ("set-imageversion-image IMAGE IMAGEVERSION [IMAGEVERSION ...]",
      "Set the image to which the given image versions belong."),
     ("set-imageversion-type IMAGEVERSIONTYPE IMAGEVERSION [IMAGEVERSION ...]",
@@ -566,10 +568,12 @@ def cmdGetImageVersions(env, args):
         if env.printIDs:
             env.out("%s\n" % iv.getId())
         elif env.verbose:
-            env.out("%s%s\n  %s\n" % (
+            env.out("%s\n  %s%s\n" % (
+                iv.getLocation(),
                 iv.isPrimary() and "Primary, " or "",
-                iv.getType(),
-                iv.getLocation()))
+                iv.getType()))
+            if iv.getComment():
+                env.out("  %s\n" % iv.getComment())
         else:
             env.out("%s\n" % iv.getLocation())
 
@@ -842,6 +846,14 @@ def cmdSetCategoryDescription(env, args):
     env.shelf.getCategoryByTag(args[0]).setDescription(args[1])
 
 
+def cmdSetImageVersionComment(env, args):
+    if len(args) < 2:
+        raise ArgumentError
+    comment = args[0]
+    for iv in args[1:]:
+        sloppyGetImageVersion(env, iv).setComment(comment)
+
+
 def cmdSetImageVersionImage(env, args):
     if len(args) < 2:
         raise ArgumentError
@@ -952,6 +964,7 @@ commandTable = {
     "search": cmdSearch,
     "set-attribute": cmdSetAttribute,
     "set-category-description": cmdSetCategoryDescription,
+    "set-imageversion-comment": cmdSetImageVersionComment,
     "set-imageversion-image": cmdSetImageVersionImage,
     "set-imageversion-type": cmdSetImageVersionType,
     "sort-album": cmdSortAlbum,
index e88972c..4acd1a6 100644 (file)
@@ -157,6 +157,9 @@ schema = """
         -- Type (original, important or other).
         type        VARCHAR(20) NOT NULL,
 
+        -- Arbitrary comment about the version.
+        comment     TEXT NOT NULL,
+
         -- Identifier string which is derived from the image data and
         -- identifies the image uniquely. Currently an MD5 checksum
         -- (in hex format) of all image data.
@@ -762,10 +765,10 @@ class Shelf:
         cursor = self.connection.cursor()
         cursor.execute(
             " select id, image, type, hash, directory, filename, mtime,"
-            "        width, height"
+            "        width, height, comment"
             " from   image_version")
         for (ivid, imageid, ivtype, ivhash, directory,
-             filename, mtime, width, height) in cursor:
+             filename, mtime, width, height, comment) in cursor:
             location = os.path.join(directory, filename)
             if ivid in self.imageversioncache:
                 yield self.imageversioncache[ivid]
@@ -773,7 +776,7 @@ class Shelf:
                 ivtype = _imageVersionTypeIdentifierToType(ivtype)
                 yield self._imageVersionFactory(
                     ivid, imageid, ivtype, ivhash, location, mtime,
-                    width, height)
+                    width, height, comment)
 
 
     def getImageVersionsInDirectory(self, directory):
@@ -786,12 +789,12 @@ class Shelf:
         cursor = self.connection.cursor()
         cursor.execute(
             " select id, image, type, hash, directory, filename, mtime,"
-            "        width, height"
+            "        width, height, comment"
             " from   image_version"
             " where  directory = %s",
             directory)
         for (ivid, imageid, ivtype, ivhash, directory, filename,
-             mtime, width, height) in cursor:
+             mtime, width, height, comment) in cursor:
             location = os.path.join(directory, filename)
             if ivid in self.imageversioncache:
                 yield self.imageversioncache[ivid]
@@ -799,7 +802,7 @@ class Shelf:
                 ivtype = _imageVersionTypeIdentifierToType(ivtype)
                 yield self._imageVersionFactory(
                     ivid, imageid, ivtype, imghash, location, mtime,
-                    width, height)
+                    width, height, comment)
 
 
     def deleteAlbum(self, albumid):
@@ -913,9 +916,9 @@ class Shelf:
             cursor.execute(
                 " insert into image_version"
                 "     (image, type, hash, directory, filename,"
-                "      mtime, width, height)"
+                "      mtime, width, height, comment)"
                 " values"
-                "     (%s, %s, %s, %s, %s, %s, %s, %s)",
+                "     (%s, %s, %s, %s, %s, %s, %s, %s, '')",
                 image.getId(),
                 _imageVersionTypeToIdentifier(ivtype),
                 ivhash,
@@ -929,7 +932,7 @@ class Shelf:
         ivid = cursor.lastrowid
         imageversion = self._imageVersionFactory(
             ivid, image.getId(), ivtype, ivhash, location, mtime,
-            width, height)
+            width, height, u"")
         imageversion.importExifTags(False)
         if image.getPrimaryVersion() == None:
             image._makeNewPrimaryVersion()
@@ -950,7 +953,7 @@ class Shelf:
         cursor = self.connection.cursor()
         cursor.execute(
             " select id, image, type, hash, directory, filename, mtime,"
-            "        width, height"
+            "        width, height, comment"
             " from   image_version"
             " where  id = %s",
             ivid)
@@ -958,12 +961,12 @@ class Shelf:
         if not row:
             raise ImageVersionDoesNotExistError, ivid
         ivid, imageid, ivtype, ivhash, directory, filename, mtime, \
-            width, height = row
+            width, height, comment = row
         location = os.path.join(directory, filename)
         ivtype = _imageVersionTypeIdentifierToType(ivtype)
         return self._imageVersionFactory(
             ivid, imageid, ivtype, ivhash, location, mtime,
-            width, height)
+            width, height, comment)
 
 
     def getImageVersionByHash(self, ivhash):
@@ -1328,10 +1331,10 @@ class Shelf:
 
 
     def _imageVersionFactory(self, ivid, imageid, ivtype, ivhash,
-                             location, mtime, width, height):
+                             location, mtime, width, height, comment):
         imageversion = ImageVersion(
             self, ivid, imageid, ivtype, ivhash, location, mtime, width,
-            height)
+            height, comment)
         self.imageversioncache[ivid] = imageversion
         return imageversion
 
@@ -2029,6 +2032,11 @@ class ImageVersion:
         return self.type
 
 
+    def getComment(self):
+        """Get the comment of the image version."""
+        return self.comment
+
+
     def getHash(self):
         """Get the hash of the image version."""
         return self.hash
@@ -2080,6 +2088,18 @@ class ImageVersion:
         self.shelf._setModified()
 
 
+    def setComment(self, comment):
+        self.comment = comment
+        cursor = self.shelf._getConnection().cursor()
+        cursor.execute(
+            " update image_version"
+            " set    comment = %s"
+            " where  id = %s",
+            comment,
+            self.id)
+        self.shelf._setModified()
+
+
     def makePrimary(self):
         cursor = self.shelf._getConnection().cursor()
         cursor.execute(
@@ -2232,7 +2252,7 @@ class ImageVersion:
 
     def __init__(
         self, shelf, ivid, imageid, ivtype, ivhash, location, mtime, width,
-        height):
+        height, comment):
         """Constructor of an ImageVersion."""
         self.shelf = shelf
         self.id = ivid
@@ -2242,6 +2262,7 @@ class ImageVersion:
         self.location = location
         self.mtime = mtime
         self.size = width, height
+        self.comment = comment
 
 
 class MagicAlbum(Album):
index 98c66b2..d156f25 100644 (file)
@@ -56,9 +56,9 @@ def tryUpgrade(location, toVersion):
         cursor.execute(
             " insert into image_version"
             "     (id, image, type, hash, directory, filename, mtime,"
-            "      width, height)"
+            "      width, height, comment)"
             " select imageid, imageid, 'original', hash, directory, filename,"
-            "        mtime, width, height"
+            "        mtime, width, height, ''"
             " from   old.image")
         cursor.execute(
             " select id"
index 0fd04b7..2ca355e 100755 (executable)
@@ -837,6 +837,14 @@ class TestImageVersion(TestShelfFixture):
     # ImageVersion.makePrimary tested in TestImage.test_getPrimaryversion.
     # ImageVersion.setImage tested in TestImage.test_getPrimaryversion.
     # ImageVersion.setType tested in TestImageVersion.test_getType.
+    # ImageVersion.setComment tested in TestImageVersion.test_getComment.
+
+    def test_getComment(self):
+        imageversion = self.shelf.getImageVersionByLocation(
+            os.path.join(PICDIR, "arlaharen.png"))
+        assert imageversion.getComment() == ""
+        imageversion.setComment(u"a comment")
+        assert imageversion.getComment() == u"a comment"
 
     def test_getHash(self):
         imageversion = self.shelf.getImageVersionByLocation(