-
Notifications
You must be signed in to change notification settings - Fork 143
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
Adds generic search thumbnail retrieval #10266
Changes from 2 commits
993f5a0
5abda8d
ff164e3
5d99e54
95a7f25
b72989f
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 |
---|---|---|
@@ -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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from logging import Logger | ||
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. Logger is unused 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. it is used on line 17 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. On 17 you are using 'logging' which is defined on line 8 imported from line 2. Logger itself is unused. I wouldn't have caught it myself had vscode not flagged it. |
||
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 """ | ||
resource = Resource.objects.get(resourceinstanceid=resource_id) | ||
try: | ||
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: | ||
pass # there is no thumbnail fetcher registered for the graph requested | ||
|
||
try: | ||
search_thumbnail_fetcher_class = cls.registry['default'] | ||
search_thumbnail_fetcher = search_thumbnail_fetcher_class(resource, **kwargs) | ||
return search_thumbnail_fetcher | ||
except KeyError: | ||
return None # there is no default thumbnail fetcher registered, return false. |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,6 +66,7 @@ | |
|
||
ELASTICSEARCH_HTTP_PORT = 9200 # this should be in increments of 200, eg: 9400, 9600, 9800 | ||
SEARCH_BACKEND = "arches.app.search.search.SearchEngine" | ||
SEARCH_THUMBNAILS = False | ||
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 should also be added to the project template's settings file 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. done |
||
# see http://elasticsearch-py.readthedocs.org/en/master/api.html#elasticsearch.Elasticsearch | ||
ELASTICSEARCH_HOSTS = [{"scheme": "https", "host": "localhost", "port": ELASTICSEARCH_HTTP_PORT}] | ||
|
||
|
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.
It's nice to have an example, but because this file isn't yet wired in as a default, I think it would be better to remove this here and add it to the docs.