Skip to content

Commit

Permalink
refactor: [AXM-200] Divide get method into smaller methods
Browse files Browse the repository at this point in the history
  • Loading branch information
KyryloKireiev committed Apr 8, 2024
1 parent b5f7e9f commit d26a8ff
Showing 1 changed file with 38 additions and 12 deletions.
50 changes: 38 additions & 12 deletions lms/djangoapps/mobile_api/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import datetime
import logging
from functools import cached_property
from typing import List, Optional
from typing import Dict, List, Optional

import pytz
from completion.exceptions import UnavailableCompletionData
Expand Down Expand Up @@ -565,34 +565,60 @@ def get(self, request, *args, **kwargs) -> Response:
Gets user's enrollments status.
"""
active_status_date = datetime.datetime.now(pytz.UTC) - datetime.timedelta(days=30)
username = kwargs.get('username')
course_ids_where_user_has_completions = self._get_course_ids_where_user_has_completions(
username,
active_status_date,
)
enrollments_status = self._build_enrollments_status_dict(
username,
active_status_date,
course_ids_where_user_has_completions
)
return Response(enrollments_status)

def _build_enrollments_status_dict(
self,
username: str,
active_status_date: datetime,
course_ids: List[str],
) -> List[Dict[str, bool]]:
"""
Builds list with dictionaries with user's enrolments statuses.
"""
user_enrollments = CourseEnrollment.objects.filter(
user__username=kwargs.get('username'),
user__username=username,
is_active=True,
)
mobile_available = [
enrollment for enrollment in user_enrollments
if is_mobile_available_for_user(self.request.user, enrollment.course_overview)
]
user_completions_last_month = BlockCompletion.objects.filter(
user__username=kwargs.get('username'),
created__gte=active_status_date
)
course_ids_where_user_has_completions = [
str(completion.block_key.course_key) for completion in user_completions_last_month
]
enrollments_status = []

for user_enrollment in mobile_available:
course_id = str(user_enrollment.course_overview.id)
enrollments_status.append(
{
'course_id': course_id,
'course_name': user_enrollment.course_overview.display_name,
'is_active': bool(
course_id in course_ids_where_user_has_completions
course_id in course_ids
or user_enrollment.created > active_status_date
)
}
)
return enrollments_status

return Response(enrollments_status)
@staticmethod
def _get_course_ids_where_user_has_completions(
username: str,
active_status_date: datetime,
) -> List[str]:
"""
Gets course ids where user has completions.
"""
user_completions_last_month = BlockCompletion.objects.filter(
user__username=username,
created__gte=active_status_date
)
return [str(completion.block_key.course_key) for completion in user_completions_last_month]

0 comments on commit d26a8ff

Please sign in to comment.