Source

stack2blog / stack2blog / stack2blogapp / views.py

from django.shortcuts import render_to_response
from django.template import RequestContext
from models import *
import stackexchange


# Create your views here.
def home_page(request):
	return render_to_response('home.html', locals(), context_instance=RequestContext(request))

def user_answers(request):
	try:
		userid = int(request.GET.get("userid", ""))
	except:
		return "Bad ID" # Make a bad answer option in main page.

	site = stackexchange.Site(stackexchange.StackOverflow)
	user = site.user(userid)

	answers = ()
	more_answers = user.answers.fetch()
	while (len(more_answers) > 0):
		answers += more_answers
		more_answers = user.answers.fetch_next()

	published_answers = [answer for answer in answers if PublishedPost.objects.filter(postid = answer.id).count() > 0]
	unpublished_answers = [answer for answer in answers if PublishedPost.objects.filter(postid = answer.id).count() == 0]

	return render_to_response('answers.html', locals(), context_instance=RequestContext(request))

def post_answer(request):
	try:
		postid = int(request.GET.get("postid", ""))
	except:
		return "Bad ID" # Make a bad answer option in main page.

	post = PublishedPost(postid = postid)
	post.save()

	return render_to_response('post.html', locals(), context_instance=RequestContext(request))