| 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-- |
|
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 | 3 |
from django.conf import settings |
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 | 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 | 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 | 10 |
from django.template import RequestContext |
| 54 | 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 | 14 |
from hdabo.utils import OrderedDict, remove_accents, normalize |
| 85 | 15 |
from hdabo.wp_utils import (normalize_tag, query_wikipedia_title, |
| 72 | 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 | 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 | 20 |
import re |
|
42
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
21 |
|
| 27 | 22 |
|
| 53 | 23 |
@login_required |
| 27 | 24 |
def home(request): |
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 | 27 |
|
| 27 | 28 |
org_list = [] |
| 85 | 29 |
all_ds_mapping = dict([(res['organisation'], res['nb_all']) for res in Datasheet.objects.values("organisation").annotate(nb_all=Count("organisation"))]) |
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"))]) |
|
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 | 32 |
# We get the hda_id from the id |
| 85 | 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")]) |
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 | 35 |
|
| 27 | 36 |
for orga in orgas : |
| 85 | 37 |
nb_all = all_ds_mapping.get(orga.id, 0) |
38 |
duo_val = validated_ds_mapping.get(orga.id, [0, None]) |
|
| 64 | 39 |
nb_val = duo_val[0] |
40 |
first_id_val = val_hda_ids.get(duo_val[1], None) |
|
| 85 | 41 |
duo_unval = unvalidated_ds_mapping.get(orga.id, [0, None]) |
| 64 | 42 |
nb_unval = duo_unval[0] |
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 | 45 |
|
| 28 | 46 |
return render_to_response("organisation_list.html", |
| 27 | 47 |
{'organisations':org_list}, |
48 |
context_instance=RequestContext(request)) |
|
49 |
||
50 |
||
| 53 | 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 | 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 | 58 |
if "index" in request.GET : |
59 |
try: |
|
60 |
index = int(request.GET["index"]) |
|
61 |
except : |
|
62 |
index = 0 |
|
63 |
else : |
|
64 |
index = 0 |
|
65 |
if "nb_sheets" in request.GET : |
|
66 |
try: |
|
67 |
nb_sheets = int(request.GET["nb_sheets"]) |
|
68 |
except : |
|
69 |
nb_sheets = None |
|
70 |
else : |
|
71 |
nb_sheets = None |
|
72 |
# If there is tag parameter, it means that we display all the ds tagged by one particular tag |
|
| 65 | 73 |
tag = None |
| 64 | 74 |
if "tag" in request.GET : |
| 65 | 75 |
tag = Tag.objects.get(id=int(request.GET["tag"])) |
| 85 | 76 |
datasheets_qs = Datasheet.objects.filter(tags__in=[tag]).order_by("organisation__name", "original_creation_date").select_related("format") |
| 64 | 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 | 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 | 84 |
# We calculate the number of sheets if necessary |
85 |
if not nb_sheets : |
|
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 | 88 |
# We get only the prev/current/next sheets |
89 |
if nb_sheets > 1 : |
|
90 |
if index == 0 : |
|
91 |
select_qs = datasheets_qs[:2] |
|
92 |
prev_index = 0 |
|
93 |
prev_id = select_qs[0].hda_id |
|
94 |
next_index = 1 |
|
95 |
next_id = select_qs[1].hda_id |
|
96 |
elif index == (nb_sheets - 1) : |
|
| 85 | 97 |
select_qs = datasheets_qs[nb_sheets - 2:] |
| 64 | 98 |
prev_index = nb_sheets - 2 |
99 |
prev_id = select_qs[0].hda_id |
|
100 |
next_index = nb_sheets - 1 |
|
101 |
next_id = select_qs[1].hda_id |
|
102 |
else : |
|
| 85 | 103 |
select_qs = datasheets_qs[index - 1:index + 2] |
| 64 | 104 |
prev_index = index - 1 |
105 |
prev_id = select_qs[0].hda_id |
|
106 |
next_index = index + 1 |
|
107 |
next_id = select_qs[2].hda_id |
|
108 |
else : |
|
109 |
prev_index = next_index = 0 |
|
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 | 115 |
displayed_index = index + 1; |
116 |
zero_id = datasheets_qs[0].hda_id |
|
117 |
last_index = max(nb_sheets - 1, 0) |
|
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 | 123 |
'zero_id':zero_id, 'prev_index':prev_index, 'prev_id':prev_id, |
| 85 | 124 |
'next_index':next_index, 'next_id':next_id, |
| 64 | 125 |
'last_index':last_index, 'last_id':last_id, |
| 65 | 126 |
'displayed_index':displayed_index, 'tag':tag, 'valid':ds.validated, |
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 | 132 |
def list_for_orga(request, orga_id=None, valid=None, start_index=None): |
133 |
||
134 |
orga = Organisation.objects.get(id=orga_id) |
|
135 |
orga_name = orga.name |
|
136 |
||
137 |
if start_index : |
|
138 |
try: |
|
139 |
start_index = int(start_index) |
|
140 |
except : |
|
141 |
start_index = 0 |
|
142 |
else : |
|
143 |
start_index = 0 |
|
144 |
||
145 |
# If valid = 0, we search unvalidated sheets |
|
146 |
# If valid = 1, we search validated sheets |
|
147 |
# If valid = 2, we search AND DISPLAY all sheets |
|
|
55
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
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>
diff
changeset
|
149 |
|
| 27 | 150 |
if valid == "1" : |
151 |
# We count all the validated sheets |
|
|
55
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
152 |
datasheets_qs = datasheets_qs.filter(validated=True) |
| 27 | 153 |
# And select the current one |
|
55
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
154 |
datasheets = [datasheets_qs[start_index]] |
| 27 | 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 | 157 |
# We count all the validated sheets |
|
55
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
158 |
datasheets_qs = datasheets_qs.filter(validated=False) |
| 27 | 159 |
# And select the current one |
|
55
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
160 |
datasheets = [datasheets_qs[start_index]] |
|
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
161 |
else: |
|
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
162 |
datasheets = datasheets_qs |
|
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
163 |
|
|
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
164 |
nb_sheets = datasheets_qs.count() |
| 27 | 165 |
|
166 |
# We get the ORDERED tags if we display one sheet (case valid = 0 and 1) |
|
167 |
ordered_tags = None |
|
168 |
if valid != "2" : |
|
|
55
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
169 |
ordered_tags = TaggedSheet.objects.filter(datasheet=datasheets[0]).select_related("tag").order_by('order') |
| 27 | 170 |
|
171 |
displayed_index = start_index + 1; |
|
172 |
prev_index = max(start_index - 1, 0); |
|
173 |
next_index = min(nb_sheets - 1, start_index + 1); |
|
174 |
last_index = max(nb_sheets - 1, 0); |
|
175 |
||
| 28 | 176 |
return render_to_response("list_for_orga.html", |
| 27 | 177 |
{'datasheets':datasheets, 'orga_name':orga_name, |
178 |
'nb_sheets':nb_sheets, 'orga_id':orga_id, 'ordered_tags':ordered_tags, |
|
179 |
'prev_index':prev_index, 'next_index':next_index, 'last_index':last_index, |
|
| 46 | 180 |
'start_index':start_index, 'displayed_index':displayed_index, 'valid':valid, |
181 |
'categories':json.dumps(get_categories())}, |
|
| 27 | 182 |
context_instance=RequestContext(request)) |
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 | 185 |
@login_required |
| 86 | 186 |
def all_tags(request, num_page=None, nb_by_page=None, sort="+pop", searched=None): |
| 51 | 187 |
|
188 |
# If the view is asked after a form sent with post vars, it means that searched is a post var. |
|
189 |
if u"searched" in request.POST : |
|
190 |
searched = request.POST["searched"] |
|
|
55
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
191 |
nb_by_page = settings.PAGINATION_DEFAULT_NB_BY_PAGE |
| 51 | 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 | 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 | 201 |
|
202 |
cursor = connection.cursor() #@UndefinedVariable |
|
203 |
fl_list = [] |
|
204 |
try: |
|
| 72 | 205 |
cursor.execute("select upper(substring(normalized_label from 1 for 1)) as fl from hdabo_tag group by fl order by fl;") |
| 51 | 206 |
|
| 56 | 207 |
for row in cursor: |
208 |
new_char = remove_accents(row[0]) |
|
209 |
if new_char not in fl_list: |
|
210 |
fl_list.append(new_char) |
|
211 |
finally: |
|
212 |
cursor.close() |
|
213 |
||
| 85 | 214 |
search_def = tuple([(c, urlquote(c + settings.SEARCH_STAR_CHARACTER)) for c in fl_list]) |
| 86 | 215 |
reverse_sort = ("-" if sort[0] == "+" else "+") + sort[1:] |
|
55
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
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 | 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 | 220 |
'num_page':num_page, 'nb_by_page':nb_by_page, 'searched':searched, |
221 |
'categories':json.dumps(get_categories()), |
|
| 86 | 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 | 226 |
@login_required |
| 27 | 227 |
def tag_up_down(request): |
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 | 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 | 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 | 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 | 253 |
ds = Datasheet.objects.get(hda_id=ds_id) |
| 47 | 254 |
ds.manual_order = True |
255 |
ds.save() |
|
| 27 | 256 |
|
257 |
return get_tag_table(request=request, ds_id=ds_id, valid=0) |
|
258 |
||
259 |
||
| 53 | 260 |
@login_required |
| 27 | 261 |
def get_tag_table(request=None, ds_id=None, valid=None): |
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 | 264 |
|
265 |
return render_to_response("partial/tag_table.html", |
|
266 |
{'ordered_tags':ordered_tags, 'valid':valid}, |
|
267 |
context_instance=RequestContext(request)) |
|
268 |
||
269 |
||
| 53 | 270 |
@login_required |
| 86 | 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 | 273 |
current_page, p, num_page, nb_by_page = get_current_page(num_page, nb_by_page, sort, searched) #@UnusedVariable |
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 | 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 | 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 | 283 |
base_queryset = Tag.objects |
|
55
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
284 |
|
| 51 | 285 |
if searched and searched != "" : |
| 72 | 286 |
searched = normalize(searched.strip()) |
| 85 | 287 |
regex = "^%s$" % (re.escape(searched).replace(re.escape(settings.SEARCH_STAR_CHARACTER), ".*")) |
| 72 | 288 |
base_queryset = base_queryset.filter(normalized_label__iregex=regex) |
|
55
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
289 |
|
|
73
83695a58e4d6
Merge with 579ac2e2789ac2ee6d2f14108a7a90c154412546
ymh <ymh.work@gmail.com>
diff
changeset
|
290 |
alltags = base_queryset.annotate(num_ds=Count('datasheet')) |
| 86 | 291 |
|
292 |
alltags = { |
|
293 |
"+lab" : alltags.order_by('normalized_label', 'label'), |
|
294 |
"-lab" : alltags.order_by('-normalized_label', '-label'), |
|
295 |
"+pop" : alltags.order_by('-popularity', '-num_ds', 'normalized_label', 'label'), |
|
| 89 | 296 |
"-pop" : alltags.order_by('popularity', 'num_ds', '-normalized_label', '-label'), |
| 86 | 297 |
}.get(sort, alltags.order_by('-popularity', '-num_ds', 'normalized_label', 'label')) |
298 |
||
| 51 | 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>
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>
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>
diff
changeset
|
316 |
|
|
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
317 |
#use the much simpler base query to have the count |
|
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
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 | 324 |
@login_required |
| 27 | 325 |
def remove_tag_from_list(request=None): |
326 |
||
327 |
ds_id = request.POST["datasheet_id"] |
|
328 |
tag_id = request.POST["tag_id"] |
|
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 | 331 |
# We get the current TaggedSheet and we delete it |
332 |
ts = ds_tags.filter(tag=Tag.objects.filter(id=tag_id))[0] |
|
333 |
ts.delete() |
|
334 |
||
| 64 | 335 |
ds = Datasheet.objects.get(hda_id=ds_id) |
| 47 | 336 |
ds.manual_order = True |
337 |
ds.save() |
|
338 |
||
| 27 | 339 |
return get_tag_table(request=request, ds_id=ds_id, valid=0) |
340 |
||
341 |
||
| 53 | 342 |
@login_required |
| 27 | 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 | 345 |
tag_id = request.POST["id"] |
346 |
tag_label = request.POST["value"] |
|
347 |
||
348 |
tag = Tag.objects.get(id=tag_id) |
|
349 |
||
350 |
if tag.label != tag_label: |
|
351 |
||
352 |
tag.label = tag_label |
|
353 |
||
354 |
site = wiki.Wiki(settings.WIKIPEDIA_API_URL) #@UndefinedVariable |
|
| 66 | 355 |
wp_res = query_wikipedia_title(site, label=tag_label) |
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 | 357 |
|
358 |
if status is not None: |
|
359 |
tag.url_status = status |
|
| 47 | 360 |
|
| 66 | 361 |
old_pageid = tag.wikipedia_pageid |
362 |
||
| 47 | 363 |
tag.wikipedia_url = url |
364 |
tag.wikipedia_pageid = pageid |
|
365 |
tag.dbpedia_uri = dbpedia_uri |
|
| 27 | 366 |
|
| 85 | 367 |
try: |
368 |
tag.save() |
|
369 |
except: |
|
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 | 371 |
|
372 |
if old_pageid != pageid: |
|
373 |
TaggedSheet.objects.filter(tag=tag).update(wikipedia_revision_id=revision_id) |
|
| 27 | 374 |
|
| 86 | 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 | 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 | 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 | 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 | 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 | 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 | 406 |
ds.manual_order = True |
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 | 411 |
|
412 |
||
| 53 | 413 |
@login_required |
| 27 | 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 | 419 |
|
420 |
tag = Tag.objects.get(id=tag_id) |
|
421 |
site = wiki.Wiki(settings.WIKIPEDIA_API_URL) #@UndefinedVariable |
|
422 |
||
423 |
tag.label = normalize_tag(tag.original_label) |
|
424 |
||
| 33 | 425 |
#reset tag info |
426 |
tag.wikipedia_url = None |
|
427 |
tag.wikipedia_pageid = None |
|
| 36 | 428 |
|
| 85 | 429 |
try: |
430 |
process_tag(site, tag) |
|
431 |
except: |
|
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 | 433 |
|
434 |
||
| 27 | 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 | 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 | 440 |
|
441 |
||
| 53 | 442 |
@login_required |
| 28 | 443 |
def add_tag(request=None): |
444 |
||
445 |
ds_id = request.POST["datasheet_id"] |
|
446 |
tag_label = request.POST["value"] |
|
| 43 | 447 |
|
| 64 | 448 |
ds = Datasheet.objects.get(hda_id=ds_id) |
| 43 | 449 |
if tag_label.lower() in [t.label.lower() for t in ds.tags.all()]: |
450 |
return HttpResponseBadRequest(json.dumps({'error': 'duplicate_tag', 'message': u"Le tag %s existe déjà pour cette fiche." % (tag_label)}), mimetype="application/json") |
|
451 |
||
| 29 | 452 |
|
| 66 | 453 |
tag, revision_id, created = get_or_create_tag(tag_label) |
| 28 | 454 |
# We put the tag at the bottom of the datasheet's tag list |
455 |
# if the tag is created or if the tag is not in the list |
|
| 43 | 456 |
|
| 28 | 457 |
list_ts = TaggedSheet.objects.filter(datasheet=ds) |
| 66 | 458 |
if created or list_ts.filter(tag=tag).count() == 0 : |
| 28 | 459 |
new_order = list_ts.aggregate(Max('order'))['order__max'] + 1 |
| 66 | 460 |
ts = TaggedSheet.objects.create(datasheet=ds, tag=tag, original_order=new_order, order=new_order, wikipedia_revision_id=revision_id) |
| 28 | 461 |
ts.save() |
| 47 | 462 |
ds.manual_order = True |
463 |
ds.save() |
|
| 28 | 464 |
|
465 |
return get_tag_table(request=request, ds_id=ds_id, valid=0) |
|
466 |
||
|
31
142d0440c9aa
Remove wp link button activated. Some js function gathered.
cavaliet
parents:
30
diff
changeset
|
467 |
|
| 53 | 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 | 475 |
|
|
31
142d0440c9aa
Remove wp link button activated. Some js function gathered.
cavaliet
parents:
30
diff
changeset
|
476 |
|
| 69 | 477 |
if u"datasheet_id" in request.POST : |
478 |
#create another tag almost identical, without the W info |
|
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 |
|
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}) |
|
481 |
||
482 |
TaggedSheet.objects.filter(tag=tag, datasheet__hda_id=request.POST["datasheet_id"]).update(tag=new_tag, wikipedia_revision_id=None) |
|
483 |
||
484 |
else: |
|
| 85 | 485 |
|
486 |
if Tag.objects.filter(label=tag.label, original_label=tag.original_label, url_status=Tag.TAG_URL_STATUS_DICT['null_result']).count() > 0: |
|
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 | 488 |
tag.wikipedia_url = None |
489 |
tag.wikipedia_pageid = None |
|
490 |
tag.dbpedia_uri = None |
|
491 |
tag.url_status = Tag.TAG_URL_STATUS_DICT['null_result'] |
|
492 |
tag.save() |
|
493 |
||
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 | 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 | 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 | 510 |
|
511 |
if request.user.is_authenticated(): |
|
512 |
user = request.user |
|
513 |
else: |
|
514 |
user = None |
|
515 |
||
| 64 | 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 | 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 | 525 |
if len(same_organisation_ds) > 0 : |
| 64 | 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 | 530 |
|
| 53 | 531 |
@login_required |
| 36 | 532 |
def update_tag_alias(request): |
533 |
# 2 cases : |
|
534 |
# - ordered tag for one datasheet : POST["datasheet_id"] is not null |
|
535 |
# - all tags list : POST["datasheet_id"] is null and POST["num_page"] and POST["nb_by_page"] are not null |
|
536 |
tag_id = request.POST["id"] |
|
537 |
tag_alias = request.POST["value"] |
|
538 |
tag = Tag.objects.get(id=tag_id) |
|
539 |
tag.alias = tag_alias |
|
540 |
tag.save() |
|
541 |
||
542 |
if u"datasheet_id" in request.POST : |
|
543 |
return get_tag_table(request=request, ds_id=request.POST["datasheet_id"], valid=0) |
|
544 |
else : |
|
| 86 | 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 | 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 | 548 |
def get_categories(): |
549 |
# List of categories in an OrderedDict |
|
550 |
categories = OrderedDict({"":""}) |
|
551 |
for c in TagCategory.objects.order_by('label') : |
|
552 |
categories.update({c.label:c.label}) |
|
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 | 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 | 567 |
|
568 |
if u"datasheet_id" in request.POST : |
|
569 |
return get_tag_table(request=request, ds_id=request.POST["datasheet_id"], valid=0) |
|
570 |
else : |
|
| 71 | 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 | 573 |
@login_required |
574 |
def reorder_tag_datasheet(request): |
|
575 |
||
576 |
ds_id = request.REQUEST['datasheet_id'] |
|
577 |
ds = Datasheet.objects.get(hda_id=ds_id) |
|
578 |
reorder_datasheet_tags(ds) |
|
579 |
||
580 |
return get_tag_table(request=request, ds_id=ds_id, valid=0) |