| author | ymh <ymh.work@gmail.com> |
| Wed, 04 Sep 2013 16:17:04 +0200 | |
| changeset 34 | cfc090f440d0 |
| parent 33 | 945b144d68c1 |
| child 35 | 544bc92e6fe7 |
| 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 |
17 |
||
| 7 | 18 |
|
| 1 | 19 |
logger = logging.getLogger(__name__) |
20 |
||
21 |
class RecordListView(ListView): |
|
22 |
||
| 5 | 23 |
queryset = Record.objects.select_related("language").prefetch_related('titles').distinct() # @UndefinedVariable |
| 1 | 24 |
paginate_by = settings.NB_RECORDS_BY_PAGE |
25 |
template_name = "p4l/p4l_home.html" |
|
| 4 | 26 |
form_class = RecordFilterForm |
27 |
||
| 24 | 28 |
@method_decorator(login_required) |
29 |
def dispatch(self, *args, **kwargs): |
|
30 |
return super(RecordListView, self).dispatch(*args, **kwargs) |
|
| 1 | 31 |
|
| 4 | 32 |
def get_context_data(self, **kwargs): |
33 |
context = ListView.get_context_data(self, **kwargs) |
|
34 |
context['filter_form'] = self.form_class() |
|
35 |
# Add filter params from GET params |
|
36 |
filter_params = {} |
|
37 |
if 'title' in self.request.GET: |
|
38 |
filter_params['title'] = self.request.GET['title'] |
|
39 |
context['filter_params'] = filter_params |
|
40 |
return context |
|
41 |
||
42 |
def get_queryset(self): |
|
43 |
qs = super(RecordListView, self).get_queryset() |
|
44 |
filter_form = self.form_class(self.request.GET) |
|
45 |
if filter_form.is_valid(): |
|
46 |
return filter_form.get_filter_qs(qs) |
|
47 |
else: |
|
48 |
return qs |
|
| 7 | 49 |
|
50 |
||
51 |
class RecordDetailView(DetailView): |
|
52 |
||
53 |
model = Record |
|
54 |
template_name = "p4l/p4l_record_view.html" |
|
| 30 | 55 |
slug_field = "identifier" |
| 7 | 56 |
|
| 24 | 57 |
@method_decorator(login_required) |
58 |
def dispatch(self, *args, **kwargs): |
|
59 |
return super(RecordDetailView, self).dispatch(*args, **kwargs) |
|
60 |
||
| 17 | 61 |
def get_context_data(self, **kwargs): |
62 |
context = DetailView.get_context_data(self, **kwargs) |
|
| 20 | 63 |
# We get the language, "fr" by default |
64 |
lang = "fr" |
|
65 |
if "lang" in self.request.GET: |
|
66 |
lang = self.request.GET["lang"] |
|
67 |
elif hasattr(self.request, "LANGUAGE_CODE") and self.request.LANGUAGE_CODE in ["fr","en","es"]: |
|
68 |
lang = self.request.LANGUAGE_CODE |
|
| 17 | 69 |
# self.object is the record entry |
70 |
# We get the subjects'labels with the Thesaurus repository |
|
71 |
uri_list = [s.uri for s in self.object.subjects.all()] |
|
| 20 | 72 |
uris_labels = get_labels_for_uris(uri_list, "http://skos.um.es/unescothes/CS000", lang, False) |
| 17 | 73 |
context['subjects_labels'] = uris_labels |
74 |
# We get the themes'labels with the Themes repository |
|
75 |
uri_list = [s.uri for s in self.object.themes.all()] |
|
| 20 | 76 |
uris_labels = get_labels_for_uris(uri_list, "http://www.iiep.unesco.org/plan4learning/scheme/Themes", lang, False) |
| 17 | 77 |
context['themes_labels'] = uris_labels |
78 |
# We get the countries'labels with the Thesaurus repository |
|
79 |
uri_list = [s.uri for s in self.object.countries.all()] |
|
| 20 | 80 |
uris_labels = get_labels_for_uris(uri_list, "http://skos.um.es/unescothes/CS000/Countries", lang, False) |
| 17 | 81 |
context['countries_labels'] = uris_labels |
82 |
# We get the languages'labels with the Languages repository |
|
83 |
if self.object.language: |
|
84 |
uri_list = [self.object.language.uri] |
|
| 20 | 85 |
uris_labels = get_labels_for_uris(uri_list, "http://www.iiep.unesco.org/plan4learning/scheme/Languages", lang, False) |
| 17 | 86 |
context['language_label'] = uris_labels[self.object.language.uri] |
87 |
# We get the other languages'labels with the Languages repository |
|
88 |
uri_list = [s.uri for s in self.object.otherLanguages.all()] |
|
| 20 | 89 |
uris_labels = get_labels_for_uris(uri_list, "http://www.iiep.unesco.org/plan4learning/scheme/Languages", lang, False) |
| 17 | 90 |
context['otherLanguages_labels'] = uris_labels |
91 |
# We get the project'labels with the Projects repository |
|
92 |
uri_list = [s.uri for s in self.object.projectNames.all()] |
|
93 |
uris_labels = get_labels_for_uris(uri_list, "http://www.iiep.unesco.org/plan4learning/scheme/Projects", None, True) |
|
94 |
context['projects_labels'] = uris_labels |
|
95 |
# We get the subjectCorporateBodies'labels with the Organizations repository |
|
96 |
uri_list = [s.uri for s in self.object.subjectCorporateBodies.all()] |
|
97 |
uris_labels = get_labels_for_uris(uri_list, "http://www.iiep.unesco.org/plan4learning/scheme/Organizations", None, True) |
|
98 |
context['subjectCorporateBodies_labels'] = uris_labels |
|
99 |
# We get the corporateAuthors'labels with the Organizations repository |
|
100 |
uri_list = [s.uri for s in self.object.corporateAuthors.all()] |
|
101 |
uris_labels = get_labels_for_uris(uri_list, "http://www.iiep.unesco.org/plan4learning/scheme/Organizations", None, True) |
|
102 |
context['corporateAuthors_labels'] = uris_labels |
|
103 |
# We get the recordType'labels with the DocumentType repository |
|
104 |
if self.object.recordType: |
|
105 |
uri_list = [self.object.recordType] |
|
| 20 | 106 |
uris_labels = get_labels_for_uris(uri_list, "http://www.iiep.unesco.org/plan4learning/scheme/DocumentType", lang, False) |
| 17 | 107 |
context['recordType_label'] = uris_labels[self.object.recordType] |
| 7 | 108 |
|
| 17 | 109 |
return context |
| 7 | 110 |
|
| 34 | 111 |
class RecordEditView(DetailView): |
| 29 | 112 |
http_method_names = ['get'] |
113 |
template_name = 'p4l/record_update_form.html' |
|
| 34 | 114 |
model = Record |
115 |
slug_field = "identifier" |
|
116 |
slug_url_kwarg = "id" |
|
|
26
a0e152dd1fad
first version of angular intégration
ymh <ymh.work@gmail.com>
parents:
20
diff
changeset
|
117 |
|
| 34 | 118 |
def get_context_data(self, **kwargs): |
119 |
context = DetailView.get_context_data(self, **kwargs) |
|
120 |
# We get the language, "fr" by default |
|
121 |
lang = "fr" |
|
122 |
if "lang" in self.request.GET: |
|
123 |
lang = self.request.GET["lang"] |
|
124 |
elif hasattr(self.request, "LANGUAGE_CODE") and self.request.LANGUAGE_CODE in ["fr","en","es"]: |
|
125 |
lang = self.request.LANGUAGE_CODE |
|
126 |
||
127 |
uri_labels = get_labels_for_uris([s.uri for s in self.object.subjects.all()], "http://skos.um.es/unescothes/CS000", lang, False) |
|
128 |
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)) |
|
129 |
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)) |
|
130 |
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)) |
|
131 |
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)) |
|
132 |
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)) |
|
133 |
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)) |
|
134 |
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)) |
|
135 |
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)) |
|
136 |
||
137 |
context['uri_labels'] = json.dumps(uri_labels) |
|
138 |
||
139 |
return context |
|
140 |
||
141 |