From 5e63d4d1fc37d2bcddbd86a8f88897d1d7883f1a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Odin=20H=C3=B8rthe=20Omdal?= <odinho@opera.com>
Date: Mon, 11 Aug 2014 23:52:23 +0200
Subject: [PATCH] Add __repr__ for Collection and CollectionItem

---
 mediagoblin/db/models.py | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/mediagoblin/db/models.py b/mediagoblin/db/models.py
index 932ba07..a07d676 100644
--- a/mediagoblin/db/models.py
+++ b/mediagoblin/db/models.py
@@ -20,7 +20,6 @@ TODO: indexes on foreignkeys, where useful.
 
 import logging
 import datetime
-import base64
 
 from sqlalchemy import Column, Integer, Unicode, UnicodeText, DateTime, \
         Boolean, ForeignKey, UniqueConstraint, PrimaryKeyConstraint, \
@@ -683,13 +682,13 @@ class MediaComment(Base, MediaCommentMixin):
         # Validate inReplyTo has ID
         if "id" not in data["inReplyTo"]:
             return False
-            
+
         # Validate that the ID is correct
         try:
             media_id = int(data["inReplyTo"]["id"])
         except ValueError:
             return False
-        
+
         media = MediaEntry.query.filter_by(id=media_id).first()
         if media is None:
             return False
@@ -734,6 +733,14 @@ class Collection(Base, CollectionMixin):
         return CollectionItem.query.filter_by(
             collection=self.id).order_by(order_col)
 
+    def __repr__(self):
+        safe_title = self.title.encode('ascii', 'replace')
+        return (u'<{classname} #{id}: {title} by {creator}>').format(
+            id=self.id,
+            classname=self.__class__.__name__,
+            creator=self.creator,
+            title=safe_title)
+
 
 class CollectionItem(Base, CollectionItemMixin):
     __tablename__ = "core__collection_items"
@@ -763,6 +770,13 @@ class CollectionItem(Base, CollectionItemMixin):
         """A dict like view on this object"""
         return DictReadAttrProxy(self)
 
+    def __repr__(self):
+        return '<{classname} #{id}: Entry {entry} in {collection}>'.format(
+            id=self.id,
+            classname=self.__class__.__name__,
+            collection=self.collection,
+            entry=self.media_entry)
+
 
 class ProcessingMetaData(Base):
     __tablename__ = 'core__processing_metadata'
-- 
1.9.1

