aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/db/sql/models.py
diff options
context:
space:
mode:
authorElrond <elrond+mediagoblin.org@samba-tng.org>2012-01-04 22:00:44 +0100
committerElrond <elrond+mediagoblin.org@samba-tng.org>2012-01-28 19:32:43 +0100
commitde91730336951f27cb83c3e60ac2d071bb3d4cef (patch)
tree8c0a93e9518c4b130855e15eedd0ecad1c39e950 /mediagoblin/db/sql/models.py
parentebc0e382398fbc665e6b133bd3925c5352d3d51d (diff)
downloadmediagoblin-de91730336951f27cb83c3e60ac2d071bb3d4cef.tar.lz
mediagoblin-de91730336951f27cb83c3e60ac2d071bb3d4cef.tar.xz
mediagoblin-de91730336951f27cb83c3e60ac2d071bb3d4cef.zip
Nearly complete support for Tags
These changes allow all of the rest of the code to use tags in sql as they were used on mongo. It's not efficient at all, as changing tags usually means to remove all old tags and adding all new. The only problem here is: Old slugs for tags are not removed, because they're shared across all MediaTags and dropping orphans is not always easy.
Diffstat (limited to 'mediagoblin/db/sql/models.py')
-rw-r--r--mediagoblin/db/sql/models.py40
1 files changed, 36 insertions, 4 deletions
diff --git a/mediagoblin/db/sql/models.py b/mediagoblin/db/sql/models.py
index 6232fff8..9abd8ec7 100644
--- a/mediagoblin/db/sql/models.py
+++ b/mediagoblin/db/sql/models.py
@@ -26,7 +26,7 @@ from sqlalchemy.sql.expression import desc
from sqlalchemy.ext.associationproxy import association_proxy
from mediagoblin.db.sql.extratypes import PathTupleWithSlashes
-from mediagoblin.db.sql.base import Base
+from mediagoblin.db.sql.base import Base, DictReadAttrProxy
from mediagoblin.db.mixin import UserMixin, MediaEntryMixin
@@ -101,6 +101,13 @@ class MediaEntry(Base, MediaEntryMixin):
creator=lambda k, v: MediaFile(name=k, file_path=v)
)
+ tags_helper = relationship("MediaTag",
+ cascade="all, delete-orphan"
+ )
+ tags = association_proxy("tags_helper", "dict_view",
+ creator=lambda v: MediaTag(name=v["name"], slug=v["slug"])
+ )
+
## TODO
# media_data
# attachment_files
@@ -153,22 +160,47 @@ class Tag(Base):
id = Column(Integer, primary_key=True)
slug = Column(Unicode, nullable=False, unique=True)
+ def __repr__(self):
+ return "<Tag %r: %r>" % (self.id, self.slug)
+
+ @classmethod
+ def find_or_new(cls, slug):
+ t = cls.query.filter_by(slug=slug).first()
+ if t is not None:
+ return t
+ return cls(slug=slug)
+
class MediaTag(Base):
__tablename__ = "media_tags"
id = Column(Integer, primary_key=True)
- tag = Column(Integer, ForeignKey('tags.id'), nullable=False)
- name = Column(Unicode)
media_entry = Column(
- Integer, ForeignKey('media_entries.id'),
+ Integer, ForeignKey(MediaEntry.id),
nullable=False)
+ tag = Column(Integer, ForeignKey('tags.id'), nullable=False)
+ name = Column(Unicode)
# created = Column(DateTime, nullable=False, default=datetime.datetime.now)
__table_args__ = (
UniqueConstraint('tag', 'media_entry'),
{})
+ tag_helper = relationship(Tag)
+ slug = association_proxy('tag_helper', 'slug',
+ creator=Tag.find_or_new
+ )
+
+ def __init__(self, name, slug):
+ Base.__init__(self)
+ self.name = name
+ self.tag_helper = Tag.find_or_new(slug)
+
+ @property
+ def dict_view(self):
+ """A dict like view on this object"""
+ return DictReadAttrProxy(self)
+
class MediaComment(Base):
__tablename__ = "media_comments"