Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
beat.web
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
beat
beat.web
Commits
41c3e35a
Commit
41c3e35a
authored
4 years ago
by
Samuel GAIST
Browse files
Options
Downloads
Patches
Plain Diff
[dataformats][api] Remove put implementation and use serializer in place
parent
f378b213
No related branches found
No related tags found
1 merge request
!327
Refactor update creation api
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
beat/web/dataformats/api.py
+25
-139
25 additions, 139 deletions
beat/web/dataformats/api.py
with
25 additions
and
139 deletions
beat/web/dataformats/api.py
+
25
−
139
View file @
41c3e35a
...
...
@@ -25,29 +25,24 @@
# #
###############################################################################
from
django.shortcuts
import
get_object_or_404
from
django.utils
import
six
from
django.core.exceptions
import
ValidationError
from
rest_framework.response
import
Response
from
rest_framework.exceptions
import
ParseError
from
rest_framework
import
serializers
from
.models
import
DataFormat
from
.serializers
import
DataFormatSerializer
from
.serializers
import
FullDataFormatSerializer
from
.serializers
import
DataFormatCreationSerializer
from
.serializers
import
DataFormatModSerializer
from
..common.responses
import
BadRequestResponse
,
ForbiddenResponse
from
..common.api
import
(
CheckContributionNameView
,
ShareView
,
ListContributionView
,
ListCreateContributionView
,
DiffView
,
RetrieveUpdateDestroyContributionView
)
from
..common.utils
import
validate_restructuredtext
,
ensure_html
from
..common.api
import
(
CheckContributionNameView
,
ShareView
,
ListContributionView
,
ListCreateContributionView
,
DiffView
,
RetrieveUpdateDestroyContributionView
,
)
import
simplejson
as
json
#----------------------------------------------------------
# ----------------------------------------------------------
class
CheckDataFormatNameView
(
CheckContributionNameView
):
...
...
@@ -55,10 +50,11 @@ class CheckDataFormatNameView(CheckContributionNameView):
This view sanitizes a data format name and
checks whether it is already used.
"""
model
=
DataFormat
#----------------------------------------------------------
#
----------------------------------------------------------
class
ShareDataFormatView
(
ShareView
):
...
...
@@ -66,166 +62,56 @@ class ShareDataFormatView(ShareView):
This view allows to share a data format with
other users and/or teams
"""
model
=
DataFormat
#----------------------------------------------------------
#
----------------------------------------------------------
class
ListDataFormatView
(
ListContributionView
):
"""
List all available data formats
"""
model
=
DataFormat
serializer_class
=
DataFormatSerializer
#----------------------------------------------------------
# ----------------------------------------------------------
class
ListCreateDataFormatsView
(
ListCreateContributionView
):
"""
Read/Write end point that list the data formats available
from a given author and allows the creation of new data formats
"""
model
=
DataFormat
serializer_class
=
DataFormatSerializer
writing_serializer_class
=
DataFormatCreationSerializer
namespace
=
'
api_dataformats
'
namespace
=
"
api_dataformats
"
#----------------------------------------------------------
#
----------------------------------------------------------
class
RetrieveUpdateDestroyDataFormatsView
(
RetrieveUpdateDestroyContributionView
):
"""
Read/Write/Delete endpoint for a given data format
"""
model
=
DataFormat
serializer_class
=
FullDataFormatSerializer
writing_serializer_class
=
DataFormatModSerializer
def
put
(
self
,
request
,
author_name
,
object_name
,
version
=
None
):
if
version
is
None
:
raise
serializers
.
ValidationError
({
'
version
'
:
'
A version number must be provided
'
})
try
:
data
=
request
.
data
except
ParseError
as
e
:
raise
serializers
.
ValidationError
({
'
data
'
:
str
(
e
)})
else
:
if
not
data
:
raise
serializers
.
ValidationError
({
'
data
'
:
'
Empty
'
})
if
'
short_description
'
in
data
:
if
not
(
isinstance
(
data
[
'
short_description
'
],
six
.
string_types
)):
raise
serializers
.
ValidationError
({
'
short_description
'
:
'
Invalid short_description data
'
})
short_description
=
data
[
'
short_description
'
]
else
:
short_description
=
None
if
'
description
'
in
data
:
if
not
(
isinstance
(
data
[
'
description
'
],
six
.
string_types
)):
raise
serializers
.
ValidationError
({
'
description
'
:
'
Invalid description data
'
})
description
=
data
[
'
description
'
]
try
:
validate_restructuredtext
(
description
)
except
ValidationError
as
errors
:
raise
serializers
.
ValidationError
({
'
description
'
:
[
error
for
error
in
errors
]})
else
:
description
=
None
if
'
declaration
'
in
data
:
if
isinstance
(
data
[
'
declaration
'
],
dict
):
json_declaration
=
data
[
'
declaration
'
]
declaration
=
json
.
dumps
(
json_declaration
,
indent
=
4
)
elif
isinstance
(
data
[
'
declaration
'
],
six
.
string_types
):
declaration
=
data
[
'
declaration
'
]
try
:
json_declaration
=
json
.
loads
(
declaration
)
except
:
raise
serializers
.
ValidationError
({
'
declaration
'
:
'
Invalid declaration data
'
})
else
:
raise
serializers
.
ValidationError
({
'
declaration
'
:
'
Invalid declaration data
'
})
if
'
#description
'
in
json_declaration
:
if
short_description
is
not
None
:
raise
serializers
.
ValidationError
({
'
short_description
'
:
'
A short description is already provided in the data format declaration
'
})
short_description
=
json_declaration
[
'
#description
'
]
elif
short_description
is
not
None
:
json_declaration
[
'
#description
'
]
=
short_description
declaration
=
json
.
dumps
(
json_declaration
,
indent
=
4
)
else
:
declaration
=
None
json_declaration
=
None
if
(
short_description
is
not
None
)
and
(
len
(
short_description
)
>
DataFormat
.
_meta
.
get_field
(
'
short_description
'
).
max_length
):
raise
serializers
.
ValidationError
({
'
short_description
'
:
'
Short description too long
'
})
# Process the query string
if
'
fields
'
in
request
.
GET
:
fields_to_return
=
request
.
GET
[
'
fields
'
].
split
(
'
,
'
)
else
:
# Available fields (not returned by default):
# - html_description
fields_to_return
=
[]
# Retrieve the data format
dataformat
=
get_object_or_404
(
DataFormat
,
author__username__iexact
=
author_name
,
name__iexact
=
object_name
,
version
=
version
)
# Check that the data format can still be modified (if applicable, the documentation
# can always be modified)
if
(
declaration
is
not
None
)
and
not
(
dataformat
.
modifiable
()):
return
ForbiddenResponse
(
"
The data format isn
'
t modifiable anymore (either shared with someone else, or needed by an attestation)
"
)
# Modification of the documentation
if
(
short_description
is
not
None
)
and
(
declaration
is
None
):
tmp_declaration
=
dataformat
.
declaration
tmp_declaration
[
'
#description
'
]
=
short_description
dataformat
.
declaration
=
tmp_declaration
if
description
is
not
None
:
dataformat
.
description
=
description
# Modification of the declaration
if
declaration
is
not
None
:
dataformat
.
declaration
=
declaration
# Save the data format model
try
:
dataformat
.
save
()
except
Exception
as
e
:
return
BadRequestResponse
(
str
(
e
))
# Nothing to return?
if
len
(
fields_to_return
)
==
0
:
return
Response
(
status
=
204
)
result
=
{}
# Retrieve the description in HTML format
if
'
html_description
'
in
fields_to_return
:
description
=
dataformat
.
description
if
len
(
description
)
>
0
:
result
[
'
html_description
'
]
=
ensure_html
(
description
)
else
:
result
[
'
html_description
'
]
=
''
return
Response
(
result
)
#----------------------------------------------------------
# ----------------------------------------------------------
class
DiffDataFormatView
(
DiffView
):
"""
This view shows the differences between two data formats
"""
model
=
DataFormat
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