Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jlc/experience relationships attributes #64

Merged
merged 1 commit into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions eox_nelp/course_experience/api/v1/serializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""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 +15,79 @@
from eox_nelp.edxapp_wrapper.course_overviews import CourseOverview

User = get_user_model()
COURSE_OVERVIEW_EXTRA_FIELD_MAPPING = {"display_name": "display_name"}
USER_EXTRA_FIELD_MAPPING = {
"first_name": "first_name",
"last_name": "last_name",
"profile_name": "profile__name",
"username": "username",
}


def get_course_extra_attributes(value=None):
"""Function to retrieve CourseOverview extra fields
Args:
value (CourseOverview instance): CourseOverview that the relation analize. Defaults to None.
Returns:
dict: dict object too add course extra fields
"""
return {
"attributes": map_attributes_from_instance_to_dict(value, COURSE_OVERVIEW_EXTRA_FIELD_MAPPING)
}


def get_user_extra_attributes(value=None):
"""Function to retrieve User extra fields
Args:
value (Userinstance): User that the relation analize. Defaults to None.
Returns:
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"
}
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 All @@ -27,9 +102,11 @@ class ExperienceSerializer(serializers.ModelSerializer):
)
course_id = ExperienceResourceRelatedField(
queryset=CourseOverview.objects,
get_extra_fields=get_course_extra_attributes,
)
author = ExperienceResourceRelatedField(
queryset=User.objects,
get_extra_fields=get_user_extra_attributes,
)


Expand Down
27 changes: 26 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,6 +12,7 @@
from rest_framework import status
from rest_framework.test import APIClient

from eox_nelp.course_experience.api.v1.serializers import get_course_extra_attributes, get_user_extra_attributes
from eox_nelp.course_experience.api.v1.views import INVALID_KEY_ERROR
from eox_nelp.edxapp_wrapper.course_overviews import CourseOverview

Expand All @@ -35,6 +36,31 @@ def setUp(self): # pylint: disable=invalid-name
self.my_course, _ = CourseOverview.objects.get_or_create(id=BASE_COURSE_ID)
self.client.force_authenticate(self.user)

def make_relationships_data(self):
"""
Make the relationships dict with custom extra attributes based in the attributes of the serializers
withe variables COURSE_OVERVIEW_EXTRA_ATTRIBUTES and USER_EXTRA_ATTRIBUTES
johanseto marked this conversation as resolved.
Show resolved Hide resolved
Returns:
dict: relationships dict with the corresponding shape, and key-values.
"""
return {
"author": {
"data": {
"type": "User",
"id": f"{self.user.id}",
**get_user_extra_attributes(self.user)
}
},
"course_id": {
"data": {
"type": "CourseOverview",
"id": f"{self.my_course.id}",
**get_course_extra_attributes(self.my_course)
}
},
}

def test_get_object_list_by_user(self):
""" Test a get request to the list endpoint for the desired view.
Expected behavior:
Expand Down Expand Up @@ -85,7 +111,6 @@ def test_patch_object_json(self):
expected_data["data"]["attributes"].update(self.patch_data)

response = self.client.patch(url_endpoint, self.patch_data, format="json")

self.assertIn(response.headers["Content-Type"], RESPONSE_CONTENT_TYPES)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.json(), expected_data)
Expand Down
75 changes: 5 additions & 70 deletions eox_nelp/course_experience/api/v1/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,7 @@ def setUp(self):
"status": self.my_unit_like.status,
"item_id": f"{self.my_unit_like.item_id}",
},
"relationships": {
"author": {
"data": {
"type": "User",
"id": f"{self.user.id}",
}
},
"course_id": {
"data": {
"type": "CourseOverview",
"id": f"{self.my_course.id}",
}
}
}
"relationships": self.make_relationships_data()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where are you testing this new behavior ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.assertEqual(response.json()["data"]["relationships"], expected_data["data"]["relationships"])

self.assertEqual(response.json(), self.base_data)

}
}
self.object_url_kwarg = {self.object_key: BASE_ITEM_ID}
Expand Down Expand Up @@ -117,20 +104,7 @@ def setUp(self):
"reason": f"{self.my_unit_report.reason}",
"item_id": f"{self.my_unit_report.item_id}",
},
"relationships": {
"author": {
"data": {
"type": "User",
"id": f"{self.user.id}",
}
},
"course_id": {
"data": {
"type": "CourseOverview",
"id": f"{self.my_course.id}",
}
}
}
"relationships": self.make_relationships_data()
}
}
self.object_url_kwarg = {self.object_key: BASE_ITEM_ID}
Expand Down Expand Up @@ -190,20 +164,7 @@ def setUp(self):
"username": f"{self.user.username}",
"status": self.my_course_like.status,
},
"relationships": {
"author": {
"data": {
"type": "User",
"id": f"{self.user.id}",
}
},
"course_id": {
"data": {
"type": "CourseOverview",
"id": f"{self.my_course.id}",
}
}
}
"relationships": self.make_relationships_data()
}
}

Expand Down Expand Up @@ -244,20 +205,7 @@ def setUp(self):
"username": f"{self.user.username}",
"reason": f"{self.my_course_report.reason}"
},
"relationships": {
"author": {
"data": {
"type": "User",
"id": f"{self.user.id}"
}
},
"course_id": {
"data": {
"type": "CourseOverview",
"id": f"{self.my_course.id}"
}
}
}
"relationships": self.make_relationships_data()
}
}

Expand Down Expand Up @@ -315,20 +263,7 @@ def setUp(self):
"public": self.my_course_feedback.public,
"recommended": self.my_course_feedback.recommended,
},
"relationships": {
"author": {
"data": {
"type": "User",
"id": f"{self.user.id}"
}
},
"course_id": {
"data": {
"type": "CourseOverview",
"id": f"{self.my_course.id}"
}
}
}
"relationships": self.make_relationships_data()
}
}

Expand Down