web/hdabo/views.py
author ymh <ymh.work@gmail.com>
Tue, 12 Jul 2011 12:36:31 +0200
changeset 89 ecacd68cf859
parent 86 417938cf4601
child 99 743a4511d93c
permissions -rw-r--r--
add sort direction, corrections
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
     1
# -*- coding: utf-8 -*-
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
     2
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
     3
from django.conf import settings
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
     4
from django.contrib.auth.decorators import login_required #@UnusedImport
32
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
     5
from django.core.paginator import Paginator
56
e70cbbc093cc improve tag letter list
ymh <ymh.work@gmail.com>
parents: 55
diff changeset
     6
from django.db import connection
63
03698c739b1d #8 : Better url management with datasheet id and context in parameter.
cavaliet
parents: 61
diff changeset
     7
from django.db.models import Max, Count, Min
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
     8
from django.http import HttpResponseBadRequest
34
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
     9
from django.shortcuts import render_to_response, redirect
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    10
from django.template import RequestContext
54
deaa9393e6a2 slightly improve queries
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    11
from django.utils.http import urlquote
42
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    12
from haystack.constants import DJANGO_ID
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    13
from haystack.query import SearchQuerySet
72
ba8ebabbaece -correct css and display
ymh <ymh.work@gmail.com>
parents: 69
diff changeset
    14
from hdabo.utils import OrderedDict, remove_accents, normalize
85
2ff78b3ac007 Correction bug #18
ymh <ymh.work@gmail.com>
parents: 73
diff changeset
    15
from hdabo.wp_utils import (normalize_tag, query_wikipedia_title,
72
ba8ebabbaece -correct css and display
ymh <ymh.work@gmail.com>
parents: 69
diff changeset
    16
    get_or_create_tag, process_tag, reorder_datasheet_tags)
44
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
    17
from models import Datasheet, Organisation, Tag, TagCategory, TaggedSheet
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    18
from wikitools import wiki
42
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    19
import django.utils.simplejson as json
54
deaa9393e6a2 slightly improve queries
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    20
import re
42
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    21
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    22
53
50f3f48467e8 Set identification. It is required now.
cavaliet
parents: 52
diff changeset
    23
@login_required
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    24
def home(request):
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    25
    
32
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
    26
    orgas = Organisation.objects.all().order_by('name')
56
e70cbbc093cc improve tag letter list
ymh <ymh.work@gmail.com>
parents: 55
diff changeset
    27
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    28
    org_list = []
85
2ff78b3ac007 Correction bug #18
ymh <ymh.work@gmail.com>
parents: 73
diff changeset
    29
    all_ds_mapping = dict([(res['organisation'], res['nb_all']) for res in Datasheet.objects.values("organisation").annotate(nb_all=Count("organisation"))])
2ff78b3ac007 Correction bug #18
ymh <ymh.work@gmail.com>
parents: 73
diff changeset
    30
    validated_ds_mapping = dict([(res['organisation'], [res['nb_val'], res['first_id_val']]) for res in Datasheet.objects.filter(validated=True).values("organisation").annotate(nb_val=Count("organisation")).annotate(first_id_val=Min("id"))])
2ff78b3ac007 Correction bug #18
ymh <ymh.work@gmail.com>
parents: 73
diff changeset
    31
    unvalidated_ds_mapping = dict([(res['organisation'], [res['nb_unval'], res['first_id_unval']]) for res in Datasheet.objects.filter(validated=False).values("organisation").annotate(nb_unval=Count("organisation")).annotate(first_id_unval=Min("id"))])
64
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    32
    # We get the hda_id from the id
85
2ff78b3ac007 Correction bug #18
ymh <ymh.work@gmail.com>
parents: 73
diff changeset
    33
    val_hda_ids = dict([(res.id, res.hda_id) for res in Datasheet.objects.filter(id__in=[v[1] for v in validated_ds_mapping.values()]).only("id", "hda_id")])
2ff78b3ac007 Correction bug #18
ymh <ymh.work@gmail.com>
parents: 73
diff changeset
    34
    unval_hda_ids = dict([(res.id, res.hda_id) for res in Datasheet.objects.filter(id__in=[v[1] for v in unvalidated_ds_mapping.values()]).only("id", "hda_id")])
64
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    35
    
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    36
    for orga in orgas :
85
2ff78b3ac007 Correction bug #18
ymh <ymh.work@gmail.com>
parents: 73
diff changeset
    37
        nb_all = all_ds_mapping.get(orga.id, 0)
2ff78b3ac007 Correction bug #18
ymh <ymh.work@gmail.com>
parents: 73
diff changeset
    38
        duo_val = validated_ds_mapping.get(orga.id, [0, None])
64
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    39
        nb_val = duo_val[0]
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    40
        first_id_val = val_hda_ids.get(duo_val[1], None)
85
2ff78b3ac007 Correction bug #18
ymh <ymh.work@gmail.com>
parents: 73
diff changeset
    41
        duo_unval = unvalidated_ds_mapping.get(orga.id, [0, None])
64
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    42
        nb_unval = duo_unval[0]
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    43
        first_id_unval = unval_hda_ids.get(duo_unval[1], None)
63
03698c739b1d #8 : Better url management with datasheet id and context in parameter.
cavaliet
parents: 61
diff changeset
    44
        org_list.append({'organisation':orga, 'nb_all':nb_all, 'nb_val':nb_val, 'first_id_val':first_id_val, 'nb_unval':nb_unval, 'first_id_unval':first_id_unval})
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    45
    
28
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
    46
    return render_to_response("organisation_list.html",
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    47
                              {'organisations':org_list},
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    48
                              context_instance=RequestContext(request))
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    49
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    50
53
50f3f48467e8 Set identification. It is required now.
cavaliet
parents: 52
diff changeset
    51
@login_required
63
03698c739b1d #8 : Better url management with datasheet id and context in parameter.
cavaliet
parents: 61
diff changeset
    52
def display_datasheet(request, ds_id=None):
03698c739b1d #8 : Better url management with datasheet id and context in parameter.
cavaliet
parents: 61
diff changeset
    53
    
03698c739b1d #8 : Better url management with datasheet id and context in parameter.
cavaliet
parents: 61
diff changeset
    54
    if ds_id :
64
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    55
        ds = Datasheet.objects.get(hda_id=ds_id)
63
03698c739b1d #8 : Better url management with datasheet id and context in parameter.
cavaliet
parents: 61
diff changeset
    56
    
03698c739b1d #8 : Better url management with datasheet id and context in parameter.
cavaliet
parents: 61
diff changeset
    57
    # In this function the context is given by the get parameters.
64
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    58
    if "index" in request.GET :
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    59
        try:
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    60
            index = int(request.GET["index"])
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    61
        except :
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    62
            index = 0
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    63
    else :
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    64
        index = 0
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    65
    if "nb_sheets" in request.GET :
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    66
        try:
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    67
            nb_sheets = int(request.GET["nb_sheets"])
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    68
        except :
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    69
            nb_sheets = None
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    70
    else :
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    71
        nb_sheets = None
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    72
    # If there is tag parameter, it means that we display all the ds tagged by one particular tag
65
35f37b6210df correction pour créer les url dans template.
cavaliet
parents: 64
diff changeset
    73
    tag = None
64
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    74
    if "tag" in request.GET :
65
35f37b6210df correction pour créer les url dans template.
cavaliet
parents: 64
diff changeset
    75
        tag = Tag.objects.get(id=int(request.GET["tag"]))
85
2ff78b3ac007 Correction bug #18
ymh <ymh.work@gmail.com>
parents: 73
diff changeset
    76
        datasheets_qs = Datasheet.objects.filter(tags__in=[tag]).order_by("organisation__name", "original_creation_date").select_related("format")
64
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    77
        # If tag is set and if ds_id is None, it means that we have to display the first ds
63
03698c739b1d #8 : Better url management with datasheet id and context in parameter.
cavaliet
parents: 61
diff changeset
    78
        if not ds_id :
64
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    79
            index = 0
63
03698c739b1d #8 : Better url management with datasheet id and context in parameter.
cavaliet
parents: 61
diff changeset
    80
            ds = datasheets_qs[0]
03698c739b1d #8 : Better url management with datasheet id and context in parameter.
cavaliet
parents: 61
diff changeset
    81
    else :
03698c739b1d #8 : Better url management with datasheet id and context in parameter.
cavaliet
parents: 61
diff changeset
    82
        # The current list depends on the ds's validation state
03698c739b1d #8 : Better url management with datasheet id and context in parameter.
cavaliet
parents: 61
diff changeset
    83
        datasheets_qs = Datasheet.objects.filter(organisation=ds.organisation).filter(validated=ds.validated).order_by("id").select_related("format")
64
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    84
    # We calculate the number of sheets if necessary
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    85
    if not nb_sheets :
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    86
        nb_sheets = datasheets_qs.count()
63
03698c739b1d #8 : Better url management with datasheet id and context in parameter.
cavaliet
parents: 61
diff changeset
    87
    
64
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    88
    # We get only the prev/current/next sheets
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    89
    if nb_sheets > 1 :
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    90
        if index == 0 :
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    91
            select_qs = datasheets_qs[:2]
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    92
            prev_index = 0
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    93
            prev_id = select_qs[0].hda_id
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    94
            next_index = 1
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    95
            next_id = select_qs[1].hda_id
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    96
        elif index == (nb_sheets - 1) :
85
2ff78b3ac007 Correction bug #18
ymh <ymh.work@gmail.com>
parents: 73
diff changeset
    97
            select_qs = datasheets_qs[nb_sheets - 2:]
64
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    98
            prev_index = nb_sheets - 2
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
    99
            prev_id = select_qs[0].hda_id
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
   100
            next_index = nb_sheets - 1
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
   101
            next_id = select_qs[1].hda_id
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
   102
        else :
85
2ff78b3ac007 Correction bug #18
ymh <ymh.work@gmail.com>
parents: 73
diff changeset
   103
            select_qs = datasheets_qs[index - 1:index + 2]
64
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
   104
            prev_index = index - 1
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
   105
            prev_id = select_qs[0].hda_id
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
   106
            next_index = index + 1
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
   107
            next_id = select_qs[2].hda_id
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
   108
    else :
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
   109
        prev_index = next_index = 0
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
   110
        prev_id = next_id = datasheets_qs[0].hda_id
63
03698c739b1d #8 : Better url management with datasheet id and context in parameter.
cavaliet
parents: 61
diff changeset
   111
    
03698c739b1d #8 : Better url management with datasheet id and context in parameter.
cavaliet
parents: 61
diff changeset
   112
    # We get the ORDERED tags if we display one sheet (case valid = 0 and 1)
03698c739b1d #8 : Better url management with datasheet id and context in parameter.
cavaliet
parents: 61
diff changeset
   113
    ordered_tags = TaggedSheet.objects.filter(datasheet=ds).select_related("tag").order_by('order')
03698c739b1d #8 : Better url management with datasheet id and context in parameter.
cavaliet
parents: 61
diff changeset
   114
    
64
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
   115
    displayed_index = index + 1;
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
   116
    zero_id = datasheets_qs[0].hda_id
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
   117
    last_index = max(nb_sheets - 1, 0)
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
   118
    last_id = datasheets_qs[last_index].hda_id
63
03698c739b1d #8 : Better url management with datasheet id and context in parameter.
cavaliet
parents: 61
diff changeset
   119
    
03698c739b1d #8 : Better url management with datasheet id and context in parameter.
cavaliet
parents: 61
diff changeset
   120
    return render_to_response("generic_sheet.html",
03698c739b1d #8 : Better url management with datasheet id and context in parameter.
cavaliet
parents: 61
diff changeset
   121
                              {'ds':ds, 'orga_name':ds.organisation.name,
03698c739b1d #8 : Better url management with datasheet id and context in parameter.
cavaliet
parents: 61
diff changeset
   122
                               'nb_sheets':nb_sheets, 'ordered_tags':ordered_tags,
64
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
   123
                               'zero_id':zero_id, 'prev_index':prev_index, 'prev_id':prev_id,
85
2ff78b3ac007 Correction bug #18
ymh <ymh.work@gmail.com>
parents: 73
diff changeset
   124
                               'next_index':next_index, 'next_id':next_id,
64
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
   125
                               'last_index':last_index, 'last_id':last_id,
65
35f37b6210df correction pour créer les url dans template.
cavaliet
parents: 64
diff changeset
   126
                               'displayed_index':displayed_index, 'tag':tag, 'valid':ds.validated,
35f37b6210df correction pour créer les url dans template.
cavaliet
parents: 64
diff changeset
   127
                               'categories':json.dumps(get_categories())},
63
03698c739b1d #8 : Better url management with datasheet id and context in parameter.
cavaliet
parents: 61
diff changeset
   128
                              context_instance=RequestContext(request))
03698c739b1d #8 : Better url management with datasheet id and context in parameter.
cavaliet
parents: 61
diff changeset
   129
03698c739b1d #8 : Better url management with datasheet id and context in parameter.
cavaliet
parents: 61
diff changeset
   130
03698c739b1d #8 : Better url management with datasheet id and context in parameter.
cavaliet
parents: 61
diff changeset
   131
@login_required
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   132
def list_for_orga(request, orga_id=None, valid=None, start_index=None):
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   133
    
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   134
    orga = Organisation.objects.get(id=orga_id)
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   135
    orga_name = orga.name
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   136
    
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   137
    if start_index :
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   138
        try:
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   139
            start_index = int(start_index)
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   140
        except :
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   141
            start_index = 0
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   142
    else :
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   143
        start_index = 0
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   144
    
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   145
    # If valid = 0, we search unvalidated sheets
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   146
    # If valid = 1, we search validated sheets
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   147
    # If valid = 2, we search AND DISPLAY all sheets
55
e1098febb9d3 Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
parents: 54 52
diff changeset
   148
    datasheets_qs = Datasheet.objects.filter(organisation=orga).order_by("id").select_related("format")
e1098febb9d3 Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
parents: 54 52
diff changeset
   149
    
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   150
    if valid == "1" :
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   151
        # We count all the validated sheets
55
e1098febb9d3 Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
parents: 54 52
diff changeset
   152
        datasheets_qs = datasheets_qs.filter(validated=True)
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   153
        # And select the current one
55
e1098febb9d3 Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
parents: 54 52
diff changeset
   154
        datasheets = [datasheets_qs[start_index]]
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   155
    elif valid != "2":
34
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   156
        valid = "0"
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   157
        # We count all the validated sheets
55
e1098febb9d3 Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
parents: 54 52
diff changeset
   158
        datasheets_qs = datasheets_qs.filter(validated=False)
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   159
        # And select the current one
55
e1098febb9d3 Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
parents: 54 52
diff changeset
   160
        datasheets = [datasheets_qs[start_index]]
e1098febb9d3 Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
parents: 54 52
diff changeset
   161
    else:
e1098febb9d3 Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
parents: 54 52
diff changeset
   162
        datasheets = datasheets_qs
e1098febb9d3 Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
parents: 54 52
diff changeset
   163
e1098febb9d3 Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
parents: 54 52
diff changeset
   164
    nb_sheets = datasheets_qs.count()
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   165
    
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   166
    # We get the ORDERED tags if we display one sheet (case valid = 0 and 1)
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   167
    ordered_tags = None
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   168
    if valid != "2" :
55
e1098febb9d3 Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
parents: 54 52
diff changeset
   169
        ordered_tags = TaggedSheet.objects.filter(datasheet=datasheets[0]).select_related("tag").order_by('order')
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   170
    
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   171
    displayed_index = start_index + 1;
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   172
    prev_index = max(start_index - 1, 0);
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   173
    next_index = min(nb_sheets - 1, start_index + 1);
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   174
    last_index = max(nb_sheets - 1, 0);
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   175
    
28
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   176
    return render_to_response("list_for_orga.html",
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   177
                              {'datasheets':datasheets, 'orga_name':orga_name,
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   178
                               'nb_sheets':nb_sheets, 'orga_id':orga_id, 'ordered_tags':ordered_tags,
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   179
                               'prev_index':prev_index, 'next_index':next_index, 'last_index':last_index,
46
3ad571e54608 get categories enhancement.
cavaliet
parents: 44
diff changeset
   180
                               'start_index':start_index, 'displayed_index':displayed_index, 'valid':valid,
3ad571e54608 get categories enhancement.
cavaliet
parents: 44
diff changeset
   181
                               'categories':json.dumps(get_categories())},
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   182
                              context_instance=RequestContext(request))
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   183
32
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   184
53
50f3f48467e8 Set identification. It is required now.
cavaliet
parents: 52
diff changeset
   185
@login_required
86
417938cf4601 improve sort and add reverse order
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   186
def all_tags(request, num_page=None, nb_by_page=None, sort="+pop", searched=None):
51
6b1338c7964c First step of search on tag list.
cavaliet
parents: 50
diff changeset
   187
    
6b1338c7964c First step of search on tag list.
cavaliet
parents: 50
diff changeset
   188
    # If the view is asked after a form sent with post vars, it means that searched is a post var.
6b1338c7964c First step of search on tag list.
cavaliet
parents: 50
diff changeset
   189
    if u"searched" in request.POST :
6b1338c7964c First step of search on tag list.
cavaliet
parents: 50
diff changeset
   190
        searched = request.POST["searched"]
55
e1098febb9d3 Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
parents: 54 52
diff changeset
   191
        nb_by_page = settings.PAGINATION_DEFAULT_NB_BY_PAGE
51
6b1338c7964c First step of search on tag list.
cavaliet
parents: 50
diff changeset
   192
        num_page = 1
32
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   193
    
52
1f01957a3eae Add popularity and number of datasheet for one tag. "Unduplication" of some code in views.py.
cavaliet
parents: 51
diff changeset
   194
    # Get paginator and current page 
86
417938cf4601 improve sort and add reverse order
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   195
    current_page, p, num_page, nb_by_page = get_current_page(num_page, nb_by_page, sort, searched)
32
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   196
    
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   197
    prev_page = max(num_page - 1, 1)
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   198
    next_page = min(num_page + 1, p.num_pages)
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   199
    last_page = p.num_pages
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   200
    
56
e70cbbc093cc improve tag letter list
ymh <ymh.work@gmail.com>
parents: 55
diff changeset
   201
    
e70cbbc093cc improve tag letter list
ymh <ymh.work@gmail.com>
parents: 55
diff changeset
   202
    cursor = connection.cursor() #@UndefinedVariable
e70cbbc093cc improve tag letter list
ymh <ymh.work@gmail.com>
parents: 55
diff changeset
   203
    fl_list = []
e70cbbc093cc improve tag letter list
ymh <ymh.work@gmail.com>
parents: 55
diff changeset
   204
    try:
72
ba8ebabbaece -correct css and display
ymh <ymh.work@gmail.com>
parents: 69
diff changeset
   205
        cursor.execute("select upper(substring(normalized_label from 1 for 1)) as fl from hdabo_tag group by fl order by fl;")
51
6b1338c7964c First step of search on tag list.
cavaliet
parents: 50
diff changeset
   206
    
56
e70cbbc093cc improve tag letter list
ymh <ymh.work@gmail.com>
parents: 55
diff changeset
   207
        for row in cursor:
e70cbbc093cc improve tag letter list
ymh <ymh.work@gmail.com>
parents: 55
diff changeset
   208
            new_char = remove_accents(row[0])
e70cbbc093cc improve tag letter list
ymh <ymh.work@gmail.com>
parents: 55
diff changeset
   209
            if new_char not in fl_list:
e70cbbc093cc improve tag letter list
ymh <ymh.work@gmail.com>
parents: 55
diff changeset
   210
                fl_list.append(new_char)
e70cbbc093cc improve tag letter list
ymh <ymh.work@gmail.com>
parents: 55
diff changeset
   211
    finally:
e70cbbc093cc improve tag letter list
ymh <ymh.work@gmail.com>
parents: 55
diff changeset
   212
        cursor.close()
e70cbbc093cc improve tag letter list
ymh <ymh.work@gmail.com>
parents: 55
diff changeset
   213
    
85
2ff78b3ac007 Correction bug #18
ymh <ymh.work@gmail.com>
parents: 73
diff changeset
   214
    search_def = tuple([(c, urlquote(c + settings.SEARCH_STAR_CHARACTER)) for c in fl_list])
86
417938cf4601 improve sort and add reverse order
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   215
    reverse_sort = ("-" if sort[0] == "+" else "+") + sort[1:]
55
e1098febb9d3 Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
parents: 54 52
diff changeset
   216
        
32
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   217
    return render_to_response("all_tags.html",
54
deaa9393e6a2 slightly improve queries
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
   218
                              {'nb_total':p.count, 'tags':current_page.object_list, 'current_page':current_page,
32
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   219
                               'prev_page':prev_page, 'next_page':next_page, 'last_page':last_page,
51
6b1338c7964c First step of search on tag list.
cavaliet
parents: 50
diff changeset
   220
                               'num_page':num_page, 'nb_by_page':nb_by_page, 'searched':searched,
6b1338c7964c First step of search on tag list.
cavaliet
parents: 50
diff changeset
   221
                               'categories':json.dumps(get_categories()),
86
417938cf4601 improve sort and add reverse order
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   222
                               'search_def':search_def, 'sort':sort, 'reverse_sort': reverse_sort},
32
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   223
                              context_instance=RequestContext(request))
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   224
    
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   225
53
50f3f48467e8 Set identification. It is required now.
cavaliet
parents: 52
diff changeset
   226
@login_required
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   227
def tag_up_down(request):
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   228
    ds_id = request.POST["datasheet_id"]
44
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   229
    # post vars new_order and old_order indicate the position (from 1) of the tag in the list.
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   230
    # NB : it is different from the TagSheet.order in the database.
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   231
    new_order = int(request.POST["new_order"]) - 1
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   232
    old_order = int(request.POST["old_order"]) - 1
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   233
    # First we get the datasheet's TaggedSheets (list to force evaluation)
68
17a1c2a67c50 Correct various request on hdabo_id (avoid sub request)
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   234
    ordered_tags = list(TaggedSheet.objects.filter(datasheet__hda_id=ds_id).order_by('order'))
44
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   235
    # We change the moved TaggedSheets's order
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   236
    new_ts_order = ordered_tags[new_order].order
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   237
    moved_ts = ordered_tags[old_order]
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   238
    moved_ts.order = new_ts_order
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   239
    moved_ts.save()
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   240
    # We move the TaggedSheets's order
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   241
    if new_order > old_order :
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   242
        # And we decrease the other ones
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   243
        for i in range(old_order + 1, new_order + 1) :
44
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   244
            ts = ordered_tags[i]
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   245
            ts.order = ts.order - 1
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   246
            ts.save()
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   247
    else :
44
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   248
        # And we increase the other ones
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   249
        for i in range(new_order, old_order) :
44
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   250
            ts = ordered_tags[i]
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   251
            ts.order = ts.order + 1
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   252
            ts.save()
64
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
   253
    ds = Datasheet.objects.get(hda_id=ds_id)
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   254
    ds.manual_order = True
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   255
    ds.save()
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   256
    
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   257
    return get_tag_table(request=request, ds_id=ds_id, valid=0)
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   258
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   259
53
50f3f48467e8 Set identification. It is required now.
cavaliet
parents: 52
diff changeset
   260
@login_required
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   261
def get_tag_table(request=None, ds_id=None, valid=None):
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   262
    
68
17a1c2a67c50 Correct various request on hdabo_id (avoid sub request)
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   263
    ordered_tags = TaggedSheet.objects.filter(datasheet__hda_id=ds_id).order_by('order')
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   264
    
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   265
    return render_to_response("partial/tag_table.html",
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   266
                              {'ordered_tags':ordered_tags, 'valid':valid},
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   267
                              context_instance=RequestContext(request))
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   268
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   269
53
50f3f48467e8 Set identification. It is required now.
cavaliet
parents: 52
diff changeset
   270
@login_required
86
417938cf4601 improve sort and add reverse order
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   271
def get_all_tags_table(request, num_page=None, nb_by_page=None, sort="+pop", searched=None):
32
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   272
    
86
417938cf4601 improve sort and add reverse order
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   273
    current_page, p, num_page, nb_by_page = get_current_page(num_page, nb_by_page, sort, searched) #@UnusedVariable
417938cf4601 improve sort and add reverse order
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   274
    reverse_sort = ("-" if sort[0] == "+" else "+") + sort[1:] 
52
1f01957a3eae Add popularity and number of datasheet for one tag. "Unduplication" of some code in views.py.
cavaliet
parents: 51
diff changeset
   275
    
1f01957a3eae Add popularity and number of datasheet for one tag. "Unduplication" of some code in views.py.
cavaliet
parents: 51
diff changeset
   276
    return render_to_response("partial/all_tags_table.html",
86
417938cf4601 improve sort and add reverse order
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   277
                              {'tags':current_page.object_list, 'nb_by_page':nb_by_page, 'searched':searched, 'sort': sort, 'reverse_sort': reverse_sort},
52
1f01957a3eae Add popularity and number of datasheet for one tag. "Unduplication" of some code in views.py.
cavaliet
parents: 51
diff changeset
   278
                              context_instance=RequestContext(request))
1f01957a3eae Add popularity and number of datasheet for one tag. "Unduplication" of some code in views.py.
cavaliet
parents: 51
diff changeset
   279
1f01957a3eae Add popularity and number of datasheet for one tag. "Unduplication" of some code in views.py.
cavaliet
parents: 51
diff changeset
   280
86
417938cf4601 improve sort and add reverse order
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   281
def get_current_page(num_page=None, nb_by_page=None, sort="+pop", searched=None):
52
1f01957a3eae Add popularity and number of datasheet for one tag. "Unduplication" of some code in views.py.
cavaliet
parents: 51
diff changeset
   282
    
66
289ded098316 add revision link cf bug #12
ymh <ymh.work@gmail.com>
parents: 61
diff changeset
   283
    base_queryset = Tag.objects
55
e1098febb9d3 Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
parents: 54 52
diff changeset
   284
    
51
6b1338c7964c First step of search on tag list.
cavaliet
parents: 50
diff changeset
   285
    if searched and searched != "" :
72
ba8ebabbaece -correct css and display
ymh <ymh.work@gmail.com>
parents: 69
diff changeset
   286
        searched = normalize(searched.strip())
85
2ff78b3ac007 Correction bug #18
ymh <ymh.work@gmail.com>
parents: 73
diff changeset
   287
        regex = "^%s$" % (re.escape(searched).replace(re.escape(settings.SEARCH_STAR_CHARACTER), ".*"))
72
ba8ebabbaece -correct css and display
ymh <ymh.work@gmail.com>
parents: 69
diff changeset
   288
        base_queryset = base_queryset.filter(normalized_label__iregex=regex)
55
e1098febb9d3 Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
parents: 54 52
diff changeset
   289
73
83695a58e4d6 Merge with 579ac2e2789ac2ee6d2f14108a7a90c154412546
ymh <ymh.work@gmail.com>
parents: 72 71
diff changeset
   290
    alltags = base_queryset.annotate(num_ds=Count('datasheet'))
86
417938cf4601 improve sort and add reverse order
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   291
    
417938cf4601 improve sort and add reverse order
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   292
    alltags = {
417938cf4601 improve sort and add reverse order
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   293
        "+lab" : alltags.order_by('normalized_label', 'label'),
417938cf4601 improve sort and add reverse order
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   294
        "-lab" : alltags.order_by('-normalized_label', '-label'),
417938cf4601 improve sort and add reverse order
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   295
        "+pop" : alltags.order_by('-popularity', '-num_ds', 'normalized_label', 'label'),
89
ecacd68cf859 add sort direction, corrections
ymh <ymh.work@gmail.com>
parents: 86
diff changeset
   296
        "-pop" : alltags.order_by('popularity', 'num_ds', '-normalized_label', '-label'),
86
417938cf4601 improve sort and add reverse order
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   297
    }.get(sort, alltags.order_by('-popularity', '-num_ds', 'normalized_label', 'label'))
417938cf4601 improve sort and add reverse order
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   298
    
51
6b1338c7964c First step of search on tag list.
cavaliet
parents: 50
diff changeset
   299
    
32
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   300
    # We build the paginator for the requested list
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   301
    if nb_by_page :
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   302
        try:
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   303
            nb_by_page = int(nb_by_page)
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   304
        except :
55
e1098febb9d3 Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
parents: 54 52
diff changeset
   305
            nb_by_page = settings.PAGINATION_DEFAULT_NB_BY_PAGE
32
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   306
    else :
55
e1098febb9d3 Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
parents: 54 52
diff changeset
   307
        nb_by_page = settings.PAGINATION_DEFAULT_NB_BY_PAGE
32
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   308
    if num_page :
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   309
        try:
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   310
            num_page = int(num_page)
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   311
        except :
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   312
            num_page = 1
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   313
    else :
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   314
        num_page = 1
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   315
    p = Paginator(alltags, nb_by_page)
55
e1098febb9d3 Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
parents: 54 52
diff changeset
   316
    
e1098febb9d3 Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
parents: 54 52
diff changeset
   317
    #use the much simpler base query to have the count
e1098febb9d3 Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
parents: 54 52
diff changeset
   318
    p._count = base_queryset.count()
32
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   319
    current_page = p.page(num_page)
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   320
    
52
1f01957a3eae Add popularity and number of datasheet for one tag. "Unduplication" of some code in views.py.
cavaliet
parents: 51
diff changeset
   321
    return current_page, p, num_page, nb_by_page
32
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   322
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   323
53
50f3f48467e8 Set identification. It is required now.
cavaliet
parents: 52
diff changeset
   324
@login_required
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   325
def remove_tag_from_list(request=None):
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   326
    
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   327
    ds_id = request.POST["datasheet_id"]
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   328
    tag_id = request.POST["tag_id"]
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   329
    # First we get the datasheet's TaggedSheets
68
17a1c2a67c50 Correct various request on hdabo_id (avoid sub request)
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   330
    ds_tags = TaggedSheet.objects.filter(datasheet__hda_id=ds_id)
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   331
    # We get the current TaggedSheet and we delete it
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   332
    ts = ds_tags.filter(tag=Tag.objects.filter(id=tag_id))[0]
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   333
    ts.delete()
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   334
    
64
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
   335
    ds = Datasheet.objects.get(hda_id=ds_id)
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   336
    ds.manual_order = True
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   337
    ds.save()
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   338
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   339
    return get_tag_table(request=request, ds_id=ds_id, valid=0)
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   340
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   341
53
50f3f48467e8 Set identification. It is required now.
cavaliet
parents: 52
diff changeset
   342
@login_required
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   343
def modify_tag(request):
42
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   344
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   345
    tag_id = request.POST["id"]
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   346
    tag_label = request.POST["value"]
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   347
        
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   348
    tag = Tag.objects.get(id=tag_id)
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   349
    
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   350
    if tag.label != tag_label:
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   351
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   352
        tag.label = tag_label
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   353
        
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   354
        site = wiki.Wiki(settings.WIKIPEDIA_API_URL) #@UndefinedVariable
66
289ded098316 add revision link cf bug #12
ymh <ymh.work@gmail.com>
parents: 61
diff changeset
   355
        wp_res = query_wikipedia_title(site, label=tag_label)
289ded098316 add revision link cf bug #12
ymh <ymh.work@gmail.com>
parents: 61
diff changeset
   356
        status, url, pageid, dbpedia_uri, revision_id = (wp_res['status'], wp_res['wikipedia_url'], wp_res['pageid'], wp_res["dbpedia_uri"], wp_res['revision_id'])
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   357
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   358
        if status is not None:
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   359
            tag.url_status = status
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   360
66
289ded098316 add revision link cf bug #12
ymh <ymh.work@gmail.com>
parents: 61
diff changeset
   361
        old_pageid = tag.wikipedia_pageid
289ded098316 add revision link cf bug #12
ymh <ymh.work@gmail.com>
parents: 61
diff changeset
   362
        
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   363
        tag.wikipedia_url = url
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   364
        tag.wikipedia_pageid = pageid            
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   365
        tag.dbpedia_uri = dbpedia_uri 
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   366
            
85
2ff78b3ac007 Correction bug #18
ymh <ymh.work@gmail.com>
parents: 73
diff changeset
   367
        try:
2ff78b3ac007 Correction bug #18
ymh <ymh.work@gmail.com>
parents: 73
diff changeset
   368
            tag.save()
2ff78b3ac007 Correction bug #18
ymh <ymh.work@gmail.com>
parents: 73
diff changeset
   369
        except:
2ff78b3ac007 Correction bug #18
ymh <ymh.work@gmail.com>
parents: 73
diff changeset
   370
            return  HttpResponseBadRequest(json.dumps({'error': 'duplicate_tag', 'message': u"Le tag %s (%s) existe déjà." % (tag_label, tag.original_label)}), mimetype="application/json")
66
289ded098316 add revision link cf bug #12
ymh <ymh.work@gmail.com>
parents: 61
diff changeset
   371
        
289ded098316 add revision link cf bug #12
ymh <ymh.work@gmail.com>
parents: 61
diff changeset
   372
        if old_pageid != pageid:
289ded098316 add revision link cf bug #12
ymh <ymh.work@gmail.com>
parents: 61
diff changeset
   373
            TaggedSheet.objects.filter(tag=tag).update(wikipedia_revision_id=revision_id)
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   374
    
86
417938cf4601 improve sort and add reverse order
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   375
    return get_all_tags_table(request=request, num_page=request.POST["num_page"], nb_by_page=request.POST["nb_by_page"], sort=request.POST["sort"], searched=request.POST["searched"])
42
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   376
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   377
53
50f3f48467e8 Set identification. It is required now.
cavaliet
parents: 52
diff changeset
   378
@login_required
42
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   379
def modify_tag_datasheet(request):
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   380
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   381
    tag_id = request.POST["id"]
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   382
    tag_label = request.POST["value"]
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   383
    ds_id = request.POST["datasheet_id"]
42
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   384
        
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   385
    tag = Tag.objects.get(id=tag_id)
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   386
    
64
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
   387
    ds = Datasheet.objects.get(hda_id=ds_id)
42
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   388
        
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   389
    if tag.label != tag_label:
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   390
        
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   391
        if tag_label.lower() in [t.label.lower() for t in ds.tags.all()]:
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   392
            return  HttpResponseBadRequest(json.dumps({'error': 'duplicate_tag', 'message': u"Le tag %s existe déjà pour cette fiche." % (tag_label)}), mimetype="application/json")
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   393
66
289ded098316 add revision link cf bug #12
ymh <ymh.work@gmail.com>
parents: 61
diff changeset
   394
        tag, revision_id, created = get_or_create_tag(tag_label) #@UnusedVariable
42
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   395
68
17a1c2a67c50 Correct various request on hdabo_id (avoid sub request)
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   396
        ts = TaggedSheet.objects.get(tag=tag_id, datasheet__hda_id=ds_id)
42
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   397
        ts.tag = tag
66
289ded098316 add revision link cf bug #12
ymh <ymh.work@gmail.com>
parents: 61
diff changeset
   398
        ts.wikipedia_revision_id = revision_id
42
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   399
        kwargs = {DJANGO_ID + "__exact": unicode(ds_id)}
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   400
        results = SearchQuerySet().filter(title=tag_label).filter_or(description=tag_label).filter(**kwargs)
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   401
        if len(results) > 0:
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   402
            ts.index_note = results[0].score        
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   403
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   404
        ts.save()
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   405
        
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   406
        ds.manual_order = True
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   407
        ds.save()        
42
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   408
    
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   409
    return get_tag_table(request=request, ds_id=ds_id, valid=0)
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   410
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   411
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   412
53
50f3f48467e8 Set identification. It is required now.
cavaliet
parents: 52
diff changeset
   413
@login_required
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   414
def reset_wikipedia_info(request):
32
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   415
    # 2 cases : 
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   416
    # - ordered tag for one datasheet : POST["datasheet_id"] is not null
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   417
    # - all tags list : POST["datasheet_id"] is null and POST["num_page"] and POST["nb_by_page"] are not null
31
142d0440c9aa Remove wp link button activated. Some js function gathered.
cavaliet
parents: 30
diff changeset
   418
    tag_id = request.POST["tag_id"]
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   419
    
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   420
    tag = Tag.objects.get(id=tag_id)
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   421
    site = wiki.Wiki(settings.WIKIPEDIA_API_URL) #@UndefinedVariable
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   422
    
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   423
    tag.label = normalize_tag(tag.original_label)
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   424
    
33
8e5be9538279 improve reset wikipedia info
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   425
    #reset tag info
8e5be9538279 improve reset wikipedia info
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   426
    tag.wikipedia_url = None
8e5be9538279 improve reset wikipedia info
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   427
    tag.wikipedia_pageid = None
36
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   428
    
85
2ff78b3ac007 Correction bug #18
ymh <ymh.work@gmail.com>
parents: 73
diff changeset
   429
    try:
2ff78b3ac007 Correction bug #18
ymh <ymh.work@gmail.com>
parents: 73
diff changeset
   430
        process_tag(site, tag)
2ff78b3ac007 Correction bug #18
ymh <ymh.work@gmail.com>
parents: 73
diff changeset
   431
    except:
2ff78b3ac007 Correction bug #18
ymh <ymh.work@gmail.com>
parents: 73
diff changeset
   432
        return HttpResponseBadRequest(json.dumps({'error': 'duplicate_tag', 'message': u"La version sémantisée du tag %s (%s) existe déjà." % (tag.label, tag.original_label)}), mimetype="application/json")
66
289ded098316 add revision link cf bug #12
ymh <ymh.work@gmail.com>
parents: 61
diff changeset
   433
289ded098316 add revision link cf bug #12
ymh <ymh.work@gmail.com>
parents: 61
diff changeset
   434
    
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   435
    
32
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   436
    if u"datasheet_id" in request.POST :
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   437
        return get_tag_table(request=request, ds_id=request.POST["datasheet_id"], valid=0)
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   438
    else :
86
417938cf4601 improve sort and add reverse order
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   439
        return get_all_tags_table(request=request, num_page=request.POST["num_page"], nb_by_page=request.POST["nb_by_page"], sort=request.POST["sort"], searched=request.POST["searched"])
28
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   440
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   441
53
50f3f48467e8 Set identification. It is required now.
cavaliet
parents: 52
diff changeset
   442
@login_required
28
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   443
def add_tag(request=None):
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   444
    
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   445
    ds_id = request.POST["datasheet_id"]
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   446
    tag_label = request.POST["value"]
43
e0812bc3ef44 update model and fixture
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
   447
    
64
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
   448
    ds = Datasheet.objects.get(hda_id=ds_id)
43
e0812bc3ef44 update model and fixture
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
   449
    if tag_label.lower() in [t.label.lower() for t in ds.tags.all()]:
e0812bc3ef44 update model and fixture
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
   450
        return  HttpResponseBadRequest(json.dumps({'error': 'duplicate_tag', 'message': u"Le tag %s existe déjà pour cette fiche." % (tag_label)}), mimetype="application/json")
e0812bc3ef44 update model and fixture
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
   451
29
d12f11960bb6 correct tag creation
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   452
66
289ded098316 add revision link cf bug #12
ymh <ymh.work@gmail.com>
parents: 61
diff changeset
   453
    tag, revision_id, created = get_or_create_tag(tag_label)    
28
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   454
    # We put the tag at the bottom of the datasheet's tag list 
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   455
    # if the tag is created or if the tag is not in the list
43
e0812bc3ef44 update model and fixture
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
   456
    
28
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   457
    list_ts = TaggedSheet.objects.filter(datasheet=ds)
66
289ded098316 add revision link cf bug #12
ymh <ymh.work@gmail.com>
parents: 61
diff changeset
   458
    if created or list_ts.filter(tag=tag).count() == 0 :
28
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   459
        new_order = list_ts.aggregate(Max('order'))['order__max'] + 1
66
289ded098316 add revision link cf bug #12
ymh <ymh.work@gmail.com>
parents: 61
diff changeset
   460
        ts = TaggedSheet.objects.create(datasheet=ds, tag=tag, original_order=new_order, order=new_order, wikipedia_revision_id=revision_id)
28
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   461
        ts.save()
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   462
        ds.manual_order = True
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   463
        ds.save()
28
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   464
    
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   465
    return get_tag_table(request=request, ds_id=ds_id, valid=0)
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   466
31
142d0440c9aa Remove wp link button activated. Some js function gathered.
cavaliet
parents: 30
diff changeset
   467
53
50f3f48467e8 Set identification. It is required now.
cavaliet
parents: 52
diff changeset
   468
@login_required
31
142d0440c9aa Remove wp link button activated. Some js function gathered.
cavaliet
parents: 30
diff changeset
   469
def remove_wp_link(request=None):
32
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   470
    # 2 cases : 
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   471
    # - ordered tag for one datasheet : POST["datasheet_id"] is not null
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   472
    # - all tags list : POST["datasheet_id"] is null and POST["num_page"] and POST["nb_by_page"] are not null
31
142d0440c9aa Remove wp link button activated. Some js function gathered.
cavaliet
parents: 30
diff changeset
   473
    tag_id = request.POST["tag_id"]
68
17a1c2a67c50 Correct various request on hdabo_id (avoid sub request)
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   474
    tag = Tag.objects.get(id=tag_id)
69
3b4a2c79524e desactivation lien W dans la fiche
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
   475
31
142d0440c9aa Remove wp link button activated. Some js function gathered.
cavaliet
parents: 30
diff changeset
   476
    
69
3b4a2c79524e desactivation lien W dans la fiche
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
   477
    if u"datasheet_id" in request.POST :
3b4a2c79524e desactivation lien W dans la fiche
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
   478
        #create another tag almost identical, without the W info
3b4a2c79524e desactivation lien W dans la fiche
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
   479
        new_tag, created = Tag.objects.get_or_create(label=tag.label, original_label=tag.original_label, url_status=Tag.TAG_URL_STATUS_DICT['null_result'], #@UnusedVariable
3b4a2c79524e desactivation lien W dans la fiche
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
   480
                                            defaults={'label':tag.label, 'original_label': tag.original_label, 'url_status':Tag.TAG_URL_STATUS_DICT['null_result'], 'wikipedia_url': None, 'wikipedia_pageid': None, 'dbpedia_uri': None, 'category':tag.category, 'alias':tag.alias, 'popularity':tag.popularity})
3b4a2c79524e desactivation lien W dans la fiche
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
   481
        
3b4a2c79524e desactivation lien W dans la fiche
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
   482
        TaggedSheet.objects.filter(tag=tag, datasheet__hda_id=request.POST["datasheet_id"]).update(tag=new_tag, wikipedia_revision_id=None)
3b4a2c79524e desactivation lien W dans la fiche
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
   483
        
3b4a2c79524e desactivation lien W dans la fiche
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
   484
    else:
85
2ff78b3ac007 Correction bug #18
ymh <ymh.work@gmail.com>
parents: 73
diff changeset
   485
        
2ff78b3ac007 Correction bug #18
ymh <ymh.work@gmail.com>
parents: 73
diff changeset
   486
        if Tag.objects.filter(label=tag.label, original_label=tag.original_label, url_status=Tag.TAG_URL_STATUS_DICT['null_result']).count() > 0:
2ff78b3ac007 Correction bug #18
ymh <ymh.work@gmail.com>
parents: 73
diff changeset
   487
            return HttpResponseBadRequest(json.dumps({'error': 'duplicate_tag', 'message': u"La version désémantisée du tag %s (%s) existe déjà." % (tag.label, tag.original_label)}), mimetype="application/json")
69
3b4a2c79524e desactivation lien W dans la fiche
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
   488
        tag.wikipedia_url = None
3b4a2c79524e desactivation lien W dans la fiche
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
   489
        tag.wikipedia_pageid = None
3b4a2c79524e desactivation lien W dans la fiche
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
   490
        tag.dbpedia_uri = None
3b4a2c79524e desactivation lien W dans la fiche
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
   491
        tag.url_status = Tag.TAG_URL_STATUS_DICT['null_result']
3b4a2c79524e desactivation lien W dans la fiche
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
   492
        tag.save()
3b4a2c79524e desactivation lien W dans la fiche
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
   493
    
3b4a2c79524e desactivation lien W dans la fiche
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
   494
        TaggedSheet.objects.filter(tag=tag).update(wikipedia_revision_id=None)
68
17a1c2a67c50 Correct various request on hdabo_id (avoid sub request)
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   495
    
32
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   496
    if u"datasheet_id" in request.POST :
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   497
        return get_tag_table(request=request, ds_id=request.POST["datasheet_id"], valid=0)
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   498
    else :
86
417938cf4601 improve sort and add reverse order
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   499
        return get_all_tags_table(request=request, num_page=request.POST["num_page"], nb_by_page=request.POST["nb_by_page"], sort=request.POST["sort"], searched=request.POST["searched"])
34
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   500
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   501
53
50f3f48467e8 Set identification. It is required now.
cavaliet
parents: 52
diff changeset
   502
@login_required
34
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   503
def validate_datasheet(request=None, ds_id=None, valid=None):
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   504
    # We set if valid is true of false, function of the url parameters
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   505
    if valid == "1" or valid == "true" or not valid :
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   506
        valid = True
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   507
    else :
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   508
        valid = False
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   509
    # We validate or unvalidate the requester datasheet
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   510
    
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   511
    if request.user.is_authenticated():
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   512
        user = request.user
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   513
    else:
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   514
        user = None
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   515
    
64
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
   516
    ds = Datasheet.objects.get(hda_id=ds_id)
34
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   517
    if valid :
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   518
        ds.validate(user)
34
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   519
    else :
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   520
        ds.unvalidate()
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   521
    ds.save()
63
03698c739b1d #8 : Better url management with datasheet id and context in parameter.
cavaliet
parents: 61
diff changeset
   522
    
34
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   523
    # If there are still some unvalidated/validated ds for the ds's orga, we display the first one
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   524
    same_organisation_ds = Datasheet.objects.filter(organisation=ds.organisation).filter(validated=not valid)
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   525
    if len(same_organisation_ds) > 0 :
64
a53a1867cec9 #15 : utiliser id_hda et pas id pour les fiches
cavaliet
parents: 63
diff changeset
   526
        return redirect('display_datasheet', ds_id=same_organisation_ds[0].hda_id)
34
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   527
    else :
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   528
        return redirect('home')
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   529
    
36
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   530
53
50f3f48467e8 Set identification. It is required now.
cavaliet
parents: 52
diff changeset
   531
@login_required
36
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   532
def update_tag_alias(request):
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   533
    # 2 cases : 
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   534
    # - ordered tag for one datasheet : POST["datasheet_id"] is not null
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   535
    # - all tags list : POST["datasheet_id"] is null and POST["num_page"] and POST["nb_by_page"] are not null
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   536
    tag_id = request.POST["id"]
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   537
    tag_alias = request.POST["value"]
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   538
    tag = Tag.objects.get(id=tag_id)
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   539
    tag.alias = tag_alias
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   540
    tag.save()
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   541
    
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   542
    if u"datasheet_id" in request.POST :
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   543
        return get_tag_table(request=request, ds_id=request.POST["datasheet_id"], valid=0)
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   544
    else :
86
417938cf4601 improve sort and add reverse order
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   545
        return get_all_tags_table(request=request, num_page=request.POST["num_page"], nb_by_page=request.POST["nb_by_page"], sort=request.POST["sort"], searched=request.POST["searched"])
36
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   546
    
44
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   547
46
3ad571e54608 get categories enhancement.
cavaliet
parents: 44
diff changeset
   548
def get_categories():
3ad571e54608 get categories enhancement.
cavaliet
parents: 44
diff changeset
   549
    # List of categories in an OrderedDict
3ad571e54608 get categories enhancement.
cavaliet
parents: 44
diff changeset
   550
    categories = OrderedDict({"":""})
3ad571e54608 get categories enhancement.
cavaliet
parents: 44
diff changeset
   551
    for c in TagCategory.objects.order_by('label') :
3ad571e54608 get categories enhancement.
cavaliet
parents: 44
diff changeset
   552
        categories.update({c.label:c.label})
3ad571e54608 get categories enhancement.
cavaliet
parents: 44
diff changeset
   553
    return categories
44
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   554
    
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   555
53
50f3f48467e8 Set identification. It is required now.
cavaliet
parents: 52
diff changeset
   556
@login_required
44
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   557
def update_tag_category(request):
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   558
    
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   559
    tag_id = request.POST["id"]
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   560
    cat = request.POST["value"]
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   561
    tag = Tag.objects.get(id=tag_id)
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   562
    if cat and cat != "" :
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   563
        tag.category = TagCategory.objects.get(label=cat)
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   564
    else :
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   565
        tag.category = None
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   566
    tag.save()
61
52fcbb0e0a67 #4 : Enlarge input size.
cavaliet
parents: 57
diff changeset
   567
    
52fcbb0e0a67 #4 : Enlarge input size.
cavaliet
parents: 57
diff changeset
   568
    if u"datasheet_id" in request.POST :
52fcbb0e0a67 #4 : Enlarge input size.
cavaliet
parents: 57
diff changeset
   569
        return get_tag_table(request=request, ds_id=request.POST["datasheet_id"], valid=0)
52fcbb0e0a67 #4 : Enlarge input size.
cavaliet
parents: 57
diff changeset
   570
    else :
71
579ac2e2789a Add alphabetical sort on tag list.
cavaliet
parents: 69
diff changeset
   571
        return get_all_tags_table(request=request, num_page=request.POST["num_page"], nb_by_page=request.POST["nb_by_page"], alpha=request.POST["alpha"], searched=request.POST["searched"])
44
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   572
    
72
ba8ebabbaece -correct css and display
ymh <ymh.work@gmail.com>
parents: 69
diff changeset
   573
@login_required
ba8ebabbaece -correct css and display
ymh <ymh.work@gmail.com>
parents: 69
diff changeset
   574
def reorder_tag_datasheet(request):
ba8ebabbaece -correct css and display
ymh <ymh.work@gmail.com>
parents: 69
diff changeset
   575
    
ba8ebabbaece -correct css and display
ymh <ymh.work@gmail.com>
parents: 69
diff changeset
   576
    ds_id = request.REQUEST['datasheet_id']
ba8ebabbaece -correct css and display
ymh <ymh.work@gmail.com>
parents: 69
diff changeset
   577
    ds = Datasheet.objects.get(hda_id=ds_id)
ba8ebabbaece -correct css and display
ymh <ymh.work@gmail.com>
parents: 69
diff changeset
   578
    reorder_datasheet_tags(ds)
ba8ebabbaece -correct css and display
ymh <ymh.work@gmail.com>
parents: 69
diff changeset
   579
    
ba8ebabbaece -correct css and display
ymh <ymh.work@gmail.com>
parents: 69
diff changeset
   580
    return get_tag_table(request=request, ds_id=ds_id, valid=0)