| author | cavaliet |
| Tue, 30 Sep 2014 12:31:48 +0200 | |
| changeset 344 | 1473ba25af1f |
| parent 327 | 3684db1579ff |
| child 405 | 19d1264a8974 |
| 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 |
| 275 | 9 |
from django.shortcuts import render_to_response, redirect, get_object_or_404 |
| 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 |
| 275 | 14 |
from hdabo.models import Datasheet, Organisation, Tag, TagCategory, TaggedSheet, Folder |
| 72 | 15 |
from hdabo.utils import OrderedDict, remove_accents, normalize |
| 275 | 16 |
from hdabo.wp_utils import (normalize_tag, query_wikipedia_title, get_or_create_tag, process_tag, reorder_datasheet_tags) |
| 27 | 17 |
from wikitools import wiki |
| 292 | 18 |
import json |
| 54 | 19 |
import re |
| 275 | 20 |
from django.views.generic.base import TemplateView, View |
| 276 | 21 |
from haystack.views import SearchView |
|
323
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
22 |
from django.http.response import HttpResponse |
|
42
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
23 |
|
| 27 | 24 |
|
| 53 | 25 |
@login_required |
| 27 | 26 |
def home(request): |
| 275 | 27 |
return render_to_response("home.html", context_instance=RequestContext(request)) |
28 |
||
29 |
||
30 |
@login_required |
|
31 |
def orga_list(request): |
|
| 27 | 32 |
|
|
32
ffd77b2b939f
First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents:
31
diff
changeset
|
33 |
orgas = Organisation.objects.all().order_by('name') |
| 56 | 34 |
|
| 27 | 35 |
org_list = [] |
| 85 | 36 |
all_ds_mapping = dict([(res['organisation'], res['nb_all']) for res in Datasheet.objects.values("organisation").annotate(nb_all=Count("organisation"))]) |
37 |
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"))]) |
|
38 |
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 | 39 |
# We get the hda_id from the id |
| 85 | 40 |
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")]) |
41 |
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 | 42 |
|
| 27 | 43 |
for orga in orgas : |
| 85 | 44 |
nb_all = all_ds_mapping.get(orga.id, 0) |
45 |
duo_val = validated_ds_mapping.get(orga.id, [0, None]) |
|
| 64 | 46 |
nb_val = duo_val[0] |
47 |
first_id_val = val_hda_ids.get(duo_val[1], None) |
|
| 85 | 48 |
duo_unval = unvalidated_ds_mapping.get(orga.id, [0, None]) |
| 64 | 49 |
nb_unval = duo_unval[0] |
50 |
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
|
51 |
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 | 52 |
|
| 28 | 53 |
return render_to_response("organisation_list.html", |
| 27 | 54 |
{'organisations':org_list}, |
55 |
context_instance=RequestContext(request)) |
|
56 |
||
57 |
||
| 53 | 58 |
@login_required |
|
63
03698c739b1d
#8 : Better url management with datasheet id and context in parameter.
cavaliet
parents:
61
diff
changeset
|
59 |
def display_datasheet(request, ds_id=None): |
|
03698c739b1d
#8 : Better url management with datasheet id and context in parameter.
cavaliet
parents:
61
diff
changeset
|
60 |
|
|
03698c739b1d
#8 : Better url management with datasheet id and context in parameter.
cavaliet
parents:
61
diff
changeset
|
61 |
if ds_id : |
| 64 | 62 |
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
|
63 |
|
|
03698c739b1d
#8 : Better url management with datasheet id and context in parameter.
cavaliet
parents:
61
diff
changeset
|
64 |
# In this function the context is given by the get parameters. |
| 64 | 65 |
if "index" in request.GET : |
66 |
try: |
|
67 |
index = int(request.GET["index"]) |
|
68 |
except : |
|
69 |
index = 0 |
|
70 |
else : |
|
71 |
index = 0 |
|
72 |
if "nb_sheets" in request.GET : |
|
73 |
try: |
|
74 |
nb_sheets = int(request.GET["nb_sheets"]) |
|
75 |
except : |
|
76 |
nb_sheets = None |
|
77 |
else : |
|
78 |
nb_sheets = None |
|
79 |
# If there is tag parameter, it means that we display all the ds tagged by one particular tag |
|
| 65 | 80 |
tag = None |
| 64 | 81 |
if "tag" in request.GET : |
| 65 | 82 |
tag = Tag.objects.get(id=int(request.GET["tag"])) |
| 85 | 83 |
datasheets_qs = Datasheet.objects.filter(tags__in=[tag]).order_by("organisation__name", "original_creation_date").select_related("format") |
| 64 | 84 |
# 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
|
85 |
if not ds_id : |
| 64 | 86 |
index = 0 |
|
63
03698c739b1d
#8 : Better url management with datasheet id and context in parameter.
cavaliet
parents:
61
diff
changeset
|
87 |
ds = datasheets_qs[0] |
|
03698c739b1d
#8 : Better url management with datasheet id and context in parameter.
cavaliet
parents:
61
diff
changeset
|
88 |
else : |
|
03698c739b1d
#8 : Better url management with datasheet id and context in parameter.
cavaliet
parents:
61
diff
changeset
|
89 |
# 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
|
90 |
datasheets_qs = Datasheet.objects.filter(organisation=ds.organisation).filter(validated=ds.validated).order_by("id").select_related("format") |
| 64 | 91 |
# We calculate the number of sheets if necessary |
92 |
if not nb_sheets : |
|
93 |
nb_sheets = datasheets_qs.count() |
|
|
63
03698c739b1d
#8 : Better url management with datasheet id and context in parameter.
cavaliet
parents:
61
diff
changeset
|
94 |
|
| 64 | 95 |
# We get only the prev/current/next sheets |
96 |
if nb_sheets > 1 : |
|
97 |
if index == 0 : |
|
98 |
select_qs = datasheets_qs[:2] |
|
99 |
prev_index = 0 |
|
100 |
prev_id = select_qs[0].hda_id |
|
101 |
next_index = 1 |
|
102 |
next_id = select_qs[1].hda_id |
|
103 |
elif index == (nb_sheets - 1) : |
|
| 85 | 104 |
select_qs = datasheets_qs[nb_sheets - 2:] |
| 64 | 105 |
prev_index = nb_sheets - 2 |
106 |
prev_id = select_qs[0].hda_id |
|
107 |
next_index = nb_sheets - 1 |
|
108 |
next_id = select_qs[1].hda_id |
|
109 |
else : |
|
| 85 | 110 |
select_qs = datasheets_qs[index - 1:index + 2] |
| 64 | 111 |
prev_index = index - 1 |
112 |
prev_id = select_qs[0].hda_id |
|
113 |
next_index = index + 1 |
|
114 |
next_id = select_qs[2].hda_id |
|
115 |
else : |
|
116 |
prev_index = next_index = 0 |
|
117 |
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
|
118 |
|
|
03698c739b1d
#8 : Better url management with datasheet id and context in parameter.
cavaliet
parents:
61
diff
changeset
|
119 |
# 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
|
120 |
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
|
121 |
|
| 64 | 122 |
displayed_index = index + 1; |
123 |
zero_id = datasheets_qs[0].hda_id |
|
124 |
last_index = max(nb_sheets - 1, 0) |
|
125 |
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
|
126 |
|
|
03698c739b1d
#8 : Better url management with datasheet id and context in parameter.
cavaliet
parents:
61
diff
changeset
|
127 |
return render_to_response("generic_sheet.html", |
|
03698c739b1d
#8 : Better url management with datasheet id and context in parameter.
cavaliet
parents:
61
diff
changeset
|
128 |
{'ds':ds, 'orga_name':ds.organisation.name, |
|
03698c739b1d
#8 : Better url management with datasheet id and context in parameter.
cavaliet
parents:
61
diff
changeset
|
129 |
'nb_sheets':nb_sheets, 'ordered_tags':ordered_tags, |
| 64 | 130 |
'zero_id':zero_id, 'prev_index':prev_index, 'prev_id':prev_id, |
| 85 | 131 |
'next_index':next_index, 'next_id':next_id, |
| 64 | 132 |
'last_index':last_index, 'last_id':last_id, |
| 65 | 133 |
'displayed_index':displayed_index, 'tag':tag, 'valid':ds.validated, |
134 |
'categories':json.dumps(get_categories())}, |
|
|
63
03698c739b1d
#8 : Better url management with datasheet id and context in parameter.
cavaliet
parents:
61
diff
changeset
|
135 |
context_instance=RequestContext(request)) |
|
03698c739b1d
#8 : Better url management with datasheet id and context in parameter.
cavaliet
parents:
61
diff
changeset
|
136 |
|
|
03698c739b1d
#8 : Better url management with datasheet id and context in parameter.
cavaliet
parents:
61
diff
changeset
|
137 |
|
|
03698c739b1d
#8 : Better url management with datasheet id and context in parameter.
cavaliet
parents:
61
diff
changeset
|
138 |
@login_required |
| 27 | 139 |
def list_for_orga(request, orga_id=None, valid=None, start_index=None): |
140 |
||
141 |
orga = Organisation.objects.get(id=orga_id) |
|
142 |
orga_name = orga.name |
|
143 |
||
144 |
if start_index : |
|
145 |
try: |
|
146 |
start_index = int(start_index) |
|
147 |
except : |
|
148 |
start_index = 0 |
|
149 |
else : |
|
150 |
start_index = 0 |
|
151 |
||
152 |
# If valid = 0, we search unvalidated sheets |
|
153 |
# If valid = 1, we search validated sheets |
|
154 |
# If valid = 2, we search AND DISPLAY all sheets |
|
|
55
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
155 |
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
|
156 |
|
| 27 | 157 |
if valid == "1" : |
158 |
# We count all the validated sheets |
|
|
55
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
159 |
datasheets_qs = datasheets_qs.filter(validated=True) |
| 27 | 160 |
# And select the current one |
|
55
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
161 |
datasheets = [datasheets_qs[start_index]] |
| 27 | 162 |
elif valid != "2": |
|
34
26368d096723
Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents:
32
diff
changeset
|
163 |
valid = "0" |
| 27 | 164 |
# We count all the validated sheets |
|
55
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
165 |
datasheets_qs = datasheets_qs.filter(validated=False) |
| 27 | 166 |
# And select the current one |
|
55
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
167 |
datasheets = [datasheets_qs[start_index]] |
|
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
168 |
else: |
|
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
169 |
datasheets = datasheets_qs |
|
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
170 |
|
|
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
171 |
nb_sheets = datasheets_qs.count() |
| 27 | 172 |
|
173 |
# We get the ORDERED tags if we display one sheet (case valid = 0 and 1) |
|
174 |
ordered_tags = None |
|
175 |
if valid != "2" : |
|
|
55
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
176 |
ordered_tags = TaggedSheet.objects.filter(datasheet=datasheets[0]).select_related("tag").order_by('order') |
| 27 | 177 |
|
178 |
displayed_index = start_index + 1; |
|
179 |
prev_index = max(start_index - 1, 0); |
|
180 |
next_index = min(nb_sheets - 1, start_index + 1); |
|
181 |
last_index = max(nb_sheets - 1, 0); |
|
182 |
||
| 28 | 183 |
return render_to_response("list_for_orga.html", |
| 27 | 184 |
{'datasheets':datasheets, 'orga_name':orga_name, |
185 |
'nb_sheets':nb_sheets, 'orga_id':orga_id, 'ordered_tags':ordered_tags, |
|
186 |
'prev_index':prev_index, 'next_index':next_index, 'last_index':last_index, |
|
| 46 | 187 |
'start_index':start_index, 'displayed_index':displayed_index, 'valid':valid, |
188 |
'categories':json.dumps(get_categories())}, |
|
| 27 | 189 |
context_instance=RequestContext(request)) |
190 |
||
|
32
ffd77b2b939f
First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents:
31
diff
changeset
|
191 |
|
| 53 | 192 |
@login_required |
| 86 | 193 |
def all_tags(request, num_page=None, nb_by_page=None, sort="+pop", searched=None): |
| 51 | 194 |
|
195 |
# If the view is asked after a form sent with post vars, it means that searched is a post var. |
|
196 |
if u"searched" in request.POST : |
|
197 |
searched = request.POST["searched"] |
|
|
55
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
198 |
nb_by_page = settings.PAGINATION_DEFAULT_NB_BY_PAGE |
| 51 | 199 |
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
|
200 |
|
|
52
1f01957a3eae
Add popularity and number of datasheet for one tag. "Unduplication" of some code in views.py.
cavaliet
parents:
51
diff
changeset
|
201 |
# Get paginator and current page |
| 86 | 202 |
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
|
203 |
|
|
ffd77b2b939f
First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents:
31
diff
changeset
|
204 |
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
|
205 |
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
|
206 |
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
|
207 |
|
| 56 | 208 |
|
209 |
cursor = connection.cursor() #@UndefinedVariable |
|
210 |
fl_list = [] |
|
211 |
try: |
|
| 72 | 212 |
cursor.execute("select upper(substring(normalized_label from 1 for 1)) as fl from hdabo_tag group by fl order by fl;") |
| 51 | 213 |
|
| 56 | 214 |
for row in cursor: |
215 |
new_char = remove_accents(row[0]) |
|
216 |
if new_char not in fl_list: |
|
217 |
fl_list.append(new_char) |
|
218 |
finally: |
|
219 |
cursor.close() |
|
220 |
||
| 85 | 221 |
search_def = tuple([(c, urlquote(c + settings.SEARCH_STAR_CHARACTER)) for c in fl_list]) |
| 86 | 222 |
reverse_sort = ("-" if sort[0] == "+" else "+") + sort[1:] |
|
55
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
223 |
|
|
32
ffd77b2b939f
First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents:
31
diff
changeset
|
224 |
return render_to_response("all_tags.html", |
| 54 | 225 |
{'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
|
226 |
'prev_page':prev_page, 'next_page':next_page, 'last_page':last_page, |
| 51 | 227 |
'num_page':num_page, 'nb_by_page':nb_by_page, 'searched':searched, |
228 |
'categories':json.dumps(get_categories()), |
|
| 86 | 229 |
'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
|
230 |
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
|
231 |
|
|
ffd77b2b939f
First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents:
31
diff
changeset
|
232 |
|
| 53 | 233 |
@login_required |
| 27 | 234 |
def tag_up_down(request): |
235 |
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
|
236 |
# 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
|
237 |
# 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
|
238 |
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
|
239 |
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
|
240 |
# 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
|
241 |
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
|
242 |
# 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
|
243 |
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
|
244 |
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
|
245 |
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
|
246 |
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
|
247 |
# 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
|
248 |
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
|
249 |
# And we decrease the other ones |
| 47 | 250 |
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
|
251 |
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
|
252 |
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
|
253 |
ts.save() |
| 27 | 254 |
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
|
255 |
# And we increase the other ones |
| 47 | 256 |
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
|
257 |
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
|
258 |
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
|
259 |
ts.save() |
| 64 | 260 |
ds = Datasheet.objects.get(hda_id=ds_id) |
| 47 | 261 |
ds.manual_order = True |
262 |
ds.save() |
|
| 27 | 263 |
|
264 |
return get_tag_table(request=request, ds_id=ds_id, valid=0) |
|
265 |
||
266 |
||
| 53 | 267 |
@login_required |
| 27 | 268 |
def get_tag_table(request=None, ds_id=None, valid=None): |
269 |
||
|
68
17a1c2a67c50
Correct various request on hdabo_id (avoid sub request)
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
270 |
ordered_tags = TaggedSheet.objects.filter(datasheet__hda_id=ds_id).order_by('order') |
| 27 | 271 |
|
272 |
return render_to_response("partial/tag_table.html", |
|
273 |
{'ordered_tags':ordered_tags, 'valid':valid}, |
|
274 |
context_instance=RequestContext(request)) |
|
275 |
||
276 |
||
| 53 | 277 |
@login_required |
| 86 | 278 |
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
|
279 |
|
| 86 | 280 |
current_page, p, num_page, nb_by_page = get_current_page(num_page, nb_by_page, sort, searched) #@UnusedVariable |
281 |
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
|
282 |
|
|
1f01957a3eae
Add popularity and number of datasheet for one tag. "Unduplication" of some code in views.py.
cavaliet
parents:
51
diff
changeset
|
283 |
return render_to_response("partial/all_tags_table.html", |
| 86 | 284 |
{'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
|
285 |
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
|
286 |
|
|
1f01957a3eae
Add popularity and number of datasheet for one tag. "Unduplication" of some code in views.py.
cavaliet
parents:
51
diff
changeset
|
287 |
|
| 86 | 288 |
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
|
289 |
|
| 66 | 290 |
base_queryset = Tag.objects |
|
55
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
291 |
|
| 51 | 292 |
if searched and searched != "" : |
| 72 | 293 |
searched = normalize(searched.strip()) |
| 85 | 294 |
regex = "^%s$" % (re.escape(searched).replace(re.escape(settings.SEARCH_STAR_CHARACTER), ".*")) |
| 72 | 295 |
base_queryset = base_queryset.filter(normalized_label__iregex=regex) |
|
55
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
296 |
|
|
73
83695a58e4d6
Merge with 579ac2e2789ac2ee6d2f14108a7a90c154412546
ymh <ymh.work@gmail.com>
diff
changeset
|
297 |
alltags = base_queryset.annotate(num_ds=Count('datasheet')) |
| 86 | 298 |
|
299 |
alltags = { |
|
300 |
"+lab" : alltags.order_by('normalized_label', 'label'), |
|
301 |
"-lab" : alltags.order_by('-normalized_label', '-label'), |
|
302 |
"+pop" : alltags.order_by('-popularity', '-num_ds', 'normalized_label', 'label'), |
|
| 89 | 303 |
"-pop" : alltags.order_by('popularity', 'num_ds', '-normalized_label', '-label'), |
| 86 | 304 |
}.get(sort, alltags.order_by('-popularity', '-num_ds', 'normalized_label', 'label')) |
305 |
||
| 51 | 306 |
|
|
32
ffd77b2b939f
First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents:
31
diff
changeset
|
307 |
# 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
|
308 |
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
|
309 |
try: |
|
ffd77b2b939f
First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents:
31
diff
changeset
|
310 |
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
|
311 |
except : |
|
55
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
312 |
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
|
313 |
else : |
|
55
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
314 |
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
|
315 |
if num_page : |
|
ffd77b2b939f
First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents:
31
diff
changeset
|
316 |
try: |
|
ffd77b2b939f
First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents:
31
diff
changeset
|
317 |
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
|
318 |
except : |
|
ffd77b2b939f
First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents:
31
diff
changeset
|
319 |
num_page = 1 |
|
ffd77b2b939f
First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents:
31
diff
changeset
|
320 |
else : |
|
ffd77b2b939f
First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents:
31
diff
changeset
|
321 |
num_page = 1 |
|
ffd77b2b939f
First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents:
31
diff
changeset
|
322 |
p = Paginator(alltags, nb_by_page) |
|
55
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
323 |
|
|
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
324 |
#use the much simpler base query to have the count |
|
e1098febb9d3
Merge with 1f01957a3eaed11ca63e7f75da1b326f8ac8de15 + some optimisations
ymh <ymh.work@gmail.com>
diff
changeset
|
325 |
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
|
326 |
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
|
327 |
|
|
52
1f01957a3eae
Add popularity and number of datasheet for one tag. "Unduplication" of some code in views.py.
cavaliet
parents:
51
diff
changeset
|
328 |
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
|
329 |
|
|
ffd77b2b939f
First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents:
31
diff
changeset
|
330 |
|
| 53 | 331 |
@login_required |
| 27 | 332 |
def remove_tag_from_list(request=None): |
333 |
||
334 |
ds_id = request.POST["datasheet_id"] |
|
335 |
tag_id = request.POST["tag_id"] |
|
336 |
# 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
|
337 |
ds_tags = TaggedSheet.objects.filter(datasheet__hda_id=ds_id) |
| 27 | 338 |
# We get the current TaggedSheet and we delete it |
339 |
ts = ds_tags.filter(tag=Tag.objects.filter(id=tag_id))[0] |
|
340 |
ts.delete() |
|
341 |
||
| 64 | 342 |
ds = Datasheet.objects.get(hda_id=ds_id) |
| 47 | 343 |
ds.manual_order = True |
344 |
ds.save() |
|
345 |
||
| 27 | 346 |
return get_tag_table(request=request, ds_id=ds_id, valid=0) |
347 |
||
348 |
||
| 53 | 349 |
@login_required |
| 27 | 350 |
def modify_tag(request): |
|
42
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
351 |
|
| 27 | 352 |
tag_id = request.POST["id"] |
353 |
tag_label = request.POST["value"] |
|
354 |
||
355 |
tag = Tag.objects.get(id=tag_id) |
|
356 |
||
357 |
if tag.label != tag_label: |
|
358 |
||
359 |
tag.label = tag_label |
|
360 |
||
361 |
site = wiki.Wiki(settings.WIKIPEDIA_API_URL) #@UndefinedVariable |
|
| 66 | 362 |
wp_res = query_wikipedia_title(site, label=tag_label) |
363 |
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 | 364 |
|
365 |
if status is not None: |
|
366 |
tag.url_status = status |
|
| 47 | 367 |
|
| 66 | 368 |
old_pageid = tag.wikipedia_pageid |
369 |
||
| 47 | 370 |
tag.wikipedia_url = url |
371 |
tag.wikipedia_pageid = pageid |
|
372 |
tag.dbpedia_uri = dbpedia_uri |
|
| 27 | 373 |
|
| 85 | 374 |
try: |
375 |
tag.save() |
|
376 |
except: |
|
377 |
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 | 378 |
|
379 |
if old_pageid != pageid: |
|
380 |
TaggedSheet.objects.filter(tag=tag).update(wikipedia_revision_id=revision_id) |
|
| 27 | 381 |
|
| 86 | 382 |
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
|
383 |
|
|
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
384 |
|
| 53 | 385 |
@login_required |
|
42
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
386 |
def modify_tag_datasheet(request): |
|
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
387 |
|
|
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
388 |
tag_id = request.POST["id"] |
|
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
389 |
tag_label = request.POST["value"] |
| 47 | 390 |
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
|
391 |
|
|
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
392 |
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
|
393 |
|
| 64 | 394 |
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
|
395 |
|
|
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
396 |
if tag.label != tag_label: |
|
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
397 |
|
|
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
398 |
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
|
399 |
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
|
400 |
|
| 66 | 401 |
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
|
402 |
|
|
68
17a1c2a67c50
Correct various request on hdabo_id (avoid sub request)
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
403 |
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
|
404 |
ts.tag = tag |
| 66 | 405 |
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
|
406 |
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
|
407 |
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
|
408 |
if len(results) > 0: |
|
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
409 |
ts.index_note = results[0].score |
|
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
410 |
|
|
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
411 |
ts.save() |
|
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
412 |
|
| 47 | 413 |
ds.manual_order = True |
414 |
ds.save() |
|
|
42
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
415 |
|
|
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
36
diff
changeset
|
416 |
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
|
417 |
|
| 27 | 418 |
|
419 |
||
| 53 | 420 |
@login_required |
| 27 | 421 |
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
|
422 |
# 2 cases : |
|
ffd77b2b939f
First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents:
31
diff
changeset
|
423 |
# - 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
|
424 |
# - 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
|
425 |
tag_id = request.POST["tag_id"] |
| 27 | 426 |
|
427 |
tag = Tag.objects.get(id=tag_id) |
|
428 |
site = wiki.Wiki(settings.WIKIPEDIA_API_URL) #@UndefinedVariable |
|
429 |
||
430 |
tag.label = normalize_tag(tag.original_label) |
|
431 |
||
| 33 | 432 |
#reset tag info |
433 |
tag.wikipedia_url = None |
|
434 |
tag.wikipedia_pageid = None |
|
| 36 | 435 |
|
| 85 | 436 |
try: |
437 |
process_tag(site, tag) |
|
438 |
except: |
|
439 |
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 | 440 |
|
441 |
||
| 27 | 442 |
|
|
32
ffd77b2b939f
First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents:
31
diff
changeset
|
443 |
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
|
444 |
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
|
445 |
else : |
| 86 | 446 |
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 | 447 |
|
448 |
||
| 53 | 449 |
@login_required |
| 28 | 450 |
def add_tag(request=None): |
451 |
||
452 |
ds_id = request.POST["datasheet_id"] |
|
453 |
tag_label = request.POST["value"] |
|
| 43 | 454 |
|
| 64 | 455 |
ds = Datasheet.objects.get(hda_id=ds_id) |
| 43 | 456 |
if tag_label.lower() in [t.label.lower() for t in ds.tags.all()]: |
457 |
return HttpResponseBadRequest(json.dumps({'error': 'duplicate_tag', 'message': u"Le tag %s existe déjà pour cette fiche." % (tag_label)}), mimetype="application/json") |
|
458 |
||
| 29 | 459 |
|
| 66 | 460 |
tag, revision_id, created = get_or_create_tag(tag_label) |
| 28 | 461 |
# We put the tag at the bottom of the datasheet's tag list |
462 |
# if the tag is created or if the tag is not in the list |
|
| 43 | 463 |
|
| 28 | 464 |
list_ts = TaggedSheet.objects.filter(datasheet=ds) |
| 66 | 465 |
if created or list_ts.filter(tag=tag).count() == 0 : |
| 28 | 466 |
new_order = list_ts.aggregate(Max('order'))['order__max'] + 1 |
| 66 | 467 |
ts = TaggedSheet.objects.create(datasheet=ds, tag=tag, original_order=new_order, order=new_order, wikipedia_revision_id=revision_id) |
| 28 | 468 |
ts.save() |
| 47 | 469 |
ds.manual_order = True |
470 |
ds.save() |
|
| 28 | 471 |
|
472 |
return get_tag_table(request=request, ds_id=ds_id, valid=0) |
|
473 |
||
|
31
142d0440c9aa
Remove wp link button activated. Some js function gathered.
cavaliet
parents:
30
diff
changeset
|
474 |
|
| 53 | 475 |
@login_required |
|
31
142d0440c9aa
Remove wp link button activated. Some js function gathered.
cavaliet
parents:
30
diff
changeset
|
476 |
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
|
477 |
# 2 cases : |
|
ffd77b2b939f
First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents:
31
diff
changeset
|
478 |
# - 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
|
479 |
# - 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
|
480 |
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
|
481 |
tag = Tag.objects.get(id=tag_id) |
| 69 | 482 |
|
|
31
142d0440c9aa
Remove wp link button activated. Some js function gathered.
cavaliet
parents:
30
diff
changeset
|
483 |
|
| 69 | 484 |
if u"datasheet_id" in request.POST : |
485 |
#create another tag almost identical, without the W info |
|
486 |
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 |
|
487 |
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}) |
|
488 |
||
489 |
TaggedSheet.objects.filter(tag=tag, datasheet__hda_id=request.POST["datasheet_id"]).update(tag=new_tag, wikipedia_revision_id=None) |
|
490 |
||
491 |
else: |
|
| 85 | 492 |
|
493 |
if Tag.objects.filter(label=tag.label, original_label=tag.original_label, url_status=Tag.TAG_URL_STATUS_DICT['null_result']).count() > 0: |
|
494 |
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 | 495 |
tag.wikipedia_url = None |
496 |
tag.wikipedia_pageid = None |
|
497 |
tag.dbpedia_uri = None |
|
498 |
tag.url_status = Tag.TAG_URL_STATUS_DICT['null_result'] |
|
499 |
tag.save() |
|
500 |
||
501 |
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
|
502 |
|
|
32
ffd77b2b939f
First step of tag list. Enhance js function for datasheet context or tag list context.
cavaliet
parents:
31
diff
changeset
|
503 |
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
|
504 |
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
|
505 |
else : |
| 86 | 506 |
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
|
507 |
|
|
26368d096723
Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents:
32
diff
changeset
|
508 |
|
| 53 | 509 |
@login_required |
|
34
26368d096723
Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents:
32
diff
changeset
|
510 |
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
|
511 |
# 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
|
512 |
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
|
513 |
valid = True |
|
26368d096723
Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents:
32
diff
changeset
|
514 |
else : |
|
26368d096723
Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents:
32
diff
changeset
|
515 |
valid = False |
|
26368d096723
Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents:
32
diff
changeset
|
516 |
# We validate or unvalidate the requester datasheet |
| 47 | 517 |
|
518 |
if request.user.is_authenticated(): |
|
519 |
user = request.user |
|
520 |
else: |
|
521 |
user = None |
|
522 |
||
| 64 | 523 |
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
|
524 |
if valid : |
| 47 | 525 |
ds.validate(user) |
|
34
26368d096723
Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents:
32
diff
changeset
|
526 |
else : |
|
26368d096723
Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents:
32
diff
changeset
|
527 |
ds.unvalidate() |
|
26368d096723
Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents:
32
diff
changeset
|
528 |
ds.save() |
|
63
03698c739b1d
#8 : Better url management with datasheet id and context in parameter.
cavaliet
parents:
61
diff
changeset
|
529 |
|
|
34
26368d096723
Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents:
32
diff
changeset
|
530 |
# 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
|
531 |
same_organisation_ds = Datasheet.objects.filter(organisation=ds.organisation).filter(validated=not valid) |
| 47 | 532 |
if len(same_organisation_ds) > 0 : |
| 64 | 533 |
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
|
534 |
else : |
|
26368d096723
Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents:
32
diff
changeset
|
535 |
return redirect('home') |
|
26368d096723
Datasheet validation, better repartition of js function, hand cursor on buttons.
cavaliet
parents:
32
diff
changeset
|
536 |
|
| 36 | 537 |
|
| 53 | 538 |
@login_required |
| 36 | 539 |
def update_tag_alias(request): |
540 |
# 2 cases : |
|
541 |
# - ordered tag for one datasheet : POST["datasheet_id"] is not null |
|
542 |
# - all tags list : POST["datasheet_id"] is null and POST["num_page"] and POST["nb_by_page"] are not null |
|
543 |
tag_id = request.POST["id"] |
|
544 |
tag_alias = request.POST["value"] |
|
545 |
tag = Tag.objects.get(id=tag_id) |
|
546 |
tag.alias = tag_alias |
|
547 |
tag.save() |
|
548 |
||
549 |
if u"datasheet_id" in request.POST : |
|
550 |
return get_tag_table(request=request, ds_id=request.POST["datasheet_id"], valid=0) |
|
551 |
else : |
|
| 86 | 552 |
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 | 553 |
|
|
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 |
|
| 46 | 555 |
def get_categories(): |
556 |
# List of categories in an OrderedDict |
|
557 |
categories = OrderedDict({"":""}) |
|
558 |
for c in TagCategory.objects.order_by('label') : |
|
559 |
categories.update({c.label:c.label}) |
|
560 |
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
|
561 |
|
|
244d805b4921
Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents:
43
diff
changeset
|
562 |
|
| 53 | 563 |
@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
|
564 |
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
|
565 |
|
|
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_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
|
567 |
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
|
568 |
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
|
569 |
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
|
570 |
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
|
571 |
else : |
|
244d805b4921
Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents:
43
diff
changeset
|
572 |
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
|
573 |
tag.save() |
| 61 | 574 |
|
575 |
if u"datasheet_id" in request.POST : |
|
576 |
return get_tag_table(request=request, ds_id=request.POST["datasheet_id"], valid=0) |
|
577 |
else : |
|
|
99
743a4511d93c
correction bug #21 et update to latest version of jeditable
ymh <ymh.work@gmail.com>
parents:
89
diff
changeset
|
578 |
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"]) |
|
44
244d805b4921
Reorder taggedsheet by drag and drop. Remove wikipedia_activated for a tag. TagCategory chosen by closed list.
cavaliet
parents:
43
diff
changeset
|
579 |
|
| 72 | 580 |
@login_required |
581 |
def reorder_tag_datasheet(request): |
|
582 |
||
583 |
ds_id = request.REQUEST['datasheet_id'] |
|
584 |
ds = Datasheet.objects.get(hda_id=ds_id) |
|
585 |
reorder_datasheet_tags(ds) |
|
586 |
||
587 |
return get_tag_table(request=request, ds_id=ds_id, valid=0) |
|
| 275 | 588 |
|
589 |
||
590 |
class Folders(TemplateView): |
|
591 |
template_name = "folders.html" |
|
592 |
def get(self, request): |
|
593 |
folders = Folder.objects.all().order_by('title') |
|
594 |
return self.render_to_response({'folders':folders}) |
|
595 |
||
596 |
||
597 |
||
598 |
class AddOrUpdateFolder(TemplateView): |
|
599 |
||
600 |
template_name = "add_or_update_folder.html" |
|
| 327 | 601 |
redirect_view = 'folders' |
| 275 | 602 |
|
603 |
def get(self, request, folder_pk=None): |
|
604 |
folder = None |
|
605 |
if folder_pk: |
|
606 |
folder = get_object_or_404(Folder, pk=folder_pk) |
|
607 |
return self.render_to_response({'folder':folder}) |
|
608 |
||
609 |
def post(self, request): |
|
610 |
||
611 |
folder = None |
|
612 |
if "pk" in request.POST and request.POST["pk"]!="": |
|
613 |
folder = get_object_or_404(Folder, pk=request.POST["pk"]) |
|
614 |
if not folder: |
|
615 |
folder = Folder.objects.create() |
|
616 |
folder.url = request.POST["url"] |
|
617 |
folder.title = request.POST["title"] |
|
618 |
folder.description = request.POST["description"] |
|
| 276 | 619 |
|
620 |
if "ds_ids" in request.POST and request.POST["ds_ids"]!="": |
|
621 |
ds_ids = request.POST["ds_ids"].split(",") |
|
622 |
ds_ids = filter(None, ds_ids) # fastest |
|
623 |
folder.datasheets = Datasheet.objects.filter(pk__in=ds_ids) |
|
624 |
||
| 275 | 625 |
folder.save() |
626 |
||
| 327 | 627 |
return redirect(self.redirect_view) |
| 275 | 628 |
|
629 |
||
630 |
||
| 276 | 631 |
class SearchDatasheet(SearchView): |
632 |
||
633 |
template = "partial/search_datasheet_for_folders.html" |
|
| 275 | 634 |
|
|
323
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
635 |
def create_response(self): |
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
636 |
""" |
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
637 |
Generates the actual HttpResponse to send back to the user. |
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
638 |
""" |
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
639 |
limit = self.request.GET.get("limit", -1) |
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
640 |
try: |
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
641 |
limit = int(limit) |
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
642 |
except: |
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
643 |
pass |
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
644 |
if limit>0: |
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
645 |
self.results_per_page = limit |
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
646 |
|
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
647 |
(paginator, page) = self.build_page() |
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
648 |
|
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
649 |
context = { |
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
650 |
'query': self.query, |
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
651 |
'form': self.form, |
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
652 |
'page': page, |
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
653 |
'paginator': paginator, |
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
654 |
'suggestion': None, |
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
655 |
} |
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
656 |
|
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
657 |
if self.results and hasattr(self.results, 'query') and self.results.query.backend.include_spelling: |
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
658 |
context['suggestion'] = self.form.get_suggestion() |
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
659 |
|
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
660 |
context.update(self.extra_context()) |
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
661 |
|
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
662 |
if self.request.GET.get("format", "")=="json": |
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
663 |
output = { "results": [] } |
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
664 |
for result in page.object_list: |
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
665 |
o = result.object |
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
666 |
output["results"].append({"title":o.title, "description":o.description, "url":o.url, "hda_id":o.hda_id}) |
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
667 |
return HttpResponse(json.dumps(output, indent=2), content_type="application/json") |
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
668 |
|
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
669 |
return render_to_response(self.template, context, context_instance=self.context_class(self.request)) |
|
67cff4e39ad8
search in notice json, and renkan search bin for notices.
cavaliet
parents:
292
diff
changeset
|
670 |
|
| 276 | 671 |
|
672 |
||
| 277 | 673 |
class DeleteFolder(View): |
| 327 | 674 |
redirect_view = 'folders' |
| 277 | 675 |
|
676 |
def get(self, request, folder_pk=None): |
|
677 |
folder = None |
|
678 |
if folder_pk: |
|
679 |
folder = get_object_or_404(Folder, pk=folder_pk) |
|
680 |
folder.delete() |
|
681 |
||
| 327 | 682 |
return redirect(self.redirect_view) |
| 276 | 683 |
|
| 277 | 684 |
|
685 |
||
686 |