Skip to content

Commit

Permalink
Merge pull request #69 from edly-io/fix/course-detail-elasticsearch
Browse files Browse the repository at this point in the history
Course detail fetch from elasticsearch, add invitation_only field
  • Loading branch information
hinakhadim authored Oct 19, 2023
2 parents bc57e19 + ddce9fb commit 1e28f1d
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 18 deletions.
26 changes: 24 additions & 2 deletions course_discovery/apps/course_metadata/data_loaders/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ def update_course_run(self, official_run, draft_run, body):
end_has_updated = validated_data.get('end') != run.end
self._update_instance(official_run, validated_data, suppress_publication=True)
self._update_instance(draft_run, validated_data, suppress_publication=True)
self.create_seat(official_run)
if BYPASS_LMS_DATA_LOADER__END_DATE_UPDATED_CHECK.is_enabled() or end_has_updated:
self._update_verified_deadline_for_course_run(official_run)
self._update_verified_deadline_for_course_run(draft_run)
Expand All @@ -189,9 +190,27 @@ def create_course_run(self, course, body):
# Start with draft version and then make official (since our utility functions expect that flow)
defaults['course'] = course.draft_version
draft_run = CourseRun.objects.create(**defaults, draft=True)
self.create_seat(draft_run)
return draft_run.update_or_create_official_version(notify_services=False)
else:
return CourseRun.objects.create(**defaults)
course_run = CourseRun.objects.create(**defaults)
self.create_seat(course_run)
return course_run

def create_seat(self, course_run, seat_type=Seat.HONOR):
if course_run.seats.count() == 0:

try:
seat_type = SeatType.objects.get(slug=seat_type)
except SeatType.DoesNotExist:
msg = ('Could not find seat type {seat_type}'.format(seat_type=seat_type))
logger.warning(msg)
return

created = course_run.seats.create(type=seat_type)

if created:
logger.info('Created seat for course with key [%s].', course_run.key)

def get_or_create_course(self, body):
course_run_key = CourseKey.from_string(body['id'])
Expand Down Expand Up @@ -265,7 +284,10 @@ def format_course_run_data(self, body, course=None):
'hidden': body.get('hidden', False),
'license': body.get('license') or '', # license cannot be None
'title_override': body['name'], # we support Studio edits, even though Publisher also owns titles
'pacing_type': self.get_pacing_type(body)
'pacing_type': self.get_pacing_type(body),
'outcome_override': json.dumps({
'invitation_only': body.get('invitation_only'),
})
}

if not self.partner.uses_publisher:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ class CourseRunDocument(BaseCourseDocument):
)
weeks_to_complete = fields.IntegerField()

course_overridden = fields.BooleanField()
card_image_url = fields.TextField()

def prepare_aggregation_key(self, obj):
# Aggregate CourseRuns by Course key since that is how we plan to dedup CourseRuns on the marketing site.
return 'courserun:{}'.format(obj.course.key)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
]

router = routers.SimpleRouter()
router.register(r'course_runs', dataloader_api.DataLoaderCourseRunViewSet, basename='course_run')
router.register(r'search/course_runs', dataloader_api.DataLoaderCourseRunSearchViewSet, basename='search-course_runs')

urlpatterns += router.urls
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import logging

from discovery_dataloader_app import serializers
from django_elasticsearch_dsl_drf.filter_backends import FilteringFilterBackend, DefaultOrderingFilterBackend
from django_elasticsearch_dsl_drf.constants import (
LOOKUP_FILTER_TERM, LOOKUP_FILTER_TERMS, LOOKUP_QUERY_EXCLUDE,
)
from django_elasticsearch_dsl_drf.filter_backends import DefaultOrderingFilterBackend, FilteringFilterBackend, MultiMatchSearchFilterBackend, SearchFilterBackend
from opaque_keys.edx.keys import CourseKey
from rest_framework import status
from rest_framework.permissions import IsAuthenticated
Expand Down Expand Up @@ -72,20 +75,28 @@ def post(self, request):
)


class DataLoaderCourseRunViewSet(CourseRunViewSet):
http_method_names = ["get"]
serializer_class = serializers.DataLoaderCourseRunWithProgramsSerializer


class DataLoaderCourseRunSearchViewSet(CourseRunSearchViewSet):
detail_serializer_class = serializers.DataLoaderCourseRunSearchModelSerializer
serializer_class = serializers.DataLoaderCourseRunSearchDocumentSerializer

filter_backends = [
FilteringFilterBackend,
SearchFilterBackend,
MultiMatchSearchFilterBackend,
DefaultOrderingFilterBackend,
]

search_fields = (
'title',
'short_description',
'full_description'
)

filter_fields = {
'key': {'field': 'key.raw', 'lookups': [LOOKUP_FILTER_TERM, LOOKUP_FILTER_TERMS, LOOKUP_QUERY_EXCLUDE]},
'published': 'published',
'availability': 'availability',
'featured': 'course_overridden',
'title': 'title',
'number': 'number',
}
12 changes: 4 additions & 8 deletions course_discovery/apps/discovery_dataloader_app/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,14 @@ class Meta(CourseRunWithProgramsSerializer.Meta):


class DataLoaderCourseRunSearchDocumentSerializer(CourseRunSearchDocumentSerializer):

featured = serializers.ReadOnlyField(source='course_overridden')

class Meta(CourseRunSearchDocumentSerializer.Meta):
""" Meta options. """

document = CourseRunDocument
fields = CourseRunSearchDocumentSerializer.Meta.fields

def to_representation(self, instance):
data = super().to_representation(instance)
course_run = CourseRun.objects.get(id=instance.pk)
data['featured'] = course_run.course_overridden
data['card_image_url'] = course_run.card_image_url
return data
fields = CourseRunSearchDocumentSerializer.Meta.fields + ('card_image_url', 'featured', 'outcome', )


class DataLoaderCourseRunSearchModelSerializer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class CustomPageNumberPagination(PageNumberPagination):

class BaseElasticsearchDocumentViewSet(mixins.DetailMixin, mixins.FacetMixin, DocumentViewSet):
lookup_field = 'key'
document_uid_field = 'key'
document_uid_field = 'key.raw'
pagination_class = CustomPageNumberPagination
permission_classes = (IsAuthenticated,)
ensure_published = True
Expand Down

0 comments on commit 1e28f1d

Please sign in to comment.