from django.conf import settings
from django.contrib.auth.forms import AuthenticationForm
from django.core.urlresolvers import reverse
from django.db.models import Count
from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404, redirect, render
from django.views.decorators.csrf import ensure_csrf_cookie

from core.common import *
from posts.forms import PostForm
from posts.models import Post
from users.models import User

@ensure_csrf_cookie
def post_form(request, post_id=None):		
	user = User.objects.get(user=request.user)

	post = Post.objects.get(pk=post_id)
	if user == post.author:
		form = PostForm(instance=post, request=request)
	else:
		return reject_user()
				
	return render(request, 'posts/edit.html', {
		'settings': settings,
		'user': user,
		'title': 'Edit ' + post.title,
		'post': post,
		'form': form,
	})
	
@ensure_csrf_cookie
def gallery(request, query=None):
	return render(request, 'posts/booru.html', {
		'settings': settings,
		'user': get_user(request.user),
		'posts': Post.objects.exclude(media=False, hidden=False),
	})
