Let some code support "media without slug"
authorElrond <elrond+mediagoblin.org@samba-tng.org>
Sun, 22 Jan 2012 16:11:43 +0000 (17:11 +0100)
committerElrond <elrond+mediagoblin.org@samba-tng.org>
Sat, 28 Jan 2012 12:53:55 +0000 (13:53 +0100)
The upcoming SQL database will allow having media without a
slug again. This might especially be useful for API
support, where the uploaded image (media) does not have
*any* information to generate a slug from.

This code change mostly allows the sql backend to have no
slug in its model and improves some parts to properly
handle that.

It specifically does not have any web frontend support to
edit/create MediaEntries with no slug.

mediagoblin/db/mixin.py
mediagoblin/db/sql/models.py
mediagoblin/templates/mediagoblin/user_pages/media.html

index d587ccb42978f0daaecea8039a12462ae96240f5..254dbcff083255e0a6f81face4057839d5f84df8 100644 (file)
@@ -63,6 +63,10 @@ class MediaEntryMixin(object):
     def main_mediafile(self):
         pass
 
+    @property
+    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
@@ -71,18 +75,11 @@ class MediaEntryMixin(object):
         """
         uploader = self.get_uploader
 
-        if self.get('slug'):
-            return urlgen(
-                'mediagoblin.user_pages.media_home',
-                user=uploader.username,
-                media=self.slug,
-                **extra_args)
-        else:
-            return urlgen(
-                'mediagoblin.user_pages.media_home',
-                user=uploader.username,
-                media=unicode(self._id),
-                **extra_args)
+        return urlgen(
+            'mediagoblin.user_pages.media_home',
+            user=uploader.username,
+            media=self.slug_or_id,
+            **extra_args)
 
     def get_fail_exception(self):
         """
index 95147d50830bf159148d08b75b142161526e81d3..6232fff856b3df0606f7edf863fd73ffa47843c8 100644 (file)
@@ -72,7 +72,7 @@ class MediaEntry(Base, MediaEntryMixin):
     id = Column(Integer, primary_key=True)
     uploader = Column(Integer, ForeignKey('users.id'), nullable=False)
     title = Column(Unicode, nullable=False)
-    slug = Column(Unicode, nullable=False)
+    slug = Column(Unicode)
     created = Column(DateTime, nullable=False, default=datetime.datetime.now)
     description = Column(UnicodeText) # ??
     description_html = Column(UnicodeText) # ??
index cbe26cbfb546553034625fe68b8a4676e98aa2ea..9df3dfbc4ce591b46c57e01730a7c24e1c709482 100644 (file)
             <a href="{{ request.urlgen('mediagoblin.user_pages.media_home.view_comment',
                    comment = comment._id,
                    user = media.get_uploader.username,
-                   media = media.slug) }}#comment">
+                   media = media.slug_or_id) }}#comment">
               {{ comment.created.strftime("%I:%M%p %Y-%m-%d") }}
             </a>
           </div>