Changeset 4058 for waeup/branches


Ignore:
Timestamp:
3 Apr 2009, 10:45:17 (16 years ago)
Author:
uli
Message:

Add import/export views and viewlets.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • waeup/branches/ulif-rewrite/src/waeup/viewlets.py

    r4055 r4058  
    33import zope.interface
    44from zope.component import getMultiAdapter
    5 from interfaces import IUniversity, IWAeUPObject, IWAeUPXMLExporter
     5from zope.location.interfaces import ISite
     6from waeup.interfaces import (IUniversity, IWAeUPObject, IWAeUPXMLExporter,
     7                        IWAeUPXMLImporter)
    68
    79grok.context(zope.interface.Interface)
     
    3537            'Content-Type', 'text/xml; charset=UTF-8')
    3638        return xml
     39
     40class ImportXML(grok.View):
     41    """Replace the context object by an object created from an XML
     42       representation.
     43
     44       XXX: This does not work for ISite objects, i.e. for instance
     45            for complete University objects.
     46    """
     47    grok.require('waeup.manageUniversity')
     48    grok.template('master')
     49
     50    def update(self, xmlfile=None, CANCEL=None, SUBMIT=None):
     51        if CANCEL is not None:
     52            self.redirect(self.url(self.context))
     53            return
     54        if not xmlfile:
     55            return
     56        importer = IWAeUPXMLImporter(self.context)
     57        obj = importer.doImport(xmlfile)
     58        if type(obj) != type(self.context):
     59            return
     60        parent = self.context.__parent__
     61        name = self.context.__name__
     62        self.context = obj
     63        if hasattr(parent, name):
     64            setattr(parent, name, obj)
     65        else:
     66            del parent[name]
     67            parent[name] = obj
     68            pass
     69        return
     70       
     71           
    3772   
    3873class Head(grok.ViewletManager):
     
    97132        return '<div class="portlet"><a href="export.xml">%s</a></div>' % (
    98133            'Export as XML',)
     134
     135class ImportLink(grok.Viewlet):
     136    grok.viewletmanager(LeftSidebar)
     137    grok.view(Index)
     138    grok.order(99)
     139    grok.require('waeup.manageUniversity')
     140
     141    def render(self):
     142        if ISite.providedBy(self.context):
     143            # We cannot simply replace local sites.
     144            return ''
     145        return '<div class="portlet"><a href="importxml">%s</a></div>' % (
     146            'Import from XML',)
    99147   
    100148class MainArea(grok.ViewletManager):
    101149    grok.name('main')
    102150
     151class ImportXMLViewlet(grok.Viewlet):
     152    grok.viewletmanager(MainArea)
     153    grok.view(ImportXML)
     154   
    103155
    104156class Footer(grok.ViewletManager):
Note: See TracChangeset for help on using the changeset viewer.