Commit | Line | Data |
---|---|---|
fbad3a9f | 1 | # GNU MediaGoblin -- federated, autonomous media hosting |
7f4ebeed | 2 | # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. |
fbad3a9f E |
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 | ||
eea6d276 E |
17 | """ |
18 | TODO: indexes on foreignkeys, where useful. | |
19 | """ | |
20 | ||
fdc34b8b | 21 | import logging |
ccca0fbf | 22 | import datetime |
007ac2e7 | 23 | import sys |
ccca0fbf | 24 | |
942084fb JW |
25 | from sqlalchemy import Column, Integer, Unicode, UnicodeText, DateTime, \ |
26 | Boolean, ForeignKey, UniqueConstraint, PrimaryKeyConstraint, \ | |
27 | SmallInteger | |
28 | from sqlalchemy.orm import relationship, backref | |
02db7e0a | 29 | from sqlalchemy.orm.collections import attribute_mapped_collection |
c47a03b9 | 30 | from sqlalchemy.sql.expression import desc |
02db7e0a | 31 | from sqlalchemy.ext.associationproxy import association_proxy |
007ac2e7 | 32 | from sqlalchemy.util import memoized_property |
ccca0fbf | 33 | |
a5acfe23 | 34 | from mediagoblin.db.extratypes import PathTupleWithSlashes, JSONEncoded |
39dc3bf8 | 35 | from mediagoblin.db.base import Base, DictReadAttrProxy, Session |
be5be115 | 36 | from mediagoblin.db.mixin import UserMixin, MediaEntryMixin, MediaCommentMixin, CollectionMixin, CollectionItemMixin |
fdc34b8b | 37 | from mediagoblin.tools.files import delete_media_files |
ccca0fbf | 38 | |
780fdd7b CAW |
39 | # It's actually kind of annoying how sqlalchemy-migrate does this, if |
40 | # I understand it right, but whatever. Anyway, don't remove this :P | |
c4869eff | 41 | # |
780fdd7b CAW |
42 | # We could do migration calls more manually instead of relying on |
43 | # this import-based meddling... | |
44 | from migrate import changeset | |
45 | ||
fdc34b8b SS |
46 | _log = logging.getLogger(__name__) |
47 | ||
7b194a79 | 48 | |
f42e49c3 | 49 | class User(Base, UserMixin): |
eea6d276 E |
50 | """ |
51 | TODO: We should consider moving some rarely used fields | |
52 | into some sort of "shadow" table. | |
53 | """ | |
2f5ce68c | 54 | __tablename__ = "core__users" |
ccca0fbf CAW |
55 | |
56 | id = Column(Integer, primary_key=True) | |
57 | username = Column(Unicode, nullable=False, unique=True) | |
58 | email = Column(Unicode, nullable=False) | |
59 | created = Column(DateTime, nullable=False, default=datetime.datetime.now) | |
60 | pw_hash = Column(Unicode, nullable=False) | |
51fba991 | 61 | email_verified = Column(Boolean, default=False) |
e365f980 | 62 | status = Column(Unicode, default=u"needs_email_verification", nullable=False) |
c4869eff JW |
63 | # Intented to be nullable=False, but migrations would not work for it |
64 | # set to nullable=True implicitly. | |
65 | wants_comment_notification = Column(Boolean, default=True) | |
ccca0fbf CAW |
66 | verification_key = Column(Unicode) |
67 | is_admin = Column(Boolean, default=False, nullable=False) | |
68 | url = Column(Unicode) | |
fbad3a9f | 69 | bio = Column(UnicodeText) # ?? |
ccca0fbf | 70 | fp_verification_key = Column(Unicode) |
7c2c56a5 | 71 | fp_token_expire = Column(DateTime) |
ccca0fbf CAW |
72 | |
73 | ## TODO | |
74 | # plugin data would be in a separate model | |
75 | ||
88a9662b JW |
76 | def __repr__(self): |
77 | return '<{0} #{1} {2} {3} "{4}">'.format( | |
78 | self.__class__.__name__, | |
79 | self.id, | |
80 | 'verified' if self.email_verified else 'non-verified', | |
81 | 'admin' if self.is_admin else 'user', | |
82 | self.username) | |
83 | ||
03b4fc50 SS |
84 | def delete(self, **kwargs): |
85 | """Deletes a User and all related entries/comments/files/...""" | |
86 | # Delete this user's Collections and all contained CollectionItems | |
87 | for collection in self.collections: | |
88 | collection.delete(commit=False) | |
89 | ||
90 | media_entries = MediaEntry.query.filter(MediaEntry.uploader == self.id) | |
91 | for media in media_entries: | |
92 | # TODO: Make sure that "MediaEntry.delete()" also deletes | |
93 | # all related files/Comments | |
94 | media.delete(del_orphan_tags=False, commit=False) | |
95 | ||
96 | # Delete now unused tags | |
97 | # TODO: import here due to cyclic imports!!! This cries for refactoring | |
3809a8b8 | 98 | from mediagoblin.db.util import clean_orphan_tags |
03b4fc50 SS |
99 | clean_orphan_tags(commit=False) |
100 | ||
101 | # Delete user, pass through commit=False/True in kwargs | |
102 | super(User, self).delete(**kwargs) | |
103 | _log.info('Deleted user "{0}" account'.format(self.username)) | |
104 | ||
ccca0fbf | 105 | |
f42e49c3 | 106 | class MediaEntry(Base, MediaEntryMixin): |
eea6d276 E |
107 | """ |
108 | TODO: Consider fetching the media_files using join | |
109 | """ | |
2f5ce68c | 110 | __tablename__ = "core__media_entries" |
ccca0fbf CAW |
111 | |
112 | id = Column(Integer, primary_key=True) | |
ecd538bb | 113 | uploader = Column(Integer, ForeignKey(User.id), nullable=False, index=True) |
7c2c56a5 | 114 | title = Column(Unicode, nullable=False) |
3e907d55 | 115 | slug = Column(Unicode) |
ecd538bb E |
116 | created = Column(DateTime, nullable=False, default=datetime.datetime.now, |
117 | index=True) | |
ccca0fbf | 118 | description = Column(UnicodeText) # ?? |
ccca0fbf | 119 | media_type = Column(Unicode, nullable=False) |
51fba991 E |
120 | state = Column(Unicode, default=u'unprocessed', nullable=False) |
121 | # or use sqlalchemy.types.Enum? | |
2788e6a1 | 122 | license = Column(Unicode) |
be5be115 | 123 | collected = Column(Integer, default=0) |
fbad3a9f | 124 | |
ccca0fbf | 125 | fail_error = Column(Unicode) |
cf27accc | 126 | fail_metadata = Column(JSONEncoded) |
ccca0fbf | 127 | |
64712915 JW |
128 | transcoding_progress = Column(SmallInteger) |
129 | ||
02db7e0a | 130 | queued_media_file = Column(PathTupleWithSlashes) |
ccca0fbf CAW |
131 | |
132 | queued_task_id = Column(Unicode) | |
133 | ||
134 | __table_args__ = ( | |
135 | UniqueConstraint('uploader', 'slug'), | |
136 | {}) | |
137 | ||
88e90f41 E |
138 | get_uploader = relationship(User) |
139 | ||
02db7e0a E |
140 | media_files_helper = relationship("MediaFile", |
141 | collection_class=attribute_mapped_collection("name"), | |
142 | cascade="all, delete-orphan" | |
143 | ) | |
144 | media_files = association_proxy('media_files_helper', 'file_path', | |
fbad3a9f | 145 | creator=lambda k, v: MediaFile(name=k, file_path=v) |
02db7e0a E |
146 | ) |
147 | ||
35029581 | 148 | attachment_files_helper = relationship("MediaAttachmentFile", |
35029581 E |
149 | order_by="MediaAttachmentFile.created" |
150 | ) | |
151 | attachment_files = association_proxy("attachment_files_helper", "dict_view", | |
152 | creator=lambda v: MediaAttachmentFile( | |
153 | name=v["name"], filepath=v["filepath"]) | |
154 | ) | |
155 | ||
de917303 | 156 | tags_helper = relationship("MediaTag", |
fdc34b8b | 157 | cascade="all, delete-orphan" # should be automatically deleted |
de917303 E |
158 | ) |
159 | tags = association_proxy("tags_helper", "dict_view", | |
160 | creator=lambda v: MediaTag(name=v["name"], slug=v["slug"]) | |
161 | ) | |
162 | ||
be5be115 AW |
163 | collections_helper = relationship("CollectionItem", |
164 | cascade="all, delete-orphan" | |
165 | ) | |
166 | collections = association_proxy("collections_helper", "in_collection") | |
167 | ||
ccca0fbf | 168 | ## TODO |
ccca0fbf | 169 | # media_data |
ccca0fbf CAW |
170 | # fail_error |
171 | ||
02ede858 E |
172 | def get_comments(self, ascending=False): |
173 | order_col = MediaComment.created | |
174 | if not ascending: | |
175 | order_col = desc(order_col) | |
176 | return MediaComment.query.filter_by( | |
177 | media_entry=self.id).order_by(order_col) | |
178 | ||
c47a03b9 E |
179 | def url_to_prev(self, urlgen): |
180 | """get the next 'newer' entry by this user""" | |
181 | media = MediaEntry.query.filter( | |
182 | (MediaEntry.uploader == self.uploader) | |
5bd0adeb | 183 | & (MediaEntry.state == u'processed') |
c47a03b9 E |
184 | & (MediaEntry.id > self.id)).order_by(MediaEntry.id).first() |
185 | ||
186 | if media is not None: | |
187 | return media.url_for_self(urlgen) | |
188 | ||
189 | def url_to_next(self, urlgen): | |
190 | """get the next 'older' entry by this user""" | |
191 | media = MediaEntry.query.filter( | |
192 | (MediaEntry.uploader == self.uploader) | |
5bd0adeb | 193 | & (MediaEntry.state == u'processed') |
c47a03b9 E |
194 | & (MediaEntry.id < self.id)).order_by(desc(MediaEntry.id)).first() |
195 | ||
196 | if media is not None: | |
197 | return media.url_for_self(urlgen) | |
198 | ||
007ac2e7 | 199 | #@memoized_property |
5fe1fd07 E |
200 | @property |
201 | def media_data(self): | |
007ac2e7 CAW |
202 | session = Session() |
203 | ||
204 | return session.query(self.media_data_table).filter_by( | |
729424be | 205 | media_entry=self.id).first() |
5fe1fd07 | 206 | |
acb21949 | 207 | def media_data_init(self, **kwargs): |
007ac2e7 CAW |
208 | """ |
209 | Initialize or update the contents of a media entry's media_data row | |
210 | """ | |
211 | session = Session() | |
212 | ||
213 | media_data = session.query(self.media_data_table).filter_by( | |
214 | media_entry=self.id).first() | |
215 | ||
216 | # No media data, so actually add a new one | |
99c2f9f0 | 217 | if media_data is None: |
007ac2e7 | 218 | media_data = self.media_data_table( |
99c2f9f0 | 219 | media_entry=self.id, |
007ac2e7 CAW |
220 | **kwargs) |
221 | session.add(media_data) | |
222 | # Update old media data | |
223 | else: | |
224 | for field, value in kwargs.iteritems(): | |
225 | setattr(media_data, field, value) | |
226 | ||
227 | @memoized_property | |
228 | def media_data_table(self): | |
229 | # TODO: memoize this | |
230 | models_module = self.media_type + '.models' | |
231 | __import__(models_module) | |
232 | return sys.modules[models_module].DATA_MODEL | |
acb21949 | 233 | |
64712915 | 234 | def __repr__(self): |
79f28e0b JW |
235 | safe_title = self.title.encode('ascii', 'replace') |
236 | ||
64712915 JW |
237 | return '<{classname} {id}: {title}>'.format( |
238 | classname=self.__class__.__name__, | |
239 | id=self.id, | |
79f28e0b | 240 | title=safe_title) |
64712915 | 241 | |
fdc34b8b SS |
242 | def delete(self, del_orphan_tags=True, **kwargs): |
243 | """Delete MediaEntry and all related files/attachments/comments | |
244 | ||
245 | This will *not* automatically delete unused collections, which | |
246 | can remain empty... | |
247 | ||
248 | :param del_orphan_tags: True/false if we delete unused Tags too | |
249 | :param commit: True/False if this should end the db transaction""" | |
250 | # User's CollectionItems are automatically deleted via "cascade". | |
251 | # Delete all the associated comments | |
252 | for comment in self.get_comments(): | |
253 | comment.delete(commit=False) | |
254 | ||
255 | # Delete all related files/attachments | |
256 | try: | |
257 | delete_media_files(self) | |
258 | except OSError, error: | |
259 | # Returns list of files we failed to delete | |
260 | _log.error('No such files from the user "{1}" to delete: ' | |
261 | '{0}'.format(str(error), self.get_uploader)) | |
262 | _log.info('Deleted Media entry id "{0}"'.format(self.id)) | |
263 | # Related MediaTag's are automatically cleaned, but we might | |
264 | # want to clean out unused Tag's too. | |
265 | if del_orphan_tags: | |
266 | # TODO: Import here due to cyclic imports!!! | |
267 | # This cries for refactoring | |
268 | from mediagoblin.db.util import clean_orphan_tags | |
269 | clean_orphan_tags(commit=False) | |
270 | # pass through commit=False/True in kwargs | |
271 | super(MediaEntry, self).delete(**kwargs) | |
272 | ||
ccca0fbf | 273 | |
a9dac7c8 E |
274 | class FileKeynames(Base): |
275 | """ | |
276 | keywords for various places. | |
277 | currently the MediaFile keys | |
278 | """ | |
279 | __tablename__ = "core__file_keynames" | |
280 | id = Column(Integer, primary_key=True) | |
281 | name = Column(Unicode, unique=True) | |
282 | ||
283 | def __repr__(self): | |
284 | return "<FileKeyname %r: %r>" % (self.id, self.name) | |
285 | ||
286 | @classmethod | |
287 | def find_or_new(cls, name): | |
288 | t = cls.query.filter_by(name=name).first() | |
289 | if t is not None: | |
290 | return t | |
291 | return cls(name=name) | |
292 | ||
293 | ||
02db7e0a | 294 | class MediaFile(Base): |
eea6d276 E |
295 | """ |
296 | TODO: Highly consider moving "name" into a new table. | |
297 | TODO: Consider preloading said table in software | |
298 | """ | |
2f5ce68c | 299 | __tablename__ = "core__mediafiles" |
02db7e0a E |
300 | |
301 | media_entry = Column( | |
302 | Integer, ForeignKey(MediaEntry.id), | |
a9dac7c8 E |
303 | nullable=False) |
304 | name_id = Column(SmallInteger, ForeignKey(FileKeynames.id), nullable=False) | |
02db7e0a E |
305 | file_path = Column(PathTupleWithSlashes) |
306 | ||
a9dac7c8 E |
307 | __table_args__ = ( |
308 | PrimaryKeyConstraint('media_entry', 'name_id'), | |
309 | {}) | |
310 | ||
02db7e0a E |
311 | def __repr__(self): |
312 | return "<MediaFile %s: %r>" % (self.name, self.file_path) | |
313 | ||
a9dac7c8 E |
314 | name_helper = relationship(FileKeynames, lazy="joined", innerjoin=True) |
315 | name = association_proxy('name_helper', 'name', | |
316 | creator=FileKeynames.find_or_new | |
317 | ) | |
318 | ||
02db7e0a | 319 | |
35029581 E |
320 | class MediaAttachmentFile(Base): |
321 | __tablename__ = "core__attachment_files" | |
322 | ||
323 | id = Column(Integer, primary_key=True) | |
324 | media_entry = Column( | |
325 | Integer, ForeignKey(MediaEntry.id), | |
326 | nullable=False) | |
327 | name = Column(Unicode, nullable=False) | |
328 | filepath = Column(PathTupleWithSlashes) | |
329 | created = Column(DateTime, nullable=False, default=datetime.datetime.now) | |
330 | ||
331 | @property | |
332 | def dict_view(self): | |
333 | """A dict like view on this object""" | |
334 | return DictReadAttrProxy(self) | |
335 | ||
336 | ||
ccca0fbf | 337 | class Tag(Base): |
2f5ce68c | 338 | __tablename__ = "core__tags" |
ccca0fbf CAW |
339 | |
340 | id = Column(Integer, primary_key=True) | |
341 | slug = Column(Unicode, nullable=False, unique=True) | |
342 | ||
de917303 E |
343 | def __repr__(self): |
344 | return "<Tag %r: %r>" % (self.id, self.slug) | |
345 | ||
346 | @classmethod | |
347 | def find_or_new(cls, slug): | |
348 | t = cls.query.filter_by(slug=slug).first() | |
349 | if t is not None: | |
350 | return t | |
351 | return cls(slug=slug) | |
352 | ||
ccca0fbf CAW |
353 | |
354 | class MediaTag(Base): | |
2f5ce68c | 355 | __tablename__ = "core__media_tags" |
ccca0fbf CAW |
356 | |
357 | id = Column(Integer, primary_key=True) | |
ccca0fbf | 358 | media_entry = Column( |
de917303 | 359 | Integer, ForeignKey(MediaEntry.id), |
ecd538bb E |
360 | nullable=False, index=True) |
361 | tag = Column(Integer, ForeignKey(Tag.id), nullable=False, index=True) | |
de917303 | 362 | name = Column(Unicode) |
ccca0fbf CAW |
363 | # created = Column(DateTime, nullable=False, default=datetime.datetime.now) |
364 | ||
365 | __table_args__ = ( | |
366 | UniqueConstraint('tag', 'media_entry'), | |
367 | {}) | |
368 | ||
de917303 E |
369 | tag_helper = relationship(Tag) |
370 | slug = association_proxy('tag_helper', 'slug', | |
371 | creator=Tag.find_or_new | |
372 | ) | |
373 | ||
6456cefa | 374 | def __init__(self, name=None, slug=None): |
de917303 | 375 | Base.__init__(self) |
6456cefa E |
376 | if name is not None: |
377 | self.name = name | |
378 | if slug is not None: | |
379 | self.tag_helper = Tag.find_or_new(slug) | |
de917303 E |
380 | |
381 | @property | |
382 | def dict_view(self): | |
383 | """A dict like view on this object""" | |
384 | return DictReadAttrProxy(self) | |
385 | ||
ccca0fbf | 386 | |
feba5c52 | 387 | class MediaComment(Base, MediaCommentMixin): |
2f5ce68c | 388 | __tablename__ = "core__media_comments" |
fbad3a9f | 389 | |
ccca0fbf CAW |
390 | id = Column(Integer, primary_key=True) |
391 | media_entry = Column( | |
ecd538bb E |
392 | Integer, ForeignKey(MediaEntry.id), nullable=False, index=True) |
393 | author = Column(Integer, ForeignKey(User.id), nullable=False) | |
ccca0fbf CAW |
394 | created = Column(DateTime, nullable=False, default=datetime.datetime.now) |
395 | content = Column(UnicodeText, nullable=False) | |
e365f980 | 396 | |
88e90f41 E |
397 | get_author = relationship(User) |
398 | ||
e365f980 | 399 | |
be5be115 | 400 | class Collection(Base, CollectionMixin): |
242776e3 SS |
401 | """An 'album' or 'set' of media by a user. |
402 | ||
403 | On deletion, contained CollectionItems get automatically reaped via | |
404 | SQL cascade""" | |
be5be115 AW |
405 | __tablename__ = "core__collections" |
406 | ||
407 | id = Column(Integer, primary_key=True) | |
408 | title = Column(Unicode, nullable=False) | |
409 | slug = Column(Unicode) | |
410 | created = Column(DateTime, nullable=False, default=datetime.datetime.now, | |
411 | index=True) | |
88a9662b | 412 | description = Column(UnicodeText) |
be5be115 | 413 | creator = Column(Integer, ForeignKey(User.id), nullable=False) |
242776e3 | 414 | # TODO: No of items in Collection. Badly named, can we migrate to num_items? |
be5be115 AW |
415 | items = Column(Integer, default=0) |
416 | ||
03b4fc50 | 417 | get_creator = relationship(User, backref="collections") |
88a9662b | 418 | |
be5be115 | 419 | def get_collection_items(self, ascending=False): |
242776e3 | 420 | #TODO, is this still needed with self.collection_items being available? |
be5be115 AW |
421 | order_col = CollectionItem.position |
422 | if not ascending: | |
423 | order_col = desc(order_col) | |
424 | return CollectionItem.query.filter_by( | |
425 | collection=self.id).order_by(order_col) | |
426 | ||
be5be115 AW |
427 | |
428 | class CollectionItem(Base, CollectionItemMixin): | |
429 | __tablename__ = "core__collection_items" | |
430 | ||
431 | id = Column(Integer, primary_key=True) | |
432 | media_entry = Column( | |
433 | Integer, ForeignKey(MediaEntry.id), nullable=False, index=True) | |
434 | collection = Column(Integer, ForeignKey(Collection.id), nullable=False) | |
435 | note = Column(UnicodeText, nullable=True) | |
436 | added = Column(DateTime, nullable=False, default=datetime.datetime.now) | |
437 | position = Column(Integer) | |
242776e3 SS |
438 | in_collection = relationship("Collection", |
439 | backref=backref( | |
440 | "collection_items", | |
441 | cascade="all, delete-orphan")) | |
be5be115 AW |
442 | |
443 | get_media_entry = relationship(MediaEntry) | |
444 | ||
be5be115 AW |
445 | __table_args__ = ( |
446 | UniqueConstraint('collection', 'media_entry'), | |
447 | {}) | |
448 | ||
449 | @property | |
450 | def dict_view(self): | |
451 | """A dict like view on this object""" | |
452 | return DictReadAttrProxy(self) | |
453 | ||
454 | ||
5354f954 JW |
455 | class ProcessingMetaData(Base): |
456 | __tablename__ = 'core__processing_metadata' | |
457 | ||
458 | id = Column(Integer, primary_key=True) | |
459 | media_entry_id = Column(Integer, ForeignKey(MediaEntry.id), nullable=False, | |
460 | index=True) | |
942084fb JW |
461 | media_entry = relationship(MediaEntry, |
462 | backref=backref('processing_metadata', | |
463 | cascade='all, delete-orphan')) | |
5354f954 JW |
464 | callback_url = Column(Unicode) |
465 | ||
466 | @property | |
467 | def dict_view(self): | |
468 | """A dict like view on this object""" | |
469 | return DictReadAttrProxy(self) | |
470 | ||
471 | ||
70b44584 | 472 | MODELS = [ |
be5be115 | 473 | User, MediaEntry, Tag, MediaTag, MediaComment, Collection, CollectionItem, MediaFile, FileKeynames, |
5354f954 | 474 | MediaAttachmentFile, ProcessingMetaData] |
70b44584 CAW |
475 | |
476 | ||
477 | ###################################################### | |
478 | # Special, migrations-tracking table | |
479 | # | |
480 | # Not listed in MODELS because this is special and not | |
481 | # really migrated, but used for migrations (for now) | |
482 | ###################################################### | |
483 | ||
484 | class MigrationData(Base): | |
2f5ce68c | 485 | __tablename__ = "core__migrations" |
70b44584 | 486 | |
bf813828 | 487 | name = Column(Unicode, primary_key=True) |
70b44584 CAW |
488 | version = Column(Integer, nullable=False, default=0) |
489 | ||
490 | ###################################################### | |
491 | ||
492 | ||
eea6d276 E |
493 | def show_table_init(engine_uri): |
494 | if engine_uri is None: | |
495 | engine_uri = 'sqlite:///:memory:' | |
e365f980 | 496 | from sqlalchemy import create_engine |
eea6d276 | 497 | engine = create_engine(engine_uri, echo=True) |
e365f980 E |
498 | |
499 | Base.metadata.create_all(engine) | |
500 | ||
501 | ||
502 | if __name__ == '__main__': | |
eea6d276 E |
503 | from sys import argv |
504 | print repr(argv) | |
505 | if len(argv) == 2: | |
506 | uri = argv[1] | |
507 | else: | |
508 | uri = None | |
509 | show_table_init(uri) |