web/hdabo/views.py
author ymh <ymh.work@gmail.com>
Wed, 22 Jun 2011 11:59:57 +0200
changeset 50 dca0c476617b
parent 47 08b008c5a07d
child 51 6b1338c7964c
permissions -rw-r--r--
correct moify tag
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
28
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
     6
from django.db.models import Max
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
     7
from django.http import HttpResponseBadRequest
34
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
     8
from django.shortcuts import render_to_response, redirect
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
     9
from django.template import RequestContext
42
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    10
from haystack.constants import DJANGO_ID
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    11
from haystack.query import SearchQuerySet
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
    12
from hdabo.wp_utils import process_tag
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
    13
from hdabo.utils import OrderedDict
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
    14
from hdabo.wp_utils import (normalize_tag, query_wikipedia_title,
42
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    15
    get_or_create_tag)
44
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
    16
from models import Datasheet, Organisation, Tag, TagCategory, TaggedSheet
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    17
from wikitools import wiki
42
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    18
import django.utils.simplejson as json
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    19
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    20
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    21
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    22
#@login_required
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    23
def home(request):
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    24
    
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
    25
    # Get all organizations
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')
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    27
    # Count all validated, unvalidated sheets for each organisation
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    28
    org_list = []
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    29
    for orga in orgas :
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    30
        all_datasheets = Datasheet.objects.filter(organisation=orga)
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    31
        nb_all = len(all_datasheets)
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    32
        nb_val = len(all_datasheets.filter(validated=True))
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    33
        nb_unval = len(all_datasheets.filter(validated=False))
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    34
        org_list.append({'organisation':orga, 'nb_all':nb_all, 'nb_val':nb_val, 'nb_unval':nb_unval})
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    35
    
28
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
    36
    return render_to_response("organisation_list.html",
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    37
                              {'organisations':org_list},
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    38
                              context_instance=RequestContext(request))
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    39
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    40
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    41
#@login_required
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    42
def list_for_orga(request, orga_id=None, valid=None, start_index=None):
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    43
    
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    44
    orga = Organisation.objects.get(id=orga_id)
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    45
    orga_name = orga.name
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    46
    
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    47
    if start_index :
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    48
        try:
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    49
            start_index = int(start_index)
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    50
        except :
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    51
            start_index = 0
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    52
    else :
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    53
        start_index = 0
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    54
    
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    55
    # If valid = 0, we search unvalidated sheets
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    56
    # If valid = 1, we search validated sheets
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    57
    # If valid = 2, we search AND DISPLAY all sheets
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    58
    if valid == "1" :
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    59
        # We count all the validated sheets
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    60
        datasheets = Datasheet.objects.filter(organisation=orga).filter(validated=True)
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    61
        nb_sheets = len(datasheets)
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    62
        # And select the current one
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    63
        datasheets = [datasheets[start_index]]
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    64
    elif valid != "2":
34
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
    65
        valid = "0"
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    66
        # We count all the validated sheets
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    67
        datasheets = Datasheet.objects.filter(organisation=orga).filter(validated=False)
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    68
        nb_sheets = len(datasheets)
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    69
        # And select the current one
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    70
        datasheets = [datasheets[start_index]]
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    71
    else :
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    72
        datasheets = Datasheet.objects.filter(organisation=orga)
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    73
        nb_sheets = len(datasheets)
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    74
    
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    75
    # We get the ORDERED tags if we display one sheet (case valid = 0 and 1)
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    76
    ordered_tags = None
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    77
    if valid != "2" :
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    78
        ordered_tags = TaggedSheet.objects.filter(datasheet=datasheets[0]).order_by('order')
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    79
    
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    80
    displayed_index = start_index + 1;
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    81
    prev_index = max(start_index - 1, 0);
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    82
    next_index = min(nb_sheets - 1, start_index + 1);
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    83
    last_index = max(nb_sheets - 1, 0);
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    84
    
28
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
    85
    return render_to_response("list_for_orga.html",
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    86
                              {'datasheets':datasheets, 'orga_name':orga_name,
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    87
                               'nb_sheets':nb_sheets, 'orga_id':orga_id, 'ordered_tags':ordered_tags,
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    88
                               'prev_index':prev_index, 'next_index':next_index, 'last_index':last_index,
46
3ad571e54608 get categories enhancement.
cavaliet
parents: 44
diff changeset
    89
                               'start_index':start_index, 'displayed_index':displayed_index, 'valid':valid,
3ad571e54608 get categories enhancement.
cavaliet
parents: 44
diff changeset
    90
                               'categories':json.dumps(get_categories())},
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    91
                              context_instance=RequestContext(request))
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
    92
32
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
    93
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
    94
#@login_required
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
    95
def all_tags(request, num_page=None, nb_by_page=None):
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
    96
    
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
    97
    alltags = Tag.objects.order_by('label')
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
    98
    nb_total = len(alltags)
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
    99
    # 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
   100
    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
   101
        try:
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   102
            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
   103
        except :
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   104
            nb_by_page = 25
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   105
    else :
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   106
        nb_by_page = 25
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   107
    if num_page :
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   108
        try:
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   109
            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
   110
        except :
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   111
            num_page = 1
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   112
    else :
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   113
        num_page = 1
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   114
    p = Paginator(alltags, nb_by_page)
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   115
    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
   116
    
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   117
    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
   118
    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
   119
    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
   120
    
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   121
    return render_to_response("all_tags.html",
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   122
                              {'nb_total':nb_total, 'tags':current_page.object_list, 'current_page':current_page,
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   123
                               'prev_page':prev_page, 'next_page':next_page, 'last_page':last_page,
46
3ad571e54608 get categories enhancement.
cavaliet
parents: 44
diff changeset
   124
                               'num_page':num_page, 'nb_by_page':nb_by_page, 'categories':json.dumps(get_categories())},
32
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   125
                              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
   126
    
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   127
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   128
#@login_required
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   129
def tag_up_down(request):
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   130
    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
   131
    # 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
   132
    # 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
   133
    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
   134
    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
   135
    # First we get the datasheet's TaggedSheets (list to force evaluation)
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   136
    ordered_tags = list(TaggedSheet.objects.filter(datasheet=Datasheet.objects.get(id=ds_id)).order_by('order'))
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   137
    # 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
   138
    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
   139
    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
   140
    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
   141
    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
   142
    # 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
   143
    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
   144
        # And we decrease the other ones
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   145
        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
   146
            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
   147
            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
   148
            ts.save()
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   149
    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
   150
        # And we increase the other ones
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   151
        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
   152
            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
   153
            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
   154
            ts.save()
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   155
    ds = Datasheet.objects.get(id=ds_id)
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   156
    ds.manual_order = True
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   157
    ds.save()
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   158
    
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   159
    return get_tag_table(request=request, ds_id=ds_id, valid=0)
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   160
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   161
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   162
#@login_required
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   163
def get_tag_table(request=None, ds_id=None, valid=None):
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   164
    
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   165
    ordered_tags = TaggedSheet.objects.filter(datasheet=Datasheet.objects.filter(id=ds_id)[0]).order_by('order')
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   166
    
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   167
    return render_to_response("partial/tag_table.html",
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   168
                              {'ordered_tags':ordered_tags, 'valid':valid},
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   169
                              context_instance=RequestContext(request))
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   170
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   171
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   172
#@login_required
32
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   173
def get_all_tags_table(request, num_page=None, nb_by_page=None):
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   174
    
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   175
    alltags = Tag.objects.order_by('label')
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   176
    # 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
   177
    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
   178
        try:
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   179
            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
   180
        except :
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   181
            nb_by_page = 25
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   182
    else :
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   183
        nb_by_page = 25
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   184
    if num_page :
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   185
        try:
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   186
            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
   187
        except :
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   188
            num_page = 1
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   189
    else :
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   190
        num_page = 1
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   191
    p = Paginator(alltags, nb_by_page)
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   192
    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
   193
    
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   194
    return render_to_response("partial/all_tags_table.html",
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   195
                              {'tags':current_page.object_list},
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   196
                              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
   197
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   198
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   199
#@login_required
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   200
def remove_tag_from_list(request=None):
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   201
    
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   202
    ds_id = request.POST["datasheet_id"]
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   203
    tag_id = request.POST["tag_id"]
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   204
    # First we get the datasheet's TaggedSheets
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   205
    ds_tags = TaggedSheet.objects.filter(datasheet=Datasheet.objects.filter(id=ds_id)[0])
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   206
    # We get the current TaggedSheet and we delete it
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   207
    ts = ds_tags.filter(tag=Tag.objects.filter(id=tag_id))[0]
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   208
    ts.delete()
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   209
    
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   210
    ds = Datasheet.objects.get(id=ds_id)
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   211
    ds.manual_order = True
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   212
    ds.save()
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   213
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   214
    return get_tag_table(request=request, ds_id=ds_id, valid=0)
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   215
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   216
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   217
#@login_required
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   218
def modify_tag(request):
42
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   219
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   220
    tag_id = request.POST["id"]
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   221
    tag_label = request.POST["value"]
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   222
        
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   223
    tag = Tag.objects.get(id=tag_id)
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   224
    
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   225
    if tag.label != tag_label:
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   226
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   227
        tag.label = tag_label
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   228
        
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   229
        site = wiki.Wiki(settings.WIKIPEDIA_API_URL) #@UndefinedVariable
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   230
        wp_res = query_wikipedia_title(site, tag_label)
50
dca0c476617b correct moify tag
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
   231
        status, url, pageid, dbpedia_uri = (wp_res['status'], wp_res['wikipedia_url'], wp_res['pageid'], wp_res["dbpedia_uri"])
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   232
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   233
        if status is not None:
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   234
            tag.url_status = status
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   235
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   236
        tag.wikipedia_url = url
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   237
        tag.wikipedia_pageid = pageid            
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   238
        tag.dbpedia_uri = dbpedia_uri 
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   239
            
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   240
        tag.save()
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   241
    
42
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   242
    return get_all_tags_table(request=request, num_page=request.POST["num_page"], nb_by_page=request.POST["nb_by_page"])
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   243
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   244
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   245
#@login_required
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   246
def modify_tag_datasheet(request):
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   247
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   248
    tag_id = request.POST["id"]
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   249
    tag_label = request.POST["value"]
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   250
    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
   251
        
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   252
    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
   253
    
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   254
    ds = Datasheet.objects.get(id=ds_id)
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   255
        
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   256
    if tag.label != tag_label:
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   257
        
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   258
        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
   259
            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
   260
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   261
        tag, created = get_or_create_tag(tag_label) #@UnusedVariable
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   262
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   263
        ts = TaggedSheet.objects.get(tag=tag_id, datasheet=ds_id)
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   264
        ts.tag = tag
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   265
        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
   266
        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
   267
        if len(results) > 0:
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   268
            ts.index_note = results[0].score        
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   269
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   270
        ts.save()
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   271
        
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   272
        ds.manual_order = True
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   273
        ds.save()        
42
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   274
    
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   275
    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
   276
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   277
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   278
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   279
#@login_required
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   280
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
   281
    # 2 cases : 
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   282
    # - 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
   283
    # - 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
   284
    tag_id = request.POST["tag_id"]
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   285
    
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   286
    tag = Tag.objects.get(id=tag_id)
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   287
    site = wiki.Wiki(settings.WIKIPEDIA_API_URL) #@UndefinedVariable
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   288
    
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   289
    tag.label = normalize_tag(tag.original_label)
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   290
    
33
8e5be9538279 improve reset wikipedia info
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   291
    #reset tag info
8e5be9538279 improve reset wikipedia info
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   292
    tag.wikipedia_url = None
8e5be9538279 improve reset wikipedia info
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   293
    tag.wikipedia_pageid = None
36
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   294
    
27
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   295
    process_tag(site, tag, 0)
056c19e37eab fusionner
cavaliet
parents: 25 26
diff changeset
   296
    
32
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   297
    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
   298
        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
   299
    else :
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   300
        return get_all_tags_table(request=request, num_page=request.POST["num_page"], nb_by_page=request.POST["nb_by_page"])
28
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   301
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   302
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   303
#@login_required
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   304
def add_tag(request=None):
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   305
    
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   306
    ds_id = request.POST["datasheet_id"]
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   307
    tag_label = request.POST["value"]
43
e0812bc3ef44 update model and fixture
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
   308
    
e0812bc3ef44 update model and fixture
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
   309
    ds = Datasheet.objects.get(id=ds_id)
e0812bc3ef44 update model and fixture
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
   310
    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
   311
        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
   312
29
d12f11960bb6 correct tag creation
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   313
42
861a78f74a37 modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
   314
    tag, created = get_or_create_tag(tag_label)    
28
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   315
    # We put the tag at the bottom of the datasheet's tag list 
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   316
    # 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
   317
    
28
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   318
    list_ts = TaggedSheet.objects.filter(datasheet=ds)
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   319
    if created or len(list_ts.filter(tag=tag)) == 0 :
28
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   320
        new_order = list_ts.aggregate(Max('order'))['order__max'] + 1
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   321
        ts = TaggedSheet.objects.create(datasheet=ds, tag=tag, original_order=new_order, order=new_order)
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   322
        ts.save()
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   323
        ds.manual_order = True
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   324
        ds.save()
28
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   325
    
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   326
    return get_tag_table(request=request, ds_id=ds_id, valid=0)
2cc86e8db2ec commit after merge.
cavaliet
parents: 27
diff changeset
   327
31
142d0440c9aa Remove wp link button activated. Some js function gathered.
cavaliet
parents: 30
diff changeset
   328
142d0440c9aa Remove wp link button activated. Some js function gathered.
cavaliet
parents: 30
diff changeset
   329
#@login_required
142d0440c9aa Remove wp link button activated. Some js function gathered.
cavaliet
parents: 30
diff changeset
   330
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
   331
    # 2 cases : 
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   332
    # - 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
   333
    # - 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
   334
    tag_id = request.POST["tag_id"]
142d0440c9aa Remove wp link button activated. Some js function gathered.
cavaliet
parents: 30
diff changeset
   335
    tag = Tag.objects.filter(id=tag_id)[0]
142d0440c9aa Remove wp link button activated. Some js function gathered.
cavaliet
parents: 30
diff changeset
   336
    tag.wikipedia_url = None
142d0440c9aa Remove wp link button activated. Some js function gathered.
cavaliet
parents: 30
diff changeset
   337
    tag.wikipedia_pageid = None
142d0440c9aa Remove wp link button activated. Some js function gathered.
cavaliet
parents: 30
diff changeset
   338
    tag.url_status = 0;
142d0440c9aa Remove wp link button activated. Some js function gathered.
cavaliet
parents: 30
diff changeset
   339
    tag.save()
142d0440c9aa Remove wp link button activated. Some js function gathered.
cavaliet
parents: 30
diff changeset
   340
    
32
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   341
    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
   342
        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
   343
    else :
ffd77b2b939f First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents: 31
diff changeset
   344
        return get_all_tags_table(request=request, num_page=request.POST["num_page"], nb_by_page=request.POST["nb_by_page"])
34
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   345
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   346
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   347
#@login_required
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   348
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
   349
    # 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
   350
    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
   351
        valid = True
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   352
    else :
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   353
        valid = False
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   354
    # We validate or unvalidate the requester datasheet
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   355
    
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   356
    if request.user.is_authenticated():
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   357
        user = request.user
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   358
    else:
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   359
        user = None
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   360
    
34
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   361
    ds = Datasheet.objects.get(id=ds_id)
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   362
    if valid :
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   363
        ds.validate(user)
34
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   364
    else :
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   365
        ds.unvalidate()
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   366
    ds.save()
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   367
    # 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
   368
    same_organisation_ds = Datasheet.objects.filter(organisation=ds.organisation).filter(validated=not valid)
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   369
    if valid :
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   370
        # We ask to display the unvalidated ds
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   371
        valid_req = 0
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   372
    else :
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   373
        # We ask to display the validated ds
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   374
        valid_req = 1
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   375
    if len(same_organisation_ds) > 0 :
34
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   376
        return redirect('list_for_orga', orga_id=ds.organisation.id, valid=valid_req)
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   377
    else :
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   378
        return redirect('home')
26368d096723 Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents: 32
diff changeset
   379
    
36
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   380
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   381
#@login_required
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   382
def update_tag_alias(request):
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   383
    # 2 cases : 
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   384
    # - ordered tag for one datasheet : POST["datasheet_id"] is not null
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   385
    # - 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
   386
    tag_id = request.POST["id"]
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   387
    tag_alias = request.POST["value"]
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   388
    tag = Tag.objects.get(id=tag_id)
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   389
    tag.alias = tag_alias
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   390
    tag.save()
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   391
    
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   392
    if u"datasheet_id" in request.POST :
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   393
        return get_tag_table(request=request, ds_id=request.POST["datasheet_id"], valid=0)
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   394
    else :
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   395
        return get_all_tags_table(request=request, num_page=request.POST["num_page"], nb_by_page=request.POST["nb_by_page"])
4e8129c9f858 Add alias update feature.
cavaliet
parents: 35
diff changeset
   396
    
44
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   397
46
3ad571e54608 get categories enhancement.
cavaliet
parents: 44
diff changeset
   398
#@login_required
3ad571e54608 get categories enhancement.
cavaliet
parents: 44
diff changeset
   399
def get_categories():
3ad571e54608 get categories enhancement.
cavaliet
parents: 44
diff changeset
   400
    # List of categories in an OrderedDict
3ad571e54608 get categories enhancement.
cavaliet
parents: 44
diff changeset
   401
    categories = OrderedDict({"":""})
3ad571e54608 get categories enhancement.
cavaliet
parents: 44
diff changeset
   402
    for c in TagCategory.objects.order_by('label') :
3ad571e54608 get categories enhancement.
cavaliet
parents: 44
diff changeset
   403
        categories.update({c.label:c.label})
3ad571e54608 get categories enhancement.
cavaliet
parents: 44
diff changeset
   404
    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
   405
    
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   406
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   407
#@login_required
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   408
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
   409
    
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   410
    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
   411
    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
   412
    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
   413
    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
   414
        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
   415
    else :
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   416
        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
   417
    tag.save()
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   418
    # This function is available only in all_tags_table context
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   419
    return get_all_tags_table(request=request, num_page=request.POST["num_page"], nb_by_page=request.POST["nb_by_page"])
244d805b4921 Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents: 43
diff changeset
   420
    
47
08b008c5a07d - add popularity
ymh <ymh.work@gmail.com>
parents: 46
diff changeset
   421