Change structure of MediaEntry and add migration
This makes the changes needed for federating MediaEntry objects as well as adding the migration and necessary methods to get the public_id just in time (JIT).
This commit is contained in:
parent
c9f6afe291
commit
35fbad47d7
@ -1461,14 +1461,14 @@ def federation_user_create_tables(db):
|
|||||||
"""
|
"""
|
||||||
Create all the tables
|
Create all the tables
|
||||||
"""
|
"""
|
||||||
metadata = MetaData(bind=db.bind)
|
|
||||||
user_table = inspect_table(metadata, "core__users")
|
|
||||||
|
|
||||||
# Create tables needed
|
# Create tables needed
|
||||||
LocalUser_V0.__table__.create(db.bind)
|
LocalUser_V0.__table__.create(db.bind)
|
||||||
RemoteUser_V0.__table__.create(db.bind)
|
RemoteUser_V0.__table__.create(db.bind)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
|
metadata = MetaData(bind=db.bind)
|
||||||
|
user_table = inspect_table(metadata, "core__users")
|
||||||
|
|
||||||
# Create the fields
|
# Create the fields
|
||||||
updated_column = Column(
|
updated_column = Column(
|
||||||
"updated",
|
"updated",
|
||||||
@ -1575,5 +1575,44 @@ def federation_remove_fields(db):
|
|||||||
wants_notifications_column = user_table.columns["wants_notifications"]
|
wants_notifications_column = user_table.columns["wants_notifications"]
|
||||||
wants_notifications_column.drop()
|
wants_notifications_column.drop()
|
||||||
|
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
@RegisterMigration(35, MIGRATIONS)
|
||||||
|
def federation_media_entry(db):
|
||||||
|
metadata = MetaData(bind=db.bind)
|
||||||
|
media_entry_table = inspect_table(metadata, "core__media_entries")
|
||||||
|
|
||||||
|
# Add new fields
|
||||||
|
public_id_column = Column(
|
||||||
|
"public_id",
|
||||||
|
Unicode,
|
||||||
|
unique=True,
|
||||||
|
nullable=True
|
||||||
|
)
|
||||||
|
public_id_column.create(
|
||||||
|
media_entry_table,
|
||||||
|
unique_name="media_public_id"
|
||||||
|
)
|
||||||
|
|
||||||
|
remote_column = Column(
|
||||||
|
"remote",
|
||||||
|
Boolean,
|
||||||
|
default=False
|
||||||
|
)
|
||||||
|
remote_column.create(media_entry_table)
|
||||||
|
|
||||||
|
updated_column = Column(
|
||||||
|
"updated",
|
||||||
|
DateTime,
|
||||||
|
default=datetime.datetime.utcnow,
|
||||||
|
)
|
||||||
|
updated_column.create(media_entry_table)
|
||||||
|
|
||||||
|
# Data migration
|
||||||
|
for entry in db.execute(media_entry_table.select()):
|
||||||
|
db.execute(media_entry_table.update().values(
|
||||||
|
updated=entry.created,
|
||||||
|
remote=False
|
||||||
|
))
|
||||||
|
|
||||||
db.commit()
|
db.commit()
|
||||||
|
@ -196,6 +196,25 @@ class MediaEntryMixin(GenerateSlugMixin):
|
|||||||
media=self.slug_or_id,
|
media=self.slug_or_id,
|
||||||
**extra_args)
|
**extra_args)
|
||||||
|
|
||||||
|
def get_public_id(self, request):
|
||||||
|
""" Returns the public_id of the MediaEntry
|
||||||
|
|
||||||
|
If the MediaEntry has no public ID one will be produced from the
|
||||||
|
current request.
|
||||||
|
"""
|
||||||
|
if self.public_id is None:
|
||||||
|
self.public_id = request.urlgen(
|
||||||
|
"mediagoblin.api.object",
|
||||||
|
object_type=self.object_type,
|
||||||
|
id=self.id,
|
||||||
|
qualified=True
|
||||||
|
)
|
||||||
|
# We need to ensure this ID is reused once we've given it out.
|
||||||
|
self.save()
|
||||||
|
|
||||||
|
return self.public_id
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def thumb_url(self):
|
def thumb_url(self):
|
||||||
"""Return the thumbnail URL (for usage in templates)
|
"""Return the thumbnail URL (for usage in templates)
|
||||||
|
@ -494,11 +494,12 @@ class MediaEntry(Base, MediaEntryMixin):
|
|||||||
__tablename__ = "core__media_entries"
|
__tablename__ = "core__media_entries"
|
||||||
|
|
||||||
id = Column(Integer, primary_key=True)
|
id = Column(Integer, primary_key=True)
|
||||||
|
public_id = Column(Unicode, unique=True, nullable=True)
|
||||||
|
remote = Column(Boolean, default=False)
|
||||||
|
|
||||||
uploader = Column(Integer, ForeignKey(User.id), nullable=False, index=True)
|
uploader = Column(Integer, ForeignKey(User.id), nullable=False, index=True)
|
||||||
title = Column(Unicode, nullable=False)
|
title = Column(Unicode, nullable=False)
|
||||||
slug = Column(Unicode)
|
slug = Column(Unicode)
|
||||||
created = Column(DateTime, nullable=False, default=datetime.datetime.utcnow,
|
|
||||||
index=True)
|
|
||||||
description = Column(UnicodeText) # ??
|
description = Column(UnicodeText) # ??
|
||||||
media_type = Column(Unicode, nullable=False)
|
media_type = Column(Unicode, nullable=False)
|
||||||
state = Column(Unicode, default=u'unprocessed', nullable=False)
|
state = Column(Unicode, default=u'unprocessed', nullable=False)
|
||||||
@ -508,6 +509,10 @@ class MediaEntry(Base, MediaEntryMixin):
|
|||||||
location = Column(Integer, ForeignKey("core__locations.id"))
|
location = Column(Integer, ForeignKey("core__locations.id"))
|
||||||
get_location = relationship("Location", lazy="joined")
|
get_location = relationship("Location", lazy="joined")
|
||||||
|
|
||||||
|
created = Column(DateTime, nullable=False, default=datetime.datetime.utcnow,
|
||||||
|
index=True)
|
||||||
|
updated = Column(DateTime, nullable=False, default=datetime.datetime.utcnow)
|
||||||
|
|
||||||
fail_error = Column(Unicode)
|
fail_error = Column(Unicode)
|
||||||
fail_metadata = Column(JSONEncoded)
|
fail_metadata = Column(JSONEncoded)
|
||||||
|
|
||||||
@ -681,17 +686,11 @@ class MediaEntry(Base, MediaEntryMixin):
|
|||||||
|
|
||||||
def serialize(self, request, show_comments=True):
|
def serialize(self, request, show_comments=True):
|
||||||
""" Unserialize MediaEntry to object """
|
""" Unserialize MediaEntry to object """
|
||||||
href = request.urlgen(
|
|
||||||
"mediagoblin.api.object",
|
|
||||||
object_type=self.object_type,
|
|
||||||
id=self.id,
|
|
||||||
qualified=True
|
|
||||||
)
|
|
||||||
author = self.get_uploader
|
author = self.get_uploader
|
||||||
published = UTC.localize(self.created)
|
published = UTC.localize(self.created)
|
||||||
updated = UTC.localize(self.created)
|
updated = UTC.localize(self.updated)
|
||||||
context = {
|
context = {
|
||||||
"id": href,
|
"id": self.get_public_id(request),
|
||||||
"author": author.serialize(request),
|
"author": author.serialize(request),
|
||||||
"objectType": self.object_type,
|
"objectType": self.object_type,
|
||||||
"url": self.url_for_self(request.urlgen, qualified=True),
|
"url": self.url_for_self(request.urlgen, qualified=True),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user