-
Notifications
You must be signed in to change notification settings - Fork 699
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimise Library page load time when channels have large Thumbnail #12530
Changes from all commits
332c49d
213665c
c73e29f
537242d
e73ce95
c6ddc76
519d50e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import hashlib | ||
import logging | ||
import re | ||
from base64 import urlsafe_b64decode | ||
from collections import OrderedDict | ||
from functools import reduce | ||
from random import sample | ||
|
@@ -14,11 +15,14 @@ | |
from django.db.models import Subquery | ||
from django.db.models.aggregates import Count | ||
from django.http import Http404 | ||
from django.http import HttpResponse | ||
from django.urls import reverse | ||
from django.utils.cache import add_never_cache_headers | ||
from django.utils.decorators import method_decorator | ||
from django.utils.encoding import force_bytes | ||
from django.utils.encoding import iri_to_uri | ||
from django.utils.translation import gettext as _ | ||
from django.views import View | ||
from django.views.decorators.cache import cache_page | ||
from django.views.decorators.cache import never_cache | ||
from django.views.decorators.http import etag | ||
|
@@ -291,6 +295,18 @@ def filter_available(self, queryset, name, value): | |
return queryset.filter(root__available=value) | ||
|
||
|
||
class ChannelThumbnailView(View): | ||
def get(self, request, channel_id): | ||
channel = get_object_or_404(models.ChannelMetadata, id=channel_id) | ||
try: | ||
header, b_64_thumbnail = channel.thumbnail.split(",", 1) | ||
mimetype = header.split(":")[1].split(";")[0] | ||
except ValueError: | ||
raise Http404("No thumbnail available") | ||
thumbnail = urlsafe_b64decode(b_64_thumbnail) | ||
return HttpResponse(thumbnail, content_type=mimetype) | ||
|
||
|
||
class BaseChannelMetadataMixin(object): | ||
filter_backends = (DjangoFilterBackend,) | ||
filterset_class = ChannelMetadataFilter | ||
|
@@ -373,9 +389,21 @@ def filter_options(self, request, **kwargs): | |
return Response(data) | ||
|
||
|
||
def _create_channel_thumbnail_url(item): | ||
return ( | ||
reverse("kolibri:core:channel-thumbnail", args=[item["id"]]) | ||
if item["thumbnail"] | ||
else "" | ||
) | ||
|
||
|
||
@method_decorator(remote_metadata_cache, name="dispatch") | ||
class ChannelMetadataViewSet(BaseChannelMetadataMixin, RemoteViewSet): | ||
pass | ||
field_map = { | ||
"thumbnail": _create_channel_thumbnail_url, | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So you would add here:
Then in the module scope you can define this function:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes this works, can you share django/python docs covering There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not a Django thing - so the documentation for it is in this code comment: https://github.com/learningequality/kolibri/blob/develop/kolibri/core/api.py#L139 |
||
field_map.update(BaseChannelMetadataMixin.field_map) | ||
|
||
|
||
MODALITIES = set(["QUIZ"]) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For writing unit test for this is there any existing
setUp
method that will set up a channel metadata model?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you look in the
test_content_app.py
file in thecontent
app, you should see some examples.