From bd6dbb073c91f50ba7808a5ad419a8ff90647349 Mon Sep 17 00:00:00 2001 From: Guillaume Pellerin Date: Fri, 31 May 2013 12:09:05 +0200 Subject: [PATCH] fix auto_code --- telemeta/views/core.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/telemeta/views/core.py b/telemeta/views/core.py index 28c75114..69fe99ce 100644 --- a/telemeta/views/core.py +++ b/telemeta/views/core.py @@ -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 + '_' + -- 2.39.5