Commit 067b2b1e by Clinton Blackburn

Merge pull request #219 from edx/extra-course-product-attributes

Exposing Additional Attributes for the Course Detail View
parents 2cc017a5 fc92d3fe
......@@ -81,6 +81,19 @@ class Course(models.Model):
return mode
@property
def type(self):
""" Returns the type of the course (based on the available seat types). """
seat_types = [(seat.attr.certificate_type or '').lower() for seat in self.seat_products]
if 'credit' in seat_types:
return 'credit'
elif 'professional' in seat_types or 'no-id-professional' in seat_types:
return 'professional'
elif 'verified' in seat_types:
return 'verified'
else:
return 'honor'
@property
def parent_seat_product(self):
""" Returns the course seat parent Product. """
return self.products.get(product_class__slug='seat', structure=Product.PARENT)
......
......@@ -176,3 +176,25 @@ class CourseTests(CourseCatalogTestMixin, TestCase):
self.assertEqual(course.products.count(), 2)
seat = course.seat_products[0]
self.assert_course_seat_valid(seat, course, certificate_type, id_verification_required, price, credit_provider)
def test_type(self):
""" Verify the property returns a type value corresponding to the available products. """
course = Course.objects.create(id='a/b/c', name='Test Course')
self.assertEqual(course.type, 'honor')
course.create_or_update_seat('honor', False, 0)
self.assertEqual(course.type, 'honor')
course.create_or_update_seat('verified', True, 10)
self.assertEqual(course.type, 'verified')
seat = course.create_or_update_seat('professional', True, 100)
self.assertEqual(course.type, 'professional')
seat.delete()
self.assertEqual(course.type, 'verified')
course.create_or_update_seat('no-id-professional', False, 100)
self.assertEqual(course.type, 'professional')
course.create_or_update_seat('credit', True, 1000, credit_provider='SMU')
self.assertEqual(course.type, 'credit')
......@@ -70,7 +70,8 @@ class ProductSerializer(serializers.HyperlinkedModelSerializer):
class Meta(object):
model = Product
fields = ('id', 'url', 'product_class', 'title', 'price', 'expires', 'attribute_values', 'is_available_to_buy',)
fields = ('id', 'url', 'structure', 'product_class', 'title', 'price', 'expires', 'attribute_values',
'is_available_to_buy',)
extra_kwargs = {
'url': {'view_name': PRODUCT_DETAIL_VIEW},
}
......@@ -96,7 +97,7 @@ class OrderSerializer(serializers.ModelSerializer):
fields = ('number', 'date_placed', 'status', 'currency', 'total_excl_tax', 'lines', 'billing_address')
class PaymentProcessorSerializer(serializers.Serializer): # pylint: disable=abstract-method
class PaymentProcessorSerializer(serializers.Serializer): # pylint: disable=abstract-method
""" Serializer to use with instances of processors.BasePaymentProcessor """
def to_representation(self, instance):
......@@ -120,7 +121,8 @@ class CourseSerializer(serializers.HyperlinkedModelSerializer):
class Meta(object):
model = Course
fields = ('id', 'url', 'name', 'products_url',)
fields = ('id', 'url', 'name', 'type', 'products_url',)
read_only_fields = ('type',)
extra_kwargs = {
'url': {'view_name': COURSE_DETAIL_VIEW}
}
......@@ -35,6 +35,7 @@ class CourseViewSetTests(TestServerUrlMixin, CourseCatalogTestMixin, UserMixin,
return {
'id': course.id,
'name': course.name,
'type': course.type,
'url': self.get_full_url(reverse('api:v2:course-detail', kwargs={'pk': course.id})),
'products_url': products_url,
}
......
......@@ -37,6 +37,7 @@ class ProductViewSetTests(TestServerUrlMixin, CourseCatalogTestMixin, UserMixin,
data = {
'id': product.id,
'url': self.get_full_url(reverse('api:v2:product-detail', kwargs={'pk': product.id})),
'structure': product.structure,
'product_class': unicode(product.get_product_class()),
'title': product.title,
'expires': product.expires.strftime(ISO_8601_FORMAT) if product.expires else None,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment