removed request arg from Pagination class

added get_page_url() in Pagination class, to generate proper urls without losing
other get arguments
This commit is contained in:
Bernhard Keller 2011-05-19 20:37:04 +02:00
parent ca3ca51c5a
commit 44e3e917fb
5 changed files with 61 additions and 46 deletions

View File

@ -20,8 +20,6 @@
{% if user %}
<h1>User page for '{{ user.username }}'</h1>
{#- Should we outsource such a media 'gallery' view to it's own file?
It could be useful for the home page and other views too -#}
<ul>
{% include "mediagoblin/utils/object_gallery.html" %}

View File

@ -16,22 +16,21 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#}
{% import 'mediagoblin/utils/pagination.html' as paginationmacro %}
{% block object_gallery_content -%}
<div>
{% if media_entries %}
<ul>
{% for entry in media_entries %}
<li>
<a href="{{ request.urlgen('mediagoblin.user_pages.media_home',
user= entry.uploader.username, m_id= entry._id) }}">
<img src="{{ request.app.public_store.file_url(
entry['media_files']['thumb']) }}" /></a>
</li>
{% endfor %}
</ul>
{% include "mediagoblin/utils/pagination.html" %}
{% endif %}
<div>
{% if media_entries %}
<ul>
{% for entry in media_entries %}
<li>
<a href="{{ request.urlgen('mediagoblin.user_pages.media_home',
user= entry.uploader.username, m_id= entry._id) }}">
<img src="{{ request.app.public_store.file_url(
entry['media_files']['thumb']) }}" /></a>
</li>
{% endfor %}
</ul>
{{ paginationmacro.render_pagination(pagination) }}
{% endif %}
</div>
</div>
{% endblock %}

View File

@ -15,22 +15,21 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#}
{# only display if {{pagination}} is defined #}
{% macro render_pagination(pagination) %}
{# only display if {{pagination}} is defined #}
{% if pagination %}
<div class=pagination>
{% if pagination %}
<div class=pagination>
{% if pagination.has_prev %}
<a href={{pagination.url_generator(pagination.page-1)}}> &laquo; Prev</a>
<a href={{ pagination.get_page_url(request.path_info,
pagination.page-1, request.GET) }}>&laquo; Prev</>
{% endif %}
{%- for page in pagination.iter_pages() %}
{% if page %}
{% if page != pagination.page %}
<a href={{pagination.url_generator(page)}}>{{ page }}</a>
<a href={{ pagination.get_page_url(request.path_info,
page, request.GET) }}>{{ page }}</a>
{% else %}
<strong>{{ page }}</strong>
{% endif %}
@ -40,9 +39,9 @@
{%- endfor %}
{% if pagination.has_next %}
<a href={{pagination.url_generator(pagination.page+1)}}>Next &raquo;</a>
<a href={{ pagination.get_page_url(request.path_info,
pagination.page+1, request.GET) }}>Next &raquo;</a>
{% endif %}
</div>
{% endif %}
</div>
{% endif %}
{% endmacro %}

View File

@ -33,9 +33,14 @@ def user_home(request):
.find({'uploader': user, 'state': 'processed'}) \
.sort('created', DESCENDING)
pagination = Pagination(2, cursor, request)
try:
page = int(request.str_GET['page'])
except KeyError:
page = 1
pagination = Pagination(cursor, page)
media_entries = pagination()
#if no data is available, return NotFound
if media_entries == None:
return exc.HTTPNotFound()

View File

@ -28,7 +28,7 @@ from mediagoblin import globals as mgoblin_globals
import urllib
from math import ceil
import copy
TESTS_ENABLED = False
def _activate_testing():
@ -297,20 +297,27 @@ def setup_gettext(locale):
class Pagination(object):
"""
Pagination class
Pagination class,
initialization through __init__(self, page=1, per_page=2, cursor)
get actual data slice through __call__()
"""
def __init__(self, per_page, cursor, request):
try:
self.page = int(request.str_GET['page'])
except KeyError:
self.page = 1
def __init__(self, cursor, page=1, per_page=2):
"""
initializes Pagination
-- page, requested page
-- per_page, number of objects per page
-- cursor, db cursor
"""
self.page = page
self.per_page = per_page
self.cursor = cursor
self.request = request
self.total_count = self.cursor.count()
def __call__(self):
"""
returns slice of objects for the requested page
"""
return self.cursor.skip((self.page-1)*self.per_page) \
.limit(self.per_page)
@ -338,7 +345,14 @@ class Pagination(object):
yield None
yield num
last = num
def url_generator(self, page):
return '%s?%s' % (self.request.path_info, \
urllib.urlencode({'page':str(page)}))
def get_page_url(self, path_info, page_no, get_params=None):
"""
Get a new page based of the path_info, the new page number,
and existing get parameters.
"""
new_get_params = copy.copy(get_params or {})
new_get_params['page'] = page_no
return "%s?%s" % (
path_info, urllib.urlencode(new_get_params))