-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds generic search thumbnail retrieval (#10266)
* Adds generic search thumbnail retrieval * PR feedback, fix for manifest URL * updates auto-formatting for black * updates auto-formatting * Update the catch block to catch additional exception if the resource does not exist
- Loading branch information
1 parent
1e97437
commit bc1695b
Showing
11 changed files
with
121 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import base64 | ||
import copy | ||
import re | ||
from typing import List, Tuple | ||
from urllib.error import HTTPError | ||
from requests.exceptions import ConnectionError | ||
import requests | ||
from urllib.parse import urlparse, urlunparse | ||
|
||
|
||
class SearchThumbnailFetcher(object): | ||
def __init__(self, resource): | ||
self.resource = resource | ||
|
||
def get_thumbnail(self, retrieve=False): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import logging | ||
from typing import Callable | ||
from arches.app.models.resource import Resource | ||
|
||
from arches.app.utils.search_thumbnail_fetcher import SearchThumbnailFetcher | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class SearchThumbnailFetcherFactory(object): | ||
registry = {} | ||
|
||
@classmethod | ||
def register(cls, name: str): | ||
def inner_wrapper(wrapped_class: SearchThumbnailFetcher) -> Callable: | ||
if name in cls.registry: | ||
logger.warning("Search Thumbnail Fetcher %s already exists. Will replace it", name) | ||
cls.registry[name] = wrapped_class | ||
return wrapped_class | ||
|
||
return inner_wrapper | ||
|
||
@classmethod | ||
def create_thumbnail_fetcher(cls, resource_id: str, **kwargs) -> SearchThumbnailFetcher: | ||
"""Factory command to create the template engine""" | ||
try: | ||
resource = Resource.objects.get(resourceinstanceid=resource_id) | ||
search_thumbnail_fetcher_class = cls.registry[str(resource.graph_id)] | ||
search_thumbnail_fetcher = search_thumbnail_fetcher_class(resource, **kwargs) | ||
return search_thumbnail_fetcher | ||
except KeyError: | ||
return None # there is no thumbnail fetcher registered for the graph requested | ||
except Resource.DoesNotExist: | ||
return None # there is no resource with this ID. This is rare, but can happen if there are issues with the index. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from django.views.generic import View | ||
from django.http import HttpResponse, HttpResponseNotFound | ||
from arches.app.utils.search_thumbnail_fetcher_factory import SearchThumbnailFetcherFactory | ||
|
||
|
||
class ThumbnailView(View): | ||
def head(self, request, resource_id): | ||
fetcher = self.get_thumbnail_fetcher(resource_id) | ||
if fetcher is None: | ||
return HttpResponseNotFound() | ||
thumbnail = fetcher.get_thumbnail(False) | ||
if thumbnail is not None: | ||
return HttpResponse() | ||
else: | ||
return HttpResponseNotFound() | ||
|
||
def get(self, request, resource_id): | ||
fetcher = self.get_thumbnail_fetcher(resource_id) | ||
if fetcher is None: | ||
return HttpResponseNotFound() | ||
|
||
thumbnail = fetcher.get_thumbnail(True) | ||
if thumbnail is not None: | ||
return HttpResponse(thumbnail[0], content_type=thumbnail[1]) | ||
else: | ||
return HttpResponseNotFound() | ||
|
||
def get_thumbnail_fetcher(self, resource_id): | ||
factory = SearchThumbnailFetcherFactory() | ||
fetcher = factory.create_thumbnail_fetcher(resource_id) | ||
return fetcher |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters