diff options
-rw-r--r-- | mediagoblin/templates/mediagoblin/user_pages/user.html | 2 | ||||
-rw-r--r-- | mediagoblin/templates/mediagoblin/utils/object_gallery.html | 35 | ||||
-rw-r--r-- | mediagoblin/templates/mediagoblin/utils/pagination.html | 23 | ||||
-rw-r--r-- | mediagoblin/user_pages/views.py | 9 | ||||
-rw-r--r-- | mediagoblin/util.py | 38 |
5 files changed, 61 insertions, 46 deletions
diff --git a/mediagoblin/templates/mediagoblin/user_pages/user.html b/mediagoblin/templates/mediagoblin/user_pages/user.html index 48516679..d1809e80 100644 --- a/mediagoblin/templates/mediagoblin/user_pages/user.html +++ b/mediagoblin/templates/mediagoblin/user_pages/user.html @@ -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" %} diff --git a/mediagoblin/templates/mediagoblin/utils/object_gallery.html b/mediagoblin/templates/mediagoblin/utils/object_gallery.html index 9e8c1875..8ae337f5 100644 --- a/mediagoblin/templates/mediagoblin/utils/object_gallery.html +++ b/mediagoblin/templates/mediagoblin/utils/object_gallery.html @@ -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 %} diff --git a/mediagoblin/templates/mediagoblin/utils/pagination.html b/mediagoblin/templates/mediagoblin/utils/pagination.html index 685a1bb9..b74cbfcf 100644 --- a/mediagoblin/templates/mediagoblin/utils/pagination.html +++ b/mediagoblin/templates/mediagoblin/utils/pagination.html @@ -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)}}> « Prev</a> + <a href={{ pagination.get_page_url(request.path_info, + pagination.page-1, request.GET) }}>« 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 »</a> + <a href={{ pagination.get_page_url(request.path_info, + pagination.page+1, request.GET) }}>Next »</a> {% endif %} - </div> - {% endif %} + </div> +{% endif %} -{% endmacro %} diff --git a/mediagoblin/user_pages/views.py b/mediagoblin/user_pages/views.py index 26c67425..76f96cf9 100644 --- a/mediagoblin/user_pages/views.py +++ b/mediagoblin/user_pages/views.py @@ -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() diff --git a/mediagoblin/util.py b/mediagoblin/util.py index b79d6b05..9247ac19 100644 --- a/mediagoblin/util.py +++ b/mediagoblin/util.py @@ -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)) + |