diff options
author | Joar Wandborg <git@wandborg.com> | 2011-07-07 18:04:19 +0200 |
---|---|---|
committer | Joar Wandborg <git@wandborg.com> | 2011-07-07 18:04:19 +0200 |
commit | af2fcba5c4ce16bde1a2267b95803bc2afc9e572 (patch) | |
tree | 6323a6a4f6a10f7660a92c918bea2d2f0321f125 /mediagoblin/util.py | |
parent | 5ed4722de8106a512a4faacfedaae7b8eda7260b (diff) | |
download | mediagoblin-af2fcba5c4ce16bde1a2267b95803bc2afc9e572.tar.lz mediagoblin-af2fcba5c4ce16bde1a2267b95803bc2afc9e572.tar.xz mediagoblin-af2fcba5c4ce16bde1a2267b95803bc2afc9e572.zip |
Issue #431 - Prevent comment link expiry - Added functionality for comment linking
* `media.html`
* Changed comment textarea handle from `comment` => `field_comment`
* Active comment is hilighted with the CSS class name `comment_active`
and also with the hyperlink anchor #comment
* Changed media.html so that pagination always uses
Route('mediagoblin.user_pages.media_home') as base_url
* `user_pages/forms.py`
* Renamed MediaComment form field `comment` => `field_comment`
* `user_pages/routing.py`
* Added route for `/u/joar/m/123..456/c/234..567/`, points to `media_home`
* `user_pages/views.py`
* `media_home` now checks if the request contains a comment id parameter
then acts accordingly with pagination whether to call it with a
`jump_to_id` or not.
* `media_post_comment` - Updated MediaCommentForm field name
`comment` => `field_comment`
* `util.py`
* `redirect` now supports querystring arguments. - NOT USED (should we
keep it? I think so, it might be useful, sometime [don't call me a
code hoarder]).
* `Pagination.__init__` now accepts one further argument, the `jump_to_id`.
It assist the comment linking functionality in finding and returning the
proper page for a comment.
This feature will work for all kinds of objects. It might not be
optimal, but it is well functional :)
Diffstat (limited to 'mediagoblin/util.py')
-rw-r--r-- | mediagoblin/util.py | 36 |
1 files changed, 32 insertions, 4 deletions
diff --git a/mediagoblin/util.py b/mediagoblin/util.py index ab219df0..7b1e4a2a 100644 --- a/mediagoblin/util.py +++ b/mediagoblin/util.py @@ -14,6 +14,8 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. +from __future__ import division + from email.MIMEText import MIMEText import gettext import pkg_resources @@ -21,7 +23,7 @@ import smtplib import sys import re import urllib -from math import ceil +from math import ceil, floor import copy from babel.localedata import exists @@ -35,6 +37,8 @@ from mediagoblin import mg_globals from mediagoblin import messages from mediagoblin.db.util import ObjectId +from itertools import izip, count + TESTS_ENABLED = False def _activate_testing(): """ @@ -133,7 +137,16 @@ def render_to_response(request, template, context): def redirect(request, *args, **kwargs): """Returns a HTTPFound(), takes a request and then urlgen params""" - return exc.HTTPFound(location=request.urlgen(*args, **kwargs)) + + querystring = None + if kwargs.get('querystring'): + querystring = kwargs.get('querystring') + del kwargs['querystring'] + + return exc.HTTPFound( + location=''.join([ + request.urlgen(*args, **kwargs), + querystring if querystring else ''])) def setup_user_in_request(request): @@ -418,7 +431,8 @@ class Pagination(object): get actual data slice through __call__(). """ - def __init__(self, page, cursor, per_page=PAGINATION_DEFAULT_PER_PAGE): + def __init__(self, page, cursor, per_page=PAGINATION_DEFAULT_PER_PAGE, + jump_to_id=False): """ Initializes Pagination @@ -426,11 +440,25 @@ class Pagination(object): - page: requested page - per_page: number of objects per page - cursor: db cursor + - jump_to_id: ObjectId, sets the page to the page containing the object + with _id == jump_to_id. """ - self.page = page + self.page = page self.per_page = per_page self.cursor = cursor self.total_count = self.cursor.count() + self.active_id = None + + if jump_to_id: + cursor = copy.copy(self.cursor) + + for (doc, increment) in izip(cursor, count(0)): + if doc['_id'] == jump_to_id: + self.page = 1 + int(floor(increment / self.per_page)) + + self.active_id = jump_to_id + break + def __call__(self): """ |