import json, sys, re, hashlib, smtplib, base64, urllib, os, difflib

from auth import *
from django.http import *
from django.shortcuts import render_to_response
from django.views.decorators.csrf import csrf_exempt
from django.core.context_processors import csrf
from django.db.utils import IntegrityError

from utils import *
from models import *

p = os.path.abspath(os.path.dirname(__file__))
if(os.path.abspath(p+"/..") not in sys.path):
  sys.path.append(os.path.abspath(p+"/.."))


'''
@author: Anant Bhardwaj
@date: Feb 12, 2012
'''

def home (request):
  try:
    conferences = Conference.objects.all().values()
    login = get_login(request)
    return render_to_response('home.html', {
          'conferences':conferences,
          'login_id': login[0],
          'login_name': login[1]
        }
    )
  except:
    pass

def team (request):
  current_team = json.loads(
      open(p+'/fixtures/' + 'team.json').read())
  past_collaborators = json.loads(
      open(p+'/fixtures/' + 'collaborators.json').read())
  login = get_login(request)
  return render_to_response(
      'team.html', {'current_team': current_team,
      'past_collaborators': past_collaborators,
      'login_id': login[0],
      'login_name': login[1]
    }
  )

def conf (request, conf):
  conf = conf.lower()
  try:
    request.session[kConf] = conf
    Conference.objects.get(unique_name=conf)
    return HttpResponseRedirect('/%s/papers' %(conf))
  except Conference.DoesNotExist:
    try:
      c = Conference.objects.get(confer_name=conf)
      request.session[kConf] = c.unique_name
      return HttpResponseRedirect('/%s/papers' %(c.unique_name))
    except:
      return HttpResponseRedirect('/')
  except:
    return HttpResponseRedirect('/')

def papers (request, conf):
  conf = conf.lower()
  try:
    Conference.objects.get(unique_name=conf)
    request.session[kConf] = conf
    login = get_login(request)
    return render_to_response('papers.html', {
        'conf':conf,
        'login_id': login[0],
        'login_name': login[1]
      }
    )
  except:
    return HttpResponseRedirect('/')
  
  

def schedule (request, conf):
  conf = conf.lower()
  try:
    Conference.objects.get(unique_name=conf)
    request.session[kConf] = conf
    login = get_login(request)
    return render_to_response('schedule.html', {
        'conf':conf,
        'login_id': login[0],
        'login_name': login[1]
      }
    )
  except:
    return HttpResponseRedirect('/')


def paper (request, conf):
  conf = conf.lower()
  try:
    request.session[kConf] = conf
    login = get_login(request)
    return render_to_response('paper.html', {
        'conf':conf,
        'login_id': login[0],
        'login_name': login[1]
      }
    )
  except:
    return HttpResponseRedirect('/')

@login_required