Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Backend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Analyze
Contributor analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Arkindex
Backend
Commits
f4c862a8
Commit
f4c862a8
authored
5 years ago
by
Erwan Rouchet
Committed by
Bastien Abadie
5 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Fix type filtering when creating elements
parent
16b2801c
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
VERSION
+1
-1
1 addition, 1 deletion
VERSION
arkindex/documents/serializers/elements.py
+8
-11
8 additions, 11 deletions
arkindex/documents/serializers/elements.py
arkindex/documents/tests/test_elements_api.py
+42
-4
42 additions, 4 deletions
arkindex/documents/tests/test_elements_api.py
with
51 additions
and
16 deletions
VERSION
+
1
−
1
View file @
f4c862a8
0.9.8-dev
3
0.9.8-dev
4
This diff is collapsed.
Click to expand it.
arkindex/documents/serializers/elements.py
+
8
−
11
View file @
f4c862a8
...
...
@@ -132,12 +132,7 @@ class ElementCreateSerializer(ElementLightSerializer):
"""
Serialize an Element with a possible parent and image
"""
type
=
serializers
.
SlugRelatedField
(
slug_field
=
'
slug
'
,
source
=
'
new_type
'
,
queryset
=
ElementNewType
.
objects
.
none
(),
)
type
=
serializers
.
SlugField
(
source
=
'
new_type.slug
'
)
corpus
=
serializers
.
PrimaryKeyRelatedField
(
queryset
=
Corpus
.
objects
.
none
())
image
=
serializers
.
PrimaryKeyRelatedField
(
queryset
=
Image
.
objects
.
all
().
prefetch_related
(
'
server
'
),
...
...
@@ -160,16 +155,18 @@ class ElementCreateSerializer(ElementLightSerializer):
corpora
=
Corpus
.
objects
.
writable
(
self
.
context
[
'
request
'
].
user
)
self
.
fields
[
'
corpus
'
].
queryset
=
corpora
self
.
fields
[
'
parent
'
].
queryset
=
Element
.
objects
.
filter
(
corpus__in
=
corpora
)
self
.
fields
[
'
type
'
].
queryset
=
ElementNewType
.
objects
.
filter
(
corpus__in
=
corpora
)
def
validate
(
self
,
data
):
errors
=
defaultdict
(
list
)
data
=
super
().
validate
(
data
)
if
data
[
'
new_type
'
].
corpus
!=
data
[
'
corpus
'
]:
errors
[
'
type
'
].
append
(
'
Type and element must be in the same corpus
'
)
try
:
data
[
'
new_type
'
]
=
data
[
'
corpus
'
].
types
.
get
(
slug
=
data
[
'
new_type
'
][
'
slug
'
])
except
ElementNewType
.
DoesNotExist
:
# Fail immediately here because the type is required for other checks
raise
ValidationError
({
'
type
'
:
[
'
Type {!r} not found in corpus {}
'
.
format
(
data
[
'
new_type
'
][
'
slug
'
],
data
[
'
corpus
'
].
name
)]
})
parent
=
data
.
get
(
'
parent
'
)
if
parent
and
parent
.
corpus
!=
data
[
'
corpus
'
]:
...
...
This diff is collapsed.
Click to expand it.
arkindex/documents/tests/test_elements_api.py
+
42
−
4
View file @
f4c862a8
...
...
@@ -230,6 +230,8 @@ class TestElementsAPI(FixtureAPITestCase):
def
test_create_parent_different_corpus
(
self
):
self
.
client
.
force_login
(
self
.
user
)
new_corpus
=
Corpus
.
objects
.
create
(
name
=
'
new
'
)
new_corpus
.
corpus_right
.
create
(
user
=
self
.
user
,
can_write
=
True
)
new_corpus
.
create_default_himanis_types
()
request
=
self
.
make_create_request
(
parent
=
str
(
self
.
vol
.
id
),
corpus
=
str
(
new_corpus
.
id
),
...
...
@@ -241,7 +243,43 @@ class TestElementsAPI(FixtureAPITestCase):
)
response
=
self
.
client
.
post
(
**
request
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_400_BAD_REQUEST
)
self
.
assertIn
(
'
corpus
'
,
response
.
json
())
self
.
assertDictEqual
(
response
.
json
(),
{
'
corpus
'
:
[
'
Parent and child must be in the same corpus
'
]
})
def
test_create_filter_type
(
self
):
"""
Ensure types are filtered by corpus
"""
self
.
assertTrue
(
self
.
corpus
.
types
.
filter
(
slug
=
'
volume
'
).
exists
())
self
.
client
.
force_login
(
self
.
user
)
new_corpus
=
Corpus
.
objects
.
create
(
name
=
'
new
'
)
new_corpus
.
corpus_right
.
create
(
user
=
self
.
user
,
can_write
=
True
)
new_vol_type
=
new_corpus
.
types
.
create
(
slug
=
'
volume
'
,
display_name
=
'
Volume
'
,
folder
=
True
)
request
=
self
.
make_create_request
(
corpus
=
str
(
new_corpus
.
id
),
elt_type
=
'
volume
'
,
name
=
'
something
'
,
)
response
=
self
.
client
.
post
(
**
request
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_201_CREATED
)
new_vol
=
new_corpus
.
elements
.
get
()
self
.
assertEqual
(
new_vol
.
new_type
,
new_vol_type
)
self
.
assertEqual
(
new_vol
.
new_type
.
corpus
,
new_corpus
)
def
test_create_wrong_type
(
self
):
self
.
assertFalse
(
self
.
corpus
.
types
.
filter
(
slug
=
'
kartoffelsalad
'
).
exists
())
self
.
client
.
force_login
(
self
.
user
)
request
=
self
.
make_create_request
(
corpus
=
str
(
self
.
corpus
.
id
),
elt_type
=
'
kartoffelsalad
'
,
name
=
'
something
'
,
)
response
=
self
.
client
.
post
(
**
request
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_400_BAD_REQUEST
)
self
.
assertDictEqual
(
response
.
json
(),
{
'
type
'
:
[
"
Type
'
kartoffelsalad
'
not found in corpus Unit Tests
"
],
})
def
test_create_wrong_image
(
self
):
self
.
client
.
force_login
(
self
.
user
)
...
...
@@ -261,9 +299,9 @@ class TestElementsAPI(FixtureAPITestCase):
# Parent is not a volume
self
.
client
.
force_login
(
self
.
user
)
request
=
self
.
make_create_request
(
parent
=
str
(
self
.
page
.
id
),
elt_type
=
'
act
'
,
metadata
=
{
'
folio
'
:
'
new
'
},
parent
=
str
(
self
.
page
.
id
),
elt_type
=
'
act
'
,
metadata
=
{
'
folio
'
:
'
new
'
},
)
response
=
self
.
client
.
post
(
**
request
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_400_BAD_REQUEST
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment