Removing print statements from convert_gps_media_data migration
[mediagoblin.git] / mediagoblin / db / mongo / models.py
index e2ac1b5a1d96b380b1e0bb84a60e2f808f7c89cb..2e35a2b8f00c24265cb57c0689cd2bb8b427d20c 100644 (file)
@@ -1,5 +1,5 @@
 # GNU MediaGoblin -- federated, autonomous media hosting
-# Copyright (C) 2011 MediaGoblin contributors.  See AUTHORS.
+# Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as published by
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 import datetime
-import uuid
 
 from mongokit import Document
 
-from mediagoblin.auth import lib as auth_lib
-from mediagoblin import mg_globals
 from mediagoblin.db.mongo import migrations
 from mediagoblin.db.mongo.util import ASCENDING, DESCENDING, ObjectId
 from mediagoblin.tools.pagination import Pagination
-from mediagoblin.tools import url, common
+from mediagoblin.db.mixin import UserMixin, MediaEntryMixin, MediaCommentMixin
+
+
+class MongoPK(object):
+    """An alias for the _id primary key"""
+    def __get__(self, instance, cls):
+       return instance['_id']   
+    def __set__(self, instance, val):
+       instance['_id'] = val  
+    def __delete__(self, instance):
+       del instance['_id']
+
 
 ###################
 # Custom validators
@@ -35,7 +43,7 @@ from mediagoblin.tools import url, common
 ########
 
 
-class User(Document):
+class User(Document, UserMixin):
     """
     A user of MediaGoblin.
 
@@ -60,7 +68,6 @@ class User(Document):
      - is_admin: Whether or not this user is an administrator or not.
      - url: this user's personal webpage/website, if appropriate.
      - bio: biography of this user (plaintext, in markdown)
-     - bio_html: biography of the user converted to proper HTML.
     """
     __collection__ = 'users'
     use_dot_notation = True
@@ -77,7 +84,6 @@ class User(Document):
         'is_admin': bool,
         'url': unicode,
         'bio': unicode,      # May contain markdown
-        'bio_html': unicode,  # May contain plaintext, or HTML
         'fp_verification_key': unicode,  # forgotten password verification key
         'fp_token_expire': datetime.datetime,
         }
@@ -88,18 +94,12 @@ class User(Document):
         'created': datetime.datetime.utcnow,
         'email_verified': False,
         'status': u'needs_email_verification',
-        'verification_key': lambda: unicode(uuid.uuid4()),
         'is_admin': False}
 
-    def check_login(self, password):
-        """
-        See if a user can login with this password
-        """
-        return auth_lib.bcrypt_check_password(
-            password, self.pw_hash)
+    id = MongoPK()
 
 
-class MediaEntry(Document):
+class MediaEntry(Document, MediaEntryMixin):
     """
     Record of a piece of media.
 
@@ -121,9 +121,6 @@ class MediaEntry(Document):
        up with MarkDown for slight fanciness (links, boldness, italics,
        paragraphs...)
 
-     - description_html: Rendered version of the description, run through
-       Markdown and cleaned with our cleaning tool.
-
      - media_type: What type of media is this?  Currently we only support
        'image' ;)
 
@@ -160,6 +157,8 @@ class MediaEntry(Document):
         "unprocessed": uploaded but needs to go through processing for display
         "processed": processed and able to be displayed
 
+     - license: URI for media's license.
+
      - queued_media_file: storage interface style filepath describing a file
        queued for processing.  This is stored in the mg_globals.queue_store
        storage system.
@@ -186,12 +185,12 @@ class MediaEntry(Document):
         'slug': unicode,
         'created': datetime.datetime,
         'description': unicode,  # May contain markdown/up
-        'description_html': unicode,  # May contain plaintext, or HTML
         'media_type': unicode,
         'media_data': dict,  # extra data relevant to this media_type
         'plugin_data': dict,  # plugins can dump stuff here.
         'tags': [dict],
         'state': unicode,
+        'license': unicode,
 
         # For now let's assume there can only be one main file queued
         # at a time
@@ -217,6 +216,11 @@ class MediaEntry(Document):
         'created': datetime.datetime.utcnow,
         'state': u'unprocessed'}
 
+    id = MongoPK()
+
+    def media_data_init(self, **kwargs):
+        self.media_data.update(kwargs)
+
     def get_comments(self, ascending=False):
         if ascending:
             order = ASCENDING
@@ -226,56 +230,6 @@ class MediaEntry(Document):
         return self.db.MediaComment.find({
                 'media_entry': self._id}).sort('created', order)
 
-    def get_display_media(self, media_map,
-                          fetch_order=common.DISPLAY_IMAGE_FETCHING_ORDER):
-        """
-        Find the best media for display.
-
-        Args:
-        - media_map: a dict like
-          {u'image_size': [u'dir1', u'dir2', u'image.jpg']}
-        - fetch_order: the order we should try fetching images in
-
-        Returns:
-        (media_size, media_path)
-        """
-        media_sizes = media_map.keys()
-
-        for media_size in common.DISPLAY_IMAGE_FETCHING_ORDER:
-            if media_size in media_sizes:
-                return media_map[media_size]
-
-    def main_mediafile(self):
-        pass
-
-    def generate_slug(self):
-        self.slug = url.slugify(self.title)
-
-        duplicate = mg_globals.database.media_entries.find_one(
-            {'slug': self.slug})
-
-        if duplicate:
-            self.slug = "%s-%s" % (self._id, self.slug)
-
-    def url_for_self(self, urlgen):
-        """
-        Generate an appropriate url for ourselves
-
-        Use a slug if we have one, else use our '_id'.
-        """
-        uploader = self.get_uploader()
-
-        if self.get('slug'):
-            return urlgen(
-                'mediagoblin.user_pages.media_home',
-                user=uploader.username,
-                media=self.slug)
-        else:
-            return urlgen(
-                'mediagoblin.user_pages.media_home',
-                user=uploader.username,
-                media=unicode(self._id))
-
     def url_to_prev(self, urlgen):
         """
         Provide a url to the previous entry from this user, if there is one
@@ -284,10 +238,8 @@ class MediaEntry(Document):
                                           'uploader': self.uploader,
                                           'state': 'processed'}).sort(
                                                     '_id', ASCENDING).limit(1)
-        if cursor.count():
-            return urlgen('mediagoblin.user_pages.media_home',
-                          user=self.get_uploader().username,
-                          media=unicode(cursor[0].slug))
+        for media in cursor:
+            return media.url_for_self(urlgen)
 
     def url_to_next(self, urlgen):
         """
@@ -298,23 +250,15 @@ class MediaEntry(Document):
                                           'state': 'processed'}).sort(
                                                     '_id', DESCENDING).limit(1)
 
-        if cursor.count():
-            return urlgen('mediagoblin.user_pages.media_home',
-                          user=self.get_uploader().username,
-                          media=unicode(cursor[0].slug))
+        for media in cursor:
+            return media.url_for_self(urlgen)
 
+    @property
     def get_uploader(self):
         return self.db.User.find_one({'_id': self.uploader})
 
-    def get_fail_exception(self):
-        """
-        Get the exception that's appropriate for this error
-        """
-        if self['fail_error']:
-            return common.import_component(self['fail_error'])
-
 
-class MediaComment(Document):
+class MediaComment(Document, MediaCommentMixin):
     """
     A comment on a MediaEntry.
 
@@ -323,8 +267,6 @@ class MediaComment(Document):
      - author: user who posted this comment
      - created: when the comment was created
      - content: plaintext (but markdown'able) version of the comment's content.
-     - content_html: the actual html-rendered version of the comment displayed.
-       Run through Markdown and the HTML cleaner.
     """
 
     __collection__ = 'media_comments'
@@ -335,7 +277,7 @@ class MediaComment(Document):
         'author': ObjectId,
         'created': datetime.datetime,
         'content': unicode,
-        'content_html': unicode}
+        }
 
     required_fields = [
         'media_entry', 'author', 'created', 'content']
@@ -346,7 +288,8 @@ class MediaComment(Document):
     def media_entry(self):
         return self.db.MediaEntry.find_one({'_id': self['media_entry']})
 
-    def author(self):
+    @property
+    def get_author(self):
         return self.db.User.find_one({'_id': self['author']})