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
d4482d70
Commit
d4482d70
authored
4 years ago
by
Samuel GAIST
Browse files
Options
Downloads
Patches
Plain Diff
[plotters][serializers] Pre-commit cleanup
parent
98397917
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/plotters/serializers.py
+164
-84
164 additions, 84 deletions
beat/web/plotters/serializers.py
with
164 additions
and
84 deletions
beat/web/plotters/serializers.py
+
164
−
84
View file @
d4482d70
...
...
@@ -25,20 +25,23 @@
# #
###############################################################################
from
..common.serializers
import
DynamicFieldsSerializer
,
ContributionSerializer
,
ContributionCreationSerializer
from
..common.serializers
import
(
DynamicFieldsSerializer
,
ContributionSerializer
,
ContributionCreationSerializer
,
)
from
.models
import
Plotter
,
PlotterParameter
,
DefaultPlotter
from
rest_framework
import
serializers
from
..code.serializers
import
CodeSerializer
,
CodeCreationSerializer
from
..libraries.serializers
import
LibraryReferenceSerializer
from
..dataformats.serializers
import
ReferencedDataFormatSerializer
from
django.utils.encoding
import
smart_str
from
django.utils.encoding
import
smart_text
import
beat.core.plotter
import
simplejson
as
json
class
PlotterSerializer
(
ContributionSerializer
):
dataformat
=
serializers
.
CharField
(
source
=
"
dataformat.fullname
"
)
...
...
@@ -46,34 +49,47 @@ class PlotterSerializer(ContributionSerializer):
class
Meta
(
ContributionSerializer
.
Meta
):
model
=
Plotter
default_fields
=
[
#'name', 'dataformat',
'
id
'
,
'
accessibility
'
,
'
modifiable
'
,
'
deletable
'
,
'
is_owner
'
,
'
name
'
,
'
dataformat
'
,
'
fork_of
'
,
'
last_version
'
,
'
previous_version
'
,
'
short_description
'
,
'
description
'
,
'
version
'
,
'
creation_date
'
,
'
data
'
,
'
sample_data
'
,
'
declaration
'
,
# 'name', 'dataformat',
"
id
"
,
"
accessibility
"
,
"
modifiable
"
,
"
deletable
"
,
"
is_owner
"
,
"
name
"
,
"
dataformat
"
,
"
fork_of
"
,
"
last_version
"
,
"
previous_version
"
,
"
short_description
"
,
"
description
"
,
"
version
"
,
"
creation_date
"
,
"
data
"
,
"
sample_data
"
,
"
declaration
"
,
]
class
PlotterParameterSerializer
(
ContributionSerializer
):
class
PlotterParameterSerializer
(
ContributionSerializer
):
class
Meta
(
ContributionSerializer
.
Meta
):
model
=
PlotterParameter
exclude
=
[]
default_fields
=
[
'
name
'
,
'
plotter
'
,
]
default_fields
=
[
"
name
"
,
"
plotter
"
]
class
DefaultPlotterSerializer
(
DynamicFieldsSerializer
):
dataformat
=
serializers
.
CharField
(
source
=
"
dataformat.fullname
"
)
plotter
=
serializers
.
CharField
(
source
=
"
plotter.fullname
"
)
parameter
=
serializers
.
CharField
(
source
=
"
parameter.fullname
"
)
plotter
=
serializers
.
CharField
(
source
=
"
plotter.fullname
"
)
parameter
=
serializers
.
CharField
(
source
=
"
parameter.fullname
"
)
class
Meta
(
DynamicFieldsSerializer
.
Meta
):
model
=
DefaultPlotter
exclude
=
[]
default_fields
=
[
'
dataformat
'
,
'
plotter
'
,
'
parameter
'
,
]
default_fields
=
[
"
dataformat
"
,
"
plotter
"
,
"
parameter
"
]
#----------------------------------------------------------
#
----------------------------------------------------------
class
PlotterCreationSerializer
(
CodeCreationSerializer
):
...
...
@@ -82,105 +98,126 @@ class PlotterCreationSerializer(CodeCreationSerializer):
beat_core_class
=
beat
.
core
.
plotter
#----------------------------------------------------------
#
----------------------------------------------------------
class
PlotterAllSerializer
(
CodeSerializer
):
dataformat
=
serializers
.
SerializerMethodField
()
declaration_file
=
serializers
.
SerializerMethodField
()
description_file
=
serializers
.
SerializerMethodField
()
source_code_file
=
serializers
.
SerializerMethodField
()
referenced_libraries
=
LibraryReferenceSerializer
(
many
=
True
)
dataformat
=
serializers
.
SerializerMethodField
()
declaration_file
=
serializers
.
SerializerMethodField
()
description_file
=
serializers
.
SerializerMethodField
()
source_code_file
=
serializers
.
SerializerMethodField
()
referenced_libraries
=
LibraryReferenceSerializer
(
many
=
True
)
class
Meta
(
CodeSerializer
.
Meta
):
model
=
Plotter
#----------------------------------------------------------
#
----------------------------------------------------------
class
FullPlotterSerializer
(
PlotterAllSerializer
):
class
Meta
(
PlotterAllSerializer
.
Meta
):
default_fields
=
PlotterAllSerializer
.
Meta
.
default_fields
+
PlotterAllSerializer
.
Meta
.
extra_fields
default_fields
=
(
PlotterAllSerializer
.
Meta
.
default_fields
+
PlotterAllSerializer
.
Meta
.
extra_fields
)
exclude
=
[]
#----------------------------------------------------------
# ----------------------------------------------------------
class
PlotterParameterCreationFailedException
(
Exception
):
pass
class
PlotterParameterCreationSerializer
(
ContributionCreationSerializer
):
data
=
serializers
.
JSONField
(
required
=
False
)
class
Meta
(
ContributionCreationSerializer
.
Meta
):
model
=
PlotterParameter
fields
=
[
'
name
'
,
'
plotter
'
,
'
data
'
,
'
version
'
,
'
previous_version
'
,
'
short_description
'
,
'
description
'
,
'
fork_of
'
]
#beat_core_class = beat.core.PlotterParameter
fields
=
[
"
name
"
,
"
plotter
"
,
"
data
"
,
"
version
"
,
"
previous_version
"
,
"
short_description
"
,
"
description
"
,
"
fork_of
"
,
]
# beat_core_class = beat.core.PlotterParameter
def
create
(
self
,
validated_data
):
plotterparameter
=
None
if
"
name
"
not
in
validated_data
:
raise
serializers
.
ValidationError
(
'
No name provided
'
)
raise
serializers
.
ValidationError
(
"
No name provided
"
)
try
:
plotterparameter
=
PlotterParameter
.
objects
.
get
(
author
=
self
.
context
[
'
request
'
].
user
,
name
=
validated_data
[
'
name
'
],
version
=
validated_data
[
'
version
'
])
except
:
pass
if
plotterparameter
is
not
None
:
raise
serializers
.
ValidationError
(
'
A plotterparameter with this name already exists
'
)
if
PlotterParameter
.
objects
.
filter
(
author
=
self
.
context
[
"
request
"
].
user
,
name
=
validated_data
[
"
name
"
],
version
=
validated_data
[
"
version
"
],
).
exists
():
raise
serializers
.
ValidationError
(
"
A plotterparameter with this name already exists
"
)
if
"
plotter
"
not
in
self
.
data
:
raise
serializers
.
ValidationError
(
'
No plotter provided
'
)
raise
serializers
.
ValidationError
(
"
No plotter provided
"
)
plotter
=
None
try
:
plotter
=
Plotter
.
objects
.
get
(
id
=
self
.
data
[
'
plotter
'
])
except
:
pass
if
plotter
is
None
:
raise
serializers
.
ValidationError
(
'
Required plotter does not exist
'
)
Plotter
.
objects
.
get
(
id
=
self
.
data
[
"
plotter
"
])
except
Exception
:
raise
serializers
.
ValidationError
(
"
Required plotter does not exist
"
)
if
"
data
"
not
in
validated_data
:
validated_data
[
'
data
'
]
=
{}
validated_data
[
"
data
"
]
=
{}
#Only create new version for latest version
#
Only create new version for latest version
if
"
previous_version
"
in
validated_data
:
if
validated_data
[
'
previous_version
'
].
version
<
validated_data
[
'
version
'
]
-
1
:
raise
serializers
.
ValidationError
(
'
A new version for this plotterparameter version already exist
'
)
#add description/short_description to new version
validated_data
[
'
short_description
'
]
=
validated_data
[
'
previous_version
'
].
short_description
validated_data
[
'
description
'
]
=
validated_data
[
'
previous_version
'
].
description
#Create fork
if
(
validated_data
[
"
previous_version
"
].
version
<
validated_data
[
"
version
"
]
-
1
):
raise
serializers
.
ValidationError
(
"
A new version for this plotterparameter version already exist
"
)
# add description/short_description to new version
validated_data
[
"
short_description
"
]
=
validated_data
[
"
previous_version
"
].
short_description
validated_data
[
"
description
"
]
=
validated_data
[
"
previous_version
"
].
description
# Create fork
if
"
fork_of
"
in
validated_data
:
#add description/short_description to new version
validated_data
[
'
short_description
'
]
=
validated_data
[
'
fork_of
'
].
short_description
validated_data
[
'
description
'
]
=
validated_data
[
'
fork_of
'
].
description
# add description/short_description to new version
validated_data
[
"
short_description
"
]
=
validated_data
[
"
fork_of
"
].
short_description
validated_data
[
"
description
"
]
=
validated_data
[
"
fork_of
"
].
description
plotterparameter
=
PlotterParameter
.
objects
.
create
(
**
validated_data
)
if
plotterparameter
is
None
:
raise
PlotterParameterCreationFailedException
()
return
plotterparameter
#----------------------------------------------------------
# ----------------------------------------------------------
class
PlotterParameterAllSerializer
(
ContributionSerializer
):
data
=
serializers
.
SerializerMethodField
()
description
=
serializers
.
SerializerMethodField
()
plotter
=
serializers
.
SerializerMethodField
()
data
=
serializers
.
SerializerMethodField
()
description
=
serializers
.
SerializerMethodField
()
plotter
=
serializers
.
SerializerMethodField
()
class
Meta
(
ContributionSerializer
.
Meta
):
model
=
PlotterParameter
#def get_referencing_experiments(self, obj):
#
def get_referencing_experiments(self, obj):
# user = self.context.get('user')
# experiments = obj.experiments.for_user(user, True).order_by('-creation_date')
...
...
@@ -193,25 +230,48 @@ class PlotterParameterAllSerializer(ContributionSerializer):
# return ordered_result
#def get_new_experiment_url(self, obj):
#
def get_new_experiment_url(self, obj):
# return obj.get_new_experiment_url()
#----------------------------------------------------------
# ----------------------------------------------------------
class
FullPlotterParameterSerializer
(
PlotterParameterAllSerializer
):
plotters
=
serializers
.
SerializerMethodField
()
class
Meta
(
PlotterParameterAllSerializer
.
Meta
):
#exclude = ['declaration']
#
exclude = ['declaration']
exclude
=
[]
#default_fields = PlotterParameterAllSerializer.Meta.default_fields + PlotterParameterAllSerializer.Meta.extra_fields
default_fields
=
[
'
id
'
,
'
accessibility
'
,
'
modifiable
'
,
'
deletable
'
,
'
is_owner
'
,
'
name
'
,
'
fork_of
'
,
'
last_version
'
,
'
previous_version
'
,
'
short_description
'
,
'
description
'
,
'
version
'
,
'
creation_date
'
,
'
data
'
,
'
plotter
'
,
'
plotters
'
]
# default_fields = PlotterParameterAllSerializer.Meta.default_fields + PlotterParameterAllSerializer.Meta.extra_fields
default_fields
=
[
"
id
"
,
"
accessibility
"
,
"
modifiable
"
,
"
deletable
"
,
"
is_owner
"
,
"
name
"
,
"
fork_of
"
,
"
last_version
"
,
"
previous_version
"
,
"
short_description
"
,
"
description
"
,
"
version
"
,
"
creation_date
"
,
"
data
"
,
"
plotter
"
,
"
plotters
"
,
]
def
get_description
(
self
,
obj
):
return
smart_text
(
obj
.
description
,
encoding
=
'
utf-8
'
,
strings_only
=
False
,
errors
=
'
strict
'
)
return
smart_text
(
obj
.
description
,
encoding
=
"
utf-8
"
,
strings_only
=
False
,
errors
=
"
strict
"
)
def
get_short_description
(
self
,
obj
):
return
smart_text
(
obj
.
short_description
,
encoding
=
'
utf-8
'
,
strings_only
=
False
,
errors
=
'
strict
'
)
return
smart_text
(
obj
.
short_description
,
encoding
=
"
utf-8
"
,
strings_only
=
False
,
errors
=
"
strict
"
)
def
get_data
(
self
,
obj
):
return
json
.
loads
(
obj
.
data
)
...
...
@@ -226,24 +286,44 @@ class FullPlotterParameterSerializer(PlotterParameterAllSerializer):
all_plotters
=
Plotter
.
objects
.
all
()
results
=
{}
for
plotter
in
all_plotters
.
iterator
():
serializer
=
FullPlotterSerializer
(
plotter
,
context
=
self
.
context
,
fields
=
[
'
id
'
,
'
accessibility
'
,
'
modifiable
'
,
'
deletable
'
,
'
is_owner
'
,
'
name
'
,
'
fork_of
'
,
'
last_version
'
,
'
previous_version
'
,
'
short_description
'
,
'
description
'
,
'
version
'
,
'
creation_date
'
,
'
data
'
,
'
sample_data
'
,
'
declaration
'
])
serializer
=
FullPlotterSerializer
(
plotter
,
context
=
self
.
context
,
fields
=
[
"
id
"
,
"
accessibility
"
,
"
modifiable
"
,
"
deletable
"
,
"
is_owner
"
,
"
name
"
,
"
fork_of
"
,
"
last_version
"
,
"
previous_version
"
,
"
short_description
"
,
"
description
"
,
"
version
"
,
"
creation_date
"
,
"
data
"
,
"
sample_data
"
,
"
declaration
"
,
],
)
results
[
plotter
.
fullname
()]
=
serializer
.
data
return
results
#def get_plotter(self, obj):
# def get_plotter(self, obj):
# return obj.author.username
#"accessibility": "public",
#"modifiable": true,
#"deletable": true,
#"is_owner": false,
#"name": "plot/bar/1",
#"fork_of": null,
#"last_version": true,
#"previous_version": null,
#"short_description": "Default parameters for bar plots",
#"description": "Raw content",
#"version": 1,
#"creation_date": "2015-09-03T16:55:47.620000",
#
"accessibility": "public",
#
"modifiable": true,
#
"deletable": true,
#
"is_owner": false,
#
"name": "plot/bar/1",
#
"fork_of": null,
#
"last_version": true,
#
"previous_version": null,
#
"short_description": "Default parameters for bar plots",
#
"description": "Raw content",
#
"version": 1,
#
"creation_date": "2015-09-03T16:55:47.620000",
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