modify behavior for tag modification on the datasheet
authorymh <ymh.work@gmail.com>
Fri, 17 Jun 2011 02:09:37 +0200
changeset 42 861a78f74a37
parent 41 5106283de457
child 43 e0812bc3ef44
modify behavior for tag modification on the datasheet
.settings/org.eclipse.core.resources.prefs
web/hdabo/static/hdabo/js/hdabo.js
web/hdabo/templates/list_for_orga.html
web/hdabo/templates/partial/tag_table.html
web/hdabo/urls.py
web/hdabo/views.py
web/hdabo/wp_utils.py
--- a/.settings/org.eclipse.core.resources.prefs	Thu Jun 16 13:40:10 2011 +0200
+++ b/.settings/org.eclipse.core.resources.prefs	Fri Jun 17 02:09:37 2011 +0200
@@ -1,4 +1,4 @@
-#Thu Jun 16 13:14:04 CEST 2011
+#Fri Jun 17 01:31:06 CEST 2011
 eclipse.preferences.version=1
 encoding//virtualenv/web/env/hdabo/lib/python2.6/site-packages/haystack/backends/__init__.py=utf-8
 encoding//virtualenv/web/env/hdabo/lib/python2.6/site-packages/sortedm2m/fields.py=utf-8
@@ -15,4 +15,5 @@
 encoding//web/hdabo/tests/models.py=utf-8
 encoding//web/hdabo/tests/sortedm2mfield.py=utf-8
 encoding//web/hdabo/utils.py=utf-8
+encoding//web/hdabo/views.py=utf-8
 encoding//web/hdabo/wp_utils.py=utf-8
--- a/web/hdabo/static/hdabo/js/hdabo.js	Thu Jun 16 13:40:10 2011 +0200
+++ b/web/hdabo/static/hdabo/js/hdabo.js	Fri Jun 17 02:09:37 2011 +0200
@@ -62,6 +62,11 @@
             $('#tag_table_container').html(value);
             init_tags_events();
     	},
+		onerror: function(settings, original, jqXHR) {
+			resp = $.parseJSON(jqXHR.responseText);
+			alert(resp.message);
+			original.reset();
+		},
     	autocomplete : {
 			source: function( request, response ) {
 				$.ajax({
--- a/web/hdabo/templates/list_for_orga.html	Thu Jun 16 13:40:10 2011 +0200
+++ b/web/hdabo/templates/list_for_orga.html	Fri Jun 17 02:09:37 2011 +0200
@@ -17,7 +17,7 @@
     var tag_up_down_url = "{% url hdabo.views.tag_up_down %}";
     var remove_tag_from_list_url = "{% url hdabo.views.remove_tag_from_list %}";
     var validate_wp_link_url = "{% url hdabo.views.validate_wp_link %}";
-    var modify_tag_url = "{% url hdabo.views.modify_tag %}";
+    var modify_tag_url = "{% url hdabo.views.modify_tag_datasheet %}";
     var reset_wp_info_url = "{% url hdabo.views.reset_wikipedia_info %}";
     var add_tag_url = "{% url hdabo.views.add_tag %}";
     var remove_wp_link_url = "{% url hdabo.views.remove_wp_link %}";
--- a/web/hdabo/templates/partial/tag_table.html	Thu Jun 16 13:40:10 2011 +0200
+++ b/web/hdabo/templates/partial/tag_table.html	Fri Jun 17 02:09:37 2011 +0200
@@ -17,11 +17,11 @@
     {% if ordered_tags %}
     	{# ordered_tags is a list of TaggedSheet #}
         {% for t in ordered_tags %}
-            <tr class="imageline {% cycle 'hdabooddline' 'hdaboevenline' %}">
+            <tr class="imageline {% cycle 'hdabooddline' 'hdaboevenline' %}" id="{{t.id}}">
             <td>{{forloop.counter}}</td>
             {% if valid != "2" %}
-            <td class="updown_td">{% if not forloop.first %}<img src="{{STATIC_URL}}hdabo/img/arrow_up.png" class="up" alt="up" id={{t.tag.id}} pos="{{forloop.counter0}}">{% endif %}</td>
-            <td class="updown_td">{% if not forloop.last %}<img src="{{STATIC_URL}}hdabo/img/arrow_down.png" class="down" alt="down" id={{t.tag.id}} pos="{{forloop.counter0}}">{% endif %}</td>
+            <td class="updown_td">{% if not forloop.first %}<img src="{{STATIC_URL}}hdabo/img/arrow_up.png" class="up" alt="up" id="{{t.tag.id}}" pos="{{forloop.counter0}}">{% endif %}</td>
+            <td class="updown_td">{% if not forloop.last %}<img src="{{STATIC_URL}}hdabo/img/arrow_down.png" class="down" alt="down" id="{{t.tag.id}}" pos="{{forloop.counter0}}">{% endif %}</td>
             {% endif %}
             <td class="reset_wp_info">{{t.tag.id}}</td>
             <td class="{{t.tag.url_status_text}} wikipediatag" id="{{t.tag.id}}" >{{t.tag.label}}</td>
--- a/web/hdabo/urls.py	Thu Jun 16 13:40:10 2011 +0200
+++ b/web/hdabo/urls.py	Fri Jun 17 02:09:37 2011 +0200
@@ -24,6 +24,7 @@
     url(r'^removetagfromlist$', 'hdabo.views.remove_tag_from_list'),
     url(r'^validatewplink$', 'hdabo.views.validate_wp_link'),
     url(r'^modifytag$', 'hdabo.views.modify_tag'),
+    url(r'^modifytagds$', 'hdabo.views.modify_tag_datasheet'),
     url(r'^resetwpinfo$', 'hdabo.views.reset_wikipedia_info'),
     url(r'^addtag$', 'hdabo.views.add_tag'),
     url(r'^removewplink$', 'hdabo.views.remove_wp_link'),
--- a/web/hdabo/views.py	Thu Jun 16 13:40:10 2011 +0200
+++ b/web/hdabo/views.py	Fri Jun 17 02:09:37 2011 +0200
@@ -1,13 +1,21 @@
+# -*- coding: utf-8 -*-
+
 from django.conf import settings
 from django.contrib.auth.decorators import login_required #@UnusedImport
 from django.core.paginator import Paginator
 from django.db.models import Max
+from django.http import HttpResponseBadRequest
 from django.shortcuts import render_to_response, redirect
 from django.template import RequestContext
+from haystack.constants import DJANGO_ID
+from haystack.query import SearchQuerySet
 from hdabo.management.commands.querywikipedia import process_tag
-from hdabo.wp_utils import normalize_tag, query_wikipedia_title
+from hdabo.wp_utils import (normalize_tag, query_wikipedia_title, 
+    get_or_create_tag)
 from models import Organisation, Datasheet, TaggedSheet, Tag
 from wikitools import wiki
+import django.utils.simplejson as json
+
 
 
 #@login_required
@@ -220,9 +228,7 @@
 
 #@login_required
 def modify_tag(request):
-    # 2 cases : 
-    # - ordered tag for one datasheet : POST["datasheet_id"] is not null
-    # - all tags list : POST["datasheet_id"] is null and POST["num_page"] and POST["nb_by_page"] are not null
+
     tag_id = request.POST["id"]
     tag_label = request.POST["value"]
         
@@ -230,7 +236,6 @@
     
     if tag.label != tag_label:
 
-
         tag.label = tag_label
         
         site = wiki.Wiki(settings.WIKIPEDIA_API_URL) #@UndefinedVariable
@@ -252,10 +257,39 @@
             
         tag.save()
     
-    if u"datasheet_id" in request.POST :
-        return get_tag_table(request=request, ds_id=request.POST["datasheet_id"], valid=0)
-    else :
-        return get_all_tags_table(request=request, num_page=request.POST["num_page"], nb_by_page=request.POST["nb_by_page"])
+    return get_all_tags_table(request=request, num_page=request.POST["num_page"], nb_by_page=request.POST["nb_by_page"])
+
+
+#@login_required
+def modify_tag_datasheet(request):
+
+    tag_id = request.POST["id"]
+    tag_label = request.POST["value"]
+    ds_id=request.POST["datasheet_id"]
+        
+    tag = Tag.objects.get(id=tag_id)
+    
+    ds = Datasheet.objects.get(id=ds_id)
+        
+    if tag.label != tag_label:
+        
+        if tag_label.lower() in [t.label.lower() for t in ds.tags.all()]:
+            return  HttpResponseBadRequest(json.dumps({'error': 'duplicate_tag', 'message': u"Le tag %s existe déjà pour cette fiche." % (tag_label)}), mimetype="application/json")
+
+        tag, created = get_or_create_tag(tag_label) #@UnusedVariable
+
+        ts = TaggedSheet.objects.get(tag=tag_id, datasheet=ds_id)
+        ts.tag = tag
+        kwargs = {DJANGO_ID + "__exact": unicode(ds_id)}
+        results = SearchQuerySet().filter(title=tag_label).filter_or(description=tag_label).filter(**kwargs)
+        if len(results) > 0:
+            ts.index_note = results[0].score        
+
+        ts.save()
+        
+    
+    return get_tag_table(request=request, ds_id=ds_id, valid=0)
+
 
 
 #@login_required
@@ -288,27 +322,8 @@
     
     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, created = Tag.objects.get_or_create(label__iexact=tag_label_normalized, defaults={'label':tag_label_normalized, 'original_label':tag_label})
 
-    site = wiki.Wiki(settings.WIKIPEDIA_API_URL) #@UndefinedVariable
-    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
-    if status is not None:
-        tag.url_status = status
-    if url is not None:
-        tag.wikipedia_url = url
-        tag.wikipedia_activated = True
-    if pageid is not None:
-        tag.wikipedia_pageid = pageid
-    tag.save()
+    tag, created = get_or_create_tag(tag_label)    
     # 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.get(id=ds_id)
--- a/web/hdabo/wp_utils.py	Thu Jun 16 13:40:10 2011 +0200
+++ b/web/hdabo/wp_utils.py	Fri Jun 17 02:09:37 2011 +0200
@@ -1,6 +1,7 @@
 # -*- coding: utf-8 -*-
+from django.conf import settings
 from hdabo.models import Tag
-from wikitools import api
+from wikitools import api, wiki
 
 def __is_homonymie(page_dict):
     for cat in page_dict.get(u"categories", []):
@@ -47,3 +48,39 @@
     tag = " ".join(tag.split())
     tag = tag[0].upper() + tag[1:]
     return tag
+
+
+def get_or_create_tag(tag_label):
+    
+    tag_label_normalized = normalize_tag(tag_label)
+    # We get the wikipedia references for the tag_label
+    # We get or create the tag object
+    
+    tag, created = Tag.objects.get_or_create(label__iexact=tag_label_normalized, defaults={'label':tag_label_normalized, 'original_label':tag_label})
+
+    if created:
+        site = wiki.Wiki(settings.WIKIPEDIA_API_URL) #@UndefinedVariable
+        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
+        if status is not None:
+            tag.url_status = status
+        if url is not None:
+            tag.wikipedia_url = url
+            tag.wikipedia_activated = True
+        else:
+            tag.wikipedia_url = None
+            tag.wikipedia_activated = False
+            
+        if pageid is not None:
+            tag.wikipedia_pageid = pageid
+        else:
+            tag.wikipedia_pageid = None
+
+        tag.save()
+        
+    return tag, created
+
+