aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/db
diff options
context:
space:
mode:
authorSebastian Spaeth <Sebastian@SSpaeth.de>2012-12-04 09:57:56 +0100
committerSebastian Spaeth <Sebastian@SSpaeth.de>2012-12-04 15:15:41 +0100
commit5f8b4ae895ecb228c5f5d615818ffe0a06a30473 (patch)
tree2ead38386621af120360ae9f1b5b59e235bc060a /mediagoblin/db
parentc39b9afc83d5e0bfd9312d762a7b16955ba949ca (diff)
downloadmediagoblin-5f8b4ae895ecb228c5f5d615818ffe0a06a30473.tar.lz
mediagoblin-5f8b4ae895ecb228c5f5d615818ffe0a06a30473.tar.xz
mediagoblin-5f8b4ae895ecb228c5f5d615818ffe0a06a30473.zip
make media_manager a property of MediaEntry in mixin.py
In all cases where get_media_manager(_media_type_as_string) was called in our code base we ultimately passed in a "MediaEntry().media_type" to get the matching MEDIA_MANAGER. It so makes sense to make this a function of the MediaEntry rather than a global function in mediagoblin.media_types and passing around media_entry.media_type as arguments all the time. It saves a few import statements and arguments. I also made it so the Media_manager property is cached for subsequent calls, although I am not too sure that this is needed (there are other cases for which this would make more sense) Also add a get_media_manager test to the media submission tests. It submits an image and checks that both media.media_type and media.media_manager return the right thing. Not sure if these tests could not be merged with an existing submission test, but it can't hurt to have things explicit. TODO: Right now we iterate through all existing media_managers to find the right one based on the string of its module name. This should be made a simple dict lookup to avoid all the extra work. Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
Diffstat (limited to 'mediagoblin/db')
-rw-r--r--mediagoblin/db/mixin.py24
1 files changed, 21 insertions, 3 deletions
diff --git a/mediagoblin/db/mixin.py b/mediagoblin/db/mixin.py
index a8436c70..3f395e90 100644
--- a/mediagoblin/db/mixin.py
+++ b/mediagoblin/db/mixin.py
@@ -27,9 +27,11 @@ These functions now live here and get "mixed in" into the
real objects.
"""
+from werkzeug.utils import cached_property
+
from mediagoblin import mg_globals
from mediagoblin.auth import lib as auth_lib
-from mediagoblin.media_types import get_media_manager
+from mediagoblin.media_types import get_media_managers, FileTypeNotSupported
from mediagoblin.tools import common, licenses
from mediagoblin.tools.text import cleaned_markdown_conversion
from mediagoblin.tools.url import slugify
@@ -99,6 +101,7 @@ class MediaEntryMixin(object):
def slug_or_id(self):
return (self.slug or self._id)
+
def url_for_self(self, urlgen, **extra_args):
"""
Generate an appropriate url for ourselves
@@ -125,11 +128,26 @@ class MediaEntryMixin(object):
else:
# No thumbnail in media available. Get the media's
# MEDIA_MANAGER for the fallback icon and return static URL
- # Raise FileTypeNotSupported in case no such manager is enabled
- manager = get_media_manager(self.media_type)
+ # Raises FileTypeNotSupported in case no such manager is enabled
+ manager = self.media_manager
thumb_url = mg_globals.app.staticdirector(manager[u'default_thumb'])
return thumb_url
+ @cached_property
+ def media_manager(self):
+ """Returns the MEDIA_MANAGER of the media's media_type
+
+ Raises FileTypeNotSupported in case no such manager is enabled
+ """
+ # TODO, we should be able to make this a simple lookup rather
+ # than iterating through all media managers.
+ for media_type, manager in get_media_managers():
+ if media_type == self.media_type:
+ return manager
+ # Not found? Then raise an error
+ raise FileTypeNotSupported(
+ "MediaManager not in enabled types. Check media_types in config?")
+
def get_fail_exception(self):
"""
Get the exception that's appropriate for this error