Added a url_for_self method for generating mediaentry links
authorChristopher Allan Webber <cwebber@dustycloud.org>
Fri, 20 May 2011 23:16:10 +0000 (18:16 -0500)
committerChristopher Allan Webber <cwebber@dustycloud.org>
Fri, 20 May 2011 23:16:10 +0000 (18:16 -0500)
This allows for optionally making the url based off of slugs or ids

mediagoblin/db/models.py
mediagoblin/templates/mediagoblin/root.html
mediagoblin/templates/mediagoblin/utils/object_gallery.html

index 1bc1da60c6275a88ce3ff8c0601fb70db0193a62..8e7889eba5a06f512bd7ab31f055d41843a7ef0d 100644 (file)
@@ -117,6 +117,24 @@ class MediaEntry(Document):
         if duplicate:
             self['slug'] = "%s-%s" % (self['_id'], self['slug'])
 
+    def url_for_self(self, urlgen):
+        """
+        Generate an appropriate url for ourselves
+
+        Use a slug if we have one, else use our '_id'.
+        """
+        if self.get('slug'):
+            return urlgen(
+                'mediagoblin.user_pages.media_home',
+                user=self['uploader']['username'],
+                media=self['slug'])
+        else:
+            return urlgen(
+                'mediagoblin.user_pages.media_home',
+                user=self['uploader']['username'],
+                media=unicode(self['_id']))
+
+
 REGISTER_MODELS = [MediaEntry, User]
 
 
index a93a7c75b80f91b55417e9c68f697e3aa9634442..059266879005db040869e5d82db56badc958d9c9 100644 (file)
@@ -44,8 +44,7 @@
     <ul>
       {% for entry in media_entries %}
         <li>
-          <a href="{{ request.urlgen('mediagoblin.user_pages.media_home',
-                   user= entry.uploader.username, m_id= entry._id) }}">
+          <a href="{{ entry.url_for_self(request.urlgen) }}">
             <img src="{{ request.app.public_store.file_url(
                       entry['media_files']['thumb']) }}" /></a>
         </li>
index 8ae337f58e3c5c397e02d3aa875444a17a46fe60..30497f4757b8bd2fd4cf43a0e7f268cb6e6bb099 100644 (file)
@@ -22,8 +22,7 @@
       <ul>
         {% for entry in media_entries %}
           <li>
-            <a href="{{ request.urlgen('mediagoblin.user_pages.media_home',
-                 user= entry.uploader.username, m_id= entry._id) }}">
+            <a href="{{ entry.url_for_self(request.urlgen) }}">
             <img src="{{ request.app.public_store.file_url(
                   entry['media_files']['thumb']) }}" /></a>
           </li>