Skip to content

Commit

Permalink
Blog post only pages now load without javascript
Browse files Browse the repository at this point in the history
  • Loading branch information
rigoneri committed Jun 17, 2012
1 parent c08555b commit 2a7c4f3
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 17 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ wsgiref==0.1.2
psycopg2==2.4.5
gunicorn==0.14.2
-e git://github.com/brosner/python-oauth2.git#egg=oauth2
pybars==0.0.1
6 changes: 2 additions & 4 deletions syte/static/js/components/blog-posts.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@

function fetchBlogPosts(post, tag) {
function fetchBlogPosts(tag) {
var blog_fetch_url = '/blog.json';

if (post)
blog_fetch_url = '/post/ajax/' + post;
else if (tag)
if (tag)
blog_fetch_url = '/tags/' + tag;

$.getJSON(blog_fetch_url, function(blog_posts) {
Expand Down
12 changes: 12 additions & 0 deletions syte/templates/blog-post.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends 'base.html' %}
{% block pagetitle %}{{ post_title }}{% endblock %}
{% block main_section %}
<section class="main-section blog-section" id="blog-posts">
{{ post_data|safe }}
</section>
{% endblock %}
{% block extra_inline_js %}
$(function() {
adjustBlogHeaders();
});
{% endblock %}
6 changes: 2 additions & 4 deletions syte/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
{% endblock %}
{% block extra_inline_js %}
$(function() {
{% if post_id %}
fetchBlogPosts("{{post_id}}")
{% elif tag_slug %}
fetchBlogPosts(null, "{{tag_slug}}");
{% if tag_slug %}
fetchBlogPosts("{{tag_slug}}");
{% else %}
fetchBlogPosts();
{% endif %}
Expand Down
1 change: 0 additions & 1 deletion syte/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

urlpatterns = patterns('',
url(r'^post/(?P<post_id>\w+)/?$', 'syte.views.blog_post'),
url(r'^post/ajax/(?P<post_id>\w+)/?$', 'syte.views.blog_post_ajax'),
url(r'^tags/(?P<tag_slug>\w+)/?$', 'syte.views.blog_tags'),
url(r'^blog.json/?$', 'syte.views.blog'),

Expand Down
31 changes: 23 additions & 8 deletions syte/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
from django.template import Context, loader
from django.http import HttpResponse, HttpResponseServerError, Http404
from django.conf import settings
from pybars import Compiler
from datetime import datetime

import os
import requests
import json
import oauth2 as oauth
Expand Down Expand Up @@ -106,19 +109,31 @@ def blog(request):


def blog_post(request, post_id):
return render(request, 'index.html', {'post_id': post_id})

context = dict()

def blog_post_ajax(request, post_id):
if request.is_ajax():
r = requests.get('{0}/posts?api_key={1}&id={2}'.format(settings.TUMBLR_API_URL,
r = requests.get('{0}/posts?api_key={1}&id={2}'.format(settings.TUMBLR_API_URL,
settings.TUMBLR_API_KEY, post_id))
return HttpResponse(content=r.text, status=r.status_code,
content_type=r.headers['content-type'])

return render(request, 'index.html', {'post_id': post_id})
if r.status_code == 200:
post_response = r.json.get('response', {})
posts = post_response.get('posts', [])

if posts:
post = posts[0]
f_date = datetime.strptime(post['date'], '%Y-%m-%d %H:%M:%S %Z')
post['formated_date'] = f_date.strftime('%B %d, %Y')

path_to_here = os.path.abspath(os.path.dirname(__file__))
f = open('{0}/static/templates/blog-post-{1}.html'.format(path_to_here, post['type']), 'r')
f_data = f.read()
f.close()

compiler = Compiler()
template = compiler.compile(unicode(f_data))
context['post_data'] = template(post)
context['post_title'] = post['title']

return render(request, 'blog-post.html', context)


def blog_tags(request, tag_slug):
Expand Down

0 comments on commit 2a7c4f3

Please sign in to comment.