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/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#}
|
#}
|
||||||
|
|
||||||
|
{% import 'mediagoblin/utils/pagination.html' as paginationmacro %}
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
{% if media_entries %}
|
{% if media_entries %}
|
||||||
<ul>
|
<ul>
|
||||||
{% for entry in media_entries %}
|
{% for entry in media_entries %}
|
||||||
<li>
|
<li>
|
||||||
<a href="{{ request.urlgen('mediagoblin.user_pages.media_home',
|
<a href="{{ request.urlgen('mediagoblin.user_pages.media_home',
|
||||||
user= entry.uploader.username, m_id= entry._id) }}">
|
user= entry.uploader.username, m_id= entry._id) }}">
|
||||||
<img src="{{ request.app.public_store.file_url(
|
<img src="{{ request.app.public_store.file_url(
|
||||||
entry['media_files']['thumb']) }}" /></a>
|
entry['media_files']['thumb']) }}" /></a>
|
||||||
</li>
|
</li>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
{% import 'mediagoblin/utils/pagination.html' as paginationmacro %}
|
{{ paginationmacro.render_pagination(pagination) }}
|
||||||
{{ paginationmacro.render_pagination(pagination) }}
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
|
@ -18,24 +18,31 @@
|
|||||||
|
|
||||||
{% macro render_pagination(pagination) %}
|
{% macro render_pagination(pagination) %}
|
||||||
|
|
||||||
{# only display if {{pagination}} is defined #}
|
{# only display if {{pagination}} is defined #}
|
||||||
|
|
||||||
{% if pagination %}
|
{% if pagination %}
|
||||||
<div class=pagination>
|
<div class=pagination>
|
||||||
{%- for page in pagination.iter_pages() %}
|
|
||||||
{% if page %}
|
{% if pagination.has_prev %}
|
||||||
{% if page != pagination.page %}
|
<a href={{pagination.url_generator(pagination.page-1)}}> « Prev</a>
|
||||||
<a href={{pagination.url_generator(page)}}>{{ page }}</a>
|
|
||||||
{% else %}
|
|
||||||
<strong>{{ page }}</strong>
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% else %}
|
|
||||||
<span class=ellipsis>…</span>
|
{%- for page in pagination.iter_pages() %}
|
||||||
{% endif %}
|
{% if page %}
|
||||||
{%- endfor %}
|
{% if page != pagination.page %}
|
||||||
{% if pagination.has_next %}
|
<a href={{pagination.url_generator(page)}}>{{ page }}</a>
|
||||||
<a href={{pagination.url_generator(pagination.page+1)}}>Next »</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 %}
|
{% endif %}
|
||||||
</div>
|
|
||||||
{% endif %}
|
|
||||||
{% endmacro %}
|
{% endmacro %}
|
||||||
|
@ -18,7 +18,8 @@ from webob import Response, exc
|
|||||||
from pymongo import DESCENDING
|
from pymongo import DESCENDING
|
||||||
from mongokit import ObjectId
|
from mongokit import ObjectId
|
||||||
import wtforms
|
import wtforms
|
||||||
from ..util import Pagination
|
from mediagoblin.util import Pagination
|
||||||
|
from pymongo import ASCENDING, DESCENDING
|
||||||
|
|
||||||
def user_home(request):
|
def user_home(request):
|
||||||
"""'Homepage' of a User()"""
|
"""'Homepage' of a User()"""
|
||||||
@ -28,12 +29,12 @@ def user_home(request):
|
|||||||
if not user:
|
if not user:
|
||||||
return exc.HTTPNotFound()
|
return exc.HTTPNotFound()
|
||||||
|
|
||||||
pagination = Pagination()
|
cursor = request.db.MediaEntry \
|
||||||
media_entries = pagination(
|
.find({'uploader': user, 'state': 'processed'}) \
|
||||||
{ 'per_page': 2,
|
.sort('created', DESCENDING)
|
||||||
'request': request,
|
|
||||||
'collection':'MediaEntry',
|
pagination = Pagination(2, cursor, request)
|
||||||
'query': { 'uploader':user, 'state':'processed'} } )
|
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:
|
||||||
|
@ -27,7 +27,6 @@ import translitcodec
|
|||||||
from mediagoblin import globals as mgoblin_globals
|
from mediagoblin import globals as mgoblin_globals
|
||||||
|
|
||||||
import urllib
|
import urllib
|
||||||
from pymongo import ASCENDING, DESCENDING
|
|
||||||
from math import ceil
|
from math import ceil
|
||||||
|
|
||||||
|
|
||||||
@ -300,53 +299,20 @@ class Pagination(object):
|
|||||||
"""
|
"""
|
||||||
Pagination class
|
Pagination class
|
||||||
"""
|
"""
|
||||||
def __init__(self):
|
def __init__(self, per_page, cursor, request):
|
||||||
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']
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.page = abs(int(args['request'].str_GET['page']))
|
self.page = int(request.str_GET['page'])
|
||||||
# set default page, if page value is not set
|
|
||||||
except KeyError:
|
except KeyError:
|
||||||
self.page = 1
|
self.page = 1
|
||||||
# return None(404 Error) if page is set, but has no value or has an invalid value
|
|
||||||
except ValueError:
|
|
||||||
return None
|
|
||||||
|
|
||||||
######################################################
|
self.per_page = per_page
|
||||||
#
|
self.cursor = cursor
|
||||||
# db queries should be changed into range based pagination
|
self.request = request
|
||||||
# save count and current page in some user session data
|
self.total_count = self.cursor.count()
|
||||||
#
|
|
||||||
######################################################
|
|
||||||
|
|
||||||
collection = getattr(self.request.db, args['collection'])
|
def __call__(self):
|
||||||
|
return self.cursor.skip((self.page-1)*self.per_page) \
|
||||||
self.total_count = collection.find(args['query']).count()
|
.limit(self.per_page)
|
||||||
|
|
||||||
#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)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def pages(self):
|
def pages(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user