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
2b543b4a
Commit
2b543b4a
authored
4 years ago
by
Samuel GAIST
Browse files
Options
Downloads
Patches
Plain Diff
[toolchains][api] Replace put with serializer
parent
b6c85776
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!327
Refactor update creation api
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
beat/web/toolchains/api.py
+2
-181
2 additions, 181 deletions
beat/web/toolchains/api.py
with
2 additions
and
181 deletions
beat/web/toolchains/api.py
+
2
−
181
View file @
2b543b4a
...
@@ -25,16 +25,6 @@
...
@@ -25,16 +25,6 @@
# #
# #
###############################################################################
###############################################################################
import
simplejson
as
json
from
django.conf
import
settings
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
import
serializers
from
rest_framework
import
exceptions
as
drf_exceptions
from
..common.api
import
(
from
..common.api
import
(
CheckContributionNameView
,
CheckContributionNameView
,
...
@@ -47,12 +37,9 @@ from .models import Toolchain
...
@@ -47,12 +37,9 @@ from .models import Toolchain
from
.serializers
import
ToolchainSerializer
from
.serializers
import
ToolchainSerializer
from
.serializers
import
FullToolchainSerializer
from
.serializers
import
FullToolchainSerializer
from
.serializers
import
ToolchainCreationSerializer
from
.serializers
import
ToolchainCreationSerializer
from
.serializers
import
ToolchainModSerializer
from
..common.api
import
ListContributionView
from
..common.api
import
ListContributionView
from
..common.utils
import
validate_restructuredtext
,
ensure_html
from
..experiments.models
import
Experiment
import
beat.core.toolchain
# ----------------------------------------------------------
# ----------------------------------------------------------
...
@@ -116,170 +103,4 @@ class RetrieveUpdateDestroyToolchainsView(RetrieveUpdateDestroyContributionView)
...
@@ -116,170 +103,4 @@ class RetrieveUpdateDestroyToolchainsView(RetrieveUpdateDestroyContributionView)
model
=
Toolchain
model
=
Toolchain
serializer_class
=
FullToolchainSerializer
serializer_class
=
FullToolchainSerializer
writing_serializer_class
=
ToolchainModSerializer
def
put
(
self
,
request
,
author_name
,
object_name
,
version
):
try
:
data
=
request
.
data
except
drf_exceptions
.
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
"
strict
"
in
data
:
strict
=
data
[
"
strict
"
]
else
:
strict
=
True
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
json
.
decoder
.
JSONDecodeError
:
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 toolchain declaration
"
}
)
short_description
=
json_declaration
[
"
description
"
]
elif
short_description
is
not
None
:
json_declaration
[
"
description
"
]
=
short_description
declaration
=
json
.
dumps
(
json_declaration
,
indent
=
4
)
toolchain_declaration
=
beat
.
core
.
toolchain
.
Toolchain
(
settings
.
PREFIX
,
json_declaration
)
if
not
toolchain_declaration
.
valid
:
if
strict
:
raise
serializers
.
ValidationError
(
{
"
declaration
"
:
toolchain_declaration
.
errors
}
)
else
:
declaration
=
None
if
(
short_description
is
not
None
)
and
(
len
(
short_description
)
>
self
.
model
.
_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
=
[
"
errors
"
]
# Retrieve the toolchain
dbtoolchain
=
get_object_or_404
(
Toolchain
,
author__username__iexact
=
author_name
,
name__iexact
=
object_name
,
version
=
version
,
)
# Check that the object can still be modified (if applicable, the
# documentation can always be modified)
if
declaration
is
not
None
and
not
dbtoolchain
.
modifiable
():
raise
drf_exceptions
.
PermissionDenied
(
"
The {} isn
'
t modifiable anymore (either shared with someone else, or needed by an attestation)
"
.
format
(
dbtoolchain
.
model_name
()
)
)
errors
=
None
# Modification of the short_description
if
(
short_description
is
not
None
)
and
(
declaration
is
None
):
tmp_declaration
=
dbtoolchain
.
declaration
tmp_declaration
[
"
description
"
]
=
short_description
dbtoolchain
.
declaration
=
tmp_declaration
# Modification of the description
if
description
is
not
None
:
dbtoolchain
.
description
=
description
# Modification of the declaration
if
declaration
is
not
None
:
errors
=
""
if
not
toolchain_declaration
.
valid
:
errors
=
"
* %s
"
%
"
\n
*
"
.
join
(
toolchain_declaration
.
errors
)
dbtoolchain
.
errors
=
errors
dbtoolchain
.
declaration
=
declaration
experiments
=
Experiment
.
objects
.
filter
(
toolchain
=
dbtoolchain
)
experiments
.
delete
()
# Save the toolchain model
try
:
dbtoolchain
.
save
()
except
Exception
as
e
:
raise
drf_exceptions
.
APIException
(
str
(
e
))
# Nothing to return?
if
len
(
fields_to_return
)
==
0
:
return
Response
(
status
=
204
)
result
=
{}
# Retrieve the errors (if necessary)
if
"
errors
"
in
fields_to_return
:
if
errors
:
result
[
"
errors
"
]
=
errors
else
:
result
[
"
errors
"
]
=
""
# Retrieve the description in HTML format (if necessary)
if
"
html_description
"
in
fields_to_return
:
description
=
dbtoolchain
.
description
if
len
(
description
)
>
0
:
result
[
"
html_description
"
]
=
ensure_html
(
description
)
else
:
result
[
"
html_description
"
]
=
""
return
Response
(
result
)
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