# w.c.s. - web application for online forms
# Copyright (C) 2005-2013  Entr'ouvert
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.

from quixote import get_publisher, get_request, get_response

from .forms.root import RootDirectory as FormsRootDirectory
from .qommon.admin.texts import TextsDirectory
from .qommon import template
from . import compat


class Backoffice(compat.TemplateWithFallbackView):
    template_name = 'wcs/backoffice.html'

    def post(self, request, *args, **kwargs):
        return self.get(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(Backoffice, self).get_context_data(**kwargs)

        with compat.request(self.request):
            get_request().response.filter = {'admin_ezt': True}
            body = get_publisher().try_publish(get_request())
            if isinstance(body, template.QommonTemplateResponse):
                body.add_media()
                body = template.render(body.templates, body.context)
                get_publisher().session_manager.finish_successful_request()
            self.quixote_response = get_request().response
            context.update(template.get_decorate_vars(body, get_response(), generate_breadcrumb=True))

        return context

backoffice = Backoffice.as_view()
