# HG changeset patch # User cavaliet # Date 1308049702 -7200 # Node ID 056c19e37eabb49f7bf791be53d61e2bc6c3d3e1 # Parent e5f8cb1020c8e4fe1caff12cf962a3dc48cba183# Parent 6999575970267e7970c35d8b12d669fe75540528 fusionner diff -r e5f8cb1020c8 -r 056c19e37eab web/hdabo/static/hdabo/js/hdabo.js --- a/web/hdabo/static/hdabo/js/hdabo.js Tue Jun 14 12:56:48 2011 +0200 +++ b/web/hdabo/static/hdabo/js/hdabo.js Tue Jun 14 13:08:22 2011 +0200 @@ -1,190 +1,232 @@ -// -*- coding: utf-8 -*- -function init_tags_events() -{ - // Up and down management for tag table lines - $(".up,.down").click(function(){ - tag_up_down(this); - }); - // Remove tag for a datasheet's list - $(".remove_tag_from_list").click(function(){ - if(confirm("Confirmez-vous la suppression du tag \"" + $(this).attr('alt') + "\" de la liste courante ?")){ - remove_tag_from_list(this); - } - }); - //activate_wp_cb - $(".activate_wp_cb").click(function(e){ - new_checked = $(this).is(':checked'); - if(new_checked){ - question = "Confirmez-vous l'activation du lien Wikipédia pour le tag \"" + $(this).attr('alt') + "\" ?"; - } else { - question = "Confirmez-vous la désactivation du lien Wikipédia pour le tag \"" + $(this).attr('alt') + "\" ?"; - } - if(confirm(question)){ - validate_wp_link(this); - } - else{ - // Since the click event is dispatched after the checked changed, we change the checked changed if confirm is false. - e.preventDefault(); // unvalidates user's click - $(this).attr('checked', !new_checked); - } - }); - - $(".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 : "", - type : "autocomplete", - tooltip : "Cliquer pour éditer...", - onblur : "submit", - submitdata: {csrfmiddlewaretoken:global_csrf_token, datasheet_id:$('#datasheet_id').val()}, - callback : function(value, settings) { - $('#tag_table_container').html(value); - init_tags_events(); - }, - autocomplete : { - source: function( request, response ) { - $.ajax({ - url: "http://fr.wikipedia.org/w/api.php", - dataType: "jsonp", - data: { - action: "opensearch", - limit: "20", - namespace: "0", - format: "json", - search: request.term - }, - success: function( data ) { - response( $.map( data[1], function( item ) { - return { - label: item, - value: item - } - })); - } - }); - }, - minLength: 2, - open: function() { - $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" ); - }, - close: function() { - $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" ); - } - } - }); -} - -function validate_wp_link(cb) -{ - var url = validate_wp_link_url; - var id_tag = $(cb).attr('id'); - new_checked = $(cb).is(':checked'); - $.ajax({ - url: url, - type: 'POST', - data: {csrfmiddlewaretoken:global_csrf_token, - datasheet_id:$('#datasheet_id').val(), - tag_id:id_tag, - activated:new_checked - }, - // bug with jquery >= 1.5, "json" adds a callback so we don't specify dataType - //dataType: 'json', - success: function(msg, textStatus, XMLHttpRequest) { - $('#tag_table_container').html(msg); - init_tags_events(); - } - }); -} - -function remove_tag_from_list(btn) -{ - var url = remove_tag_from_list_url; - var id_tag = $(btn).attr('id'); - $.ajax({ - url: url, - type: 'POST', - data: {csrfmiddlewaretoken:global_csrf_token, - datasheet_id:$('#datasheet_id').val(), - tag_id:id_tag - }, - // bug with jquery >= 1.5, "json" adds a callback so we don't specify dataType - //dataType: 'json', - success: function(msg, textStatus, XMLHttpRequest) { - $('#tag_table_container').html(msg); - init_tags_events(); - } - }); -} - -function remove_tag_from_list(btn) -{ - var url = remove_tag_from_list_url; - var id_tag = $(btn).attr('id'); - $.ajax({ - url: url, - type: 'POST', - data: {csrfmiddlewaretoken:global_csrf_token, - datasheet_id:$('#datasheet_id').val(), - tag_id:id_tag - }, - // bug with jquery >= 1.5, "json" adds a callback so we don't specify dataType - //dataType: 'json', - success: function(msg, textStatus, XMLHttpRequest) { - $('#tag_table_container').html(msg); - init_tags_events(); - } - }); -} - -function tag_up_down(arrow) -{ - if ($(arrow).is(".up")) { - mv = "u"; - } else { - mv = "d"; - } - var url = tag_up_down_url; - var id_tag = $(arrow).attr('id'); - // This indicates the position (from 0) of the tag in the list. NB : it is different from the TagSheet.order in the database. - var pos_tag = $(arrow).attr('pos'); - $.ajax({ - url: url, - type: 'POST', - data: {csrfmiddlewaretoken:global_csrf_token, - datasheet_id:$('#datasheet_id').val(), - tag_id:id_tag, - move:mv, - tag_pos:pos_tag - }, - // bug with jquery >= 1.5, "json" adds a callback so we don't specify dataType - //dataType: 'json', - success: function(msg, textStatus, XMLHttpRequest) { - $('#tag_table_container').html(msg); - 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(); - } - }); +// -*- coding: utf-8 -*- +function init_tags_events() +{ + // Up and down management for tag table lines + $(".up,.down").click(function(){ + tag_up_down(this); + }); + // Remove tag for a datasheet's list + $(".remove_tag_from_list").click(function(){ + if(confirm("Confirmez-vous la suppression du tag \"" + $(this).attr('alt') + "\" de la liste courante ?")){ + remove_tag_from_list(this); + } + }); + //activate_wp_cb + $(".activate_wp_cb").click(function(e){ + new_checked = $(this).is(':checked'); + if(new_checked){ + question = "Confirmez-vous l'activation du lien Wikipédia pour le tag \"" + $(this).attr('alt') + "\" ?"; + } else { + question = "Confirmez-vous la désactivation du lien Wikipédia pour le tag \"" + $(this).attr('alt') + "\" ?"; + } + if(confirm(question)){ + validate_wp_link(this); + } + else{ + // Since the click event is dispatched after the checked changed, we change the checked changed if confirm is false. + e.preventDefault(); // unvalidates user's click + $(this).attr('checked', !new_checked); + } + }); + + $("#wp_search").autocomplete({ + source: function( request, response ) { + $.ajax({ + url: "http://fr.wikipedia.org/w/api.php", + dataType: "jsonp", + data: { + action: "opensearch", + limit: "20", + namespace: "0", + format: "json", + search: request.term + }, + success: function( data ) { + response( $.map( data[1], function( item ) { + return { + label: item, + value: item + } + })); + } + }); + }, + minLength: 2, + open: function() { + $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" ); + }, + close: function() { + $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" ); + $("#ok_search").show(); + } + }); + + $("#ok_search").click(function(){ + if($("#wp_search").val()!=""){ + $("#ok_search").html(""); + var url = add_tag_url; + $.ajax({ + url: url, + type: 'POST', + data: {csrfmiddlewaretoken:global_csrf_token, + datasheet_id:$('#datasheet_id').val(), + value:$("#wp_search").val() + }, + // bug with jquery >= 1.5, "json" adds a callback so we don't specify dataType + //dataType: 'json', + success: function(msg, textStatus, XMLHttpRequest) { + $('#tag_table_container').html(msg); + init_tags_events(); + // And scroll to the bottom + $("html").animate({ scrollTop: $(document).height() }, 500); + }, + complete: function(){ + // We empty the input and hide the ok button + $("#wp_search").val(""); + $("#ok_search").hide(); + $("#ok_search").html("OK"); + } + }); + } + }); + + $.editable.addInputType('autocomplete', { + element : $.editable.types.text.element, + plugin : function(settings, original) { + $('input', this).autocomplete(settings.autocomplete); + } + }); + + $(".wikipediatag").editable(modify_tag_url, { + indicator : "", + type : "autocomplete", + tooltip : "Cliquer pour éditer...", + onblur : "submit", + submitdata: {csrfmiddlewaretoken:global_csrf_token, datasheet_id:$('#datasheet_id').val()}, + callback : function(value, settings) { + $('#tag_table_container').html(value); + init_tags_events(); + }, + autocomplete : { + source: function( request, response ) { + $.ajax({ + url: "http://fr.wikipedia.org/w/api.php", + dataType: "jsonp", + data: { + action: "opensearch", + limit: "20", + namespace: "0", + format: "json", + search: request.term + }, + success: function( data ) { + response( $.map( data[1], function( item ) { + return { + label: item, + value: item + } + })); + } + }); + }, + minLength: 2, + open: function() { + $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" ); + }, + close: function() { + $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" ); + } + } + }); +} + +function validate_wp_link(cb) +{ + var url = validate_wp_link_url; + var id_tag = $(cb).attr('id'); + new_checked = $(cb).is(':checked'); + $.ajax({ + url: url, + type: 'POST', + data: {csrfmiddlewaretoken:global_csrf_token, + datasheet_id:$('#datasheet_id').val(), + tag_id:id_tag, + activated:new_checked + }, + // bug with jquery >= 1.5, "json" adds a callback so we don't specify dataType + //dataType: 'json', + success: function(msg, textStatus, XMLHttpRequest) { + $('#tag_table_container').html(msg); + init_tags_events(); + } + }); +} + +function remove_tag_from_list(btn) +{ + var url = remove_tag_from_list_url; + var id_tag = $(btn).attr('id'); + $.ajax({ + url: url, + type: 'POST', + data: {csrfmiddlewaretoken:global_csrf_token, + datasheet_id:$('#datasheet_id').val(), + tag_id:id_tag + }, + // bug with jquery >= 1.5, "json" adds a callback so we don't specify dataType + //dataType: 'json', + success: function(msg, textStatus, XMLHttpRequest) { + $('#tag_table_container').html(msg); + init_tags_events(); + } + }); +} + +function remove_tag_from_list(btn) +{ + var url = remove_tag_from_list_url; + var id_tag = $(btn).attr('id'); + $.ajax({ + url: url, + type: 'POST', + data: {csrfmiddlewaretoken:global_csrf_token, + datasheet_id:$('#datasheet_id').val(), + tag_id:id_tag + }, + // bug with jquery >= 1.5, "json" adds a callback so we don't specify dataType + //dataType: 'json', + success: function(msg, textStatus, XMLHttpRequest) { + $('#tag_table_container').html(msg); + init_tags_events(); + } + }); +} + +function tag_up_down(arrow) +{ + if ($(arrow).is(".up")) { + mv = "u"; + } else { + mv = "d"; + } + var url = tag_up_down_url; + var id_tag = $(arrow).attr('id'); + // This indicates the position (from 0) of the tag in the list. NB : it is different from the TagSheet.order in the database. + var pos_tag = $(arrow).attr('pos'); + $.ajax({ + url: url, + type: 'POST', + data: {csrfmiddlewaretoken:global_csrf_token, + datasheet_id:$('#datasheet_id').val(), + tag_id:id_tag, + move:mv, + tag_pos:pos_tag + }, + // bug with jquery >= 1.5, "json" adds a callback so we don't specify dataType + //dataType: 'json', + success: function(msg, textStatus, XMLHttpRequest) { + $('#tag_table_container').html(msg); + init_tags_events(); + } + }); } \ No newline at end of file diff -r e5f8cb1020c8 -r 056c19e37eab web/hdabo/templates/hdabo_base.html --- a/web/hdabo/templates/hdabo_base.html Tue Jun 14 12:56:48 2011 +0200 +++ b/web/hdabo/templates/hdabo_base.html Tue Jun 14 13:08:22 2011 +0200 @@ -1,79 +1,80 @@ -{% load i18n %} -{% comment %}{% load logintag %}{% endcomment %} -{% comment %}{% load navigation %}{% endcomment %} - - - - -{% block head %} - - - - {% block title %}HDA-BO{% endblock %} - {% block js_import %} - - - - - - - {% endblock %} - - {% block css_declaration %} - - {% endblock %} - - {% block css_import %} - - - - {% endblock %} - - {% block js_declaration %} - {% endblock %} - -{% endblock %} - - -{% block body %} -
- -
-
- {% block toolbar %} -

home

- {% endblock %} -
-
- {% block content %} - {% endblock %} -
-
- -
-{% endblock %} - - +{% load i18n %} +{% comment %}{% load logintag %}{% endcomment %} +{% comment %}{% load navigation %}{% endcomment %} + + + + +{% block head %} + + + + {% block title %}HDA-BO{% endblock %} + {% block js_import %} + + + + + + + + {% endblock %} + + {% block css_declaration %} + + {% endblock %} + + {% block css_import %} + + + + {% endblock %} + + {% block js_declaration %} + {% endblock %} + +{% endblock %} + + +{% block body %} +
+ +
+
+ {% block toolbar %} +

home

+ {% endblock %} +
+
+ {% block content %} + {% endblock %} +
+
+ +
+{% endblock %} + + diff -r e5f8cb1020c8 -r 056c19e37eab web/hdabo/templates/list_for_orga.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/hdabo/templates/list_for_orga.html Tue Jun 14 13:08:22 2011 +0200 @@ -0,0 +1,78 @@ +{% extends "hdabo_base.html" %} + {% block title %}HDA - BO : Fiches pour {{ orga_name }}{% endblock %} + + {% block js_import %} + {{block.super}} + + + {% endblock %} + + {% block js_declaration %} + {{block.super}} + {% if valid != "2" %} + + {% endif %} + {% endblock %} + + {% block css_import %} + {{block.super}} + + {% endblock %} + + {% block css_declaration %} + {{block.super}} + + {% endblock %} + + + {% block content %} + {{block.super}} +

Fiche(s) pour l'organisation {{ orga_name }} : {{nb_sheets}} fiche(s)

+ {% if valid != "2" %} +
+

<<   + <   + {{displayed_index}}/{{nb_sheets}}   + >   + >>

+
+ {% endif %} + + {% for ds in datasheets %} + + {% endfor %} +
+ {% include "partial/one_sheet.html" %} +
+ {% if valid != "2" %} +
+

<<   + <   + {{displayed_index}}/{{nb_sheets}}   + >   + >>

+
+ {% endif %} + {% endblock %} diff -r e5f8cb1020c8 -r 056c19e37eab web/hdabo/templates/organisation_list.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/hdabo/templates/organisation_list.html Tue Jun 14 13:08:22 2011 +0200 @@ -0,0 +1,32 @@ +{% extends "hdabo_base.html" %} + {% block title %}HDA-BO : Liste des organisations{% endblock %} + + {% block content %} + {{block.super}} +

Afficher les fiches non validées pour les lieux suivants :

+ + + + + + + + {% for item in organisations %} + + + + + + + {% endfor %} +
NomFiches non validéesFiches validéesToutes les fiches
{{ item.organisation.name|default:"_" }} (lien) + {% if item.nb_unval > 0 %} + {{item.nb_unval}} + {% else %}0{% endif %} + {% if item.nb_val > 0 %} + {{item.nb_val}} + {% else %}0{% endif %} + {% if item.nb_all > 0 %} + {{item.nb_all}} + {% else %}0{% endif %}
+ {% endblock %} diff -r e5f8cb1020c8 -r 056c19e37eab web/hdabo/templates/partial/one_sheet.html --- a/web/hdabo/templates/partial/one_sheet.html Tue Jun 14 12:56:48 2011 +0200 +++ b/web/hdabo/templates/partial/one_sheet.html Tue Jun 14 13:08:22 2011 +0200 @@ -25,6 +25,11 @@       par :       le {{ds.validation_date}} {% endif %} + {% if valid != "2" %} + Ajouter un tag : + + + {% endif %}

{% csrf_token %} diff -r e5f8cb1020c8 -r 056c19e37eab web/hdabo/templates/partial/organisation_list.html --- a/web/hdabo/templates/partial/organisation_list.html Tue Jun 14 12:56:48 2011 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,32 +0,0 @@ -{% extends "hdabo_base.html" %} - {% block title %}HDA-BO : Liste des organisations{% endblock %} - - {% block content %} - {{block.super}} -

Afficher les fiches non validées pour les lieux suivants :

- - - - - - - - {% for item in organisations %} - - - - - - - {% endfor %} -
NomFiches non validéesFiches validéesToutes les fiches
{{ item.organisation.name|default:"_" }} (lien) - {% if item.nb_unval > 0 %} - {{item.nb_unval}} - {% else %}0{% endif %} - {% if item.nb_val > 0 %} - {{item.nb_val}} - {% else %}0{% endif %} - {% if item.nb_all > 0 %} - {{item.nb_all}} - {% else %}0{% endif %}
- {% endblock %} diff -r e5f8cb1020c8 -r 056c19e37eab web/hdabo/urls.py --- a/web/hdabo/urls.py Tue Jun 14 12:56:48 2011 +0200 +++ b/web/hdabo/urls.py Tue Jun 14 13:08:22 2011 +0200 @@ -25,4 +25,5 @@ url(r'^validatelink$', 'hdabo.views.validate_wp_link'), url(r'^modifytag$', 'hdabo.views.modify_tag'), url(r'^resetwpinfo$', 'hdabo.views.reset_wikipedia_info'), + url(r'^addtag$', 'hdabo.views.add_tag'), ) diff -r e5f8cb1020c8 -r 056c19e37eab web/hdabo/views.py --- a/web/hdabo/views.py Tue Jun 14 12:56:48 2011 +0200 +++ b/web/hdabo/views.py Tue Jun 14 13:08:22 2011 +0200 @@ -1,201 +1,201 @@ -from django.conf import settings -from django.contrib.auth.decorators import login_required #@UnusedImport -from django.shortcuts import render_to_response -from django.template import RequestContext -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 - - -#@login_required -def home(request): - - # Get all organisations - orgas = Organisation.objects.all() - # Count all validated, unvalidated sheets for each organisation - org_list = [] - for orga in orgas : - all_datasheets = Datasheet.objects.filter(organisation=orga) - nb_all = len(all_datasheets) - nb_val = len(all_datasheets.filter(validated=True)) - nb_unval = len(all_datasheets.filter(validated=False)) - org_list.append({'organisation':orga, 'nb_all':nb_all, 'nb_val':nb_val, 'nb_unval':nb_unval}) - - - return render_to_response("partial/organisation_list.html", - {'organisations':org_list}, - context_instance=RequestContext(request)) - - -#@login_required -def list_for_orga(request, orga_id=None, valid=None, start_index=None): - - orga = Organisation.objects.get(id=orga_id) - orga_name = orga.name - - if start_index : - try: - start_index = int(start_index) - except : - start_index = 0 - else : - start_index = 0 - - # If valid = 0, we search unvalidated sheets - # If valid = 1, we search validated sheets - # If valid = 2, we search AND DISPLAY all sheets - if valid == "1" : - # We count all the validated sheets - datasheets = Datasheet.objects.filter(organisation=orga).filter(validated=True) - nb_sheets = len(datasheets) - # And select the current one - datasheets = [datasheets[start_index]] - elif valid != "2": - # We count all the validated sheets - datasheets = Datasheet.objects.filter(organisation=orga).filter(validated=False) - nb_sheets = len(datasheets) - # And select the current one - datasheets = [datasheets[start_index]] - else : - datasheets = Datasheet.objects.filter(organisation=orga) - nb_sheets = len(datasheets) - - # We get the ORDERED tags if we display one sheet (case valid = 0 and 1) - ordered_tags = None - if valid != "2" : - ordered_tags = TaggedSheet.objects.filter(datasheet=datasheets[0]).order_by('order') - - displayed_index = start_index + 1; - prev_index = max(start_index - 1, 0); - next_index = min(nb_sheets - 1, start_index + 1); - last_index = max(nb_sheets - 1, 0); - - return render_to_response("partial/list_for_orga.html", - {'datasheets':datasheets, 'orga_name':orga_name, - 'nb_sheets':nb_sheets, 'orga_id':orga_id, 'ordered_tags':ordered_tags, - 'prev_index':prev_index, 'next_index':next_index, 'last_index':last_index, - 'start_index':start_index, 'displayed_index':displayed_index, 'valid':valid}, - context_instance=RequestContext(request)) - -#@login_required -def tag_up_down(request): - ds_id = request.POST["datasheet_id"] - #tag_id = request.POST["tag_id"] - # tag_pos indicates the position (from 0) of the tag in the list. NB : it is different from the TagSheet.order in the database. - tag_pos = int(request.POST["tag_pos"]) - # move can be "u" or "d", for up and down - move = request.POST["move"] - # First we get the datasheet's TaggedSheets - ordered_tags = TaggedSheet.objects.filter(datasheet=Datasheet.objects.filter(id=ds_id)[0]).order_by('order') - # We get the current TaggedSheet and its's order - #ts = ordered_tags.filter(tag=Tag.objects.filter(id=tag_id))[0] - ts = ordered_tags[tag_pos] - tag_order = ts.order - # We get the other TaggedSheet that will be moved - if move == "u" : - other_ts = ordered_tags[tag_pos - 1] - elif move == "d" : - other_ts = ordered_tags[tag_pos + 1] - else : - other_ts = None - # We switch the orders - if other_ts : - ts.order = other_ts.order - other_ts.order = tag_order - ts.save() - other_ts.save() - - return get_tag_table(request=request, ds_id=ds_id, valid=0) - - -#@login_required -def get_tag_table(request=None, ds_id=None, valid=None): - - ordered_tags = TaggedSheet.objects.filter(datasheet=Datasheet.objects.filter(id=ds_id)[0]).order_by('order') - - return render_to_response("partial/tag_table.html", - {'ordered_tags':ordered_tags, 'valid':valid}, - context_instance=RequestContext(request)) - - -#@login_required -def remove_tag_from_list(request=None): - - ds_id = request.POST["datasheet_id"] - tag_id = request.POST["tag_id"] - # First we get the datasheet's TaggedSheets - ds_tags = TaggedSheet.objects.filter(datasheet=Datasheet.objects.filter(id=ds_id)[0]) - # We get the current TaggedSheet and we delete it - ts = ds_tags.filter(tag=Tag.objects.filter(id=tag_id))[0] - ts.delete() - - return get_tag_table(request=request, ds_id=ds_id, valid=0) - - -#@login_required -def validate_wp_link(request=None): - - ds_id = request.POST["datasheet_id"] - tag_id = request.POST["tag_id"] - if request.POST["activated"] != "false" : - wp_activ = True - else : - wp_activ = False - # First we get the tag object - tag = Tag.objects.filter(id=tag_id)[0] - tag.wikipedia_activated = wp_activ - tag.save() - - return get_tag_table(request=request, ds_id=ds_id, valid=0) - -#@login_required -def modify_tag(request): - - tag_id = request.POST["id"] - tag_label = request.POST["value"] - ds_id = request.POST["datasheet_id"] - - tag = Tag.objects.get(id=tag_id) - - if tag.label != tag_label: - - - tag.label = tag_label - - 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) +from django.conf import settings +from django.contrib.auth.decorators import login_required #@UnusedImport +from django.shortcuts import render_to_response +from django.template import RequestContext +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 + + +#@login_required +def home(request): + + # Get all organisations + orgas = Organisation.objects.all() + # Count all validated, unvalidated sheets for each organisation + org_list = [] + for orga in orgas : + all_datasheets = Datasheet.objects.filter(organisation=orga) + nb_all = len(all_datasheets) + nb_val = len(all_datasheets.filter(validated=True)) + nb_unval = len(all_datasheets.filter(validated=False)) + org_list.append({'organisation':orga, 'nb_all':nb_all, 'nb_val':nb_val, 'nb_unval':nb_unval}) + + + return render_to_response("partial/organisation_list.html", + {'organisations':org_list}, + context_instance=RequestContext(request)) + + +#@login_required +def list_for_orga(request, orga_id=None, valid=None, start_index=None): + + orga = Organisation.objects.get(id=orga_id) + orga_name = orga.name + + if start_index : + try: + start_index = int(start_index) + except : + start_index = 0 + else : + start_index = 0 + + # If valid = 0, we search unvalidated sheets + # If valid = 1, we search validated sheets + # If valid = 2, we search AND DISPLAY all sheets + if valid == "1" : + # We count all the validated sheets + datasheets = Datasheet.objects.filter(organisation=orga).filter(validated=True) + nb_sheets = len(datasheets) + # And select the current one + datasheets = [datasheets[start_index]] + elif valid != "2": + # We count all the validated sheets + datasheets = Datasheet.objects.filter(organisation=orga).filter(validated=False) + nb_sheets = len(datasheets) + # And select the current one + datasheets = [datasheets[start_index]] + else : + datasheets = Datasheet.objects.filter(organisation=orga) + nb_sheets = len(datasheets) + + # We get the ORDERED tags if we display one sheet (case valid = 0 and 1) + ordered_tags = None + if valid != "2" : + ordered_tags = TaggedSheet.objects.filter(datasheet=datasheets[0]).order_by('order') + + displayed_index = start_index + 1; + prev_index = max(start_index - 1, 0); + next_index = min(nb_sheets - 1, start_index + 1); + last_index = max(nb_sheets - 1, 0); + + return render_to_response("partial/list_for_orga.html", + {'datasheets':datasheets, 'orga_name':orga_name, + 'nb_sheets':nb_sheets, 'orga_id':orga_id, 'ordered_tags':ordered_tags, + 'prev_index':prev_index, 'next_index':next_index, 'last_index':last_index, + 'start_index':start_index, 'displayed_index':displayed_index, 'valid':valid}, + context_instance=RequestContext(request)) + +#@login_required +def tag_up_down(request): + ds_id = request.POST["datasheet_id"] + #tag_id = request.POST["tag_id"] + # tag_pos indicates the position (from 0) of the tag in the list. NB : it is different from the TagSheet.order in the database. + tag_pos = int(request.POST["tag_pos"]) + # move can be "u" or "d", for up and down + move = request.POST["move"] + # First we get the datasheet's TaggedSheets + ordered_tags = TaggedSheet.objects.filter(datasheet=Datasheet.objects.filter(id=ds_id)[0]).order_by('order') + # We get the current TaggedSheet and its's order + #ts = ordered_tags.filter(tag=Tag.objects.filter(id=tag_id))[0] + ts = ordered_tags[tag_pos] + tag_order = ts.order + # We get the other TaggedSheet that will be moved + if move == "u" : + other_ts = ordered_tags[tag_pos - 1] + elif move == "d" : + other_ts = ordered_tags[tag_pos + 1] + else : + other_ts = None + # We switch the orders + if other_ts : + ts.order = other_ts.order + other_ts.order = tag_order + ts.save() + other_ts.save() + + return get_tag_table(request=request, ds_id=ds_id, valid=0) + + +#@login_required +def get_tag_table(request=None, ds_id=None, valid=None): + + ordered_tags = TaggedSheet.objects.filter(datasheet=Datasheet.objects.filter(id=ds_id)[0]).order_by('order') + + return render_to_response("partial/tag_table.html", + {'ordered_tags':ordered_tags, 'valid':valid}, + context_instance=RequestContext(request)) + + +#@login_required +def remove_tag_from_list(request=None): + + ds_id = request.POST["datasheet_id"] + tag_id = request.POST["tag_id"] + # First we get the datasheet's TaggedSheets + ds_tags = TaggedSheet.objects.filter(datasheet=Datasheet.objects.filter(id=ds_id)[0]) + # We get the current TaggedSheet and we delete it + ts = ds_tags.filter(tag=Tag.objects.filter(id=tag_id))[0] + ts.delete() + + return get_tag_table(request=request, ds_id=ds_id, valid=0) + + +#@login_required +def validate_wp_link(request=None): + + ds_id = request.POST["datasheet_id"] + tag_id = request.POST["tag_id"] + if request.POST["activated"] != "false" : + wp_activ = True + else : + wp_activ = False + # First we get the tag object + tag = Tag.objects.filter(id=tag_id)[0] + tag.wikipedia_activated = wp_activ + tag.save() + + return get_tag_table(request=request, ds_id=ds_id, valid=0) + +#@login_required +def modify_tag(request): + + tag_id = request.POST["id"] + tag_label = request.POST["value"] + ds_id = request.POST["datasheet_id"] + + tag = Tag.objects.get(id=tag_id) + + if tag.label != tag_label: + + + tag.label = tag_label + + 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)