From 138a18fd6ed18b7415bc3bdcf9c0300c3d6a8c85 Mon Sep 17 00:00:00 2001 From: Sebastian Spaeth Date: Mon, 26 Nov 2012 14:55:33 +0100 Subject: [PATCH] Implement licenses.get_license_by_url MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Rather than exploding in the user's face (for example if we custom-configure licenses in our MG instance, and there are still media with now "unknown" licenses in the db), simply return a License object as a fallback, where all attributes are set to the URL we were handed. Signed-off-by: Sebastian Spaeth --- mediagoblin/db/mixin.py | 2 +- mediagoblin/tools/licenses.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/mediagoblin/db/mixin.py b/mediagoblin/db/mixin.py index 7b4bafce..a8436c70 100644 --- a/mediagoblin/db/mixin.py +++ b/mediagoblin/db/mixin.py @@ -139,7 +139,7 @@ class MediaEntryMixin(object): def get_license_data(self): """Return license dict for requested license""" - return licenses.SUPPORTED_LICENSES[self.license or ""] + return licenses.get_license_by_url(self.license or "") def exif_display_iter(self): from mediagoblin.tools.exif import USEFUL_TAGS diff --git a/mediagoblin/tools/licenses.py b/mediagoblin/tools/licenses.py index cda3c1f6..a964980e 100644 --- a/mediagoblin/tools/licenses.py +++ b/mediagoblin/tools/licenses.py @@ -50,6 +50,16 @@ SORTED_LICENSES = [ SUPPORTED_LICENSES = dict(((l.uri, l) for l in SORTED_LICENSES)) +def get_license_by_url(url): + """Look up a license by its url and return the License object""" + try: + return SUPPORTED_LICENSES[url] + except KeyError: + # in case of an unknown License, just display the url given + # rather than exploding in the user's face. + return License(url, url, url) + + def licenses_as_choices(): """List of (uri, abbreviation) tuples for HTML choice field population -- 2.25.1