Skip to content

Commit

Permalink
style: clean pylint and add docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
johanseto committed Jul 7, 2023
1 parent 617eaf7 commit 9bd8f11
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
26 changes: 25 additions & 1 deletion eox_nelp/course_experience/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,43 @@ def get_user_extra_attributes(value=None):


def map_attributes_from_instance_to_dict(instance, attributes_mapping):
"""Create a dictionary that represents some fields or attributes of a instance based on
a attributes_mapping dictionary. This dict would have key, values which the key represent the
attribute to look in the instance and the value the key name in the output dict.
Based in the `attributes_mapping` you should use a dict with the following config:
{
"field_name": "key_name"
}
This would check in the instace instance.field_name and the value send it to output dict
like {"key_name": instance.field_name}
Also its is possible to check nested fields if you declarate the field of instance separated by `__`
eg:
{
"field_level1__field_level2": "key_name"
}
This example would check in the instace like instance.field_level1.field_level2 and in the output
dict like {"key_name": nstance.field_level1.field_level2}
Args:
instance (instance class): Model or instance of class to retrieved fields.
attributes_mapping (dict): Dictionary map that has the fields to search and the keys name to output,
Returns:
instance_dict: dict representing the instance
"""
instance_dict = {}
for instance_field, extra_field in attributes_mapping.items():
extra_value = None
instance_level = copy(instance)
for instance_field in instance_field.split("__"):
if hasattr(instance_level, instance_field):
instance_level = getattr(instance_level, instance_field)
extra_value = instance_level
extra_value = instance_level

instance_dict[extra_field] = extra_value

return instance_dict


class ExperienceSerializer(serializers.ModelSerializer):
"""Class to configure serializer for Experiences.
Expand Down
6 changes: 5 additions & 1 deletion eox_nelp/course_experience/api/v1/tests/mixins_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
from rest_framework import status
from rest_framework.test import APIClient

from eox_nelp.course_experience.api.v1.serializers import COURSE_OVERVIEW_EXTRA_FIELD_MAPPING, USER_EXTRA_FIELD_MAPPING, map_attributes_from_instance_to_dict
from eox_nelp.course_experience.api.v1.serializers import (
COURSE_OVERVIEW_EXTRA_FIELD_MAPPING,
USER_EXTRA_FIELD_MAPPING,
map_attributes_from_instance_to_dict,
)
from eox_nelp.course_experience.api.v1.views import INVALID_KEY_ERROR
from eox_nelp.edxapp_wrapper.course_overviews import CourseOverview

Expand Down

0 comments on commit 9bd8f11

Please sign in to comment.