#
#  Copyright (c) 2008 Helmut Merz helmutm@cy55.de
#
#  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, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

"""
view class(es) for import/export.

$Id$
"""

from zope.interface import Interface, implements
from zope.app import zapi
from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
from zope.cachedescriptors.property import Lazy
from zope.security.proxy import removeSecurityProxy
from cStringIO import StringIO

from loops.external.external import IExternalContentSource
from loops.external.external import ILoader, IExporter


class NodesExportImport(object):
    """ View providing export and import functionality.
    """

    def __init__(self, context, request):
        #self.context = removeSecurityProxy(context)
        self.context = context
        self.request = request
        self.message = u''

    def submit(self):
        action = self.request.get('loops.action', None)
        if action:
            method = getattr(self, action, None)
            if method:
                return method()
        return False

    def export(self):
        f = StringIO()
        exporter = IExporter(self.context)
        exporter.dumpData(f, noclose=True)
        text = f.getvalue()
        f.close()
        self.setDownloadHeader(self.request, text)
        return text

    def upload(self):
        data = self.request.get('field.data', None)
        if not data:
            return False
        importer = IExternalContentSource(self.context)
        text = importer.getData(data)
        loader = ILoader(self.context)
        loader.load(text)
        self.message = u'Content uploaded and imported.'
        return False

    def setDownloadHeader(self, request, text):
        response = request.response
        response.setHeader('Content-Disposition',
                           'attachment; filename=loopscontent.dmp')
        response.setHeader('Content-Type', 'text/plain')
        response.setHeader('Content-Length', len(text))

