| 1 | # GNU MediaGoblin -- federated, autonomous media hosting |
| 2 | # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. |
| 3 | # |
| 4 | # This program is free software: you can redistribute it and/or modify |
| 5 | # it under the terms of the GNU Affero General Public License as published by |
| 6 | # the Free Software Foundation, either version 3 of the License, or |
| 7 | # (at your option) any later version. |
| 8 | # |
| 9 | # This program is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | # GNU Affero General Public License for more details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU Affero General Public License |
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | """ |
| 18 | TODO: indexes on foreignkeys, where useful. |
| 19 | """ |
| 20 | |
| 21 | |
| 22 | import datetime |
| 23 | |
| 24 | from sqlalchemy import ( |
| 25 | Column, Integer, Unicode, UnicodeText, DateTime, Boolean, ForeignKey, |
| 26 | UniqueConstraint) |
| 27 | from sqlalchemy.orm import relationship |
| 28 | from sqlalchemy.orm.collections import attribute_mapped_collection |
| 29 | from sqlalchemy.sql.expression import desc |
| 30 | from sqlalchemy.ext.associationproxy import association_proxy |
| 31 | |
| 32 | from mediagoblin.db.sql.extratypes import PathTupleWithSlashes, JSONEncoded |
| 33 | from mediagoblin.db.sql.base import Base, DictReadAttrProxy |
| 34 | from mediagoblin.db.mixin import UserMixin, MediaEntryMixin, MediaCommentMixin |
| 35 | |
| 36 | # It's actually kind of annoying how sqlalchemy-migrate does this, if |
| 37 | # I understand it right, but whatever. Anyway, don't remove this :P |
| 38 | # |
| 39 | # We could do migration calls more manually instead of relying on |
| 40 | # this import-based meddling... |
| 41 | from migrate import changeset |
| 42 | |
| 43 | |
| 44 | class SimpleFieldAlias(object): |
| 45 | """An alias for any field""" |
| 46 | def __init__(self, fieldname): |
| 47 | self.fieldname = fieldname |
| 48 | |
| 49 | def __get__(self, instance, cls): |
| 50 | return getattr(instance, self.fieldname) |
| 51 | |
| 52 | def __set__(self, instance, val): |
| 53 | setattr(instance, self.fieldname, val) |
| 54 | |
| 55 | |
| 56 | class User(Base, UserMixin): |
| 57 | """ |
| 58 | TODO: We should consider moving some rarely used fields |
| 59 | into some sort of "shadow" table. |
| 60 | """ |
| 61 | __tablename__ = "users" |
| 62 | |
| 63 | id = Column(Integer, primary_key=True) |
| 64 | username = Column(Unicode, nullable=False, unique=True) |
| 65 | email = Column(Unicode, nullable=False) |
| 66 | created = Column(DateTime, nullable=False, default=datetime.datetime.now) |
| 67 | pw_hash = Column(Unicode, nullable=False) |
| 68 | email_verified = Column(Boolean, default=False) |
| 69 | status = Column(Unicode, default=u"needs_email_verification", nullable=False) |
| 70 | verification_key = Column(Unicode) |
| 71 | is_admin = Column(Boolean, default=False, nullable=False) |
| 72 | url = Column(Unicode) |
| 73 | bio = Column(UnicodeText) # ?? |
| 74 | fp_verification_key = Column(Unicode) |
| 75 | fp_token_expire = Column(DateTime) |
| 76 | |
| 77 | ## TODO |
| 78 | # plugin data would be in a separate model |
| 79 | |
| 80 | _id = SimpleFieldAlias("id") |
| 81 | |
| 82 | |
| 83 | class MediaEntry(Base, MediaEntryMixin): |
| 84 | """ |
| 85 | TODO: Consider fetching the media_files using join |
| 86 | """ |
| 87 | __tablename__ = "media_entries" |
| 88 | |
| 89 | id = Column(Integer, primary_key=True) |
| 90 | uploader = Column(Integer, ForeignKey('users.id'), nullable=False) |
| 91 | title = Column(Unicode, nullable=False) |
| 92 | slug = Column(Unicode) |
| 93 | created = Column(DateTime, nullable=False, default=datetime.datetime.now) |
| 94 | description = Column(UnicodeText) # ?? |
| 95 | media_type = Column(Unicode, nullable=False) |
| 96 | state = Column(Unicode, default=u'unprocessed', nullable=False) |
| 97 | # or use sqlalchemy.types.Enum? |
| 98 | license = Column(Unicode) |
| 99 | |
| 100 | fail_error = Column(Unicode) |
| 101 | fail_metadata = Column(JSONEncoded) |
| 102 | |
| 103 | queued_media_file = Column(PathTupleWithSlashes) |
| 104 | |
| 105 | queued_task_id = Column(Unicode) |
| 106 | |
| 107 | __table_args__ = ( |
| 108 | UniqueConstraint('uploader', 'slug'), |
| 109 | {}) |
| 110 | |
| 111 | get_uploader = relationship(User) |
| 112 | |
| 113 | media_files_helper = relationship("MediaFile", |
| 114 | collection_class=attribute_mapped_collection("name"), |
| 115 | cascade="all, delete-orphan" |
| 116 | ) |
| 117 | media_files = association_proxy('media_files_helper', 'file_path', |
| 118 | creator=lambda k, v: MediaFile(name=k, file_path=v) |
| 119 | ) |
| 120 | |
| 121 | tags_helper = relationship("MediaTag", |
| 122 | cascade="all, delete-orphan" |
| 123 | ) |
| 124 | tags = association_proxy("tags_helper", "dict_view", |
| 125 | creator=lambda v: MediaTag(name=v["name"], slug=v["slug"]) |
| 126 | ) |
| 127 | |
| 128 | ## TODO |
| 129 | # media_data |
| 130 | # attachment_files |
| 131 | # fail_error |
| 132 | |
| 133 | _id = SimpleFieldAlias("id") |
| 134 | |
| 135 | def get_comments(self, ascending=False): |
| 136 | order_col = MediaComment.created |
| 137 | if not ascending: |
| 138 | order_col = desc(order_col) |
| 139 | return MediaComment.query.filter_by( |
| 140 | media_entry=self.id).order_by(order_col) |
| 141 | |
| 142 | def url_to_prev(self, urlgen): |
| 143 | """get the next 'newer' entry by this user""" |
| 144 | media = MediaEntry.query.filter( |
| 145 | (MediaEntry.uploader == self.uploader) |
| 146 | & (MediaEntry.state == 'processed') |
| 147 | & (MediaEntry.id > self.id)).order_by(MediaEntry.id).first() |
| 148 | |
| 149 | if media is not None: |
| 150 | return media.url_for_self(urlgen) |
| 151 | |
| 152 | def url_to_next(self, urlgen): |
| 153 | """get the next 'older' entry by this user""" |
| 154 | media = MediaEntry.query.filter( |
| 155 | (MediaEntry.uploader == self.uploader) |
| 156 | & (MediaEntry.state == 'processed') |
| 157 | & (MediaEntry.id < self.id)).order_by(desc(MediaEntry.id)).first() |
| 158 | |
| 159 | if media is not None: |
| 160 | return media.url_for_self(urlgen) |
| 161 | |
| 162 | |
| 163 | class MediaFile(Base): |
| 164 | """ |
| 165 | TODO: Highly consider moving "name" into a new table. |
| 166 | TODO: Consider preloading said table in software |
| 167 | """ |
| 168 | __tablename__ = "mediafiles" |
| 169 | |
| 170 | media_entry = Column( |
| 171 | Integer, ForeignKey(MediaEntry.id), |
| 172 | nullable=False, primary_key=True) |
| 173 | name = Column(Unicode, primary_key=True) |
| 174 | file_path = Column(PathTupleWithSlashes) |
| 175 | |
| 176 | def __repr__(self): |
| 177 | return "<MediaFile %s: %r>" % (self.name, self.file_path) |
| 178 | |
| 179 | |
| 180 | class Tag(Base): |
| 181 | __tablename__ = "tags" |
| 182 | |
| 183 | id = Column(Integer, primary_key=True) |
| 184 | slug = Column(Unicode, nullable=False, unique=True) |
| 185 | |
| 186 | def __repr__(self): |
| 187 | return "<Tag %r: %r>" % (self.id, self.slug) |
| 188 | |
| 189 | @classmethod |
| 190 | def find_or_new(cls, slug): |
| 191 | t = cls.query.filter_by(slug=slug).first() |
| 192 | if t is not None: |
| 193 | return t |
| 194 | return cls(slug=slug) |
| 195 | |
| 196 | |
| 197 | class MediaTag(Base): |
| 198 | __tablename__ = "media_tags" |
| 199 | |
| 200 | id = Column(Integer, primary_key=True) |
| 201 | media_entry = Column( |
| 202 | Integer, ForeignKey(MediaEntry.id), |
| 203 | nullable=False) |
| 204 | tag = Column(Integer, ForeignKey('tags.id'), nullable=False) |
| 205 | name = Column(Unicode) |
| 206 | # created = Column(DateTime, nullable=False, default=datetime.datetime.now) |
| 207 | |
| 208 | __table_args__ = ( |
| 209 | UniqueConstraint('tag', 'media_entry'), |
| 210 | {}) |
| 211 | |
| 212 | tag_helper = relationship(Tag) |
| 213 | slug = association_proxy('tag_helper', 'slug', |
| 214 | creator=Tag.find_or_new |
| 215 | ) |
| 216 | |
| 217 | def __init__(self, name=None, slug=None): |
| 218 | Base.__init__(self) |
| 219 | if name is not None: |
| 220 | self.name = name |
| 221 | if slug is not None: |
| 222 | self.tag_helper = Tag.find_or_new(slug) |
| 223 | |
| 224 | @property |
| 225 | def dict_view(self): |
| 226 | """A dict like view on this object""" |
| 227 | return DictReadAttrProxy(self) |
| 228 | |
| 229 | |
| 230 | class MediaComment(Base, MediaCommentMixin): |
| 231 | __tablename__ = "media_comments" |
| 232 | |
| 233 | id = Column(Integer, primary_key=True) |
| 234 | media_entry = Column( |
| 235 | Integer, ForeignKey('media_entries.id'), nullable=False) |
| 236 | author = Column(Integer, ForeignKey('users.id'), nullable=False) |
| 237 | created = Column(DateTime, nullable=False, default=datetime.datetime.now) |
| 238 | content = Column(UnicodeText, nullable=False) |
| 239 | |
| 240 | get_author = relationship(User) |
| 241 | |
| 242 | _id = SimpleFieldAlias("id") |
| 243 | |
| 244 | |
| 245 | MODELS = [ |
| 246 | User, MediaEntry, Tag, MediaTag, MediaComment] |
| 247 | |
| 248 | |
| 249 | ###################################################### |
| 250 | # Special, migrations-tracking table |
| 251 | # |
| 252 | # Not listed in MODELS because this is special and not |
| 253 | # really migrated, but used for migrations (for now) |
| 254 | ###################################################### |
| 255 | |
| 256 | class MigrationData(Base): |
| 257 | __tablename__ = "migrations" |
| 258 | |
| 259 | name = Column(Unicode, primary_key=True) |
| 260 | version = Column(Integer, nullable=False, default=0) |
| 261 | |
| 262 | ###################################################### |
| 263 | |
| 264 | |
| 265 | def show_table_init(engine_uri): |
| 266 | if engine_uri is None: |
| 267 | engine_uri = 'sqlite:///:memory:' |
| 268 | from sqlalchemy import create_engine |
| 269 | engine = create_engine(engine_uri, echo=True) |
| 270 | |
| 271 | Base.metadata.create_all(engine) |
| 272 | |
| 273 | |
| 274 | if __name__ == '__main__': |
| 275 | from sys import argv |
| 276 | print repr(argv) |
| 277 | if len(argv) == 2: |
| 278 | uri = argv[1] |
| 279 | else: |
| 280 | uri = None |
| 281 | show_table_init(uri) |