Skip to content

Commit

Permalink
refactor: create utils file to share in project
Browse files Browse the repository at this point in the history
This create the utils file in eox-nelp and also move
the `map_instance_attributes_to_dict` to could be used by other
subproject folders.
  • Loading branch information
johanseto committed Jul 10, 2023
1 parent e75982e commit 9dc290b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 43 deletions.
45 changes: 3 additions & 42 deletions eox_nelp/course_experience/api/v1/serializers.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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"}
Expand All @@ -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)
}


Expand All @@ -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):
Expand Down
40 changes: 40 additions & 0 deletions eox_nelp/utils.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion eox_nelp/views.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 9dc290b

Please sign in to comment.