# HG changeset patch # User ymh # Date 1352949119 -3600 # Node ID 316a0101512e57ee65177c40aeec8b139a357fa3 # Parent 0931f368cb9a28facd14a4852d32abf4acd159f0 add pagination new ldt version diff -r 0931f368cb9a -r 316a0101512e src/hp/__init__.py --- a/src/hp/__init__.py Wed Nov 14 18:47:17 2012 +0100 +++ b/src/hp/__init__.py Thu Nov 15 04:11:59 2012 +0100 @@ -1,4 +1,4 @@ -VERSION = (0, 1, 0, "alpha", 0) +VERSION = (0, 2, 0, "alpha", 0) VERSION_STR = unicode(".".join(map(lambda i:"%02d" % (i,), VERSION[:2]))) diff -r 0931f368cb9a -r 316a0101512e src/hp/config.py.tmpl --- a/src/hp/config.py.tmpl Wed Nov 14 18:47:17 2012 +0100 +++ b/src/hp/config.py.tmpl Thu Nov 15 04:11:59 2012 +0100 @@ -104,7 +104,14 @@ } } -LDT_DOMAIN = 'http://localhost' -LDT_API_URL = LDT_DOMAIN + '/~ymh/hp_ldt/ldtplatform/api/1.0/' +LDT_NETLOC = "localhost" +LDT_DOMAIN = 'http://'+LDT_NETLOC +LDT_BASE_URL = LDT_DOMAIN + '/~ymh/hp_ldt/' +LDT_URL = LDT_BASE_URL + "ldtplatform/" +LDT_API_URL = LDT_URL + 'api/ldt/1.0/' +LDT_STATIC_URL = LDT_BASE_URL + "static/site/" +PER_PAGE = 9 +KC_URL = 'http://176.32.94.234/kn-concierge/' + diff -r 0931f368cb9a -r 316a0101512e src/hp/forms.py --- a/src/hp/forms.py Wed Nov 14 18:47:17 2012 +0100 +++ b/src/hp/forms.py Thu Nov 15 04:11:59 2012 +0100 @@ -5,10 +5,13 @@ @author: ymh ''' -from django import forms +from . import settings from .models import VideoKCRel -import requests -from . import settings +from .utils import get_all_objects +from django import forms +import logging + +logger = logging.getLogger(__name__) class VideoKCRelForm(forms.ModelForm): @@ -17,10 +20,9 @@ def __init__(self, *args, **kwargs): super(VideoKCRelForm, self).__init__(*args, **kwargs) - url = settings.LDT_API_URL + "contents/" - #limit=20&offset=20 - r = requests.get(url) - self.fields['iri_id'].widget.choices = [(content['iri_id'], content['title']) for content in r.json['objects']] + url = settings.LDT_API_URL + "contents/?limit=" + str(settings.LDT_MAX_FETCH) + contents = get_all_objects(url, 'front_project') + self.fields['iri_id'].widget.choices = [(content['iri_id'], content['title']) for content in contents] class Meta: model = VideoKCRel diff -r 0931f368cb9a -r 316a0101512e src/hp/settings.py --- a/src/hp/settings.py Wed Nov 14 18:47:17 2012 +0100 +++ b/src/hp/settings.py Thu Nov 15 04:11:59 2012 +0100 @@ -163,7 +163,8 @@ } } -LDT_DOMAIN = 'http://capsicum' +LDT_NETLOC = "capsicum" +LDT_DOMAIN = 'http://'+LDT_NETLOC LDT_BASE_URL = LDT_DOMAIN + '/pf/' LDT_URL = LDT_BASE_URL + "ldtplatform/" LDT_API_URL = LDT_URL + 'api/ldt/1.0/' @@ -171,9 +172,12 @@ KC_URL = 'http://176.32.94.234/kn-concierge/' +PER_PAGE = 9 +LDT_MAX_FETCH = 200 + + from .config import * #@UnusedWildImport - diff -r 0931f368cb9a -r 316a0101512e src/hp/static/hp/css/videos.css --- a/src/hp/static/hp/css/videos.css Wed Nov 14 18:47:17 2012 +0100 +++ b/src/hp/static/hp/css/videos.css Thu Nov 15 04:11:59 2012 +0100 @@ -30,3 +30,9 @@ .video-author { text-decoration: underline; } + +.pagination { + float: right; + width: 110px; + text-align: center; +} diff -r 0931f368cb9a -r 316a0101512e src/hp/templates/hp/all_videos.html --- a/src/hp/templates/hp/all_videos.html Wed Nov 14 18:47:17 2012 +0100 +++ b/src/hp/templates/hp/all_videos.html Thu Nov 15 04:11:59 2012 +0100 @@ -21,7 +21,7 @@

{% trans 'All videos' %}

- {% for content in results.objects %} + {% for content in results %} + {% if results.has_other_pages %} + + {% endif %}
{% endblock %} diff -r 0931f368cb9a -r 316a0101512e src/hp/utils.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/hp/utils.py Thu Nov 15 04:11:59 2012 +0100 @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +''' +Created on Nov 14, 2012 + +@author: ymh +''' +import urlparse +import requests +from . import settings +import logging + +logger = logging.getLogger(__name__) + +def get_abs_url(url, default_domain): + + if not url: + return url + url_part = urlparse.urlparse(url) + if url_part.netloc: + return url + else: + pr = urlparse.ParseResult('http', default_domain, url_part.path, url_part.params, url_part.query, url_part.fragment) + return pr.geturl() + + +def get_all_objects(res_url, field_filter): + contents = [] + url = res_url + while url: + r = requests.get(url) + if r.status_code != requests.codes.ok: #@UndefinedVariable + logger.error("Error when requesting contents " + repr(r.status_code) + " : " + repr(r.text)) + break + contents.extend([ c for c in r.json['objects'] if c.get(field_filter, None) ]) + url = get_abs_url(r.json.get('meta',{}).get('next',None), settings.LDT_NETLOC) + return contents diff -r 0931f368cb9a -r 316a0101512e src/hp/views.py --- a/src/hp/views.py Wed Nov 14 18:47:17 2012 +0100 +++ b/src/hp/views.py Thu Nov 15 04:11:59 2012 +0100 @@ -5,10 +5,12 @@ @author: ymh ''' from django.conf import settings +from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage +from django.http import HttpResponse from django.shortcuts import render_to_response from django.template.context import RequestContext -from django.http import HttpResponse from hp.models import VideoKCRel +from hp.utils import get_all_objects import logging import requests import urlparse @@ -18,20 +20,28 @@ def all_videos(request): - #get all videos - #render - url = settings.LDT_API_URL + "contents/" - #limit=20&offset=20 - r = requests.get(url) - - - - results = r.json - - for content in results.get('objects', []): + url = settings.LDT_API_URL + "contents/?limit=" + str(settings.LDT_MAX_FETCH) + + #pagination TODO : implement a lazy loader + contents = get_all_objects(url, 'front_project') + for content in contents: url_parts = urlparse.urlparse(content.get('image','')) if not url_parts.netloc: content['image'] = settings.LDT_DOMAIN + content.get('image','') + + paginator = Paginator(contents, settings.PER_PAGE, 0, True) + + page = request.GET.get('page',1) + if page == 'last': + page = paginator.num_pages + try: + results = paginator.page(page) + except PageNotAnInteger: + # If page is not an integer, deliver first page. + results = paginator.page(1) + except EmptyPage: + # If page is out of range (e.g. 9999), deliver last page of results. + results = paginator.page(paginator.num_pages) return render_to_response('hp/all_videos.html',{'results':results}, context_instance=RequestContext(request)) diff -r 0931f368cb9a -r 316a0101512e virtualenv/res/src/ldt-1.30.tar.gz Binary file virtualenv/res/src/ldt-1.30.tar.gz has changed