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
71082882
Commit
71082882
authored
4 years ago
by
Samuel GAIST
Committed by
Samuel GAIST
4 years ago
Browse files
Options
Downloads
Patches
Plain Diff
[databases][serializers] Pre-commit cleanup
parent
08c286fb
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
beat/web/databases/serializers.py
+54
-39
54 additions, 39 deletions
beat/web/databases/serializers.py
with
54 additions
and
39 deletions
beat/web/databases/serializers.py
+
54
−
39
View file @
71082882
...
...
@@ -37,6 +37,7 @@ from .exceptions import DatabaseCreationError
import
beat.core.database
class
DatabaseSerializer
(
VersionableSerializer
):
protocols
=
serializers
.
SerializerMethodField
()
description
=
serializers
.
SerializerMethodField
()
...
...
@@ -44,41 +45,42 @@ class DatabaseSerializer(VersionableSerializer):
class
Meta
(
VersionableSerializer
.
Meta
):
model
=
Database
exclude
=
[
'
description_file
'
,
'
declaration_file
'
,
'
source_code_file
'
]
exclude
=
[
"
description_file
"
,
"
declaration_file
"
,
"
source_code_file
"
]
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
(
DatabaseSerializer
,
self
).
__init__
(
*
args
,
**
kwargs
)
self
.
experiments_stats
=
{}
def
get_description
(
self
,
obj
):
return
obj
.
description
.
decode
(
'
utf8
'
)
return
obj
.
description
.
decode
(
"
utf8
"
)
def
get_protocols
(
self
,
obj
):
protocols_options
=
self
.
context
[
'
request
'
].
query_params
.
get
(
'
protocols_options
'
,
[])
include_datasets
=
'
datasets
'
in
protocols_options
include_outputs
=
'
outputs
'
in
protocols_options
protocols_options
=
self
.
context
[
"
request
"
].
query_params
.
get
(
"
protocols_options
"
,
[]
)
include_datasets
=
"
datasets
"
in
protocols_options
include_outputs
=
"
outputs
"
in
protocols_options
entries
=
[]
for
protocol
in
obj
.
protocols
.
all
():
protocol_entry
=
{
'
name
'
:
protocol
.
name
,
}
protocol_entry
=
{
"
name
"
:
protocol
.
name
}
if
include_datasets
:
protocol_entry
[
'
datasets
'
]
=
[]
protocol_entry
[
"
datasets
"
]
=
[]
for
dataset
in
protocol
.
sets
.
order_by
(
'
name
'
):
dataset_entry
=
{
'
name
'
:
dataset
.
name
,
}
for
dataset
in
protocol
.
sets
.
order_by
(
"
name
"
):
dataset_entry
=
{
"
name
"
:
dataset
.
name
}
if
include_outputs
:
dataset_entry
[
'
outputs
'
]
=
map
(
lambda
x
:
{
'
name
'
:
x
.
name
,
'
dataformat
'
:
x
.
dataformat
.
fullname
(),
},
dataset
.
template
.
outputs
.
order_by
(
'
name
'
))
dataset_entry
[
"
outputs
"
]
=
map
(
lambda
x
:
{
"
name
"
:
x
.
name
,
"
dataformat
"
:
x
.
dataformat
.
fullname
(),
},
dataset
.
template
.
outputs
.
order_by
(
"
name
"
),
)
protocol_entry
[
'
datasets
'
].
append
(
dataset_entry
)
protocol_entry
[
"
datasets
"
].
append
(
dataset_entry
)
entries
.
append
(
protocol_entry
)
return
entries
...
...
@@ -92,18 +94,25 @@ class DatabaseCreationSerializer(serializers.ModelSerializer):
class
Meta
:
model
=
Database
fields
=
[
'
name
'
,
'
short_description
'
,
'
description
'
,
'
declaration
'
,
'
code
'
,
'
previous_version
'
]
fields
=
[
"
name
"
,
"
short_description
"
,
"
description
"
,
"
declaration
"
,
"
code
"
,
"
previous_version
"
,
]
beat_core_class
=
beat
.
core
.
database
def
validate
(
self
,
data
):
user
=
self
.
context
.
get
(
'
user
'
)
name
=
self
.
Meta
.
model
.
sanitize_name
(
data
[
'
name
'
])
data
[
'
name
'
]
=
name
if
'
previous_version
'
in
data
:
previous_version_id
=
self
.
Meta
.
beat_core_class
.
Storage
(
settings
.
PREFIX
,
data
[
'
previous_version
'
])
user
=
self
.
context
.
get
(
"
user
"
)
name
=
self
.
Meta
.
model
.
sanitize_name
(
data
[
"
name
"
])
data
[
"
name
"
]
=
name
if
"
previous_version
"
in
data
:
previous_version_id
=
self
.
Meta
.
beat_core_class
.
Storage
(
settings
.
PREFIX
,
data
[
"
previous_version
"
]
)
else
:
previous_version_id
=
None
...
...
@@ -111,33 +120,39 @@ class DatabaseCreationSerializer(serializers.ModelSerializer):
if
previous_version_id
is
not
None
:
try
:
previous_version
=
self
.
Meta
.
model
.
objects
.
get
(
name
=
previous_version_id
.
name
,
version
=
previous_version_id
.
version
)
except
:
raise
serializers
.
ValidationError
(
"
Database
'
%s
'
not found
"
%
\
previous_version_id
.
fullname
)
name
=
previous_version_id
.
name
,
version
=
previous_version_id
.
version
)
except
Exception
:
raise
serializers
.
ValidationError
(
"
Database
'
%s
'
not found
"
%
previous_version_id
.
fullname
)
is_accessible
=
previous_version
.
accessibility_for
(
user
)
if
not
is_accessible
[
0
]:
raise
serializers
.
ValidationError
(
'
No access allowed
'
)
data
[
'
previous_version
'
]
=
previous_version
raise
serializers
.
ValidationError
(
"
No access allowed
"
)
data
[
"
previous_version
"
]
=
previous_version
# Determine the version number
last_version
=
None
if
previous_version_id
is
not
None
:
if
(
previous_version_id
.
name
==
name
):
last_version
=
self
.
Meta
.
model
.
objects
.
filter
(
name
=
name
).
order_by
(
'
-version
'
)[
0
]
if
previous_version_id
.
name
==
name
:
last_version
=
self
.
Meta
.
model
.
objects
.
filter
(
name
=
name
).
order_by
(
"
-version
"
)[
0
]
if
last_version
is
None
:
if
self
.
Meta
.
model
.
objects
.
filter
(
name
=
name
).
count
()
>
0
:
raise
serializers
.
ValidationError
(
'
This {} name already exists
'
.
format
(
self
.
Meta
.
model
.
__name__
.
lower
()))
raise
serializers
.
ValidationError
(
"
This {} name already exists
"
.
format
(
self
.
Meta
.
model
.
__name__
.
lower
()
)
)
data
[
'
version
'
]
=
(
last_version
.
version
+
1
if
last_version
is
not
None
else
1
)
data
[
"
version
"
]
=
last_version
.
version
+
1
if
last_version
is
not
None
else
1
return
data
def
create
(
self
,
validated_data
):
(
db_object
,
errors
)
=
self
.
Meta
.
model
.
objects
.
create_database
(
**
validated_data
)
if
errors
:
...
...
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