コード例 #1
0
class MinimalCourseRunSerializer(TimestampModelSerializer):
    image = ImageField(read_only=True, source='card_image_url')
    marketing_url = serializers.SerializerMethodField()

    @classmethod
    def prefetch_queryset(cls):
        return CourseRun.objects.all().select_related(
            'course').prefetch_related(
                'course__partner',
                Prefetch('seats', queryset=SeatSerializer.prefetch_queryset()),
            )

    class Meta:
        model = CourseRun
        fields = (
            'key',
            'uuid',
            'title',
            'image',
            'short_description',
            'marketing_url',
            'start',
            'end',
            'enrollment_start',
            'enrollment_end',
            'pacing_type',
            'type',
        )

    def get_marketing_url(self, obj):
        return get_marketing_url_for_user(
            self.context['request'].user,
            obj.marketing_url,
            exclude_utm=self.context.get('exclude_utm'))
コード例 #2
0
    def get_expected_data(self, request, course_run):
        course = course_run.course
        serializer_context = {'request': request}
        expected = dict(
            CourseRunSerializer(course_run, context=serializer_context).data)
        expected.update({
            'subjects':
            self.serialize_items(course.subjects.all(), 'name'),
            'seats':
            self.serialize_seats(course_run),
            'owners':
            self.serialize_items(course.authoring_organizations.all(), 'key'),
            'sponsors':
            self.serialize_items(course.sponsoring_organizations.all(), 'key'),
            'prerequisites':
            self.serialize_items(course.prerequisites.all(), 'name'),
            'level_type':
            course_run.level_type.name if course_run.level_type else None,
            'expected_learning_items':
            self.serialize_items(course.expected_learning_items.all(),
                                 'value'),
            'course_key':
            course.key,
            'image':
            ImageField().to_representation(course_run.card_image_url),
        })

        # Remove fields found in CourseRunSerializer, but not in FlattenedCourseRunWithCourseSerializer.
        fields_to_remove = set(CourseRunSerializer.Meta.fields) - set(
            FlattenedCourseRunWithCourseSerializer.Meta.fields)
        for key in fields_to_remove:
            del expected[key]

        return expected
コード例 #3
0
 def get_expected_data(self, course_run, request):  # pylint: disable=unused-argument
     return {
         'key':
         course_run.key,
         'uuid':
         str(course_run.uuid),
         'title':
         course_run.title,
         'short_description':
         course_run.short_description,
         'image':
         ImageField().to_representation(course_run.card_image_url),
         'marketing_url':
         '{url}?{params}'.format(url=course_run.marketing_url,
                                 params=urlencode({
                                     'utm_source':
                                     request.user.username,
                                     'utm_medium':
                                     request.user.referral_tracking_id,
                                 })),
         'start':
         json_date_format(course_run.start),
         'end':
         json_date_format(course_run.end),
         'enrollment_start':
         json_date_format(course_run.enrollment_start),
         'enrollment_end':
         json_date_format(course_run.enrollment_end),
         'pacing_type':
         course_run.pacing_type,
         'type':
         course_run.type,
     }
コード例 #4
0
def test_imagefield_to_representation():
    value = 'https://example.com/image.jpg'
    expected = {
        'src': value,
        'description': None,
        'height': None,
        'width': None
    }
    assert ImageField().to_representation(value) == expected
コード例 #5
0
 def test_to_representation(self):
     value = 'https://example.com/image.jpg'
     expected = {
         'src': value,
         'description': None,
         'height': None,
         'width': None
     }
     self.assertEqual(ImageField().to_representation(value), expected)
コード例 #6
0
class MinimalCourseSerializer(TimestampModelSerializer):
    course_runs = MinimalCourseRunSerializer(many=True)
    owners = MinimalOrganizationSerializer(many=True, source='authoring_organizations')
    image = ImageField(read_only=True, source='card_image_url')

    @classmethod
    def prefetch_queryset(cls):
        return Course.objects.all().select_related('partner').prefetch_related(
            'authoring_organizations',
            Prefetch('course_runs', queryset=MinimalCourseRunSerializer.prefetch_queryset()),
        )

    class Meta:
        model = Course
        fields = ('key', 'uuid', 'title', 'course_runs', 'owners', 'image',)
コード例 #7
0
    def get_expected_data(self, course, request):
        context = {'request': request}

        return {
            'key':
            course.key,
            'uuid':
            str(course.uuid),
            'title':
            course.title,
            'course_runs':
            MinimalCourseRunSerializer(course.course_runs,
                                       many=True,
                                       context=context).data,
            'owners':
            MinimalOrganizationSerializer(course.authoring_organizations,
                                          many=True,
                                          context=context).data,
            'image':
            ImageField().to_representation(course.card_image_url),
        }
コード例 #8
0
class FlattenedCourseRunWithCourseSerializer(CourseRunSerializer):
    seats = serializers.SerializerMethodField()
    owners = serializers.SerializerMethodField()
    sponsors = serializers.SerializerMethodField()
    subjects = serializers.SerializerMethodField()
    prerequisites = serializers.SerializerMethodField()
    expected_learning_items = serializers.SerializerMethodField()
    course_key = serializers.SlugRelatedField(read_only=True, source='course', slug_field='key')
    image = ImageField(read_only=True, source='card_image_url')

    class Meta:
        model = CourseRun
        fields = (
            'key', 'title', 'short_description', 'full_description', 'level_type', 'subjects', 'prerequisites',
            'start', 'end', 'enrollment_start', 'enrollment_end', 'announcement', 'seats', 'content_language',
            'transcript_languages', 'staff', 'pacing_type', 'min_effort', 'max_effort', 'course_key',
            'expected_learning_items', 'image', 'video', 'owners', 'sponsors', 'modified', 'marketing_url',
        )

    def get_seats(self, obj):
        seats = {
            'audit': {
                'type': ''
            },
            'honor': {
                'type': ''
            },
            'verified': {
                'type': '',
                'currency': '',
                'price': '',
                'upgrade_deadline': '',
            },
            'professional': {
                'type': '',
                'currency': '',
                'price': '',
                'upgrade_deadline': '',
            },
            'credit': {
                'type': [],
                'currency': [],
                'price': [],
                'upgrade_deadline': [],
                'credit_provider': [],
                'credit_hours': [],
            },
        }

        for seat in obj.seats.all():
            for key in seats[seat.type].keys():
                if seat.type == 'credit':
                    seats['credit'][key].append(SeatSerializer(seat).data[key])
                else:
                    seats[seat.type][key] = SeatSerializer(seat).data[key]

        for credit_attr in seats['credit']:
            seats['credit'][credit_attr] = ','.join([str(e) for e in seats['credit'][credit_attr]])

        return seats

    def get_owners(self, obj):
        return ','.join([owner.key for owner in obj.course.authoring_organizations.all()])

    def get_sponsors(self, obj):
        return ','.join([sponsor.key for sponsor in obj.course.sponsoring_organizations.all()])

    def get_subjects(self, obj):
        return ','.join([subject.name for subject in obj.course.subjects.all()])

    def get_prerequisites(self, obj):
        return ','.join([prerequisite.name for prerequisite in obj.course.prerequisites.all()])

    def get_expected_learning_items(self, obj):
        return ','.join(
            [expected_learning_item.value for expected_learning_item in obj.course.expected_learning_items.all()]
        )