from mediagoblin.db.mongo.util import ObjectId
from mediagoblin.db.sql.models import (Base, User, MediaEntry, MediaComment,
- Tag, MediaTag)
+ Tag, MediaTag, MediaFile)
from mediagoblin.db.sql.open import setup_connection_and_db_from_config as \
sql_connect
from mediagoblin.db.mongo.open import setup_connection_and_db_from_config as \
session.flush()
add_obj_ids(entry, new_entry)
+ for key, value in entry.media_files.iteritems():
+ new_file = MediaFile(name=key, file_path=value)
+ new_file.media_entry = new_entry.id
+ Session.add(new_file)
+
session.commit()
session.close()
--- /dev/null
+from sqlalchemy.types import TypeDecorator, Unicode
+
+
+class PathTupleWithSlashes(TypeDecorator):
+ "Represents a Tuple of strings as a slash separated string."
+
+ impl = Unicode
+
+ def process_bind_param(self, value, dialect):
+ if value is not None:
+ assert len(value), "Does not support empty lists"
+ value = '/'.join(value)
+ return value
+
+ def process_result_value(self, value, dialect):
+ if value is not None:
+ value = tuple(value.split('/'))
+ return value
Column, Integer, Unicode, UnicodeText, DateTime, Boolean, ForeignKey,
UniqueConstraint)
from sqlalchemy.orm import relationship
+from sqlalchemy.orm.collections import attribute_mapped_collection
+from sqlalchemy.ext.associationproxy import association_proxy
+from mediagoblin.db.sql.extratypes import PathTupleWithSlashes
from mediagoblin.db.sql.base import GMGTableBase
from mediagoblin.db.mixin import UserMixin, MediaEntryMixin
fail_error = Column(Unicode)
fail_metadata = Column(UnicodeText)
- queued_media_file = Column(Unicode)
+ queued_media_file = Column(PathTupleWithSlashes)
queued_task_id = Column(Unicode)
get_uploader = relationship(User)
+ media_files_helper = relationship("MediaFile",
+ collection_class=attribute_mapped_collection("name"),
+ cascade="all, delete-orphan"
+ )
+ media_files = association_proxy('media_files_helper', 'file_path',
+ creator=lambda k,v: MediaFile(name=k, file_path=v)
+ )
+
## TODO
- # media_files
# media_data
# attachment_files
# fail_error
+class MediaFile(Base):
+ __tablename__ = "mediafiles"
+
+ media_entry = Column(
+ Integer, ForeignKey(MediaEntry.id),
+ nullable=False, primary_key=True)
+ name = Column(Unicode, primary_key=True)
+ file_path = Column(PathTupleWithSlashes)
+
+ def __repr__(self):
+ return "<MediaFile %s: %r>" % (self.name, self.file_path)
+
+
class Tag(Base):
__tablename__ = "tags"