Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
MOOC packages by hacklab
django-courses
Commits
f6491939
Commit
f6491939
authored
1 year ago
by
Bruno Martin
Browse files
Options
Download
Email Patches
Plain Diff
support read_only fields in ModelsTranslateSerializer
fix CourseSerializer thumbs related fields
parent
1ee24be6
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
3 deletions
+29
-3
courses/serializers.py
courses/serializers.py
+29
-3
No files found.
courses/serializers.py
View file @
f6491939
...
...
@@ -41,6 +41,27 @@ class TranslatableModelSerializerMixin(object):
fields
+=
tuple
(
get_translation_fields
(
field
))
return
fields
def
get_extra_kwargs
(
self
):
"""
Return a dictionary mapping field names to a dictionary of
additional keyword arguments.
This class extends the original method to add tranlated fields
to read_only_fields if the original field is read_only
"""
extra_kwargs
=
super
().
get_extra_kwargs
()
read_only_fields
=
getattr
(
self
.
Meta
,
'read_only_fields'
,
None
)
if
read_only_fields
is
not
None
:
model_translatable_fields
=
get_translatable_fields_for_model
(
self
.
Meta
.
model
)
for
field_name
in
read_only_fields
:
if
field_name
in
model_translatable_fields
:
for
translation_field_name
in
get_translation_fields
(
field_name
):
kwargs
=
extra_kwargs
.
get
(
translation_field_name
,
{})
kwargs
[
'read_only'
]
=
True
extra_kwargs
[
translation_field_name
]
=
kwargs
return
extra_kwargs
class
SimpleUserSerializer
(
serializers
.
ModelSerializer
):
...
...
@@ -83,19 +104,24 @@ class CourseSerializer(TranslatableModelSerializerMixin, serializers.ModelSerial
class
Meta
:
model
=
Course
fields
=
(
'id'
,
'slug'
,
'name'
,
'intro_video'
,
'application'
,
'requirement'
,
fields
=
[
'id'
,
'slug'
,
'name'
,
'intro_video'
,
'application'
,
'requirement'
,
'abstract'
,
'structure'
,
'workload'
,
'status'
,
'is_public'
,
'home_position'
,
'thumbnail'
,
'thumbnail_url'
,
'home_thumbnail'
,
'home_thumbnail_url'
,
'home_position'
,
'home_thumbnail_url'
,
'start_date'
,
'home_published'
,
'authors_names'
,
'has_started'
,
'min_percent_to_complete'
,
'is_user_assistant'
,
'is_user_coordinator'
,
'is_assistant_or_coordinator'
,
'professors'
,
'track'
,
'forum_id'
,
'informations'
,
'description'
,
'lang'
,
'course_load'
,
'ct_id'
,
'has_certification'
,
)
]
read_only_fields
=
[
'thumbnail'
,
'thumbnail_url'
,
'home_thumbnail'
,
'home_thumbnail_url'
,
]
@
staticmethod
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment