Permalink
Browse files

lazy load home page

  • Loading branch information...
peterbe committed Oct 24, 2013
1 parent afd1d10 commit 3512920b03dd935117fc294ea426da7438db3824
@@ -0,0 +1,33 @@
{% for post in blogitems %}
<div class="row">
<div class="span12">
<div class="page-header">
<h1>
<a href="{{ url('blog_post', post.oid) }}">{{ post.title }}</a><br>
<small style="float:right">{{ post.pub_date.strftime('%d %B %Y') }}</small>
{% set count = post.count_comments() %}
<small>{{ count }} comment{% if count != 1 %}s{% endif %}</small>
{% for category in post.categories.all() %}
{% if loop.first %}&nbsp; <small>{% endif %}
<a rel="nofollow" href="{{ url('only_category', category.name.replace(' ','+')) }}"
title="Filter by the '{{ category.name }}' category"
>{{ category.name }}</a>{% if not loop.last %}, {% endif %}
{% if loop.last %}</small>{% endif %}
{% endfor %}
</h1>
{% if post.url %}
<h4><a href="{{ post.url }}">{{ post.url }}</a></h4>
{% endif %}
</div>
{% if post.has_carousel_tag %}
{{ expand_carousel_thumbnails(post.rendered, post)|safe }}
{% else %}
{{ post.rendered|safe }}
{% endif %}
</div>
</div>
{% endfor %}
@@ -2,11 +2,6 @@
{% block title %}Peterbe.com - Stuff in Peter's head{% endblock %}
{% block extrahead %}
<link rel="prefetch" href="{{ first_post_url }}"> <!-- Firefox -->
<link rel="prerender" href="{{ first_post_url }}"> <!-- Chrome -->
{% endblock %}
{% block pre_content %}
<header class="jumbotron masthead">
<div class="inner">
@@ -36,40 +31,10 @@ <h1>Peterbe.com</h1>
</div>
{% endif %}
{% for post in blogitems %}
<div class="row">
<div class="span12">
<div class="page-header">
<h1>
<a href="{{ url('blog_post', post.oid) }}">{{ post.title }}</a><br>
<small style="float:right">{{ post.pub_date.strftime('%d %B %Y') }}</small>
{% set count = post.count_comments() %}
<small>{{ count }} comment{% if count != 1 %}s{% endif %}</small>
{% for category in post.categories.all() %}
{% if loop.first %}&nbsp; <small>{% endif %}
<a rel="nofollow" href="{{ url('only_category', category.name.replace(' ','+')) }}"
title="Filter by the '{{ category.name }}' category"
>{{ category.name }}</a>{% if not loop.last %}, {% endif %}
{% if loop.last %}</small>{% endif %}
{% endfor %}
</h1>
{% if post.url %}
<h4><a href="{{ post.url }}">{{ post.url }}</a></h4>
{% endif %}
</div>
{% if post.has_carousel_tag %}
{{ expand_carousel_thumbnails(post.rendered, post)|safe }}
{% else %}
{{ post.rendered|safe }}
{% endif %}
</div>
</div>
{% endfor %}
{% include "homepage/_posts.html" %}
<div id="rest">
</div>
<ul class="pager">
{% if previous_page %}
@@ -85,3 +50,16 @@ <h4><a href="{{ post.url }}">{{ post.url }}</a></h4>
</ul>
{% endblock %}
{% block extrajs %}
{% if rest %}
<script>{% include "homepage/microajax.minified.js" %}
;onload = function() {
microAjax("/rest/{{ rest.from_index }}/{{ rest.to_index }}/", function (res) {
document.getElementById('rest').innerHTML = res;
});
};
</script>
{% endif %}
{% endblock %}

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -7,6 +7,7 @@
urlpatterns = patterns('',
url('^$', views.home, name='home'),
url('^rest/(?P<from_index>\d+)/(?P<to_index>\d+)/$', views.home_rest, name='home_rest'),
url(r'(.*?)/?rss.xml$', cache_page(PlogFeed(), 60 * 60)),
url('^search$', views.search, name='search'),
url('^About$', lambda x: HttpResponsePermanentRedirect('/about/')),
View
@@ -93,6 +93,15 @@ def home(request, oc=None):
if (page + 1) * BATCH_SIZE < max_count:
data['next_page'] = page + 2
data['previous_page'] = page
if n == 0 and not oc:
# On the first page and no category filtering.
# Then, load only the first two posts and tell the template
# to render the other remaining ones later
data['rest'] = {'from_index': 2, 'to_index': m}
m = 2
else:
data['rest'] = None
data['blogitems'] = (
qs
.prefetch_related('categories')
@@ -102,6 +111,38 @@ def home(request, oc=None):
return render(request, 'homepage/home.html', data)
def _home_rest_key_prefixer(request):
if request.method != 'GET':
return None
prefix = make_prefix(request.GET)
cache_key = 'latest_comment_add_date'
latest_date = cache.get(cache_key)
if latest_date is None:
qs = BlogItem.objects.all()
latest, = (qs
.order_by('-modify_date')
.values('modify_date')[:1])
latest_date = latest['modify_date'].strftime('%f')
cache.set(cache_key, latest_date, 60 * 60 * 5)
prefix += str(latest_date)
return prefix
@cache_page(60 * 60 * 5, # five hours (same as for `home()` above)
key_prefix=_home_rest_key_prefixer,
)
def home_rest(request, from_index, to_index):
qs = (
BlogItem.objects.filter(pub_date__lt=utc_now())
.order_by('-pub_date')
)
context = {
'blogitems': qs[from_index : to_index]
}
return render(request, 'homepage/_posts.html', context)
STOPWORDS = "a able about across after all almost also am among an and "\
"any are as at be because been but by can cannot could dear "\
"did do does either else ever every for from get got had has "\

0 comments on commit 3512920

Please sign in to comment.