# 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

    def get_queryset(self, *args, **kwargs):
        return Course.objects.filter(provider__active=True,
                                     source__pk=self.kwargs.get('source_id'))


class CourseDetail(generics.RetrieveUpdateAPIView):
    # permission_classes = (IsAuthenticated,)
    serializer_class = CourseRetrieveUpdateSerializer

    def get_queryset(self, *args, **kwargs):
        return Course.objects.filter(provider__active=True)

class CategoryList(generics.ListAPIView):
    """ Primary difference form API V1, is that now id's that we return are equal to our internal
    objects and `category_id` is renamed to `merlot_id` to make it clearer, that it's an external
    identifier"""

    serializer_class = CategoryListSerializerAPI2
    model = MerlotCategory
    paginate_by = 2000

    def get_queryset(self):
        qs = MerlotCategory.objects.filter(parent=None)

        return MerlotCategory.objects.add_related_count(qs, Course, 'merlot_categories', 'o_count', True)

    def serialize_tree(self, queryset, depth=0, max_depth=-1):
        depth += 1
        for obj in queryset:

# 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


    def get_queryset(self, *args, **kwargs):
        return Course.objects.filter(provider__active=True,
                                     source__pk=self.kwargs.get('source_id'))


class CourseDetail(generics.RetrieveUpdateAPIView):
    # permission_classes = (IsAuthenticated,)
    serializer_class = CourseRetrieveUpdateSerializer

    def get_queryset(self, *args, **kwargs):
        return Course.objects.filter(provider__active=True)

class CategoryList(generics.ListAPIView):
    """ Primary difference form API V1, is that now id's that we return are equal to our internal
    objects and `category_id` is renamed to `merlot_id` to make it clearer, that it's an external
    identifier"""

    serializer_class = CategoryListSerializerAPI2
    model = MerlotCategory
    paginate_by = 2000

    def get_queryset(self):
        qs = MerlotCategory.objects.filter(parent=None)

        return MerlotCategory.objects.add_related_count(qs, Course, 'merlot_categories', 'o_count', True)

    def serialize_tree(self, queryset, depth=0, max_depth=-1):
        depth += 1
        for obj in queryset:

            data = CategoryListSerializerAPI2(obj).data