from django.shortcuts import get_list_or_404, render_to_response, get_object_or_404
from django.template import RequestContext
from django.http import Http404
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.conf import settings
from datetime import datetime

from classifieds.models import Classified, Subcategory

# from classifieds.forms import ClassifiedForm


def index(request):
    subcategories = Subcategory.objects.filter(
        classified__expires_on__gt=datetime.now()
    ).distinct()
    return render_to_response(
        "classifieds/index.html", locals(), context_instance=RequestContext(request)
    )


def subcat_detail(request, slug):
    subcategories = Subcategory.objects.filter(
        classified__expires_on__gte=datetime.now()
    ).distinct()
    object = Subcategory.objects.get(slug=slug)
    featured_objects = Classified.featured_objects.filter(subcategory__slug=slug).extra(
        order_by=["copy"]
    )
    objects = Classified.live_objects.filter(subcategory__slug=slug, featured=False)
    return render_to_response(
        "classifieds/subcat_detail.html",
        locals(),
        context_instance=RequestContext(request),
    )


def subcat_old_redirect(request, underscore_slug):
    slug = underscore_slug.replace("_", "-")
    subcategories = Subcategory.objects.filter(
        classified__expires_on__gte=datetime.now()
    ).distinct()
    try:
        object = Subcategory.objects.get(slug=slug)
    except Subcategory.DoesNotExist:
        raise Http404
    except AssertionError:
        raise Http404
    featured_objects = Classified.featured_objects.filter(subcategory__slug=slug).extra(
        order_by=["copy"]
    )
    objects = Classified.live_objects.filter(subcategory__slug=slug, featured=False)
    return render_to_response(
        "classifieds/subcat_detail.html",
        locals(),
        context_instance=RequestContext(request),
    )


"""
def classified_detail(request, id):
    if request.user.is_authenticated():
        ad = Classified.objects.get(pk=id)
        if ad.owner == request.user:
            object=ad
    else:
        try:
            object=Classified.live_objects.get(pk=id)
            objects=Classified.live_objects.filter(subcategory__pk=object.subcategory.pk).exclude(pk=object.pk)
        except:
            pass
    return render_to_response('classifieds/classified_detail.html', locals(),
                              context_instance=RequestContext(request))

@login_required
def classified_create(request):
    form=ClassifiedForm(request.POST or None)
    if form.is_valid():
        classified=form.save(commit=False)
        classified.owner=request.user
        classified.save()
        return HttpResponseRedirect(reverse('classifieds.views.classified_detail', args=[classified.id]))
        
    return render_to_response('classifieds/classified_create.html', locals(),
                              context_instance=RequestContext(request))

@login_required
def classified_edit(request, id):
    instance = None
    if id is not None:
        instance = get_object_or_404(Classified, pk=id)
    if request.method == 'POST':
        form=ClassifiedForm(request.POST, instance=instance)
        if form.is_valid():
            classified=form.save()
            return HttpResponseRedirect(reverse('classifieds.views.classified_detail', args=[classified.id]))
    else:
        form = ClassifiedForm(instance=instance)
        
    return render_to_response('classifieds/classified_edit.html', locals(),
                              context_instance=RequestContext(request))
"""
