From 7bd8197f32f17466f14d730546a06166ed6da67a Mon Sep 17 00:00:00 2001 From: Joar Wandborg Date: Wed, 29 Jun 2011 01:16:51 +0200 Subject: [PATCH] Issue #362 - Updated the MediaComment model * `MediaComment.get_comments()` now uses pagination * `MediaComment.get_comments()` now sorts by `created` DESC * `MediaComment.media_entry` is now **required** * `MediaComment.media_entry()` now returns parent `MediaEntry` --- mediagoblin/db/models.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/mediagoblin/db/models.py b/mediagoblin/db/models.py index 5d00aa34..5196dede 100644 --- a/mediagoblin/db/models.py +++ b/mediagoblin/db/models.py @@ -22,7 +22,8 @@ from mediagoblin import util from mediagoblin.auth import lib as auth_lib from mediagoblin import mg_globals from mediagoblin.db import migrations -from mediagoblin.db.util import ObjectId +from mediagoblin.db.util import DESCENDING, ObjectId +from mediagoblin.util import Pagination ################### # Custom validators @@ -115,7 +116,22 @@ class MediaEntry(Document): def main_mediafile(self): pass - + + def get_comments(self, page): + cursor = self.db.MediaComment.find({ + 'media_entry': self['_id']}).sort('created', DESCENDING) + + pagination = Pagination(page, cursor) + comments = pagination() + + data = list() + for comment in comments: + comment['author'] = self.db.User.find_one({ + '_id': comment['author']}) + data.append(comment) + + return (data, pagination) + def generate_slug(self): self['slug'] = util.slugify(self['title']) @@ -158,13 +174,13 @@ class MediaComment(Document): 'content_html': unicode} required_fields = [ - 'author', 'created', 'content'] + 'media_entry', 'author', 'created', 'content'] default_values = { 'created': datetime.datetime.utcnow} def media_entry(self): - pass + return self.db.MediaEntry.find_one({'_id': self['media_entry']}) def author(self): return self.db.User.find_one({'_id': self['author']}) -- 2.25.1