from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.core.urlresolvers import reverse
from django.shortcuts import render
from django.http import HttpResponseRedirect

from core.models import Post, Category, Tag
from backend.forms import PostForm, CategoryForm, TagForm

# Create your views here.
@login_required()
def index(request):
    context = {}
    context['nav_active'] = 'index'

    return render(request, 'backend/index.html', context)

@login_required()
def posts(request):
    context = {}
    context['nav_active'] = 'posts'

    post_list = Post.objects.all()

    paginator = Paginator(list(reversed(post_list)), 10)

    page = request.GET.get('page')

    try:
        posts = paginator.page(page)
    except PageNotAnInteger:
        posts = paginator.page(1)
    except EmptyPage:
        posts = paginator.page(paginator.num_pages)

    context['posts'] = posts

    return render(request, 'backend/posts.html', context)

@login_required()
def add_post(request):
    context = {}
    context['nav_active'] = 'posts'

    form = PostForm()

    if request.method == 'POST':
        form = PostForm(request.POST, request.FILES)

        if form.is_valid():
            form.save()
            messages.success(request, 'Post created.')

            return HttpResponseRedirect(reverse('user_panel_posts'))

    context['form'] = form

    return render(request, 'backend/edit_post.html', context)

@login_required()
def edit_post(request, post_id):
    context = {}
    context['nav_active'] = 'posts'

    post = Post.objects.get(pk=post_id)
    context['post'] = post

    form = PostForm(instance=post)

    if request.method == 'POST':
        form = PostForm(request.POST, request.FILES, instance=post)

        if form.is_valid():
            form.save()
            messages.success(request, 'Post updated.')

            return HttpResponseRedirect(reverse('user_panel_posts'))

    context['form'] = form

    return render(request, 'backend/edit_post.html', context)

@login_required()
def delete_post(request, post_id):
    context = {}
    context['nav_active'] = 'posts'

    post = Post.objects.get(pk=post_id)
    post.delete()

    messages.success(request, 'Post deleted.')

    return HttpResponseRedirect(reverse('user_panel_posts'))

@login_required()
def categories(request):
    context = {}
    context['nav_active'] = 'categories'

    categories_list = Category.objects.all()

    paginator = Paginator(list(reversed(categories_list)), 10)

    page = request.GET.get('page')

    try:
        categories = paginator.page(page)
    except PageNotAnInteger:
        categories = paginator.page(1)
    except EmptyPage:
        categories = paginator.page(paginator.num_pages)

    context['categories'] = categories
    return render(request, 'backend/categories.html', context)

@login_required()
def add_category(request):
    context = {}
    context['nav_active'] = 'categories'

    form = CategoryForm()

    if request.method == 'POST':
        form = CategoryForm(request.POST, request.FILES)

        if form.is_valid():
            form.save()
            messages.success(request, 'Category created.')

            return HttpResponseRedirect(reverse('user_panel_categories'))

    context['form'] = form

    return render(request, 'backend/edit_category.html', context)

@login_required()
def edit_category(request, category_id):
    context = {}
    context['nav_active'] = 'categories'

    category = Category.objects.get(pk=category_id)
    context['category'] = category

    form = CategoryForm(instance=category)

    if request.method == 'POST':
        form = CategoryForm(request.POST, request.FILES, instance=category)

        if form.is_valid():
            form.save()
            messages.success(request, 'Category updated.')

            return HttpResponseRedirect(reverse('user_panel_categories'))

    context['form'] = form

    return render(request, 'backend/edit_category.html', context)

@login_required()
def delete_category(request, category_id):
    context = {}
    context['nav_active'] = 'categories'

    category = Category.objects.get(pk=category_id)
    category.delete()

    messages.success(request, 'Category deleted.')

    return HttpResponseRedirect(reverse('user_panel_categories'))

@login_required()
def tags(request):
    context = {}
    context['nav_active'] = 'tags'

    tags_list = Tag.objects.all()

    paginator = Paginator(list(reversed(tags_list)), 10)

    page = request.GET.get('page')

    try:
        tags = paginator.page(page)
    except PageNotAnInteger:
        tags = paginator.page(1)
    except EmptyPage:
        tags = paginator.page(paginator.num_pages)

    context['tags'] = tags
    return render(request, 'backend/tags.html', context)

@login_required()
def add_tag(request):
    context = {}
    context['nav_active'] = 'tags'

    form = TagForm()

    if request.method == 'POST':
        form = TagForm(request.POST, request.FILES)

        if form.is_valid():
            form.save()
            messages.success(request, 'Tag created.')

            return HttpResponseRedirect(reverse('user_panel_tags'))

    context['form'] = form

    return render(request, 'backend/edit_tag.html', context)

@login_required()
def edit_tag(request, tag_id):
    context = {}
    context['nav_active'] = 'tags'

    tag = Tag.objects.get(pk=tag_id)
    context['tag'] = tag

    form = TagForm(instance=tag)

    if request.method == 'POST':
        form = TagForm(request.POST, request.FILES, instance=tag)

        if form.is_valid():
            form.save()
            messages.success(request, 'Tag updated.')

            return HttpResponseRedirect(reverse('user_panel_tags'))

    context['form'] = form

    return render(request, 'backend/edit_tag.html', context)

@login_required()
def delete_tag(request, tag_id):
    context = {}
    context['nav_active'] = 'tags'

    tag = Tag.objects.get(pk=tag_id)
    tag.delete()

    messages.success(request, 'Tag deleted.')

    return HttpResponseRedirect(reverse('user_panel_tags'))
[(12, 'django.contrib.auth.decorators.login_required', 'login_required', False, 'from django.contrib.auth.decorators import login_required\n'), (19, 'django.contrib.auth.decorators.login_required', 'login_required', False, 'from django.contrib.auth.decorators import login_required\n'), (41, 'django.contrib.auth.decorators.login_required', 'login_required', False, 'from django.contrib.auth.decorators import login_required\n'), (61, 'django.contrib.auth.decorators.login_required', 'login_required', False, 'from django.contrib.auth.decorators import login_required\n'), (84, 'django.contrib.auth.decorators.login_required', 'login_required', False, 'from django.contrib.auth.decorators import login_required\n'), (96, 'django.contrib.auth.decorators.login_required', 'login_required', False, 'from django.contrib.auth.decorators import login_required\n'), (117, 'django.contrib.auth.decorators.login_required', 'login_required', False, 'from django.contrib.auth.decorators import login_required\n'), (137, 'django.contrib.auth.decorators.login_required', 'login_required', False, 'from django.contrib.auth.decorators import login_required\n'), (160, 'django.contrib.auth.decorators.login_required', 'login_required', False, 'from django.contrib.auth.decorators import login_required\n'), (172, 'django.contrib.auth.decorators.login_required', 'login_required', False, 'from django.contrib.auth.decorators import login_required\n'), (193, 'django.contrib.auth.decorators.login_required', 'login_required', False, 'from django.contrib.auth.decorators import login_required\n'), (213, 'django.contrib.auth.decorators.login_required', 'login_required', False, 'from django.contrib.auth.decorators import login_required\n'), (236, 'django.contrib.auth.decorators.login_required', 'login_required', False, 'from django.contrib.auth.decorators import login_required\n'), (17, 'django.shortcuts.render', 'render', False, 'from django.shortcuts import render\n'), (24, 'core.models.Post.objects.all', 'Post.objects.all', False, 'from core.models import Post, Category, Tag\n'), (39, 'django.shortcuts.render', 'render', False, 'from django.shortcuts import render\n'), (46, 'backend.forms.PostForm', 'PostForm', False, 'from backend.forms import PostForm, CategoryForm, TagForm\n'), (59, 'django.shortcuts.render', 'render', False, 'from django.shortcuts import render\n'), (66, 'core.models.Post.objects.get', 'Post.objects.get', False, 'from core.models import Post, Category, Tag\n'), (69, 'backend.forms.PostForm', 'PostForm', False, 'from backend.forms import PostForm, CategoryForm, TagForm\n'), (82, 'django.shortcuts.render', 'render', False, 'from django.shortcuts import render\n'), (89, 'core.models.Post.objects.get', 'Post.objects.get', False, 'from core.models import Post, Category, Tag\n'), (92, 'django.contrib.messages.success', 'messages.success', False, 'from django.contrib import messages\n'), (101, 'core.models.Category.objects.all', 'Category.objects.all', False, 'from core.models import Post, Category, Tag\n'), (115, 'django.shortcuts.render', 'render', False, 'from django.shortcuts import render\n'), (122, 'backend.forms.CategoryForm', 'CategoryForm', False, 'from backend.forms import PostForm, CategoryForm, TagForm\n'), (135, 'django.shortcuts.render', 'render', False, 'from django.shortcuts import render\n'), (142, 'core.models.Category.objects.get', 'Category.objects.get', False, 'from core.models import Post, Category, Tag\n'), (145, 'backend.forms.CategoryForm', 'CategoryForm', False, 'from backend.forms import PostForm, CategoryForm, TagForm\n'), (158, 'django.shortcuts.render', 'render', False, 'from django.shortcuts import render\n'), (165, 'core.models.Category.objects.get', 'Category.objects.get', False, 'from core.models import Post, Category, Tag\n'), (168, 'django.contrib.messages.success', 'messages.success', False, 'from django.contrib import messages\n'), (177, 'core.models.Tag.objects.all', 'Tag.objects.all', False, 'from core.models import Post, Category, Tag\n'), (191, 'django.shortcuts.render', 'render', False, 'from django.shortcuts import render\n'), (198, 'backend.forms.TagForm', 'TagForm', False, 'from backend.forms import PostForm, CategoryForm, TagForm\n'), (211, 'django.shortcuts.render', 'render', False, 'from django.shortcuts import render\n'), (218, 'core.models.Tag.objects.get', 'Tag.objects.get', False, 'from core.models import Post, Category, Tag\n'), (221, 'backend.forms.TagForm', 'TagForm', False, 'from backend.forms import PostForm, CategoryForm, TagForm\n'), (234, 'django.shortcuts.render', 'render', False, 'from django.shortcuts import render\n'), (241, 'core.models.Tag.objects.get', 'Tag.objects.get', False, 'from core.models import Post, Category, Tag\n'), (244, 'django.contrib.messages.success', 'messages.success', False, 'from django.contrib import messages\n'), (49, 'backend.forms.PostForm', 'PostForm', False, 'from backend.forms import PostForm, CategoryForm, TagForm\n'), (72, 'backend.forms.PostForm', 'PostForm', False, 'from backend.forms import PostForm, CategoryForm, TagForm\n'), (94, 'django.core.urlresolvers.reverse', 'reverse', False, 'from django.core.urlresolvers import reverse\n'), (125, 'backend.forms.CategoryForm', 'CategoryForm', False, 'from backend.forms import PostForm, CategoryForm, TagForm\n'), (148, 'backend.forms.CategoryForm', 'CategoryForm', False, 'from backend.forms import PostForm, CategoryForm, TagForm\n'), (170, 'django.core.urlresolvers.reverse', 'reverse', False, 'from django.core.urlresolvers import reverse\n'), (201, 'backend.forms.TagForm', 'TagForm', False, 'from backend.forms import PostForm, CategoryForm, TagForm\n'), (224, 'backend.forms.TagForm', 'TagForm', False, 'from backend.forms import PostForm, CategoryForm, TagForm\n'), (246, 'django.core.urlresolvers.reverse', 'reverse', False, 'from django.core.urlresolvers import reverse\n'), (53, 'django.contrib.messages.success', 'messages.success', False, 'from django.contrib import messages\n'), (76, 'django.contrib.messages.success', 'messages.success', False, 'from django.contrib import messages\n'), (129, 'django.contrib.messages.success', 'messages.success', False, 'from django.contrib import messages\n'), (152, 'django.contrib.messages.success', 'messages.success', False, 'from django.contrib import messages\n'), (205, 'django.contrib.messages.success', 'messages.success', False, 'from django.contrib import messages\n'), (228, 'django.contrib.messages.success', 'messages.success', False, 'from django.contrib import messages\n'), (55, 'django.core.urlresolvers.reverse', 'reverse', False, 'from django.core.urlresolvers import reverse\n'), (78, 'django.core.urlresolvers.reverse', 'reverse', False, 'from django.core.urlresolvers import reverse\n'), (131, 'django.core.urlresolvers.reverse', 'reverse', False, 'from django.core.urlresolvers import reverse\n'), (154, 'django.core.urlresolvers.reverse', 'reverse', False, 'from django.core.urlresolvers import reverse\n'), (207, 'django.core.urlresolvers.reverse', 'reverse', False, 'from django.core.urlresolvers import reverse\n'), (230, 'django.core.urlresolvers.reverse', 'reverse', False, 'from django.core.urlresolvers import reverse\n')]