| author | ymh <ymh.work@gmail.com> |
| Wed, 04 Sep 2013 12:19:06 +0200 | |
| changeset 29 | 3a3b90b1abb2 |
| parent 27 | d2a40f44cbb9 |
| child 30 | a84e31f1f223 |
| 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 |
| 7 | 10 |
from django.shortcuts import get_object_or_404 |
| 24 | 11 |
from django.utils.decorators import method_decorator |
|
26
a0e152dd1fad
first version of angular intégration
ymh <ymh.work@gmail.com>
parents:
20
diff
changeset
|
12 |
from django.views.generic import ListView, DetailView, TemplateView, UpdateView |
|
a0e152dd1fad
first version of angular intégration
ymh <ymh.work@gmail.com>
parents:
20
diff
changeset
|
13 |
from p4l.forms import RecordFilterForm |
|
a0e152dd1fad
first version of angular intégration
ymh <ymh.work@gmail.com>
parents:
20
diff
changeset
|
14 |
from p4l.models import Record |
|
a0e152dd1fad
first version of angular intégration
ymh <ymh.work@gmail.com>
parents:
20
diff
changeset
|
15 |
from p4l.utils import get_labels_for_uris |
| 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" |
|
55 |
slug_field = "uri" # Even if it is useless because we override get_objet |
|
56 |
||
| 24 | 57 |
@method_decorator(login_required) |
58 |
def dispatch(self, *args, **kwargs): |
|
59 |
return super(RecordDetailView, self).dispatch(*args, **kwargs) |
|
60 |
||
| 7 | 61 |
def get_object(self, queryset=None): |
62 |
if "uri" not in self.request.GET: |
|
63 |
raise AttributeError(u"Record view must be called uri GET parameter") |
|
| 17 | 64 |
return get_object_or_404(Record.objects.select_related("language"), uri=self.request.GET["uri"]) |
65 |
||
66 |
def get_context_data(self, **kwargs): |
|
67 |
context = DetailView.get_context_data(self, **kwargs) |
|
| 20 | 68 |
# We get the language, "fr" by default |
69 |
lang = "fr" |
|
70 |
if "lang" in self.request.GET: |
|
71 |
lang = self.request.GET["lang"] |
|
72 |
elif hasattr(self.request, "LANGUAGE_CODE") and self.request.LANGUAGE_CODE in ["fr","en","es"]: |
|
73 |
lang = self.request.LANGUAGE_CODE |
|
| 17 | 74 |
# self.object is the record entry |
75 |
# We get the subjects'labels with the Thesaurus repository |
|
76 |
uri_list = [s.uri for s in self.object.subjects.all()] |
|
| 20 | 77 |
uris_labels = get_labels_for_uris(uri_list, "http://skos.um.es/unescothes/CS000", lang, False) |
| 17 | 78 |
context['subjects_labels'] = uris_labels |
79 |
# We get the themes'labels with the Themes repository |
|
80 |
uri_list = [s.uri for s in self.object.themes.all()] |
|
| 20 | 81 |
uris_labels = get_labels_for_uris(uri_list, "http://www.iiep.unesco.org/plan4learning/scheme/Themes", lang, False) |
| 17 | 82 |
context['themes_labels'] = uris_labels |
83 |
# We get the countries'labels with the Thesaurus repository |
|
84 |
uri_list = [s.uri for s in self.object.countries.all()] |
|
| 20 | 85 |
uris_labels = get_labels_for_uris(uri_list, "http://skos.um.es/unescothes/CS000/Countries", lang, False) |
| 17 | 86 |
context['countries_labels'] = uris_labels |
87 |
# We get the languages'labels with the Languages repository |
|
88 |
if self.object.language: |
|
89 |
uri_list = [self.object.language.uri] |
|
| 20 | 90 |
uris_labels = get_labels_for_uris(uri_list, "http://www.iiep.unesco.org/plan4learning/scheme/Languages", lang, False) |
| 17 | 91 |
context['language_label'] = uris_labels[self.object.language.uri] |
92 |
# We get the other languages'labels with the Languages repository |
|
93 |
uri_list = [s.uri for s in self.object.otherLanguages.all()] |
|
| 20 | 94 |
uris_labels = get_labels_for_uris(uri_list, "http://www.iiep.unesco.org/plan4learning/scheme/Languages", lang, False) |
| 17 | 95 |
context['otherLanguages_labels'] = uris_labels |
96 |
# We get the project'labels with the Projects repository |
|
97 |
uri_list = [s.uri for s in self.object.projectNames.all()] |
|
98 |
uris_labels = get_labels_for_uris(uri_list, "http://www.iiep.unesco.org/plan4learning/scheme/Projects", None, True) |
|
99 |
context['projects_labels'] = uris_labels |
|
100 |
# We get the subjectCorporateBodies'labels with the Organizations repository |
|
101 |
uri_list = [s.uri for s in self.object.subjectCorporateBodies.all()] |
|
102 |
uris_labels = get_labels_for_uris(uri_list, "http://www.iiep.unesco.org/plan4learning/scheme/Organizations", None, True) |
|
103 |
context['subjectCorporateBodies_labels'] = uris_labels |
|
104 |
# We get the corporateAuthors'labels with the Organizations repository |
|
105 |
uri_list = [s.uri for s in self.object.corporateAuthors.all()] |
|
106 |
uris_labels = get_labels_for_uris(uri_list, "http://www.iiep.unesco.org/plan4learning/scheme/Organizations", None, True) |
|
107 |
context['corporateAuthors_labels'] = uris_labels |
|
108 |
# We get the recordType'labels with the DocumentType repository |
|
109 |
if self.object.recordType: |
|
110 |
uri_list = [self.object.recordType] |
|
| 20 | 111 |
uris_labels = get_labels_for_uris(uri_list, "http://www.iiep.unesco.org/plan4learning/scheme/DocumentType", lang, False) |
| 17 | 112 |
context['recordType_label'] = uris_labels[self.object.recordType] |
| 7 | 113 |
|
| 17 | 114 |
return context |
| 7 | 115 |
|
|
26
a0e152dd1fad
first version of angular intégration
ymh <ymh.work@gmail.com>
parents:
20
diff
changeset
|
116 |
|
| 29 | 117 |
class RecordEditView(TemplateView): |
118 |
http_method_names = ['get'] |
|
119 |
template_name = 'p4l/record_update_form.html' |
|
|
26
a0e152dd1fad
first version of angular intégration
ymh <ymh.work@gmail.com>
parents:
20
diff
changeset
|
120 |
|
| 29 | 121 |
# class RecordEditView(UpdateView): |
122 |
# model = Record |
|
123 |
# template_name_suffix = '_update_form' |
|
124 |
# slug_field = "identifier" |
|
125 |
# slug_url_kwarg = "id" |
|
|
26
a0e152dd1fad
first version of angular intégration
ymh <ymh.work@gmail.com>
parents:
20
diff
changeset
|
126 |