Attachment support in the SQL backend
attachments working with the sql backend. - SQL Schema for attachment files, ordering attachments by their name, not by the submission order (as earlier). - Dot-Notation for attachments, where missing. - convert existing attachments over from mongo -> sql
This commit is contained in:
parent
e9f87f728c
commit
3502958113
@ -19,7 +19,7 @@ from mediagoblin.init import setup_global_and_app_config, setup_database
|
|||||||
from mediagoblin.db.mongo.util import ObjectId
|
from mediagoblin.db.mongo.util import ObjectId
|
||||||
|
|
||||||
from mediagoblin.db.sql.models import (Base, User, MediaEntry, MediaComment,
|
from mediagoblin.db.sql.models import (Base, User, MediaEntry, MediaComment,
|
||||||
Tag, MediaTag, MediaFile)
|
Tag, MediaTag, MediaFile, MediaAttachmentFile)
|
||||||
from mediagoblin.db.sql.open import setup_connection_and_db_from_config as \
|
from mediagoblin.db.sql.open import setup_connection_and_db_from_config as \
|
||||||
sql_connect
|
sql_connect
|
||||||
from mediagoblin.db.mongo.open import setup_connection_and_db_from_config as \
|
from mediagoblin.db.mongo.open import setup_connection_and_db_from_config as \
|
||||||
@ -92,6 +92,15 @@ def convert_media_entries(mk_db):
|
|||||||
new_file.media_entry = new_entry.id
|
new_file.media_entry = new_entry.id
|
||||||
Session.add(new_file)
|
Session.add(new_file)
|
||||||
|
|
||||||
|
for attachment in entry.attachment_files:
|
||||||
|
new_attach = MediaAttachmentFile(
|
||||||
|
name=attachment["name"],
|
||||||
|
filepath=attachment["filepath"],
|
||||||
|
created=attachment["created"]
|
||||||
|
)
|
||||||
|
new_attach.media_entry = new_entry.id
|
||||||
|
Session.add(new_attach)
|
||||||
|
|
||||||
session.commit()
|
session.commit()
|
||||||
session.close()
|
session.close()
|
||||||
|
|
||||||
|
@ -118,6 +118,15 @@ class MediaEntry(Base, MediaEntryMixin):
|
|||||||
creator=lambda k, v: MediaFile(name=k, file_path=v)
|
creator=lambda k, v: MediaFile(name=k, file_path=v)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
attachment_files_helper = relationship("MediaAttachmentFile",
|
||||||
|
cascade="all, delete-orphan",
|
||||||
|
order_by="MediaAttachmentFile.created"
|
||||||
|
)
|
||||||
|
attachment_files = association_proxy("attachment_files_helper", "dict_view",
|
||||||
|
creator=lambda v: MediaAttachmentFile(
|
||||||
|
name=v["name"], filepath=v["filepath"])
|
||||||
|
)
|
||||||
|
|
||||||
tags_helper = relationship("MediaTag",
|
tags_helper = relationship("MediaTag",
|
||||||
cascade="all, delete-orphan"
|
cascade="all, delete-orphan"
|
||||||
)
|
)
|
||||||
@ -127,7 +136,6 @@ class MediaEntry(Base, MediaEntryMixin):
|
|||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
# media_data
|
# media_data
|
||||||
# attachment_files
|
|
||||||
# fail_error
|
# fail_error
|
||||||
|
|
||||||
_id = SimpleFieldAlias("id")
|
_id = SimpleFieldAlias("id")
|
||||||
@ -177,6 +185,23 @@ class MediaFile(Base):
|
|||||||
return "<MediaFile %s: %r>" % (self.name, self.file_path)
|
return "<MediaFile %s: %r>" % (self.name, self.file_path)
|
||||||
|
|
||||||
|
|
||||||
|
class MediaAttachmentFile(Base):
|
||||||
|
__tablename__ = "core__attachment_files"
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True)
|
||||||
|
media_entry = Column(
|
||||||
|
Integer, ForeignKey(MediaEntry.id),
|
||||||
|
nullable=False)
|
||||||
|
name = Column(Unicode, nullable=False)
|
||||||
|
filepath = Column(PathTupleWithSlashes)
|
||||||
|
created = Column(DateTime, nullable=False, default=datetime.datetime.now)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def dict_view(self):
|
||||||
|
"""A dict like view on this object"""
|
||||||
|
return DictReadAttrProxy(self)
|
||||||
|
|
||||||
|
|
||||||
class Tag(Base):
|
class Tag(Base):
|
||||||
__tablename__ = "tags"
|
__tablename__ = "tags"
|
||||||
|
|
||||||
|
@ -120,7 +120,7 @@ def edit_attachments(request, media):
|
|||||||
finally:
|
finally:
|
||||||
request.POST['attachment_file'].file.close()
|
request.POST['attachment_file'].file.close()
|
||||||
|
|
||||||
media['attachment_files'].append(dict(
|
media.attachment_files.append(dict(
|
||||||
name=request.POST['attachment_name'] \
|
name=request.POST['attachment_name'] \
|
||||||
or request.POST['attachment_file'].filename,
|
or request.POST['attachment_file'].filename,
|
||||||
filepath=attachment_public_filepath,
|
filepath=attachment_public_filepath,
|
||||||
|
@ -27,6 +27,6 @@ def delete_media_files(media):
|
|||||||
mg_globals.public_store.delete_file(
|
mg_globals.public_store.delete_file(
|
||||||
listpath)
|
listpath)
|
||||||
|
|
||||||
for attachment in media['attachment_files']:
|
for attachment in media.attachment_files:
|
||||||
mg_globals.public_store.delete_file(
|
mg_globals.public_store.delete_file(
|
||||||
attachment['filepath'])
|
attachment['filepath'])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user