add command to reset wikipedia info on a tag
authorymh <ymh.work@gmail.com>
Tue, 14 Jun 2011 12:56:48 +0200
changeset 25 e5f8cb1020c8
parent 24 8f84b9bbd22d
child 27 056c19e37eab
add command to reset wikipedia info on a tag
.settings/org.eclipse.core.resources.prefs
web/hdabo/management/commands/importcsv.py
web/hdabo/management/commands/querywikipedia.py
web/hdabo/static/hdabo/js/hdabo.js
web/hdabo/templates/partial/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	Tue Jun 14 01:51:26 2011 +0200
+++ b/.settings/org.eclipse.core.resources.prefs	Tue Jun 14 12:56:48 2011 +0200
@@ -1,10 +1,11 @@
-#Tue Jun 14 01:30:55 CEST 2011
+#Tue Jun 14 10:02:51 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
 encoding//virtualenv/web/env/hdabo/lib/python2.6/site-packages/sortedm2m/forms.py=utf-8
 encoding//virtualenv/web/env/hdabo/lib/python2.6/site-packages/sortedm2m/tests.py=utf-8
 encoding//virtualenv/web/env/hdabo/lib/python2.6/site-packages/whoosh/analysis.py=utf8
+encoding//virtualenv/web/env/hdabo/lib/python2.6/site-packages/wikitools/wiki.py=utf-8
 encoding//web/hdabo/fields.py=utf-8
 encoding//web/hdabo/forms.py=utf-8
 encoding//web/hdabo/management/commands/importcsv.py=utf-8
--- a/web/hdabo/management/commands/importcsv.py	Tue Jun 14 01:51:26 2011 +0200
+++ b/web/hdabo/management/commands/importcsv.py	Tue Jun 14 12:56:48 2011 +0200
@@ -11,6 +11,7 @@
 from django.db import transaction
 from hdabo.models import (Author, Datasheet, DocumentFormat, Domain, Organisation,
     Tag, TaggedSheet, TimePeriod, Location)
+from hdabo.wp_utils import normalize_tag
 from optparse import make_option
 import csv
 import datetime
@@ -82,15 +83,6 @@
             sys.stdout.write("\n")
         sys.stdout.flush()
 
-
-    def normalize_tag(self, tag):
-        if len(tag) == 0:
-            return tag
-        tag = tag.strip()
-        tag = tag.replace("_", " ")
-        tag = " ".join(tag.split())
-        tag = tag[0].upper() + tag[1:]
-        return tag
     
     def create_domain_period(self, row_value, klass, school_period):
         res_list = []
@@ -191,7 +183,7 @@
             for i, tag in enumerate([t.strip() for t in row[u'Tag'].split(u";")]):
                 if len(tag) == 0:
                     continue
-                tag_label = self.normalize_tag(tag)
+                tag_label = normalize_tag(tag)
                 tag_objs = Tag.objects.filter(label__iexact=tag_label)
                 if len(tag_objs) == 0:
                     tag_obj = Tag(label=tag_label, original_label=tag)
--- a/web/hdabo/management/commands/querywikipedia.py	Tue Jun 14 01:51:26 2011 +0200
+++ b/web/hdabo/management/commands/querywikipedia.py	Tue Jun 14 12:56:48 2011 +0200
@@ -15,6 +15,28 @@
 import math
 import sys
 
+
+def process_tag(site, tag, verbosity):
+    new_label, status, url, pageid, response = query_wikipedia_title(site, tag.label)
+    
+    if verbosity >= 2 :
+        print "response from query to %s with parameters %s :" % (site.apibase, repr(new_label))
+        print repr(response)
+    
+    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()
+
+
+
 class Command(NoArgsCommand):
     '''
     query and update wikipedia for tag title.
@@ -166,23 +188,7 @@
                 print "processing tag %s (%d/%d)" % (tag.label, i + 1, count)
             else:
                 self.show_progress(i + 1, count, tag.label, 60)                            
-                
-            new_label, status, url, pageid, response = query_wikipedia_title(site, tag.label)
-
-            if verbosity >= 2 :
-                print "response from query to %s with parameters %s :" % (site_url, repr(new_label))
-                print repr(response)
             
-            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()
+            process_tag(site, tag, verbosity)
             
             
--- a/web/hdabo/static/hdabo/js/hdabo.js	Tue Jun 14 01:51:26 2011 +0200
+++ b/web/hdabo/static/hdabo/js/hdabo.js	Tue Jun 14 12:56:48 2011 +0200
@@ -29,14 +29,13 @@
         }
     });
     
-    
-    $.editable.addInputType('autocomplete', {
-    	element : $.editable.types.text.element,
-    	plugin : function(settings, original) {
-    		$('input', this).autocomplete(settings.autocomplete);
-    	}
+    $(".reset_wp_info").click(function(e){
+        if(confirm("Confirmez vous le réytablissement du label original de ce tag ?")){
+            reset_wp_info(this);
+        }
     });
     
+        
     $(".wikipediatag").editable(modify_tag_url, { 
     	indicator : "<img src='"+static_url+"hdabo/img/indicator.gif'>",
     	type      : "autocomplete",
@@ -169,4 +168,23 @@
             init_tags_events();
         }
     });
+}
+
+function reset_wp_info(cell)
+{
+	var tag_id = $(cell).html()
+	
+	$.ajax({
+		url: reset_wp_info_url,
+		type: 'POST',
+		data: {
+			csrfmiddlewaretoken:global_csrf_token, 
+            datasheet_id:$('#datasheet_id').val(),
+            tag_id:tag_id
+        },
+        success: function(msg, textStatus, XMLHttpRequest) {
+            $('#tag_table_container').html(msg);
+            init_tags_events();
+        }
+	});
 }
\ No newline at end of file
--- a/web/hdabo/templates/partial/list_for_orga.html	Tue Jun 14 01:51:26 2011 +0200
+++ b/web/hdabo/templates/partial/list_for_orga.html	Tue Jun 14 12:56:48 2011 +0200
@@ -18,8 +18,15 @@
     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 reset_wp_info_url = "{% url hdabo.views.reset_wikipedia_info %}";
     
     $(document).ready(function(){
+        $.editable.addInputType('autocomplete', {
+            element : $.editable.types.text.element,
+            plugin : function(settings, original) {
+                $('input', this).autocomplete(settings.autocomplete);
+            }
+        });
         init_tags_events();
     });
     </script>
--- a/web/hdabo/templates/partial/tag_table.html	Tue Jun 14 01:51:26 2011 +0200
+++ b/web/hdabo/templates/partial/tag_table.html	Tue Jun 14 12:56:48 2011 +0200
@@ -23,7 +23,7 @@
             <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>{{t.tag.id}}</td>
+            <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>
             {% comment %}<td>{{t.tag.original_label}}</td>{% endcomment %}
             <td class="text_centered">
--- a/web/hdabo/urls.py	Tue Jun 14 01:51:26 2011 +0200
+++ b/web/hdabo/urls.py	Tue Jun 14 12:56:48 2011 +0200
@@ -24,4 +24,5 @@
     url(r'^removetagfromlist$', 'hdabo.views.remove_tag_from_list'),
     url(r'^validatelink$', 'hdabo.views.validate_wp_link'),
     url(r'^modifytag$', 'hdabo.views.modify_tag'),
+    url(r'^resetwpinfo$', 'hdabo.views.reset_wikipedia_info'),
 )
--- a/web/hdabo/views.py	Tue Jun 14 01:51:26 2011 +0200
+++ b/web/hdabo/views.py	Tue Jun 14 12:56:48 2011 +0200
@@ -2,7 +2,8 @@
 from django.contrib.auth.decorators import login_required #@UnusedImport
 from django.shortcuts import render_to_response
 from django.template import RequestContext
-from hdabo.wp_utils import query_wikipedia_title
+from hdabo.management.commands.querywikipedia import process_tag
+from hdabo.wp_utils import normalize_tag, query_wikipedia_title
 from models import Organisation, Datasheet, TaggedSheet, Tag
 from wikitools import wiki
 
@@ -154,23 +155,47 @@
     tag_id = request.POST["id"]
     tag_label = request.POST["value"]
     ds_id = request.POST["datasheet_id"]
-    
-    site = wiki.Wiki(settings.WIKIPEDIA_API_URL) #@UndefinedVariable
-    
-    new_label, status, url, pageid, response = query_wikipedia_title(site, tag_label) #@UnusedVariable
-    
+        
     tag = Tag.objects.get(id=tag_id)
     
-    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
+    if tag.label != tag_label:
+
+
+        tag.label = tag_label
         
-    tag.save()
+        site = wiki.Wiki(settings.WIKIPEDIA_API_URL) #@UndefinedVariable
+        new_label, status, url, pageid, response = query_wikipedia_title(site, tag_label) #@UnusedVariable                    
+
+        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 get_tag_table(request=request, ds_id=ds_id, valid=0)
+
+
+#@login_required
+def reset_wikipedia_info(request):
+    
+    tag_id = request.POST["tag_id"]    
+    ds_id = request.POST["datasheet_id"]
+    
+    tag = Tag.objects.get(id=tag_id)
+    site = wiki.Wiki(settings.WIKIPEDIA_API_URL) #@UndefinedVariable
+    
+    tag.label = normalize_tag(tag.original_label)
+    
+    process_tag(site, tag, 0)
+    
+    return get_tag_table(request=request, ds_id=ds_id, valid=0)
--- a/web/hdabo/wp_utils.py	Tue Jun 14 01:51:26 2011 +0200
+++ b/web/hdabo/wp_utils.py	Tue Jun 14 12:56:48 2011 +0200
@@ -39,3 +39,11 @@
 
     return new_label, status, url, pageid, response
 
+def normalize_tag(tag):
+    if len(tag) == 0:
+        return tag
+    tag = tag.strip()
+    tag = tag.replace("_", " ")
+    tag = " ".join(tag.split())
+    tag = tag[0].upper() + tag[1:]
+    return tag