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
77f788c4
Commit
77f788c4
authored
4 years ago
by
Samuel GAIST
Browse files
Options
Downloads
Patches
Plain Diff
[algorithms][api] Replace put with serializer
parent
2784629d
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/algorithms/api.py
+38
-36
38 additions, 36 deletions
beat/web/algorithms/api.py
with
38 additions
and
36 deletions
beat/web/algorithms/api.py
+
38
−
36
View file @
77f788c4
...
...
@@ -29,26 +29,28 @@ from django.http import Http404
from
django.http
import
HttpResponse
from
django.http
import
HttpResponseForbidden
from
django.http
import
HttpResponseBadRequest
from
django.http
import
HttpResponseNotAllowed
from
django.shortcuts
import
get_object_or_404
from
django.conf
import
settings
import
os
from
.models
import
Algorithm
from
.serializers
import
AlgorithmSerializer
from
.serializers
import
FullAlgorithmSerializer
from
.serializers
import
AlgorithmCreationSerializer
from
.serializers
import
AlgorithmModSerializer
from
..code.api
import
ShareCodeView
,
RetrieveUpdateDestroyCodeView
from
..code.serializers
import
CodeDiffSerializer
from
..common.api
import
(
CheckContributionNameView
,
ListContributionView
,
ListCreateContributionView
)
from
..common.api
import
(
CheckContributionNameView
,
ListContributionView
,
ListCreateContributionView
,
)
from
..code.api
import
DiffView
#----------------------------------------------------------
#
----------------------------------------------------------
class
CheckAlgorithmNameView
(
CheckContributionNameView
):
...
...
@@ -56,10 +58,11 @@ class CheckAlgorithmNameView(CheckContributionNameView):
This view sanitizes an algorithm name and
checks whether it is already used.
"""
model
=
Algorithm
#----------------------------------------------------------
#
----------------------------------------------------------
class
ShareAlgorithmView
(
ShareCodeView
):
...
...
@@ -67,21 +70,23 @@ class ShareAlgorithmView(ShareCodeView):
This view allows to share an algorithm with
other users and/or teams
"""
model
=
Algorithm
#----------------------------------------------------------
#
----------------------------------------------------------
class
ListAlgorithmsView
(
ListContributionView
):
"""
List all available algorithms
"""
model
=
Algorithm
serializer_class
=
AlgorithmSerializer
#----------------------------------------------------------
#
----------------------------------------------------------
class
ListCreateAlgorithmsView
(
ListCreateContributionView
):
...
...
@@ -89,13 +94,14 @@ class ListCreateAlgorithmsView(ListCreateContributionView):
Read/Write end point that list the algorithms available
from a given author and allows the creation of new algorithms
"""
model
=
Algorithm
serializer_class
=
AlgorithmSerializer
writing_serializer_class
=
AlgorithmCreationSerializer
namespace
=
'
api_algorithms
'
namespace
=
"
api_algorithms
"
#----------------------------------------------------------
#
----------------------------------------------------------
class
RetrieveUpdateDestroyAlgorithmsView
(
RetrieveUpdateDestroyCodeView
):
...
...
@@ -105,39 +111,30 @@ class RetrieveUpdateDestroyAlgorithmsView(RetrieveUpdateDestroyCodeView):
model
=
Algorithm
serializer_class
=
FullAlgorithmSerializer
writing_serializer_class
=
AlgorithmModSerializer
def
do_update
(
self
,
request
,
author_name
,
object_name
,
version
=
None
):
modified
,
algorithm
=
super
(
RetrieveUpdateDestroyAlgorithmsView
,
self
).
do_update
(
request
,
author_name
,
object_name
,
version
)
if
modified
:
# Delete existing experiments using the algorithm (code changed)
experiments
=
list
(
set
(
map
(
lambda
x
:
x
.
experiment
,
algorithm
.
blocks
.
iterator
())))
for
experiment
in
experiments
:
experiment
.
delete
()
return
modified
,
algorithm
#----------------------------------------------------------
# ----------------------------------------------------------
class
DiffAlgorithmView
(
DiffView
):
"""
This view shows the differences between two algorithms
"""
model
=
Algorithm
serializer_class
=
CodeDiffSerializer
#----------------------------------------------------------
#
----------------------------------------------------------
def
binary
(
request
,
author_name
,
object_name
,
version
=
None
):
"""
Returns the shared library of a binary algorithm
"""
if
request
.
method
not
in
[
'
GET
'
,
'
POST
'
]:
return
HttpResponseNotAllowed
([
'
GET
'
,
'
POST
'
])
if
request
.
method
not
in
[
"
GET
"
,
"
POST
"
]:
return
HttpResponseNotAllowed
([
"
GET
"
,
"
POST
"
])
# Retrieves the algorithm
if
version
:
...
...
@@ -148,8 +145,9 @@ def binary(request, author_name, object_name, version=None):
version
=
int
(
version
),
)
else
:
algorithm
=
Algorithm
.
objects
.
filter
(
author__username__iexact
=
author_name
,
name__iexact
=
object_name
).
order_by
(
'
-version
'
)
algorithm
=
Algorithm
.
objects
.
filter
(
author__username__iexact
=
author_name
,
name__iexact
=
object_name
).
order_by
(
"
-version
"
)
if
not
algorithm
:
raise
Http404
()
else
:
...
...
@@ -158,18 +156,22 @@ def binary(request, author_name, object_name, version=None):
if
not
algorithm
.
is_binary
():
raise
Http404
()
if
request
.
method
==
'
GET
'
:
(
has_access
,
_
,
accessibility
)
=
algorithm
.
accessibility_for
(
request
.
user
,
without_usable
=
True
)
if
request
.
method
==
"
GET
"
:
(
has_access
,
_
,
accessibility
)
=
algorithm
.
accessibility_for
(
request
.
user
,
without_usable
=
True
)
if
not
has_access
:
raise
Http404
()
binary_data
=
algorithm
.
source_code
response
=
HttpResponse
(
binary_data
,
content_type
=
'
application/octet-stream
'
)
response
=
HttpResponse
(
binary_data
,
content_type
=
"
application/octet-stream
"
)
response
[
'
Content-Length
'
]
=
len
(
binary_data
)
response
[
'
Content-Disposition
'
]
=
'
attachment; filename=%d.so
'
%
algorithm
.
version
response
[
"
Content-Length
"
]
=
len
(
binary_data
)
response
[
"
Content-Disposition
"
]
=
(
"
attachment; filename=%d.so
"
%
algorithm
.
version
)
return
response
...
...
@@ -177,12 +179,12 @@ def binary(request, author_name, object_name, version=None):
if
request
.
user
.
is_anonymous
()
or
(
request
.
user
.
username
!=
author_name
):
return
HttpResponseForbidden
()
if
'
binary
'
not
in
request
.
FILES
:
if
"
binary
"
not
in
request
.
FILES
:
return
HttpResponseBadRequest
()
file
=
request
.
FILES
[
'
binary
'
]
file
=
request
.
FILES
[
"
binary
"
]
binary_data
=
b
''
binary_data
=
b
""
for
chunk
in
file
.
chunks
():
binary_data
+=
chunk
...
...
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