aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorElrond <elrond+mediagoblin.org@samba-tng.org>2012-02-24 21:16:02 +0100
committerElrond <elrond+mediagoblin.org@samba-tng.org>2012-03-08 21:11:18 +0100
commita9dac7c83df860ed66ef187411394d25afd77598 (patch)
treeaa14b34a0d50f8223afe1699d7e06609f9cfa131
parent703d09b9854087bbe620cb8f514e04d2447888ae (diff)
downloadmediagoblin-a9dac7c83df860ed66ef187411394d25afd77598.tar.lz
mediagoblin-a9dac7c83df860ed66ef187411394d25afd77598.tar.xz
mediagoblin-a9dac7c83df860ed66ef187411394d25afd77598.zip
Normalize MediaFile.name (make it a foreignkey)
The name part of a MediaFile is only using a very limited number of items. Currently things like "original" or "thumb". So instead of storing the string on each entry, just store a short integer referencing the FileKeynames table and have the appropiate string there.
-rw-r--r--mediagoblin/db/sql/models.py35
1 files changed, 32 insertions, 3 deletions
diff --git a/mediagoblin/db/sql/models.py b/mediagoblin/db/sql/models.py
index d0d1a32d..38c32e14 100644
--- a/mediagoblin/db/sql/models.py
+++ b/mediagoblin/db/sql/models.py
@@ -23,7 +23,7 @@ import datetime
from sqlalchemy import (
Column, Integer, Unicode, UnicodeText, DateTime, Boolean, ForeignKey,
- UniqueConstraint)
+ UniqueConstraint, PrimaryKeyConstraint, SmallInteger)
from sqlalchemy.orm import relationship
from sqlalchemy.orm.collections import attribute_mapped_collection
from sqlalchemy.sql.expression import desc
@@ -177,6 +177,26 @@ class MediaEntry(Base, MediaEntryMixin):
pass
+class FileKeynames(Base):
+ """
+ keywords for various places.
+ currently the MediaFile keys
+ """
+ __tablename__ = "core__file_keynames"
+ id = Column(Integer, primary_key=True)
+ name = Column(Unicode, unique=True)
+
+ def __repr__(self):
+ return "<FileKeyname %r: %r>" % (self.id, self.name)
+
+ @classmethod
+ def find_or_new(cls, name):
+ t = cls.query.filter_by(name=name).first()
+ if t is not None:
+ return t
+ return cls(name=name)
+
+
class MediaFile(Base):
"""
TODO: Highly consider moving "name" into a new table.
@@ -186,13 +206,22 @@ class MediaFile(Base):
media_entry = Column(
Integer, ForeignKey(MediaEntry.id),
- nullable=False, primary_key=True)
- name = Column(Unicode, primary_key=True)
+ nullable=False)
+ name_id = Column(SmallInteger, ForeignKey(FileKeynames.id), nullable=False)
file_path = Column(PathTupleWithSlashes)
+ __table_args__ = (
+ PrimaryKeyConstraint('media_entry', 'name_id'),
+ {})
+
def __repr__(self):
return "<MediaFile %s: %r>" % (self.name, self.file_path)
+ name_helper = relationship(FileKeynames, lazy="joined", innerjoin=True)
+ name = association_proxy('name_helper', 'name',
+ creator=FileKeynames.find_or_new
+ )
+
class MediaAttachmentFile(Base):
__tablename__ = "core__attachment_files"