|
16
|
1 |
# -*- coding: utf-8 -*- |
|
126
|
2 |
# |
|
|
3 |
# Copyright IRI (2013) |
|
|
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 |
# |
|
16
|
33 |
|
|
47
|
34 |
from django.core.exceptions import ValidationError |
|
48
|
35 |
from p4l.models import Record |
|
16
|
36 |
from rest_framework import serializers |
|
47
|
37 |
import logging |
|
48
|
38 |
from rest_framework.serializers import NestedValidationError |
|
|
39 |
import copy |
|
|
40 |
from django.db.models.fields.related import ForeignKey |
|
47
|
41 |
|
|
|
42 |
logger = logging.getLogger(__name__) |
|
|
43 |
|
|
|
44 |
class ThesaurusSerializer(serializers.SlugRelatedField): |
|
|
45 |
|
|
|
46 |
def __init__(self, *args, **kwargs): |
|
|
47 |
kwargs.update({ |
|
|
48 |
'read_only': False, |
|
|
49 |
}) |
|
|
50 |
serializers.SlugRelatedField.__init__(self, *args, **kwargs) |
|
|
51 |
|
|
|
52 |
def from_native(self, data): |
|
|
53 |
if self.queryset is None: |
|
|
54 |
raise Exception('Writable related fields must include a `queryset` argument') |
|
|
55 |
try: |
|
|
56 |
res, _ = self.queryset.get_or_create(**{self.slug_field: data}) |
|
|
57 |
return res |
|
|
58 |
except (TypeError, ValueError): |
|
|
59 |
msg = self.error_messages['invalid'] |
|
|
60 |
raise ValidationError(msg) |
|
|
61 |
|
|
16
|
62 |
|
|
48
|
63 |
class P4lModelSerializer(serializers.ModelSerializer): |
|
|
64 |
|
|
16
|
65 |
|
|
48
|
66 |
def get_nested_field(self, model_field, related_model, to_many): |
|
|
67 |
field_exclude = () |
|
|
68 |
if model_field is None: |
|
|
69 |
fk = [f.name for f in related_model._meta.fields if (isinstance(f, ForeignKey) and f.rel.to == Record)][0] |
|
|
70 |
field_exclude = ('id', fk) |
|
|
71 |
class NestedModelSerializer(P4lModelSerializer): |
|
|
72 |
class Meta: |
|
|
73 |
model = related_model |
|
|
74 |
depth = self.opts.depth - 1 |
|
|
75 |
exclude = field_exclude |
|
16
|
76 |
|
|
48
|
77 |
return NestedModelSerializer(many=to_many) |
|
16
|
78 |
|
|
48
|
79 |
|
|
|
80 |
def field_from_native(self, data, files, field_name, into): |
|
|
81 |
""" |
|
|
82 |
Override default so that the serializer can be used as a writable |
|
|
83 |
nested field across relationships. |
|
|
84 |
""" |
|
|
85 |
if self.read_only: |
|
|
86 |
return |
|
16
|
87 |
|
|
48
|
88 |
try: |
|
|
89 |
value = data[field_name] |
|
|
90 |
except KeyError: |
|
|
91 |
if self.default is not None and not self.partial: |
|
|
92 |
# Note: partial updates shouldn't set defaults |
|
|
93 |
value = copy.deepcopy(self.default) |
|
|
94 |
else: |
|
|
95 |
if self.required: |
|
|
96 |
raise ValidationError(self.error_messages['required']) |
|
|
97 |
return |
|
16
|
98 |
|
|
48
|
99 |
# Set the serializer object if it exists |
|
|
100 |
obj = getattr(self.parent.object, field_name) if self.parent.object else None |
|
16
|
101 |
|
|
48
|
102 |
if self.source == '*': |
|
|
103 |
if value: |
|
|
104 |
into.update(value) |
|
|
105 |
else: |
|
|
106 |
if value in (None, ''): |
|
|
107 |
into[(self.source or field_name)] = None |
|
|
108 |
elif self.many and hasattr(value, '__iter__'): |
|
117
|
109 |
if obj is not None: |
|
|
110 |
obj.all().delete() |
|
48
|
111 |
nested_items = [] |
|
|
112 |
for val in value: |
|
|
113 |
kwargs = { |
|
|
114 |
'instance': None, |
|
|
115 |
'data': val, |
|
|
116 |
'context': self.context, |
|
|
117 |
'partial': self.partial, |
|
|
118 |
'many': False |
|
|
119 |
} |
|
|
120 |
serializer = self.__class__(**kwargs) |
|
|
121 |
nested_items.append(serializer.from_native(val, files)) |
|
|
122 |
into[self.source or field_name] = nested_items |
|
|
123 |
else: |
|
|
124 |
kwargs = { |
|
|
125 |
'instance': obj, |
|
|
126 |
'data': value, |
|
|
127 |
'context': self.context, |
|
|
128 |
'partial': self.partial, |
|
|
129 |
'many': self.many |
|
|
130 |
} |
|
|
131 |
serializer = self.__class__(**kwargs) |
|
16
|
132 |
|
|
48
|
133 |
if serializer.is_valid(): |
|
|
134 |
into[self.source or field_name] = serializer.object |
|
|
135 |
else: |
|
|
136 |
# Propagate errors up to our parent |
|
|
137 |
raise NestedValidationError(serializer.errors) |
|
16
|
138 |
|
|
47
|
139 |
|
|
48
|
140 |
|
|
|
141 |
|
|
|
142 |
|
|
|
143 |
#class RecordSerializer(serializers.ModelSerializer): |
|
|
144 |
class RecordSerializer(P4lModelSerializer): |
|
16
|
145 |
''' |
|
|
146 |
Serializer for record |
|
|
147 |
''' |
|
93
|
148 |
language = ThesaurusSerializer(many=False, required=False, slug_field='uri') |
|
117
|
149 |
otherLanguages = ThesaurusSerializer(many=True, required=False, slug_field='uri') |
|
|
150 |
subjects = ThesaurusSerializer(many=True, required=False, slug_field='uri') |
|
|
151 |
themes = ThesaurusSerializer(many=True, required=False, slug_field='uri') |
|
|
152 |
countries = ThesaurusSerializer(many=True, required=False, slug_field='uri') |
|
|
153 |
projectNames = ThesaurusSerializer(many=True, required=False, slug_field='uri') |
|
|
154 |
subjectCorporateBodies = ThesaurusSerializer(many=True, required=False, slug_field='uri') |
|
|
155 |
corporateAuthors = ThesaurusSerializer(many=True, required=False, slug_field='uri') |
|
|
156 |
audiences = ThesaurusSerializer(many=True, required=False, slug_field='uri') |
|
16
|
157 |
|
|
|
158 |
class Meta: |
|
|
159 |
model = Record |
|
|
160 |
depth = 1 |
|
47
|
161 |
fields = ("identifier", "uri", "subjects", "notes", "otherLanguages", |
|
|
162 |
"language", "editionStatement", "recordType", "isDocumentPart", |
|
105
|
163 |
"hidden", "restricted", "themes", "countries", "projectNames", |
|
98
|
164 |
"subjectCorporateBodies", "corporateAuthors", "corporateAuthorLabel", |
|
|
165 |
"imprints", "titles", "addedTitles", "issns", "isbns", "documentCodes", |
|
48
|
166 |
"abstracts", "titlesMainDocument", "collations", "volumeIssues", |
|
|
167 |
"periodicals", "meetings", "subjectMeetings", "series", |
|
98
|
168 |
"authors", "subjectPersons", "urls", "audiences") |
|
16
|
169 |
|
|
47
|
170 |
|