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
2caa6959
Commit
2caa6959
authored
4 years ago
by
Samuel GAIST
Browse files
Options
Downloads
Patches
Plain Diff
[code][api] Use mod serializer in place of custom put impementation
parent
26cb5c43
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/code/api.py
+0
-157
0 additions, 157 deletions
beat/web/code/api.py
with
0 additions
and
157 deletions
beat/web/code/api.py
+
0
−
157
View file @
2caa6959
...
@@ -25,25 +25,17 @@
...
@@ -25,25 +25,17 @@
# #
# #
###############################################################################
###############################################################################
from
django.utils
import
six
from
django.shortcuts
import
get_object_or_404
from
django.core.exceptions
import
ValidationError
from
rest_framework
import
generics
from
rest_framework
import
generics
from
rest_framework.response
import
Response
from
rest_framework.response
import
Response
from
rest_framework.exceptions
import
PermissionDenied
,
ParseError
from
rest_framework
import
serializers
from
..common.responses
import
ForbiddenResponse
from
..common.responses
import
ForbiddenResponse
from
..common.api
import
ShareView
,
RetrieveUpdateDestroyContributionView
from
..common.api
import
ShareView
,
RetrieveUpdateDestroyContributionView
from
..common.utils
import
validate_restructuredtext
,
ensure_html
from
..common.serializers
import
DiffSerializer
from
..common.serializers
import
DiffSerializer
from
..code.models
import
Code
from
..code.models
import
Code
from
.serializers
import
CodeSharingSerializer
,
CodeSerializer
from
.serializers
import
CodeSharingSerializer
,
CodeSerializer
import
simplejson
as
json
class
ShareCodeView
(
ShareView
):
class
ShareCodeView
(
ShareView
):
serializer_class
=
CodeSharingSerializer
serializer_class
=
CodeSharingSerializer
...
@@ -102,131 +94,6 @@ class RetrieveUpdateDestroyCodeView(RetrieveUpdateDestroyContributionView):
...
@@ -102,131 +94,6 @@ class RetrieveUpdateDestroyCodeView(RetrieveUpdateDestroyContributionView):
model
=
Code
model
=
Code
serializer_class
=
CodeSerializer
serializer_class
=
CodeSerializer
def
do_update
(
self
,
request
,
author_name
,
object_name
,
version
=
None
):
if
version
is
None
:
raise
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
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
Exception
:
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 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
)
>
self
.
model
.
_meta
.
get_field
(
"
short_description
"
).
max_length
):
raise
ValidationError
({
"
short_description
"
:
"
Short description too long
"
})
if
"
code
"
in
data
:
if
not
(
isinstance
(
data
[
"
code
"
],
six
.
string_types
)):
raise
ValidationError
({
"
code
"
:
"
Invalid code data
"
})
code
=
data
[
"
code
"
]
else
:
code
=
None
# Retrieve the object
db_object
=
get_object_or_404
(
self
.
model
,
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
)
or
(
code
is
not
None
))
and
not
(
db_object
.
modifiable
()
):
raise
PermissionDenied
(
"
The {} isn
'
t modifiable anymore (either shared with someone else, or needed by an attestation)
"
.
format
(
db_object
.
model_name
()
)
)
# Modification of the documentation
if
(
short_description
is
not
None
)
and
(
declaration
is
None
):
tmp_declaration
=
db_object
.
declaration
tmp_declaration
[
"
description
"
]
=
short_description
db_object
.
declaration
=
tmp_declaration
if
description
is
not
None
:
db_object
.
description
=
description
# Modification of the declaration
modified
=
False
if
declaration
is
not
None
:
db_object
.
declaration
=
declaration
modified
=
True
# Modification of the source code
if
code
is
not
None
:
db_object
.
source_code
=
code
modified
=
True
db_object
.
save
()
return
modified
,
db_object
def
get
(
self
,
request
,
*
args
,
**
kwargs
):
def
get
(
self
,
request
,
*
args
,
**
kwargs
):
db_object
=
self
.
get_object
()
db_object
=
self
.
get_object
()
...
@@ -256,27 +123,3 @@ class RetrieveUpdateDestroyCodeView(RetrieveUpdateDestroyContributionView):
...
@@ -256,27 +123,3 @@ class RetrieveUpdateDestroyCodeView(RetrieveUpdateDestroyContributionView):
serializer
=
self
.
get_serializer
(
db_object
,
fields
=
fields_to_return
)
serializer
=
self
.
get_serializer
(
db_object
,
fields
=
fields_to_return
)
return
Response
(
serializer
.
data
)
return
Response
(
serializer
.
data
)
def
put
(
self
,
request
,
author_name
,
object_name
,
version
=
None
):
(
modified
,
db_object
)
=
self
.
do_update
(
request
,
author_name
,
object_name
,
version
)
# Available fields (not returned by default):
# - html_description
if
"
fields
"
in
request
.
GET
:
fields_to_return
=
request
.
GET
[
"
fields
"
].
split
(
"
,
"
)
else
:
return
Response
(
status
=
204
)
result
=
{}
# Retrieve the description in HTML format
if
"
html_description
"
in
fields_to_return
:
description
=
db_object
.
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