Ticket #944: 0001-Add-__repr__-for-Collection-and-CollectionItem.patch

File 0001-Add-__repr__-for-Collection-and-CollectionItem.patch, 2.3 KB (added by Odin Hørthe Omdal (Velmont), 10 years ago)
  • mediagoblin/db/models.py

    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 b TODO: indexes on foreignkeys, where useful.  
    2020
    2121import logging
    2222import datetime
    23 import base64
    2423
    2524from sqlalchemy import Column, Integer, Unicode, UnicodeText, DateTime, \
    2625        Boolean, ForeignKey, UniqueConstraint, PrimaryKeyConstraint, \
    class MediaComment(Base, MediaCommentMixin):  
    683682        # Validate inReplyTo has ID
    684683        if "id" not in data["inReplyTo"]:
    685684            return False
    686            
     685
    687686        # Validate that the ID is correct
    688687        try:
    689688            media_id = int(data["inReplyTo"]["id"])
    690689        except ValueError:
    691690            return False
    692        
     691
    693692        media = MediaEntry.query.filter_by(id=media_id).first()
    694693        if media is None:
    695694            return False
    class Collection(Base, CollectionMixin):  
    734733        return CollectionItem.query.filter_by(
    735734            collection=self.id).order_by(order_col)
    736735
     736    def __repr__(self):
     737        safe_title = self.title.encode('ascii', 'replace')
     738        return (u'<{classname} #{id}: {title} by {creator}>').format(
     739            id=self.id,
     740            classname=self.__class__.__name__,
     741            creator=self.creator,
     742            title=safe_title)
     743
    737744
    738745class CollectionItem(Base, CollectionItemMixin):
    739746    __tablename__ = "core__collection_items"
    class CollectionItem(Base, CollectionItemMixin):  
    763770        """A dict like view on this object"""
    764771        return DictReadAttrProxy(self)
    765772
     773    def __repr__(self):
     774        return '<{classname} #{id}: Entry {entry} in {collection}>'.format(
     775            id=self.id,
     776            classname=self.__class__.__name__,
     777            collection=self.collection,
     778            entry=self.media_entry)
     779
    766780
    767781class ProcessingMetaData(Base):
    768782    __tablename__ = 'core__processing_metadata'