from django.shortcuts import render, redirect
from django.core import serializers
from django.http import HttpResponse
from tablib import Dataset

from .resources import PersonResource
from .forms import PostForm
from . models import feed

import json

# Create your views here.


def index(request):
    template = 'posts/index.html'
    results = feed.objects.all()
    jsondata = serializers.serialize('json', results)
    context = {
        'results': results,
        'jsondata': jsondata,
    }
    return render(request, template, context)


def getdata(request):
    results = feed.objects.all()
    jsondata = serializers.serialize('json', results)
    return HttpResponse(jsondata)


def base_layout(request):
    template = 'posts/base.html'
    return render(request, template)


def about_layout(request):
    template = 'posts/about.html'
    return render(request, template)


def post_new(request):
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            # post.author = request.user
            # post.published_date = timezone.now()
            post.save()
            return redirect('index')
    else:
        form = PostForm()
    return render(request, 'posts/post_edit.html', {'form': form})


def export(request):
    person_resource = PersonResource()
    dataset = person_resource.export()
    response = HttpResponse(dataset.csv, content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="persons.csv"'
    return response


def simple_upload(request):
    if request.method == 'POST':
        person_resource = PersonResource()
        dataset = Dataset()
        new_persons = request.FILES['myfile']

        imported_data = dataset.load(new_persons.read())
        result = person_resource.import_data(
            dataset, dry_run=True)  # Test the data import

        if not result.has_errors():
            person_resource.import_data(
                dataset, dry_run=False)  # Actually import now

    return render(request, 'core/simple_upload.html')


def feed_export(request):
    person_resource = FeedResource()
    dataset = person_resource.export()
    response = HttpResponse(dataset.csv, content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="feed.csv"'
    return response


def feed_upload(request):
    if request.method == 'POST':
        person_resource = FeedResource()
        dataset = Dataset()
        new_persons = request.FILES['myfile']

        imported_data = dataset.load(new_persons.read())
        result = person_resource.import_data(
            dataset, dry_run=True)  # Test the data import

        if not result.has_errors():
            person_resource.import_data(
                dataset, dry_run=False)  # Actually import now

    return render(request, 'posts/simple_upload.html')


def calander_layout(request):
    template = 'posts/calander.html'
    return render(request, template)
