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
0cea5f07
Commit
0cea5f07
authored
4 years ago
by
Samuel GAIST
Committed by
Samuel GAIST
4 years ago
Browse files
Options
Downloads
Patches
Plain Diff
[experiments][serializers] Implement ExperimentCreationSerializer
parent
3efeff1b
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/experiments/serializers.py
+99
-2
99 additions, 2 deletions
beat/web/experiments/serializers.py
with
99 additions
and
2 deletions
beat/web/experiments/serializers.py
+
99
−
2
View file @
0cea5f07
...
...
@@ -25,18 +25,115 @@
# #
###############################################################################
import
simplejson
as
json
import
beat.core
from
datetime
import
datetime
from
rest_framework
import
serializers
from
django.contrib.humanize.templatetags.humanize
import
naturaltime
from
django.db.models
import
CharField
,
Value
as
V
from
django.db.models.functions
import
Concat
from
..common.serializers
import
ShareableSerializer
from
..common.fields
import
JSONSerializerField
from
..common.utils
import
validate_restructuredtext
from
..common.exceptions
import
ContributionCreationError
from
..ui.templatetags.markup
import
restructuredtext
from
..toolchains.models
import
Toolchain
from
.models
import
Experiment
,
Block
from
datetime
import
datetime
import
simplejson
as
json
# ----------------------------------------------------------
class
ExperimentCreationSerializer
(
serializers
.
ModelSerializer
):
declaration
=
JSONSerializerField
()
description
=
serializers
.
CharField
(
required
=
False
,
allow_blank
=
True
)
fork_of
=
serializers
.
JSONField
(
required
=
False
)
toolchain
=
serializers
.
CharField
()
class
Meta
:
model
=
Experiment
beat_core_class
=
beat
.
core
.
experiment
fields
=
[
"
name
"
,
"
short_description
"
,
"
description
"
,
"
declaration
"
,
"
toolchain
"
,
"
fork_of
"
,
]
def
toolchain_queryset
(
self
):
user
=
self
.
context
.
get
(
"
user
"
)
return
Toolchain
.
objects
.
for_user
(
user
,
True
).
annotate
(
full_name
=
Concat
(
"
author__username
"
,
V
(
"
/
"
),
"
name
"
,
V
(
"
/
"
),
"
version
"
,
output_field
=
CharField
(),
)
)
def
validate_name
(
self
,
name
):
# sanitize_name is a static method of Versionable models but Experiment
# is currently not such a model
return
Toolchain
.
sanitize_name
(
name
)
def
validate_description
(
self
,
description
):
if
description
.
find
(
"
\\
"
)
>=
0
:
# was escaped, unescape
description
=
description
.
decode
(
"
string_escape
"
)
validate_restructuredtext
(
description
)
return
description
def
validate_toolchain
(
self
,
toolchain
):
if
not
self
.
toolchain_queryset
().
filter
(
full_name
=
toolchain
).
exists
():
raise
serializers
.
ValidationError
(
"
Invalid toolchain: {}
"
.
format
(
toolchain
))
return
toolchain
def
validate
(
self
,
data
):
user
=
self
.
context
.
get
(
"
user
"
)
name
=
data
[
"
name
"
]
toolchain
=
data
[
"
toolchain
"
]
if
self
.
Meta
.
model
.
objects
.
filter
(
author
=
user
,
name
=
name
,
toolchain
=
self
.
toolchain_queryset
().
get
(
full_name
=
toolchain
),
).
exists
():
raise
serializers
.
ValidationError
(
"
{} {} with toolchain {} already exists on this account
"
.
format
(
self
.
Meta
.
model
.
__name__
.
lower
(),
name
,
toolchain
)
)
return
data
def
create
(
self
,
validated_data
):
toolchain
=
self
.
toolchain_queryset
().
get
(
full_name
=
validated_data
[
"
toolchain
"
])
experiment
,
_
,
errors
=
Experiment
.
objects
.
create_experiment
(
author
=
self
.
context
.
get
(
"
user
"
),
toolchain
=
toolchain
,
name
=
validated_data
[
"
name
"
],
short_description
=
validated_data
.
get
(
"
short_description
"
,
""
),
description
=
validated_data
.
get
(
"
description
"
,
""
),
declaration
=
validated_data
[
"
declaration
"
],
)
if
errors
:
raise
ContributionCreationError
(
errors
)
return
experiment
# ----------------------------------------------------------
...
...
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