changed some coding styles and changed the interface for pagination from __call__ to the
__init__, also getting a cursor as input, instead of the query details
This commit is contained in:
parent
ae85ed0f97
commit
ca3ca51c5a
@ -16,21 +16,22 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#}
|
||||
|
||||
{% import 'mediagoblin/utils/pagination.html' as paginationmacro %}
|
||||
|
||||
<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>
|
||||
|
||||
{% import 'mediagoblin/utils/pagination.html' as paginationmacro %}
|
||||
{{ paginationmacro.render_pagination(pagination) }}
|
||||
{% endif %}
|
||||
</div>
|
||||
<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>
|
||||
|
@ -18,24 +18,31 @@
|
||||
|
||||
{% macro render_pagination(pagination) %}
|
||||
|
||||
{# only display if {{pagination}} is defined #}
|
||||
{# only display if {{pagination}} is defined #}
|
||||
|
||||
{% if pagination %}
|
||||
<div class=pagination>
|
||||
{%- for page in pagination.iter_pages() %}
|
||||
{% if page %}
|
||||
{% if page != pagination.page %}
|
||||
<a href={{pagination.url_generator(page)}}>{{ page }}</a>
|
||||
{% else %}
|
||||
<strong>{{ page }}</strong>
|
||||
{% if pagination %}
|
||||
<div class=pagination>
|
||||
|
||||
{% if pagination.has_prev %}
|
||||
<a href={{pagination.url_generator(pagination.page-1)}}> « Prev</a>
|
||||
{% endif %}
|
||||
{% else %}
|
||||
<span class=ellipsis>…</span>
|
||||
{% endif %}
|
||||
{%- endfor %}
|
||||
{% if pagination.has_next %}
|
||||
<a href={{pagination.url_generator(pagination.page+1)}}>Next »</a>
|
||||
|
||||
{%- for page in pagination.iter_pages() %}
|
||||
{% if page %}
|
||||
{% if page != pagination.page %}
|
||||
<a href={{pagination.url_generator(page)}}>{{ page }}</a>
|
||||
{% else %}
|
||||
<strong>{{ page }}</strong>
|
||||
{% endif %}
|
||||
{% else %}
|
||||
<span class=ellipsis>…</span>
|
||||
{% endif %}
|
||||
{%- endfor %}
|
||||
|
||||
{% if pagination.has_next %}
|
||||
<a href={{pagination.url_generator(pagination.page+1)}}>Next »</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% endmacro %}
|
||||
|
@ -18,7 +18,8 @@ from webob import Response, exc
|
||||
from pymongo import DESCENDING
|
||||
from mongokit import ObjectId
|
||||
import wtforms
|
||||
from ..util import Pagination
|
||||
from mediagoblin.util import Pagination
|
||||
from pymongo import ASCENDING, DESCENDING
|
||||
|
||||
def user_home(request):
|
||||
"""'Homepage' of a User()"""
|
||||
@ -28,12 +29,12 @@ def user_home(request):
|
||||
if not user:
|
||||
return exc.HTTPNotFound()
|
||||
|
||||
pagination = Pagination()
|
||||
media_entries = pagination(
|
||||
{ 'per_page': 2,
|
||||
'request': request,
|
||||
'collection':'MediaEntry',
|
||||
'query': { 'uploader':user, 'state':'processed'} } )
|
||||
cursor = request.db.MediaEntry \
|
||||
.find({'uploader': user, 'state': 'processed'}) \
|
||||
.sort('created', DESCENDING)
|
||||
|
||||
pagination = Pagination(2, cursor, request)
|
||||
media_entries = pagination()
|
||||
|
||||
#if no data is available, return NotFound
|
||||
if media_entries == None:
|
||||
|
@ -27,7 +27,6 @@ import translitcodec
|
||||
from mediagoblin import globals as mgoblin_globals
|
||||
|
||||
import urllib
|
||||
from pymongo import ASCENDING, DESCENDING
|
||||
from math import ceil
|
||||
|
||||
|
||||
@ -300,53 +299,20 @@ class Pagination(object):
|
||||
"""
|
||||
Pagination class
|
||||
"""
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def __call__(self, args):
|
||||
"""
|
||||
input values:
|
||||
{'page': ..., --- requested page
|
||||
'per_page': ..., --- objects per page
|
||||
'request': ..., --- webob request object for url generation
|
||||
'collection' ... --- db collection, thats to be queried
|
||||
'query': {'user': xxx}, query restrictions, db.collection.find(query)
|
||||
}
|
||||
|
||||
add:
|
||||
option for sorting attribute
|
||||
ascending, descending option
|
||||
range based pagination
|
||||
"""
|
||||
self.per_page = args['per_page']
|
||||
self.request = args['request']
|
||||
|
||||
def __init__(self, per_page, cursor, request):
|
||||
try:
|
||||
self.page = abs(int(args['request'].str_GET['page']))
|
||||
# set default page, if page value is not set
|
||||
self.page = int(request.str_GET['page'])
|
||||
except KeyError:
|
||||
self.page = 1
|
||||
# return None(404 Error) if page is set, but has no value or has an invalid value
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
######################################################
|
||||
#
|
||||
# db queries should be changed into range based pagination
|
||||
# save count and current page in some user session data
|
||||
#
|
||||
######################################################
|
||||
self.per_page = per_page
|
||||
self.cursor = cursor
|
||||
self.request = request
|
||||
self.total_count = self.cursor.count()
|
||||
|
||||
collection = getattr(self.request.db, args['collection'])
|
||||
|
||||
self.total_count = collection.find(args['query']).count()
|
||||
|
||||
#check if requested page is valid, not larger than available number of pages
|
||||
if self.page > self.pages:
|
||||
return None
|
||||
|
||||
return collection.find(args['query']).sort('created',DESCENDING) \
|
||||
.skip((self.page-1)*self.per_page).limit(self.per_page)
|
||||
def __call__(self):
|
||||
return self.cursor.skip((self.page-1)*self.per_page) \
|
||||
.limit(self.per_page)
|
||||
|
||||
@property
|
||||
def pages(self):
|
||||
|
Loading…
x
Reference in New Issue
Block a user