# Path: search/web/serializers.py
# class SourceSerializer(serializers.ModelSerializer):
#     name = serializers.CharField(source='provider.name', read_only=True)
#     merlot_missing = serializers.CharField(source='get_merlot_missing', read_only=True)
#     course_count = serializers.CharField(source='get_total_count', read_only=True)
#     courses = serializers.PrimaryKeyRelatedField(source='course_set', many=True, read_only=True)
# 
#     class Meta:
#         model = Source
#         fields = ('id', 'name', 'kind', 'merlot_missing', 'course_count', 'courses')
# 
# class CourseRetrieveUpdateSerializer(serializers.ModelSerializer):
#     source = serializers.CharField(source='source.provider.name', read_only=True)
#     merlot_detail_url = serializers.CharField(read_only=True)
#     categories = serializers.PrimaryKeyRelatedField(source='merlot_categories', many=True, read_only=True)
# 
#     class Meta:
#         model = Course
#         fields = ('source', 'id', 'title', 'description', 'language',
#                     'merlot_present', 'merlot_detail_url', 'categories')
#         read_only_fields = ('merlot_present',)
# 
# class CategoryListSerializerAPI2(serializers.ModelSerializer):
#     merlot_id = serializers.CharField()
# 
#     class Meta:
#         model = MerlotCategory
#         fields = ('id', 'name', 'merlot_id')
# 
# class CategoryFlatListSerializer(serializers.ModelSerializer):
#     name = serializers.SerializerMethodField()
#     merlot_id = serializers.CharField()
# 
#     class Meta:
#         model = MerlotCategory
#         fields = ('id', 'name', 'merlot_id')
# 
#     def get_name(self, obj):
#         return ' / '.join( map( unicode, obj.get_ancestors() ) + [obj.name] )


from collections import OrderedDict
from rest_framework.response import Response
from rest_framework.decorators import api_view
from rest_framework.reverse import reverse
from rest_framework import generics
from data.models import Source, Course, MerlotCategory
from ..serializers import SourceSerializer, CourseRetrieveUpdateSerializer, \
    CategoryListSerializerAPI2, CategoryFlatListSerializer

@api_view(['GET'])
def index(request):
    """
    This is v2 Open Data API to Courses that are currently tracked by OpenCourseWare Consortium.

    It's currently under active development.
    """
    return Response(OrderedDict([
        ('sources', reverse('api2:sources', request=request)),
        ('source-detail', reverse('api2:source-detail', args=('15',), request=request)),
        ('source-course-list', reverse('api2:source-course-list', args=('15',), request=request)),
        ('course-detail', reverse('api2:course-detail', args=('26',), request=request)),
        ('category-list', reverse('api2:category-list', request=request))
    ]))


class SourceList(generics.ListAPIView):
    # permission_classes = (IsAuthenticated,)
    serializer_class = SourceSerializer
    paginate_by = 200
    queryset = Source.objects.filter(provider__active=True)

    def get_queryset(self):
        return Source.objects.filter(provider__active=True, **self.kwargs) \
                .order_by('provider__name')


class SourceDetail(generics.RetrieveAPIView):
    # permission_classes = (IsAuthenticated,)

# Path: search/web/serializers.py
# class SourceSerializer(serializers.ModelSerializer):
#     name = serializers.CharField(source='provider.name', read_only=True)
#     merlot_missing = serializers.CharField(source='get_merlot_missing', read_only=True)
#     course_count = serializers.CharField(source='get_total_count', read_only=True)
#     courses = serializers.PrimaryKeyRelatedField(source='course_set', many=True, read_only=True)
# 
#     class Meta:
#         model = Source
#         fields = ('id', 'name', 'kind', 'merlot_missing', 'course_count', 'courses')
# 
# class CourseRetrieveUpdateSerializer(serializers.ModelSerializer):
#     source = serializers.CharField(source='source.provider.name', read_only=True)
#     merlot_detail_url = serializers.CharField(read_only=True)
#     categories = serializers.PrimaryKeyRelatedField(source='merlot_categories', many=True, read_only=True)
# 
#     class Meta:
#         model = Course
#         fields = ('source', 'id', 'title', 'description', 'language',
#                     'merlot_present', 'merlot_detail_url', 'categories')
#         read_only_fields = ('merlot_present',)
# 
# class CategoryListSerializerAPI2(serializers.ModelSerializer):
#     merlot_id = serializers.CharField()
# 
#     class Meta:
#         model = MerlotCategory
#         fields = ('id', 'name', 'merlot_id')
# 
# class CategoryFlatListSerializer(serializers.ModelSerializer):
#     name = serializers.SerializerMethodField()
#     merlot_id = serializers.CharField()
# 
#     class Meta:
#         model = MerlotCategory
#         fields = ('id', 'name', 'merlot_id')
# 
#     def get_name(self, obj):
#         return ' / '.join( map( unicode, obj.get_ancestors() ) + [obj.name] )


# Path: search/web/views/api2.py
from collections import OrderedDict
from rest_framework.response import Response
from rest_framework.decorators import api_view
from rest_framework.reverse import reverse
from rest_framework import generics
from data.models import Source, Course, MerlotCategory
from ..serializers import SourceSerializer, CourseRetrieveUpdateSerializer, \
    CategoryListSerializerAPI2, CategoryFlatListSerializer


@api_view(['GET'])
def index(request):
    """
    This is v2 Open Data API to Courses that are currently tracked by OpenCourseWare Consortium.

    It's currently under active development.
    """
    return Response(OrderedDict([
        ('sources', reverse('api2:sources', request=request)),
        ('source-detail', reverse('api2:source-detail', args=('15',), request=request)),
        ('source-course-list', reverse('api2:source-course-list', args=('15',), request=request)),
        ('course-detail', reverse('api2:course-detail', args=('26',), request=request)),
        ('category-list', reverse('api2:category-list', request=request))
    ]))


class SourceList(generics.ListAPIView):
    # permission_classes = (IsAuthenticated,)
    serializer_class = SourceSerializer
    paginate_by = 200
    queryset = Source.objects.filter(provider__active=True)

    def get_queryset(self):
        return Source.objects.filter(provider__active=True, **self.kwargs) \
                .order_by('provider__name')


class SourceDetail(generics.RetrieveAPIView):
    # permission_classes = (IsAuthenticated,)

    serializer_class = SourceSerializer