rogueojiiofwales/views.py
2012-08-18 22:03:53 -07:00

56 lines
2 KiB
Python

from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import redirect, render_to_response
from django.template import RequestContext
def index(request):
'''Index page. Everyone starts here. If the user is logged in (that is, they
have a session id) return the follower_graph view. Otherwise, render the
index page.'''
if request.user.is_authenticated():
return redirect('graph_followers')
# Set a test cookie. When the user clicks the 'Login' button, test and make
# sure this cookie was set properly.
request.session.set_test_cookie()
return render_to_response('index.html', RequestContext(request))
def login(request):
'''Do a quick check to make sure cookies are enabled. If so, redirect to
GitHub so the user can login.'''
# Make sure the user can accept cookies.
if request.session.test_cookie_worked():
request.session.delete_test_cookie()
return redirect('socialauth_begin', backend='github')
else:
# During development, I've landed here a lot, despite having cookies
# enabled. So, set the test cookie so that trying to login from here
# actually works.
request.session.set_test_cookie()
# Render an error -- fix your damn cookies!
return render_to_response('index.html',
{ 'error': "Fix your damn cookies!" })
def _sorted_repos(request):
repos = [r for r in request.github.get_iter('users/%s/repos' %
request.user.username)]
repos.sort(key=lambda x: x['name'])
return repos
@login_required
def graph_followers(request):
return render_to_response('graph_followers.html', {
'repos': _sorted_repos(request)
}, RequestContext(request))
@login_required
def graph_repo(request, user=None, repo=None):
return render_to_response('graph_repo.html', {
'graph_user': user,
'graph_repo': repo,
'repos': _sorted_repos(request)
}, RequestContext(request))