v0.8.8 : avoid imported collection doubloon V00.08.08
authorcavaliet
Fri, 14 Mar 2014 16:27:42 +0100
changeset 266 09051e8a61dd
parent 265 3f2a118121b7
child 267 b69347b6a914
v0.8.8 : avoid imported collection doubloon
src/egonomy/__init__.py
src/egonomy/views.py
--- a/src/egonomy/__init__.py	Fri Mar 14 12:04:25 2014 +0100
+++ b/src/egonomy/__init__.py	Fri Mar 14 16:27:42 2014 +0100
@@ -1,4 +1,4 @@
-VERSION = (0, 8, 7, "final", 1)
+VERSION = (0, 8, 8, "final", 1)
 
 VERSION_STR = unicode(".".join(map(lambda i:"%02d" % (i,), VERSION[:2])))
 
--- a/src/egonomy/views.py	Fri Mar 14 12:04:25 2014 +0100
+++ b/src/egonomy/views.py	Fri Mar 14 16:27:42 2014 +0100
@@ -366,7 +366,7 @@
     frg.image = img
     frg.coordinates = frg_path
     # We build the svg xml
-    file = os.path.join(settings.MEDIA_ROOT, str(img.info.image_file))
+    file_path = os.path.join(settings.MEDIA_ROOT, str(img.info.image_file))
     image_file = default.kvstore.get_or_set(ImageFile(img.info.image_file))
     ratio = int(100 * frg.ratio * image_file.ratio)
     svg = '<svg preserveAspectRatio="none" width="' + str(ratio) + 'px" height="100px" viewBox="' + frg.viewbox +'" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">\n\
@@ -375,8 +375,8 @@
            <path d="' + frg_path + '" />\n\
        </clipPath>\n\
     </defs>\n\
-    <image xlink:href="' + file + '" x="0" y="0" preserveAspectRatio="none" width="1" height="1" opacity=".1"/>\n\
-    <image xlink:href="' + file + '" x="0" y="0" preserveAspectRatio="none" width="1" height="1" clip-path="url(#fragment-clip)"/>\n\
+    <image xlink:href="' + file_path + '" x="0" y="0" preserveAspectRatio="none" width="1" height="1" opacity=".1"/>\n\
+    <image xlink:href="' + file_path + '" x="0" y="0" preserveAspectRatio="none" width="1" height="1" clip-path="url(#fragment-clip)"/>\n\
 </svg>'
     # We save the svg file
     uid = str(uuid.uuid1())
@@ -387,7 +387,7 @@
     # We execute the batik command
     args = ["java", "-jar", settings.BATIK_RASTERIZER_PATH, svg_file.name]
     p = subprocess.Popen(args, stdout=subprocess.PIPE,stderr=subprocess.PIPE)
-    out, err = p.communicate()
+    _, err = p.communicate()
     if p.returncode!=0:
         return HttpResponse("Batik error : " + str(err))
     
@@ -804,6 +804,30 @@
                 return HttpResponse("Wrong image id. It must be like 'XX/XXXXXXX.jpg'.", status=400)
     if len(img_ids)==0:
         return HttpResponse("imagelist must not be empty.")
+    # First we check if the user has not imported a collection with exactly the same images.
+    user_collections = Collection.objects.filter(author=request.user)
+    img_content_type = ContentType.objects.get_for_model(Image)
+    for uc in user_collections:
+        items = CollectionItem.objects.filter(collection=uc).select_related('content_type', 'object_id').order_by("order")
+        # We check length
+        if len(items)==len(img_ids):
+            # We check if all items are images
+            all_items_are_img = True
+            for i in items:
+                if i.content_type != img_content_type:
+                    all_items_are_img = False
+                    break
+            # If necessary, we check if all img's ids are the same
+            if all_items_are_img:
+                col_img_ids = items.values_list('object_id', flat=True)
+                same_ids = True
+                for i in img_ids:
+                    if i not in col_img_ids:
+                        same_ids = False
+                        break
+                if same_ids:
+                    return redirect("view_collection", collection_pk=uc.pk)
+            
     # New collection
     col = Collection() 
     col.author = request.user
@@ -813,13 +837,12 @@
     col.publication_type = 1
     col.public = True
     col.save()
-    content_type = ContentType.objects.get_for_model(Image)
     order = 1
     for i in img_ids:
         item = get_object_or_404(Image, id=i)
         col_item = CollectionItem()
         col_item.collection = col
-        col_item.content_type = content_type
+        col_item.content_type = img_content_type
         col_item.object_id = i
         col_item.content_object = item
         col_item.description = ""