| author | ymh <ymh.work@gmail.com> |
| Tue, 01 Oct 2013 02:53:54 +0200 | |
| changeset 131 | f1854630734f |
| parent 126 | a345f1a67bf1 |
| child 145 | 7c6fe1dab213 |
| permissions | -rw-r--r-- |
| 1 | 1 |
# -*- coding: utf-8 -*- |
| 126 | 2 |
# |
| 131 | 3 |
# Copyright IRI (c) 2013 |
| 126 | 4 |
# |
5 |
# contact@iri.centrepompidou.fr |
|
6 |
# |
|
7 |
# This software is governed by the CeCILL-B license under French law and |
|
8 |
# abiding by the rules of distribution of free software. You can use, |
|
9 |
# modify and/ or redistribute the software under the terms of the CeCILL-B |
|
10 |
# license as circulated by CEA, CNRS and INRIA at the following URL |
|
11 |
# "http://www.cecill.info". |
|
12 |
# |
|
13 |
# As a counterpart to the access to the source code and rights to copy, |
|
14 |
# modify and redistribute granted by the license, users are provided only |
|
15 |
# with a limited warranty and the software's author, the holder of the |
|
16 |
# economic rights, and the successive licensors have only limited |
|
17 |
# liability. |
|
18 |
# |
|
19 |
# In this respect, the user's attention is drawn to the risks associated |
|
20 |
# with loading, using, modifying and/or developing or reproducing the |
|
21 |
# software by the user in light of its specific status of free software, |
|
22 |
# that may mean that it is complicated to manipulate, and that also |
|
23 |
# therefore means that it is reserved for developers and experienced |
|
24 |
# professionals having in-depth computer knowledge. Users are therefore |
|
25 |
# encouraged to load and test the software's suitability as regards their |
|
26 |
# requirements in conditions enabling the security of their systems and/or |
|
27 |
# data to be ensured and, more generally, to use and operate it in the |
|
28 |
# same conditions as regards security. |
|
29 |
# |
|
30 |
# The fact that you are presently reading this means that you have had |
|
31 |
# knowledge of the CeCILL-B license and that you accept its terms. |
|
32 |
# |
|
| 1 | 33 |
|
|
113
c05567404888
First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
99
diff
changeset
|
34 |
import json |
|
c05567404888
First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
99
diff
changeset
|
35 |
import logging |
| 117 | 36 |
import time |
|
113
c05567404888
First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
99
diff
changeset
|
37 |
|
| 1 | 38 |
from django.conf import settings |
| 92 | 39 |
from django.shortcuts import redirect, get_object_or_404 |
|
113
c05567404888
First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
99
diff
changeset
|
40 |
from django.views.generic import DetailView, View |
| 117 | 41 |
from rest_framework.renderers import UnicodeJSONRenderer |
|
113
c05567404888
First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
99
diff
changeset
|
42 |
|
| 117 | 43 |
from p4l.api.serializers import RecordSerializer |
|
26
a0e152dd1fad
first version of angular intégration
ymh <ymh.work@gmail.com>
parents:
20
diff
changeset
|
44 |
from p4l.models import Record |
|
a0e152dd1fad
first version of angular intégration
ymh <ymh.work@gmail.com>
parents:
20
diff
changeset
|
45 |
from p4l.utils import get_labels_for_uris |
| 1 | 46 |
|
| 7 | 47 |
|
| 1 | 48 |
logger = logging.getLogger(__name__) |
49 |
||
| 117 | 50 |
QUERY_FIELDS_TRANSLATION = { |
51 |
'url': 'dataurl', |
|
52 |
'filter': 'dataquery', |
|
53 |
'root': 'datarootquery', |
|
54 |
'childs': 'datachildsquery', |
|
55 |
'child-count': 'datachildcountquery' |
|
56 |
} |
|
57 |
||
58 |
||
59 |
def get_record_uri_labels(record, lang): |
|
60 |
uri_labels = get_labels_for_uris([s.uri for s in record.subjects.all()], settings.RDF_SCHEMES['subjects'], lang, False) |
|
61 |
uri_labels.update(get_labels_for_uris([s.uri for s in record.themes.all()], settings.RDF_SCHEMES['themes'], lang, False)) |
|
62 |
uri_labels.update(get_labels_for_uris([s.uri for s in record.countries.all()], settings.RDF_SCHEMES['countries'], lang, False)) |
|
63 |
uri_labels.update(get_labels_for_uris([record.language.uri] if record.language else [], settings.RDF_SCHEMES['languages'], lang, False)) |
|
64 |
uri_labels.update(get_labels_for_uris([s.uri for s in record.otherLanguages.all()], settings.RDF_SCHEMES['languages'], lang, False)) |
|
65 |
uri_labels.update(get_labels_for_uris([s.uri for s in record.projectNames.all()], settings.RDF_SCHEMES['projects'], None, True)) |
|
66 |
uri_labels.update(get_labels_for_uris([s.uri for s in record.subjectCorporateBodies.all()], settings.RDF_SCHEMES['organizations'], None, True)) |
|
67 |
uri_labels.update(get_labels_for_uris([s.uri for s in record.corporateAuthors.all()], settings.RDF_SCHEMES['organizations'], None, True)) |
|
68 |
uri_labels.update(get_labels_for_uris([record.recordType] if record.recordType else [], settings.RDF_SCHEMES['types'], lang, False)) |
|
69 |
uri_labels.update(get_labels_for_uris([s.uri for s in record.audiences.all()], settings.RDF_SCHEMES['audiences'], None, True)) |
|
70 |
return uri_labels |
|
71 |
||
| 7 | 72 |
|
73 |
class RecordDetailView(DetailView): |
|
74 |
||
75 |
model = Record |
|
| 117 | 76 |
template_name = "p4l/record_view.html" |
| 30 | 77 |
slug_field = "identifier" |
| 7 | 78 |
|
| 17 | 79 |
def get_context_data(self, **kwargs): |
80 |
context = DetailView.get_context_data(self, **kwargs) |
|
| 20 | 81 |
# We get the language, "fr" by default |
82 |
lang = "fr" |
|
83 |
if "lang" in self.request.GET: |
|
84 |
lang = self.request.GET["lang"] |
|
85 |
elif hasattr(self.request, "LANGUAGE_CODE") and self.request.LANGUAGE_CODE in ["fr","en","es"]: |
|
86 |
lang = self.request.LANGUAGE_CODE |
|
| 117 | 87 |
|
88 |
context['uri_labels'] = get_record_uri_labels(self.object,lang) |
|
89 |
||
| 7 | 90 |
|
| 17 | 91 |
return context |
| 7 | 92 |
|
| 34 | 93 |
class RecordEditView(DetailView): |
| 29 | 94 |
http_method_names = ['get'] |
95 |
template_name = 'p4l/record_update_form.html' |
|
| 34 | 96 |
model = Record |
97 |
slug_field = "identifier" |
|
| 117 | 98 |
is_create_view = False |
99 |
||
100 |
def get_empty_object(self): |
|
101 |
||
102 |
obj = Record() |
|
103 |
obj.id = -1 |
|
104 |
obj.identifier = "T" + str(int(time.time())) |
|
105 |
obj.uri = "http://www.iiep.unesco.org/plan4learning/record/" + obj.identifier |
|
106 |
||
107 |
return obj |
|
108 |
||
109 |
||
110 |
def get_object(self, *args, **kwargs): |
|
111 |
""" |
|
112 |
Returns the object the view is displaying. |
|
113 |
||
114 |
By default this requires `self.queryset` and a `pk` or `slug` argument |
|
115 |
in the URLconf, but subclasses can override this to return any object. |
|
116 |
""" |
|
117 |
if self.is_create_view: |
|
118 |
return self.get_empty_object() |
|
119 |
||
120 |
return super(RecordEditView, self).get_object(*args, **kwargs) |
|
121 |
||
|
26
a0e152dd1fad
first version of angular intégration
ymh <ymh.work@gmail.com>
parents:
20
diff
changeset
|
122 |
|
| 34 | 123 |
def get_context_data(self, **kwargs): |
| 117 | 124 |
|
125 |
context = super(RecordEditView, self).get_context_data(**kwargs) |
|
126 |
||
127 |
if self.object: |
|
128 |
serializer = RecordSerializer(self.object) |
|
129 |
context['object_json'] = UnicodeJSONRenderer().render(serializer.data) |
|
130 |
else: |
|
131 |
context['object_json'] = "null" |
|
132 |
||
| 34 | 133 |
# We get the language, "fr" by default |
134 |
lang = "fr" |
|
135 |
if "lang" in self.request.GET: |
|
136 |
lang = self.request.GET["lang"] |
|
137 |
elif hasattr(self.request, "LANGUAGE_CODE") and self.request.LANGUAGE_CODE in ["fr","en","es"]: |
|
138 |
lang = self.request.LANGUAGE_CODE |
|
139 |
||
| 117 | 140 |
context['uri_labels'] = json.dumps(get_record_uri_labels(self.object,lang)) |
| 34 | 141 |
|
| 60 | 142 |
# lang must be like "XX" in the sparql request |
143 |
lang = '"' + lang + '"' |
|
| 117 | 144 |
|
145 |
query_dicts = dict([(k , dict([ (QUERY_FIELDS_TRANSLATION[key],value.format(lang=lang)) for key,value in v.items() ])) for k,v in settings.SPARQL_REF_QUERIES.items()]) |
|
| 57 | 146 |
context['query_dicts'] = json.dumps(query_dicts) |
| 38 | 147 |
|
| 70 | 148 |
# Languages list used in drop down list |
149 |
context['languages_list'] = json.dumps(settings.LANGUAGES_LIST) |
|
| 119 | 150 |
|
151 |
context['is_create_view'] = json.dumps(self.is_create_view) |
|
| 74 | 152 |
|
| 34 | 153 |
return context |
| 92 | 154 |
|
155 |
||
156 |
class RecordDeleteView(View): |
|
157 |
def get(self, request, slug, **kwargs): |
|
158 |
rec = get_object_or_404(Record, identifier=slug) |
|
159 |
rec.delete() |
|
160 |
return redirect('p4l_home') |
|
| 34 | 161 |