Okay, we don't know that indexes are falsey, so let's make it clearer. Yeek!
[mediagoblin.git] / mediagoblin / db / mixin.py
index 9f566e36d5a7ce9d6ad15ed90d50565886ab5c40..1f2e7ec30262d762deab08704059cc15b02bd6f2 100644 (file)
@@ -29,13 +29,14 @@ real objects.
 
 import uuid
 import re
-import datetime
+from datetime import datetime
 
 from werkzeug.utils import cached_property
 
 from mediagoblin import mg_globals
-from mediagoblin.media_types import get_media_managers, FileTypeNotSupported
+from mediagoblin.media_types import FileTypeNotSupported
 from mediagoblin.tools import common, licenses
+from mediagoblin.tools.pluginapi import hook_handle
 from mediagoblin.tools.text import cleaned_markdown_conversion
 from mediagoblin.tools.url import slugify
 
@@ -45,6 +46,11 @@ class UserMixin(object):
     def bio_html(self):
         return cleaned_markdown_conversion(self.bio)
 
+    def url_for_self(self, urlgen, **kwargs):
+        """Generate a URL for this User's home page."""
+        return urlgen('mediagoblin.user_pages.user_home',
+                      user=self.username, **kwargs)
+
 
 class GenerateSlugMixin(object):
     """
@@ -196,20 +202,31 @@ class MediaEntryMixin(GenerateSlugMixin):
             thumb_url = mg_globals.app.staticdirector(manager[u'default_thumb'])
         return thumb_url
 
+    @property
+    def original_url(self):
+        """ Returns the URL for the original image
+        will return self.thumb_url if original url doesn't exist"""
+        if u"original" not in self.media_files:
+            return self.thumb_url
+        
+        return mg_globals.app.public_store.file_url(
+            self.media_files[u"original"]
+            )
+
     @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(self)
+        manager = hook_handle(('media_manager', self.media_type))
+        if manager:
+            return manager(self)
+
         # Not found?  Then raise an error
         raise FileTypeNotSupported(
-            "MediaManager not in enabled types.  Check media_types in config?")
+            "MediaManager not in enabled types. Check media_type plugins are"
+            " enabled in config?")
 
     def get_fail_exception(self):
         """
@@ -242,7 +259,7 @@ class MediaEntryMixin(GenerateSlugMixin):
 
         if 'Image DateTimeOriginal' in exif_all:
             # format date taken
-            takendate = datetime.datetime.strptime(
+            takendate = datetime.strptime(
                 exif_all['Image DateTimeOriginal']['printable'],
                 '%Y:%m:%d %H:%M:%S').date()
             taken = takendate.strftime('%B %d %Y')
@@ -288,6 +305,20 @@ class MediaCommentMixin(object):
         """
         return cleaned_markdown_conversion(self.content)
 
+    def __unicode__(self):
+        return u'<{klass} #{id} {author} "{comment}">'.format(
+            klass=self.__class__.__name__,
+            id=self.id,
+            author=self.get_author,
+            comment=self.content)
+
+    def __repr__(self):
+        return '<{klass} #{id} {author} "{comment}">'.format(
+            klass=self.__class__.__name__,
+            id=self.id,
+            author=self.get_author,
+            comment=self.content)
+
 
 class CollectionMixin(GenerateSlugMixin):
     def check_slug_used(self, slug):