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:
parent
ca3ca51c5a
commit
44e3e917fb
@ -20,8 +20,6 @@
|
|||||||
{% if user %}
|
{% if user %}
|
||||||
<h1>User page for '{{ user.username }}'</h1>
|
<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>
|
<ul>
|
||||||
|
|
||||||
{% include "mediagoblin/utils/object_gallery.html" %}
|
{% include "mediagoblin/utils/object_gallery.html" %}
|
||||||
|
@ -16,22 +16,21 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# 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>
|
</div>
|
||||||
{% if media_entries %}
|
{% endblock %}
|
||||||
<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>
|
|
||||||
|
@ -15,22 +15,21 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#}
|
#}
|
||||||
|
|
||||||
|
{# only display if {{pagination}} is defined #}
|
||||||
|
|
||||||
{% macro render_pagination(pagination) %}
|
{% if pagination %}
|
||||||
|
<div class=pagination>
|
||||||
{# only display if {{pagination}} is defined #}
|
|
||||||
|
|
||||||
{% if pagination %}
|
|
||||||
<div class=pagination>
|
|
||||||
|
|
||||||
{% if pagination.has_prev %}
|
{% if pagination.has_prev %}
|
||||||
<a href={{pagination.url_generator(pagination.page-1)}}> « Prev</a>
|
<a href={{ pagination.get_page_url(request.path_info,
|
||||||
|
pagination.page-1, request.GET) }}>« Prev</>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{%- for page in pagination.iter_pages() %}
|
{%- for page in pagination.iter_pages() %}
|
||||||
{% if page %}
|
{% if page %}
|
||||||
{% if page != pagination.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 %}
|
{% else %}
|
||||||
<strong>{{ page }}</strong>
|
<strong>{{ page }}</strong>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
@ -40,9 +39,9 @@
|
|||||||
{%- endfor %}
|
{%- endfor %}
|
||||||
|
|
||||||
{% if pagination.has_next %}
|
{% if pagination.has_next %}
|
||||||
<a href={{pagination.url_generator(pagination.page+1)}}>Next »</a>
|
<a href={{ pagination.get_page_url(request.path_info,
|
||||||
|
pagination.page+1, request.GET) }}>Next »</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% endmacro %}
|
|
||||||
|
@ -33,9 +33,14 @@ def user_home(request):
|
|||||||
.find({'uploader': user, 'state': 'processed'}) \
|
.find({'uploader': user, 'state': 'processed'}) \
|
||||||
.sort('created', DESCENDING)
|
.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()
|
media_entries = pagination()
|
||||||
|
|
||||||
#if no data is available, return NotFound
|
#if no data is available, return NotFound
|
||||||
if media_entries == None:
|
if media_entries == None:
|
||||||
return exc.HTTPNotFound()
|
return exc.HTTPNotFound()
|
||||||
|
@ -28,7 +28,7 @@ from mediagoblin import globals as mgoblin_globals
|
|||||||
|
|
||||||
import urllib
|
import urllib
|
||||||
from math import ceil
|
from math import ceil
|
||||||
|
import copy
|
||||||
|
|
||||||
TESTS_ENABLED = False
|
TESTS_ENABLED = False
|
||||||
def _activate_testing():
|
def _activate_testing():
|
||||||
@ -297,20 +297,27 @@ def setup_gettext(locale):
|
|||||||
|
|
||||||
class Pagination(object):
|
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.per_page = per_page
|
||||||
self.cursor = cursor
|
self.cursor = cursor
|
||||||
self.request = request
|
|
||||||
self.total_count = self.cursor.count()
|
self.total_count = self.cursor.count()
|
||||||
|
|
||||||
def __call__(self):
|
def __call__(self):
|
||||||
|
"""
|
||||||
|
returns slice of objects for the requested page
|
||||||
|
"""
|
||||||
return self.cursor.skip((self.page-1)*self.per_page) \
|
return self.cursor.skip((self.page-1)*self.per_page) \
|
||||||
.limit(self.per_page)
|
.limit(self.per_page)
|
||||||
|
|
||||||
@ -338,7 +345,14 @@ class Pagination(object):
|
|||||||
yield None
|
yield None
|
||||||
yield num
|
yield num
|
||||||
last = num
|
last = num
|
||||||
|
|
||||||
def url_generator(self, page):
|
def get_page_url(self, path_info, page_no, get_params=None):
|
||||||
return '%s?%s' % (self.request.path_info, \
|
"""
|
||||||
urllib.urlencode({'page':str(page)}))
|
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))
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user