| author | ymh <ymh.work@gmail.com> |
| Tue, 02 Jul 2013 10:15:50 +0200 | |
| changeset 57 | a82c3913c1fc |
| parent 55 | bcbd95da9be2 |
| child 61 | 0048668779c0 |
| permissions | -rw-r--r-- |
| 0 | 1 |
# -*- coding: utf-8 -*- |
2 |
''' |
|
3 |
Created on Jun 12, 2013 |
|
4 |
||
5 |
@author: ymh |
|
6 |
''' |
|
7 |
||
8 |
from .forms import (ModifyWpLinkForm, ValidateTermForm, RemoveWpLinkForm, |
|
9 |
TermFilterForm) |
|
| 5 | 10 |
from .utils import JocondePaginator |
| 0 | 11 |
from core.models import Term, TERM_URL_STATUS_DICT |
|
35
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
12 |
from core.models.term import (TERM_WK_LINK_SEMANTIC_LEVEL_DICT, |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
13 |
TERM_WK_LINK_SEMANTIC_LEVEL_CHOICES) |
| 0 | 14 |
from core.wp_utils import process_term as wp_process_term |
15 |
from django.conf import settings |
|
16 |
from django.http.response import HttpResponse |
|
17 |
from django.views.generic import ListView, DetailView, View |
|
|
35
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
18 |
from jocondelab.forms import WikipediaEditionForm, LinkSemanticLevelForm |
| 0 | 19 |
import logging |
| 37 | 20 |
import urllib |
| 51 | 21 |
from django.views.generic.list import MultipleObjectMixin |
| 0 | 22 |
|
23 |
logger = logging.getLogger(__name__) |
|
24 |
||
25 |
class TermListView(ListView): |
|
26 |
||
27 |
model = Term |
|
28 |
paginate_by = settings.TERM_LIST_PAGE_SIZE |
|
| 5 | 29 |
paginator_class = JocondePaginator |
| 0 | 30 |
template_name = "jocondelab/term_list.html" |
| 5 | 31 |
filter_form_class = TermFilterForm |
| 0 | 32 |
|
33 |
def get_filter_form(self): |
|
34 |
initial = { 'order_by':'label', |
|
35 |
'order_dir': 'asc', |
|
36 |
'thesaurus': None, |
|
37 |
'label': None, |
|
38 |
'link_status': -1, |
|
39 |
'validated': None} |
|
| 5 | 40 |
return self.filter_form_class(self.request.GET, initial=initial, auto_id=True) |
| 0 | 41 |
|
42 |
def get_context_data(self, **kwargs): |
|
43 |
context = ListView.get_context_data(self, **kwargs) |
|
44 |
context['filter_form'] = self.get_filter_form() |
|
45 |
return context |
|
46 |
||
47 |
def get_queryset(self): |
|
|
48
f4fadc1b9d70
cache the nb_notice query to improve perf.
ymh <ymh.work@gmail.com>
parents:
43
diff
changeset
|
48 |
qs = super(TermListView, self).get_queryset() |
| 0 | 49 |
filter_form = self.get_filter_form() |
50 |
if filter_form.is_valid(): |
|
51 |
return filter_form.get_filter_qs(qs) |
|
52 |
else: |
|
53 |
return None |
|
54 |
||
| 5 | 55 |
class TermListTableView(TermListView): |
56 |
||
57 |
template_name = "jocondelab/partial/term_list_table.html" |
|
58 |
||
59 |
||
60 |
||
| 51 | 61 |
class TermEditView(DetailView, MultipleObjectMixin): |
| 0 | 62 |
|
63 |
queryset = Term.objects.select_related() |
|
64 |
pk_url_kwarg = "term_id" |
|
65 |
context_object_name = "term" |
|
66 |
template_name = "jocondelab/term_edit.html" |
|
| 5 | 67 |
filter_form_class = TermFilterForm |
| 51 | 68 |
model = Term |
69 |
paginate_by = settings.TERM_LIST_PAGE_SIZE |
|
70 |
paginator_class = JocondePaginator |
|
71 |
||
72 |
def get_object(self, queryset=None): |
|
73 |
||
74 |
if queryset is None: |
|
75 |
queryset = self.queryset |
|
76 |
||
77 |
return DetailView.get_object(self, queryset) |
|
78 |
||
79 |
def get_queryset(self): |
|
80 |
qs = self.queryset._clone() |
|
81 |
filter_form = self.get_filter_form() |
|
82 |
if filter_form.is_valid(): |
|
83 |
return filter_form.get_filter_qs(qs) |
|
84 |
else: |
|
85 |
return None |
|
86 |
||
| 5 | 87 |
|
88 |
def get_filter_form(self): |
|
89 |
initial = { 'order_by':'label', |
|
90 |
'order_dir': 'asc', |
|
91 |
'thesaurus': None, |
|
92 |
'label': None, |
|
93 |
'link_status': -1, |
|
94 |
'validated': None} |
|
95 |
return self.filter_form_class(self.request.GET, initial=initial, auto_id=True) |
|
96 |
||
97 |
||
98 |
def get_context_data(self, **kwargs): |
|
| 51 | 99 |
|
100 |
self.object_list = self.get_queryset() |
|
101 |
if kwargs is None : |
|
102 |
kwargs = {} |
|
103 |
kwargs['object_list'] = self.object_list |
|
104 |
||
105 |
# Beware: because of multiple inheritance this call MultipleObjectMixin.get_context_data(self, **context) |
|
| 5 | 106 |
context = DetailView.get_context_data(self, **kwargs) |
|
55
bcbd95da9be2
add preview of notices + add missing img for wp
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
107 |
|
|
bcbd95da9be2
add preview of notices + add missing img for wp
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
108 |
context['notices'] = self.object.notices.select_related().all().prefetch_related('images')[:10] |
| 51 | 109 |
|
| 5 | 110 |
context['filter_form'] = self.get_filter_form() |
| 37 | 111 |
context['link_semantic_level_choice'] = TERM_WK_LINK_SEMANTIC_LEVEL_CHOICES |
|
57
a82c3913c1fc
add direct link to notice on ref field
ymh <ymh.work@gmail.com>
parents:
55
diff
changeset
|
112 |
context['JOCONDE_IMG_BASE_URL'] = settings.JOCONDE_IMG_BASE_URL |
|
a82c3913c1fc
add direct link to notice on ref field
ymh <ymh.work@gmail.com>
parents:
55
diff
changeset
|
113 |
context['JOCONDE_NOTICE_BASE_URL'] = settings.JOCONDE_NOTICE_BASE_URL |
| 37 | 114 |
|
115 |
field_index = { |
|
116 |
'DOMN' : 1, |
|
117 |
'AUTR' : 3, |
|
118 |
'ECOL' : 4, |
|
119 |
'REPR' : 5, |
|
120 |
'PERI' : 6, |
|
121 |
'EPOQ' : 6, |
|
122 |
'LIEUX': 4, |
|
123 |
'SREP' : 9 |
|
124 |
}[self.object.thesaurus.label] |
|
125 |
||
|
38
7abb8925853d
Correct joconde search link, try to improve thesaurus name
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
126 |
field_name = { |
|
7abb8925853d
Correct joconde search link, try to improve thesaurus name
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
127 |
'SREP' : u"Source sujet représenté" |
|
7abb8925853d
Correct joconde search link, try to improve thesaurus name
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
128 |
}.get(self.object.thesaurus.label, self.object.thesaurus.label) |
|
7abb8925853d
Correct joconde search link, try to improve thesaurus name
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
129 |
|
| 37 | 130 |
encoded_label = self.object.label.encode('latin1') if self.object.label is not None else "" |
131 |
||
|
38
7abb8925853d
Correct joconde search link, try to improve thesaurus name
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
132 |
context['encoded_term_label_query_parameter'] = urllib. urlencode({ |
|
7abb8925853d
Correct joconde search link, try to improve thesaurus name
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
133 |
'FIELD_%d' % field_index: field_name.encode('latin1'), |
|
7abb8925853d
Correct joconde search link, try to improve thesaurus name
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
134 |
'VALUE_%d' % field_index: encoded_label}).replace('+','%20') |
| 51 | 135 |
|
136 |
#prev_id, nex_id, prev_page, next_page |
|
137 |
page = context['page_obj'] |
|
138 |
||
139 |
prev_id = None |
|
140 |
prev_page = 0 |
|
141 |
next_id = None |
|
142 |
next_page = 0 |
|
143 |
||
144 |
||
145 |
object_list_ids = [obj.id for obj in list(page.object_list)] |
|
146 |
current_index = object_list_ids.index(self.object.id) |
|
147 |
||
148 |
if current_index > 0: |
|
149 |
prev_id = object_list_ids[current_index-1] |
|
150 |
prev_page = page.number |
|
151 |
elif page.has_previous(): |
|
152 |
prev_page = page.previous_page_number() |
|
153 |
prev_id = page.paginator.object_list[page.start_index() - 2].id |
|
154 |
||
155 |
if current_index < (len(page)-1): |
|
156 |
next_id = object_list_ids[current_index+1] |
|
157 |
next_page = page.number |
|
158 |
elif page.has_next(): |
|
159 |
next_page = page.next_page_number() |
|
160 |
next_id = page.paginator.object_list[page.end_index()].id |
|
161 |
||
162 |
context.update({ |
|
163 |
'prev_id': prev_id, |
|
164 |
'prev_page': prev_page, |
|
165 |
'next_id': next_id, |
|
166 |
'next_page': next_page |
|
167 |
}) |
|
168 |
||
169 |
return context |
|
| 0 | 170 |
|
171 |
||
172 |
||
173 |
class TermUpdateView(View): |
|
174 |
||
175 |
form_class = None |
|
176 |
http_method_names = ['post'] |
|
177 |
||
178 |
def __init__(self, **kwargs): |
|
179 |
View.__init__(self, **kwargs) |
|
180 |
self.form = None |
|
181 |
self.form_values = None |
|
182 |
self.term = None |
|
183 |
||
184 |
def post(self, request, *args, **kwargs): |
|
185 |
self.form = self.form_class(request.POST) |
|
186 |
if not self.form.is_valid(): |
|
187 |
return HttpResponse("Parameters not valid : %s" % (self.form.cleaned_data), status=400) |
|
188 |
||
189 |
self.form_values = self.form.cleaned_data |
|
190 |
||
191 |
try: |
|
192 |
self.term = Term.objects.get(id=self.form_values['term_id']) |
|
193 |
except Term.DoesNotExist: |
|
194 |
return HttpResponse("Term %d not found" % self.form_values['term_id'],status=404) |
|
195 |
||
196 |
return self.process_term(request) |
|
197 |
||
198 |
def process_term(self, request): |
|
199 |
raise NotImplemented() |
|
200 |
||
201 |
||
202 |
class TermValidate(TermUpdateView): |
|
203 |
||
204 |
form_class = ValidateTermForm |
|
205 |
||
206 |
def process_term(self, request): |
|
207 |
if self.form_values['validation_val']: |
|
208 |
self.term.validate(request.user) |
|
209 |
else: |
|
210 |
self.term.unvalidate() |
|
211 |
||
212 |
return HttpResponse(status=204) |
|
213 |
||
214 |
||
215 |
class TermRemoveWpLink(TermUpdateView): |
|
216 |
||
217 |
form_class = RemoveWpLinkForm |
|
218 |
||
219 |
def process_term(self, request): |
|
220 |
||
221 |
self.term.wp_label = None |
|
222 |
self.term.wp_alternative_label = None |
|
223 |
self.term.alternative_wikipedia_url = None |
|
224 |
self.term.alternative_wikipedia_pageid = None |
|
225 |
self.term.wikipedia_url =None |
|
226 |
self.term.wikipedia_pageid = None |
|
227 |
self.term.dbpedia_uri = None |
|
228 |
self.term.wikipedia_revision_id = None |
|
|
24
1d20eaea6169
small change of status link unsemantized.
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
229 |
self.term.url_status = TERM_URL_STATUS_DICT["unsemantized"] |
|
35
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
230 |
self.link_semantic_level = TERM_WK_LINK_SEMANTIC_LEVEL_DICT['--'] |
| 0 | 231 |
|
232 |
self.term.save() |
|
233 |
||
234 |
return HttpResponse(status=204) |
|
235 |
||
236 |
||
237 |
||
238 |
class TermModifyWpLink(TermUpdateView): |
|
239 |
||
240 |
form_class = ModifyWpLinkForm |
|
241 |
||
242 |
def process_term(self, request): |
|
243 |
||
244 |
label = self.form_values['label'] |
|
|
26
758b9289aa9a
add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents:
24
diff
changeset
|
245 |
|
| 0 | 246 |
wp_process_term(None, self.term, label) |
247 |
||
|
26
758b9289aa9a
add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents:
24
diff
changeset
|
248 |
return HttpResponse(status=204) |
|
758b9289aa9a
add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents:
24
diff
changeset
|
249 |
|
|
758b9289aa9a
add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents:
24
diff
changeset
|
250 |
|
|
758b9289aa9a
add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents:
24
diff
changeset
|
251 |
class TermWikipediaEdition(TermUpdateView): |
|
758b9289aa9a
add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents:
24
diff
changeset
|
252 |
|
|
758b9289aa9a
add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents:
24
diff
changeset
|
253 |
form_class = WikipediaEditionForm |
|
758b9289aa9a
add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents:
24
diff
changeset
|
254 |
|
|
758b9289aa9a
add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents:
24
diff
changeset
|
255 |
def process_term(self, request): |
|
758b9289aa9a
add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents:
24
diff
changeset
|
256 |
|
|
758b9289aa9a
add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents:
24
diff
changeset
|
257 |
self.term.wikipedia_edition = self.form_values['wikipedia_edition'] |
|
758b9289aa9a
add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents:
24
diff
changeset
|
258 |
self.term.save() |
|
758b9289aa9a
add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents:
24
diff
changeset
|
259 |
|
|
758b9289aa9a
add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents:
24
diff
changeset
|
260 |
return HttpResponse(status=204) |
|
35
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
261 |
|
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
262 |
class TermLinkSemanticLevelEdition(TermUpdateView): |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
263 |
|
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
264 |
form_class = LinkSemanticLevelForm |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
265 |
|
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
266 |
def process_term(self, request): |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
267 |
|
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
268 |
self.term.link_semantic_level = self.form_values['link_semantic_level'] |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
269 |
self.term.save() |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
270 |
|
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
271 |
return HttpResponse(status=204) |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
272 |