| author | cavaliet |
| Thu, 05 Sep 2013 13:09:57 +0200 | |
| changeset 38 | c4e5bb735ec1 |
| parent 35 | 544bc92e6fe7 |
| child 40 | cc7149ca6863 |
| permissions | -rw-r--r-- |
| 1 | 1 |
# -*- coding: utf-8 -*- |
2 |
''' |
|
3 |
Created on Aug 27, 2013 |
|
4 |
||
5 |
@author: tc |
|
6 |
''' |
|
7 |
||
8 |
from django.conf import settings |
|
| 24 | 9 |
from django.contrib.auth.decorators import login_required |
10 |
from django.utils.decorators import method_decorator |
|
| 34 | 11 |
from django.views.generic import ListView, DetailView |
|
26
a0e152dd1fad
first version of angular intégration
ymh <ymh.work@gmail.com>
parents:
20
diff
changeset
|
12 |
from p4l.forms import RecordFilterForm |
|
a0e152dd1fad
first version of angular intégration
ymh <ymh.work@gmail.com>
parents:
20
diff
changeset
|
13 |
from p4l.models import Record |
|
a0e152dd1fad
first version of angular intégration
ymh <ymh.work@gmail.com>
parents:
20
diff
changeset
|
14 |
from p4l.utils import get_labels_for_uris |
| 34 | 15 |
import json |
| 1 | 16 |
import logging |
| 38 | 17 |
from p4l.semantictree.forms.forms import SubjectForm |
18 |
from django.utils.safestring import mark_safe |
|
| 1 | 19 |
|
| 7 | 20 |
|
| 1 | 21 |
logger = logging.getLogger(__name__) |
22 |
||
23 |
class RecordListView(ListView): |
|
24 |
||
| 5 | 25 |
queryset = Record.objects.select_related("language").prefetch_related('titles').distinct() # @UndefinedVariable |
| 1 | 26 |
paginate_by = settings.NB_RECORDS_BY_PAGE |
27 |
template_name = "p4l/p4l_home.html" |
|
| 4 | 28 |
form_class = RecordFilterForm |
29 |
||
| 24 | 30 |
@method_decorator(login_required) |
31 |
def dispatch(self, *args, **kwargs): |
|
32 |
return super(RecordListView, self).dispatch(*args, **kwargs) |
|
| 1 | 33 |
|
| 4 | 34 |
def get_context_data(self, **kwargs): |
35 |
context = ListView.get_context_data(self, **kwargs) |
|
36 |
context['filter_form'] = self.form_class() |
|
37 |
# Add filter params from GET params |
|
38 |
filter_params = {} |
|
39 |
if 'title' in self.request.GET: |
|
40 |
filter_params['title'] = self.request.GET['title'] |
|
41 |
context['filter_params'] = filter_params |
|
42 |
return context |
|
43 |
||
44 |
def get_queryset(self): |
|
45 |
qs = super(RecordListView, self).get_queryset() |
|
46 |
filter_form = self.form_class(self.request.GET) |
|
47 |
if filter_form.is_valid(): |
|
48 |
return filter_form.get_filter_qs(qs) |
|
49 |
else: |
|
50 |
return qs |
|
| 7 | 51 |
|
52 |
||
53 |
class RecordDetailView(DetailView): |
|
54 |
||
55 |
model = Record |
|
56 |
template_name = "p4l/p4l_record_view.html" |
|
| 30 | 57 |
slug_field = "identifier" |
| 7 | 58 |
|
| 24 | 59 |
@method_decorator(login_required) |
60 |
def dispatch(self, *args, **kwargs): |
|
61 |
return super(RecordDetailView, self).dispatch(*args, **kwargs) |
|
62 |
||
| 17 | 63 |
def get_context_data(self, **kwargs): |
64 |
context = DetailView.get_context_data(self, **kwargs) |
|
| 20 | 65 |
# We get the language, "fr" by default |
66 |
lang = "fr" |
|
67 |
if "lang" in self.request.GET: |
|
68 |
lang = self.request.GET["lang"] |
|
69 |
elif hasattr(self.request, "LANGUAGE_CODE") and self.request.LANGUAGE_CODE in ["fr","en","es"]: |
|
70 |
lang = self.request.LANGUAGE_CODE |
|
| 17 | 71 |
# self.object is the record entry |
72 |
# We get the subjects'labels with the Thesaurus repository |
|
73 |
uri_list = [s.uri for s in self.object.subjects.all()] |
|
| 20 | 74 |
uris_labels = get_labels_for_uris(uri_list, "http://skos.um.es/unescothes/CS000", lang, False) |
| 17 | 75 |
context['subjects_labels'] = uris_labels |
76 |
# We get the themes'labels with the Themes repository |
|
77 |
uri_list = [s.uri for s in self.object.themes.all()] |
|
| 20 | 78 |
uris_labels = get_labels_for_uris(uri_list, "http://www.iiep.unesco.org/plan4learning/scheme/Themes", lang, False) |
| 17 | 79 |
context['themes_labels'] = uris_labels |
80 |
# We get the countries'labels with the Thesaurus repository |
|
81 |
uri_list = [s.uri for s in self.object.countries.all()] |
|
| 20 | 82 |
uris_labels = get_labels_for_uris(uri_list, "http://skos.um.es/unescothes/CS000/Countries", lang, False) |
| 17 | 83 |
context['countries_labels'] = uris_labels |
84 |
# We get the languages'labels with the Languages repository |
|
85 |
if self.object.language: |
|
86 |
uri_list = [self.object.language.uri] |
|
| 20 | 87 |
uris_labels = get_labels_for_uris(uri_list, "http://www.iiep.unesco.org/plan4learning/scheme/Languages", lang, False) |
| 17 | 88 |
context['language_label'] = uris_labels[self.object.language.uri] |
89 |
# We get the other languages'labels with the Languages repository |
|
90 |
uri_list = [s.uri for s in self.object.otherLanguages.all()] |
|
| 20 | 91 |
uris_labels = get_labels_for_uris(uri_list, "http://www.iiep.unesco.org/plan4learning/scheme/Languages", lang, False) |
| 17 | 92 |
context['otherLanguages_labels'] = uris_labels |
93 |
# We get the project'labels with the Projects repository |
|
94 |
uri_list = [s.uri for s in self.object.projectNames.all()] |
|
95 |
uris_labels = get_labels_for_uris(uri_list, "http://www.iiep.unesco.org/plan4learning/scheme/Projects", None, True) |
|
96 |
context['projects_labels'] = uris_labels |
|
97 |
# We get the subjectCorporateBodies'labels with the Organizations repository |
|
98 |
uri_list = [s.uri for s in self.object.subjectCorporateBodies.all()] |
|
99 |
uris_labels = get_labels_for_uris(uri_list, "http://www.iiep.unesco.org/plan4learning/scheme/Organizations", None, True) |
|
100 |
context['subjectCorporateBodies_labels'] = uris_labels |
|
101 |
# We get the corporateAuthors'labels with the Organizations repository |
|
102 |
uri_list = [s.uri for s in self.object.corporateAuthors.all()] |
|
103 |
uris_labels = get_labels_for_uris(uri_list, "http://www.iiep.unesco.org/plan4learning/scheme/Organizations", None, True) |
|
104 |
context['corporateAuthors_labels'] = uris_labels |
|
105 |
# We get the recordType'labels with the DocumentType repository |
|
106 |
if self.object.recordType: |
|
107 |
uri_list = [self.object.recordType] |
|
| 20 | 108 |
uris_labels = get_labels_for_uris(uri_list, "http://www.iiep.unesco.org/plan4learning/scheme/DocumentType", lang, False) |
| 17 | 109 |
context['recordType_label'] = uris_labels[self.object.recordType] |
| 7 | 110 |
|
| 17 | 111 |
return context |
| 7 | 112 |
|
| 34 | 113 |
class RecordEditView(DetailView): |
| 29 | 114 |
http_method_names = ['get'] |
115 |
template_name = 'p4l/record_update_form.html' |
|
| 34 | 116 |
model = Record |
117 |
slug_field = "identifier" |
|
|
26
a0e152dd1fad
first version of angular intégration
ymh <ymh.work@gmail.com>
parents:
20
diff
changeset
|
118 |
|
| 34 | 119 |
def get_context_data(self, **kwargs): |
120 |
context = DetailView.get_context_data(self, **kwargs) |
|
121 |
# We get the language, "fr" by default |
|
122 |
lang = "fr" |
|
123 |
if "lang" in self.request.GET: |
|
124 |
lang = self.request.GET["lang"] |
|
125 |
elif hasattr(self.request, "LANGUAGE_CODE") and self.request.LANGUAGE_CODE in ["fr","en","es"]: |
|
126 |
lang = self.request.LANGUAGE_CODE |
|
127 |
||
128 |
uri_labels = get_labels_for_uris([s.uri for s in self.object.subjects.all()], "http://skos.um.es/unescothes/CS000", lang, False) |
|
129 |
uri_labels.update(get_labels_for_uris([s.uri for s in self.object.themes.all()], "http://www.iiep.unesco.org/plan4learning/scheme/Themes", lang, False)) |
|
130 |
uri_labels.update(get_labels_for_uris([s.uri for s in self.object.countries.all()], "http://skos.um.es/unescothes/CS000/Countries", lang, False)) |
|
131 |
uri_labels.update(get_labels_for_uris([self.object.language.uri] if self.object.language else [], "http://www.iiep.unesco.org/plan4learning/scheme/Languages", lang, False)) |
|
132 |
uri_labels.update(get_labels_for_uris([s.uri for s in self.object.otherLanguages.all()], "http://www.iiep.unesco.org/plan4learning/scheme/Languages", lang, False)) |
|
133 |
uri_labels.update(get_labels_for_uris([s.uri for s in self.object.projectNames.all()], "http://www.iiep.unesco.org/plan4learning/scheme/Projects", None, True)) |
|
134 |
uri_labels.update(get_labels_for_uris([s.uri for s in self.object.subjectCorporateBodies.all()], "http://www.iiep.unesco.org/plan4learning/scheme/Organizations", None, True)) |
|
135 |
uri_labels.update(get_labels_for_uris([s.uri for s in self.object.corporateAuthors.all()], "http://www.iiep.unesco.org/plan4learning/scheme/Organizations", None, True)) |
|
136 |
uri_labels.update(get_labels_for_uris([self.object.recordType] if self.object.recordType else [], "http://www.iiep.unesco.org/plan4learning/scheme/DocumentType", lang, False)) |
|
137 |
context['uri_labels'] = json.dumps(uri_labels) |
|
138 |
||
| 38 | 139 |
context['subjects_query_dict'] = json.dumps({ |
140 |
'data-url': settings.SPARQL_QUERY_ENDPOINT, |
|
141 |
'data-query': |
|
142 |
""" |
|
143 |
PREFIX skos:<http://www.w3.org/2004/02/skos/core#> |
|
144 |
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
|
145 |
PREFIX owl:<http://www.w3.org/2002/07/owl#> |
|
146 |
PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> |
|
147 |
SELECT DISTINCT ?uri ?label |
|
148 |
WHERE { |
|
149 |
?uri a skos:Concept. |
|
150 |
?uri skos:inScheme <http://skos.um.es/unescothes/CS000> . |
|
151 |
?uri skos:prefLabel ?label. |
|
152 |
FILTER (lang(?label) = ?language). |
|
153 |
?uri skos:prefLabel ?lab. |
|
154 |
FILTER regex (str(?lab), ?reg, 'i'). |
|
155 |
FILTER (lang (?lab) = ?language). |
|
156 |
BIND (STRLEN(STRBEFORE (str(?lab), ?reg)) AS ?place). |
|
157 |
BIND (STRLEN(STR(?lab)) AS ?len) |
|
158 |
} |
|
159 |
ORDER BY ?place ?len ?lab |
|
160 |
""", |
|
161 |
'data-root-query': |
|
162 |
""" |
|
163 |
PREFIX skos:<http://www.w3.org/2004/02/skos/core#> |
|
164 |
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
|
165 |
PREFIX owl:<http://www.w3.org/2002/07/owl#> |
|
166 |
PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> |
|
167 |
SELECT DISTINCT ?uri ?label |
|
168 |
WHERE { |
|
169 |
?uri a skos:Collection ; |
|
170 |
skos:inScheme <http://skos.um.es/unescothes/CS000> ; |
|
171 |
rdfs:label ?label . |
|
172 |
FILTER (lang(?label) = ?language). |
|
173 |
FILTER NOT EXISTS { [skos:member ?uri] } |
|
174 |
} |
|
175 |
""", |
|
176 |
'data-childs-query': |
|
177 |
""" |
|
178 |
PREFIX skos:<http://www.w3.org/2004/02/skos/core#> |
|
179 |
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
|
180 |
PREFIX owl:<http://www.w3.org/2002/07/owl#> |
|
181 |
PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> |
|
182 |
SELECT DISTINCT ?uri ?label |
|
183 |
WHERE { |
|
184 |
?uri skos:inScheme <http://skos.um.es/unescothes/CS000> . |
|
185 |
{ ?uri a ?type |
|
186 |
FILTER (?type = skos:Collection || ?type = skos:Concept) }. |
|
187 |
?root skos:narrower|skos:member ?uri. |
|
188 |
?uri skos:prefLabel|rdfs:label ?label. |
|
189 |
FILTER (lang(?label) = ?language). |
|
190 |
} |
|
191 |
""", |
|
192 |
'data-child-count-query': |
|
193 |
""" |
|
194 |
PREFIX skos:<http://www.w3.org/2004/02/skos/core#> |
|
195 |
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
|
196 |
PREFIX owl:<http://www.w3.org/2002/07/owl#> |
|
197 |
PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> |
|
198 |
SELECT (COUNT(?uri) as ?nb) |
|
199 |
WHERE { |
|
200 |
?uri skos:inScheme <http://skos.um.es/unescothes/CS000> . |
|
201 |
?root skos:narrower|skos:member ?uri. |
|
202 |
} |
|
203 |
""" |
|
204 |
}) |
|
205 |
||
| 34 | 206 |
return context |
207 |
||
208 |