# Path: cmsplugin_shop/utils.py
# def get_view(name):
#     view = import_string(getattr(
#         settings.settings,
#         'CMSPLUGIN_SHOP_{}_VIEW'.format(name.upper()),
#         'cmsplugin_shop.views.{}'.format(name),
#     ))
#     return hasattr(view, 'as_view') and view.as_view() or view
# 
# def get_form(name):
#     return import_string(getattr(settings.settings,
#         'CMSPLUGIN_SHOP_{}_FORM'.format(name.upper()),
#         'cmsplugin_shop.forms.{}Form'.format(name),
#     ))


import cStringIO
from cms.views import details as cms_page
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import reverse
from django.forms import ModelForm
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
from django.views.generic import (
    CreateView, DetailView, FormView, ListView,
    TemplateView, UpdateView, View,
)
from formtools.wizard.views import SessionWizardView
from os.path import basename
from xhtml2pdf import pisa
from . import models, settings
from .utils import get_view, get_form


class PdfViewMixin(object):
    """
    A base view for displaying a Pdf
    """

    def get_attachment_filename(self):
        if hasattr(self, 'attachment_filename'):
            return self.attachment_filename
        filename = basename(self.request.path)
        return filename.endswith('.pdf') and filename or '{}.pdf'.format(filename)

    def get(self, request, *args, **kwargs):
        content = super(PdfViewMixin, self).get(request, *args, **kwargs).render().content

        result = cStringIO.StringIO()
        pdf = pisa.CreatePDF(cStringIO.StringIO(content), result, encoding='UTF-8')
        if pdf.err:
            raise Exception(pdf.err)

        response = HttpResponse(content_type='application/pdf')
        response['Content-Disposition'] = 'attachment; filename="{}"'.format(self.get_attachment_filename())
        response.write(result.getvalue())
        result.close()
        return response



class ProductView(FormView):

# Path: cmsplugin_shop/utils.py
# def get_view(name):
#     view = import_string(getattr(
#         settings.settings,
#         'CMSPLUGIN_SHOP_{}_VIEW'.format(name.upper()),
#         'cmsplugin_shop.views.{}'.format(name),
#     ))
#     return hasattr(view, 'as_view') and view.as_view() or view
# 
# def get_form(name):
#     return import_string(getattr(settings.settings,
#         'CMSPLUGIN_SHOP_{}_FORM'.format(name.upper()),
#         'cmsplugin_shop.forms.{}Form'.format(name),
#     ))


# Path: cmsplugin_shop/views.py
import cStringIO
from cms.views import details as cms_page
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import reverse
from django.forms import ModelForm
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
from django.views.generic import (
    CreateView, DetailView, FormView, ListView,
    TemplateView, UpdateView, View,
)
from formtools.wizard.views import SessionWizardView
from os.path import basename
from xhtml2pdf import pisa
from . import models, settings
from .utils import get_view, get_form



class PdfViewMixin(object):
    """
    A base view for displaying a Pdf
    """

    def get_attachment_filename(self):
        if hasattr(self, 'attachment_filename'):
            return self.attachment_filename
        filename = basename(self.request.path)
        return filename.endswith('.pdf') and filename or '{}.pdf'.format(filename)

    def get(self, request, *args, **kwargs):
        content = super(PdfViewMixin, self).get(request, *args, **kwargs).render().content

        result = cStringIO.StringIO()
        pdf = pisa.CreatePDF(cStringIO.StringIO(content), result, encoding='UTF-8')
        if pdf.err:
            raise Exception(pdf.err)

        response = HttpResponse(content_type='application/pdf')
        response['Content-Disposition'] = 'attachment; filename="{}"'.format(self.get_attachment_filename())
        response.write(result.getvalue())
        result.close()
        return response



class ProductView(FormView):

    form_class = get_form('CartItem')