from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
import reversion
from datetime import datetime, timedelta, date
from reversion.models import Version
from models import Story

import pdb
import markdown

# from http://dwiel.net/blog/python-parsing-the-output-of-datetime-isoformat/
def _parse_iso_datetime(s) :
    # parse a datetime generated by datetime.isoformat()
    try :
        return datetime.strptime(s, "%Y-%m-%dT%H:%M:%S")
    except ValueError :
        return datetime.strptime(s, "%Y-%m-%dT%H:%M:%S.%f")

def index(request, template='lstory/index.html'):
    stories = Story.objects.filter(published__isnull=False)
    return render_to_response(template, {
        'stories': stories,
        }, context_instance=RequestContext(request))

def version(request, slug, date, template='lstory/version.html'):
    obj = Story.objects.get(slug=slug)
    date = _parse_iso_datetime(date)
    version = obj.versions.for_date(date)

    return render_to_response(template, {
        'current': version,
        'date': date,
        }, context_instance=RequestContext(request))



def serve_highlighted_text(request, slug, model, field_to_diff, sessionvar, template='lstory/highlight.html'):
    obj = get_object_or_404(model, slug=slug)

    sessionvar = sessionvar % slug

    if not hasattr(obj, field_to_diff):
        raise Exception("the selected object does not have a %s field" % field_to_diff)

    fromdate = None
    fromdate = request.GET.get('since', None)
    fromdate = request.session.get(sessionvar, None) if not fromdate else fromdate
    fromdate = unicode(datetime.now().isoformat()) if not fromdate else fromdate
    fromdate = _parse_iso_datetime(fromdate) if fromdate else None


    todate = datetime.now()

    current = obj.versions.for_date(todate)
    previous = obj.versions.for_date(fromdate)

    field_diff = current.diff_to_older(previous)

    request.session[sessionvar] = datetime.now().isoformat()



    return render_to_response(template, {
        'current': current,
        'previous': previous,
        'field_diff': field_diff,
        'fromdate': fromdate,
        'todate': todate,
        }, context_instance=RequestContext(request))


from django.shortcuts import get_object_or_404
from django.views.generic.simple import direct_to_template
from django.template import RequestContext
import reversion
from datetime import datetime, timedelta, date
from reversion.models import Version
from models import Story

import pdb
import markdown

# from http://dwiel.net/blog/python-parsing-the-output-of-datetime-isoformat/
def _parse_iso_datetime(s) :
    # parse a datetime generated by datetime.isoformat()
    try :
        return datetime.strptime(s, "%Y-%m-%dT%H:%M:%S")
    except ValueError :
        return datetime.strptime(s, "%Y-%m-%dT%H:%M:%S.%f")

def index(request, template='lstory/index.html'):
    stories = Story.objects.filter(published__isnull=False)
    return direct_to_template(request, template, {
        'stories': stories,
        })

def version(request, slug, date, template='lstory/version.html'):
    obj = Story.objects.get(slug=slug)
    date = _parse_iso_datetime(date)
    version = obj.versions.for_date(date)

    return direct_to_template(request, template, {
        'current': version,
        'date': date,
        })



def serve_highlighted_text(request, slug, model, field_to_diff, sessionvar, template='lstory/highlight.html'):
    obj = get_object_or_404(model, slug=slug)

    sessionvar = sessionvar % slug

    if not hasattr(obj, field_to_diff):
        raise Exception("the selected object does not have a %s field" % field_to_diff)

    fromdate = None
    fromdate = request.GET.get('since', None)
    fromdate = request.session.get(sessionvar, None) if not fromdate else fromdate
    fromdate = unicode(datetime.now().isoformat()) if not fromdate else fromdate
    fromdate = _parse_iso_datetime(fromdate) if fromdate else None


    todate = datetime.now()

    current = obj.versions.for_date(todate)
    previous = obj.versions.for_date(fromdate)

    field_diff = current.diff_to_older(previous)

    request.session[sessionvar] = datetime.now().isoformat()



    return direct_to_template(request, template, {
        'current': current,
        'previous': previous,
        'field_diff': field_diff,
        'fromdate': fromdate,
        'todate': todate,
        })


replace render_to_response with direct_to_template calls