diff --git a/eox_nelp/course_experience/api/v1/serializers.py b/eox_nelp/course_experience/api/v1/serializers.py index f87ff933..de726245 100644 --- a/eox_nelp/course_experience/api/v1/serializers.py +++ b/eox_nelp/course_experience/api/v1/serializers.py @@ -1,6 +1,4 @@ """Serializers used for the experience views.""" -from copy import copy - from django.contrib.auth import get_user_model from rest_framework_json_api import serializers @@ -13,6 +11,7 @@ ReportUnit, ) from eox_nelp.edxapp_wrapper.course_overviews import CourseOverview +from eox_nelp.utils import map_instance_attributes_to_dict User = get_user_model() COURSE_OVERVIEW_EXTRA_FIELD_MAPPING = {"display_name": "display_name"} @@ -34,7 +33,7 @@ def get_course_extra_attributes(value=None): dict: dict object too add course extra fields """ return { - "attributes": map_attributes_from_instance_to_dict(value, COURSE_OVERVIEW_EXTRA_FIELD_MAPPING) + "attributes": map_instance_attributes_to_dict(value, COURSE_OVERVIEW_EXTRA_FIELD_MAPPING) } @@ -48,46 +47,8 @@ def get_user_extra_attributes(value=None): dict: dict object too add user extra fields """ return { - "attributes": map_attributes_from_instance_to_dict(value, USER_EXTRA_FIELD_MAPPING) - } - - -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: - { - "key_name": "field_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: - { - "key_name": "field_level1__field_level2" + "attributes": map_instance_attributes_to_dict(value, USER_EXTRA_FIELD_MAPPING) } - This example would check in the instace like instance.field_level1.field_level2 and in the output - dict like {"key_name": instance.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 extra_field, instance_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 - - instance_dict[extra_field] = extra_value - - return instance_dict class ExperienceSerializer(serializers.ModelSerializer): diff --git a/eox_nelp/utils.py b/eox_nelp/utils.py new file mode 100644 index 00000000..a0d12927 --- /dev/null +++ b/eox_nelp/utils.py @@ -0,0 +1,40 @@ +"""Utils that can be used for the plugin project""" +from copy import copy + + +def map_instance_attributes_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: + { + "key_name": "field_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: + { + "key_name": "field_level1__field_level2" + } + This example would check in the instace like instance.field_level1.field_level2 and in the output + dict like {"key_name": instance.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 extra_field, instance_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 + + instance_dict[extra_field] = extra_value + + return instance_dict diff --git a/eox_nelp/views.py b/eox_nelp/views.py index a16071a8..2343595f 100644 --- a/eox_nelp/views.py +++ b/eox_nelp/views.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -"""The generic views for the exc-core plugin project""" +"""The generic views for the eox-nelp plugin project""" from __future__ import unicode_literals