correct tag creation
authorymh <ymh.work@gmail.com>
Tue, 14 Jun 2011 14:17:12 +0200
changeset 29 d12f11960bb6
parent 28 2cc86e8db2ec
child 30 c21f88ad164d
correct tag creation
web/hdabo/views.py
--- a/web/hdabo/views.py	Tue Jun 14 14:02:23 2011 +0200
+++ b/web/hdabo/views.py	Tue Jun 14 14:17:12 2011 +0200
@@ -207,15 +207,25 @@
     
     ds_id = request.POST["datasheet_id"]
     tag_label = request.POST["value"]
+    
+    tag_label_normalized = normalize_tag(tag_label)
     # We get the wikipedia references for the tag_label
+    # We get or create the tag object
+    tag_objs = Tag.objects.filter(label__iexact=tag_label_normalized)
+    if len(tag_objs) == 0:
+        tag = Tag(label=tag_label_normalized, original_label=tag_label)
+        tag.save()
+        created=True
+    else:
+        tag = tag_objs[0]
+        created=False
+
     site = wiki.Wiki(settings.WIKIPEDIA_API_URL) #@UndefinedVariable
-    new_label, status, url, pageid, response = query_wikipedia_title(site, tag_label) #@UnusedVariable
-    # We get or create the tag object
-    tag, created = Tag.objects.get_or_create(label=new_label)
+    new_label, status, url, pageid, response = query_wikipedia_title(site, tag_label_normalized) #@UnusedVariable
+
     # We save the datas
     if new_label is not None:
         tag.label = new_label
-        tag.original_label = new_label
     if status is not None:
         tag.url_status = status
     if url is not None:
@@ -226,7 +236,7 @@
     tag.save()
     # We put the tag at the bottom of the datasheet's tag list 
     # if the tag is created or if the tag is not in the list
-    ds = Datasheet.objects.filter(id=ds_id)[0]
+    ds = Datasheet.objects.get(id=ds_id)
     list_ts = TaggedSheet.objects.filter(datasheet=ds)
     if created or len(list_ts.filter(tag=tag))==0 :
         new_order = list_ts.aggregate(Max('order'))['order__max'] + 1