from mongokit import Document
-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
-from mediagoblin.db.mixin import UserMixin, MediaEntryMixin
+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
'status': u'needs_email_verification',
'is_admin': False}
+ id = MongoPK()
+
class MediaEntry(Document, MediaEntryMixin):
"""
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' ;)
'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.
'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
return self.db.MediaComment.find({
'media_entry': self._id}).sort('created', order)
- 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_to_prev(self, urlgen):
"""
Provide a url to the previous entry from this user, if there is one
return self.db.User.find_one({'_id': self.uploader})
-class MediaComment(Document):
+class MediaComment(Document, MediaCommentMixin):
"""
A comment on a MediaEntry.
- 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'
'author': ObjectId,
'created': datetime.datetime,
'content': unicode,
- 'content_html': unicode}
+ }
required_fields = [
'media_entry', 'author', 'created', 'content']