import json

from django.core.urlresolvers import reverse
from django.shortcuts import redirect
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt

from mc2.controllers.base.views import ControllerCreateView, ControllerEditView
from mc2.views import HomepageView
from freebasics.forms import FreeBasicsControllerForm

from freebasics.models import FreeBasicsTemplateData, FreeBasicsController
from freebasics.serializers import FreeBasicsDataSerializer

from rest_framework import generics


class TemplateDataCreate(generics.ListCreateAPIView):
    queryset = FreeBasicsTemplateData.objects.all()
    serializer_class = FreeBasicsDataSerializer

    def perform_create(self, serializer):
        controller = FreeBasicsController.objects.create(
            owner=self.request.user)
        serializer.save(controller=controller)


class TemplateDataManage(generics.RetrieveUpdateDestroyAPIView):
    queryset = FreeBasicsTemplateData.objects.all()
    serializer_class = FreeBasicsDataSerializer


class FreeBasicsHomepageView(HomepageView):

    def dispatch(self, request, *args, **kwargs):
        if not self.get_queryset().exists():
            return redirect(reverse('freebasics_add'))
        return super(
            FreeBasicsHomepageView, self).dispatch(request, *args, **kwargs)


class FreeBasicsControllerCreateView(ControllerCreateView):
    form_class = FreeBasicsControllerForm
    template_name = 'freebasics_controller_edit.html'
    permissions = ['controllers.docker.add_dockercontroller']


class FreeBasicsControllerEditView(ControllerEditView):
    form_class = FreeBasicsControllerForm
    template_name = 'freebasics_controller_edit.html'
    permissions = ['controllers.docker.add_dockercontroller']


@csrf_exempt
def template_data_api_stub(request):
    if request.method == 'POST':
        json.loads(request.body)
        data = {'success': True}
    else:
        data = {
            "general": {
                "siteName": "Happy Place",
                "siteNameUrl": "happy-place"
            },
            "styles": {
                "base-bcolor": {"background-color": "#efefef"},
                "fb-body": {
                    "font-family": '"Open Sans", Helvetica, sans-serif'},
                "block-heading": {
                    "background-color": "#dfdfdf",
                    "font-family": '"Montserrat", Courier, monospace',
                    "text-transform": "uppercase"
                },
                "fb-accent-1": {"color": "#69269d"},
                "fb-accent-2": {"color": "#c736c0"}
            },
            "blocks": {
                "fb-block-header": {"position": 0},
                "fb-block-article": {"position": 2},
                "fb-block-banner": {"position": 3},
                "fb-block-category": {"position": 1},
                "fb-block-poll": {"position": 4},
                "fb-block-footer": {"position": 5}
            }
        }
    return HttpResponse(json.dumps(data), content_type='application/json')

@@ -1,17 +1,4 @@
-import json%0A%0A
 from
@@ -79,98 +79,8 @@
 rect
-%0Afrom django.http import HttpResponse%0Afrom django.views.decorators.csrf import csrf_exempt
 %0A%0Afr
@@ -1663,1282 +1663,4 @@
 r'%5D%0A
-%0A%0A@csrf_exempt%0Adef template_data_api_stub(request):%0A    if request.method == 'POST':%0A        json.loads(request.body)%0A        data = %7B'success': True%7D%0A    else:%0A        data = %7B%0A            %22general%22: %7B%0A                %22siteName%22: %22Happy Place%22,%0A                %22siteNameUrl%22: %22happy-place%22%0A            %7D,%0A            %22styles%22: %7B%0A                %22base-bcolor%22: %7B%22background-color%22: %22#efefef%22%7D,%0A                %22fb-body%22: %7B%0A                    %22font-family%22: '%22Open Sans%22, Helvetica, sans-serif'%7D,%0A                %22block-heading%22: %7B%0A                    %22background-color%22: %22#dfdfdf%22,%0A                    %22font-family%22: '%22Montserrat%22, Courier, monospace',%0A                    %22text-transform%22: %22uppercase%22%0A                %7D,%0A                %22fb-accent-1%22: %7B%22color%22: %22#69269d%22%7D,%0A                %22fb-accent-2%22: %7B%22color%22: %22#c736c0%22%7D%0A            %7D,%0A            %22blocks%22: %7B%0A                %22fb-block-header%22: %7B%22position%22: 0%7D,%0A                %22fb-block-article%22: %7B%22position%22: 2%7D,%0A                %22fb-block-banner%22: %7B%22position%22: 3%7D,%0A                %22fb-block-category%22: %7B%22position%22: 1%7D,%0A                %22fb-block-poll%22: %7B%22position%22: 4%7D,%0A                %22fb-block-footer%22: %7B%22position%22: 5%7D%0A            %7D%0A        %7D%0A    return HttpResponse(json.dumps(data), content_type='application/json')%0A

remove api stub