loops/external/browser.py
helmutm 5271981119 merged Dojo 1.0 branch
git-svn-id: svn://svn.cy55.de/Zope3/src/loops/trunk@2388 fd906abe-77d9-0310-91a1-e0d9ade77398
2008-02-10 09:56:42 +00:00

83 lines
2.6 KiB
Python

#
# Copyright (c) 2006 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 import component
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.base import Loader, Extractor
from loops.external.interfaces import IReader, IWriter
class ExportImport(object):
""" View providing export and import functionality.
"""
def __init__(self, context, request):
self.context = removeSecurityProxy(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()
extractor = Extractor(self.context)
elements = extractor.extract()
writer = component.getUtility(IWriter)
writer.write(elements, f)
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
reader = component.getUtility(IReader)
elements = reader.read(data)
loader = Loader(self.context)
loader.load(elements)
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))