Repository: learningequality/kolibri

Issue:
0.5 -> 0.6 causes breakage in Learn Topics
## Summary

* 0.5 -> 0.6 causes breakage in Learn Topics
* Performed migrations 
```
http://127.0.0.1:8000/api/contentnode/bcd99d8aeef04ce6b9e25a88d87eedb7/ancestors/?1504215141699=1504215141699
http://127.0.0.1:8000/api/contentnode/bcd99d8aeef04ce6b9e25a88d87eedb7/?1504215141714=1504215141714
```
both return `{detail: "Not found."}`



Discussion:
Note: this happens if you just do importchannel, and not also importcontent.

Will update to prevent loading a channel for which there is zero availability.
Also, it will happen on upgrade, as I think the upgrade path is effectively just doing import channel, and not the necessary annotation.

Resolving patch:
diff --git a/kolibri/content/api.py b/kolibri/content/api.py
--- a/kolibri/content/api.py
+++ b/kolibri/content/api.py
@@ -23,9 +23,22 @@ def _join_with_logical_operator(lst, operator):
     return "(({items}))".format(items=op.join(lst))
 
 
+class ChannelMetadataFilter(filters.FilterSet):
+    available = filters.django_filters.MethodFilter()
+
+    def filter_available(self, queryset, value):
+        return queryset.filter(root__available=value)
+
+    class Meta:
+        model = models.ChannelMetadata
+        fields = ['available', ]
+
+
 class ChannelMetadataViewSet(viewsets.ModelViewSet):
     permission_classes = (OnlyCanManageContentCanDelete,)
     serializer_class = serializers.ChannelMetadataSerializer
+    filter_backends = (filters.DjangoFilterBackend,)
+    filter_class = ChannelMetadataFilter
 
     def get_queryset(self):
         return models.ChannelMetadata.objects.all()
diff --git a/kolibri/content/management/commands/importcontent.py b/kolibri/content/management/commands/importcontent.py
--- a/kolibri/content/management/commands/importcontent.py
+++ b/kolibri/content/management/commands/importcontent.py
@@ -124,11 +124,7 @@ def _transfer(self, method, channel_id, path=None):  # noqa: max-complexity=16
                     os.remove(dest)
                 self.cancel()
             else:
-                annotation.mark_local_files_as_available(file_checksums_to_annotate)
-
-                annotation.set_leaf_node_availability_from_local_file_availability()
-
-                annotation.recurse_availability_up_tree()
+                annotation.set_availability(file_checksums_to_annotate)
 
     def handle_async(self, *args, **options):
         if options['command'] == 'network':
diff --git a/kolibri/content/utils/annotation.py b/kolibri/content/utils/annotation.py
--- a/kolibri/content/utils/annotation.py
+++ b/kolibri/content/utils/annotation.py
@@ -25,9 +25,14 @@ def update_channel_metadata():
     """
     from .channel_import import import_channel_from_local_db
     channel_ids = get_channel_ids_for_content_database_dir(settings.CONTENT_DATABASE_DIR)
+    imported = False
     for channel_id in channel_ids:
         if not ChannelMetadata.objects.filter(id=channel_id).exists():
             import_channel_from_local_db(channel_id)
+            imported = True
+    if imported:
+        set_availability()
+
 
 def set_leaf_node_availability_from_local_file_availability():
     bridge = Bridge(app_name=CONTENT_APP_NAME)
@@ -137,3 +142,12 @@ def recurse_availability_up_tree():
             ContentNodeTable.c.kind == content_kinds.TOPIC).values(available=exists(available_nodes)).execution_options(autocommit=True))
 
     bridge.end()
+
+def set_availability(checksums=None):
+    if checksums is None:
+        set_local_file_availability_from_disk()
+    else:
+        mark_local_files_as_available(checksums)
+
+    set_leaf_node_availability_from_local_file_availability()
+    recurse_availability_up_tree()


Test patch: