# encoding: utf-8
import logging
import json

from django.http import HttpResponse, JsonResponse
from django.views.generic import CreateView, DeleteView, ListView, View
#from django.views import View
from .models import Picture
from .response import JSONResponse, response_mimetype
from .serialize import serialize

logger = logging.getLogger(__name__)

class PictureCreateView(CreateView):
    model = Picture
    fields = "__all__"

    def form_valid(self, form):
        self.object = form.save()
        files = [serialize(self.object)]
        data = {'files': files}
        response = JSONResponse(data, mimetype=response_mimetype(self.request))
        response['Content-Disposition'] = 'inline; filename=files.json'
        return response

    def form_invalid(self, form):
        data = json.dumps(form.errors)
        return HttpResponse(content=data, status=400, content_type='application/json')


class PictureDeleteView(DeleteView):
    model = Picture

    def delete(self, request, *args, **kwargs):
        self.object = self.get_object()
        self.object.delete()
        response = JSONResponse(True, mimetype=response_mimetype(request))
        response['Content-Disposition'] = 'inline; filename=files.json'
        return response


class PictureListView(ListView):
    model = Picture

    def render_to_response(self, context, **response_kwargs):
        files = [ serialize(p) for p in self.get_queryset() ]
        data = {'files': files}
        response = JSONResponse(data, mimetype=response_mimetype(self.request))
        response['Content-Disposition'] = 'inline; filename=files.json'
        return response


class ProcessCsvView(View):

    def get(self, request, *args, **kwargs):
        return JsonResponse({'hello': 'world'})


## maybe the shit below can go away

class BasicVersionCreateView(PictureCreateView):
    template_name_suffix = '_basic_form'


class BasicPlusVersionCreateView(PictureCreateView):
    template_name_suffix = '_basicplus_form'


class AngularVersionCreateView(PictureCreateView):
    template_name_suffix = '_angular_form'


class jQueryVersionCreateView(PictureCreateView):
    template_name_suffix = '_jquery_form'



@@ -166,39 +166,8 @@
 iew%0A
-#from django.views import View%0A
 from
@@ -277,16 +277,33 @@
 rialize%0A
+import requests%0A%0A
 %0Alogger 
@@ -1714,46 +1714,654 @@
     
-return JsonResponse(%7B'hello': 'world'%7D
+sqlizer_url = 'https://sqlizer.io/api/files/'%0A        sqlizer_headers = %7B'Authorization':%0A                'Bearer lZicTcPoMhNGbzFmd_S_xox0G2RZ5SST026wUnKstQ88TTvZWa7eFbm3j1QUkelOm-4plUPZWGkhZL7I3ZVzkQ=='%7D%0A        resource_id = request.path.split('/')%5B2%5D%0A        csv = Picture.objects.get(pk = resource_id)%0A        data = %7B'DatabaseType': 'SQLServer',%0A            'FileType': 'csv',%0A            'FileName': csv.slug,%0A            'TableName': csv.file_type,%0A            'FileHasHeaders': True%0A            %7D%0A        r = requests.post(sqlizer_url, headers = sqlizer_headers,%0A                data = data)%0A        return JsonResponse(r.text, safe=False
 )%0A%0A%0A

UPDATE Houston, we have succesful sqlizer api requests