]> git.parisson.com Git - telemeta.git/commitdiff
fix auto_code
authorGuillaume Pellerin <yomguy@parisson.com>
Fri, 31 May 2013 10:09:05 +0000 (12:09 +0200)
committerGuillaume Pellerin <yomguy@parisson.com>
Fri, 31 May 2013 10:09:05 +0000 (12:09 +0200)
telemeta/views/core.py

index 28c75114ff16ba44ddec9e71b0625b4bcfcdb7df..69fe99ce2722848f46cc36e1776d7d9a8b34430f 100644 (file)
@@ -242,6 +242,24 @@ def check_related_media(medias):
             media.save()
 
 def auto_code(collection):
-    suffixes = [int(item.code.split('_')[-1]) for item in collection.items.all()]
-    return collection.code + '_' + str(max(suffixes)+1)
+    items = collection.items.all()
+    suffixes = []
+
+    if items:
+        for item in items:
+            if '_' in item.public_id:
+                try:
+                    split = item.public_id.split('_')
+                    suffix = int(split[-1])
+                    prefix = split[:-1]
+                except:
+                    suffix = 999
+
+                suffixes.append(suffix)
+
+    if suffixes:
+        return collection.code + '_' + str(max(suffixes)+1)
+    else:
+        return collection.code + '_'
+