-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from scaife-viewer/core/collections-sort
Add hooks to customize sorting of CTS collections
- Loading branch information
Showing
4 changed files
with
62 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,35 @@ | ||
import importlib | ||
|
||
from django.conf import settings # noqa | ||
from django.core.exceptions import ImproperlyConfigured | ||
|
||
from appconf import AppConf | ||
|
||
|
||
def load_path_attr(path): | ||
i = path.rfind(".") | ||
module, attr = path[:i], path[i + 1 :] | ||
try: | ||
mod = importlib.import_module(module) | ||
except ImportError as e: | ||
raise ImproperlyConfigured("Error importing {0}: '{1}'".format(module, e)) | ||
try: | ||
attr = getattr(mod, attr) | ||
except AttributeError: | ||
raise ImproperlyConfigured( | ||
"Module '{0}' does not define a '{1}'".format(module, attr) | ||
) | ||
return attr | ||
|
||
|
||
class CoreAppConf(AppConf): | ||
ALLOW_TRAILING_COLON = False | ||
|
||
# Other | ||
HOOKSET = "scaife_viewer.core.hooks.DefaultHookSet" | ||
|
||
class Meta: | ||
prefix = "scaife_viewer_core" | ||
|
||
def configure_hookset(self, value): | ||
return load_path_attr(value)() |
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,19 @@ | ||
class DefaultHookSet: | ||
def sort_text_groups(self, text_groups): | ||
return sorted(text_groups, key=lambda tg: tg.urn) | ||
|
||
def sort_works(self, works): | ||
return sorted(works, key=lambda w: w.urn) | ||
|
||
def sort_texts(self, texts): | ||
return sorted(texts, key=lambda t: (t.kind, t.label)) | ||
|
||
|
||
class HookProxy: | ||
def __getattr__(self, attr): | ||
from .conf import settings # noqa; avoids race condition | ||
|
||
return getattr(settings.SCAIFE_VIEWER_CORE_HOOKSET, attr) | ||
|
||
|
||
hookset = HookProxy() |