Esempio n. 1
0
def export_xml_resources(modeladmin, request, queryset):
    from StringIO import StringIO
    from zipfile import ZipFile
    from xml.etree import ElementTree
    from metashare.xml_utils import pretty_xml
    from django import http

    zipfilename = "resources_export.zip"
    in_memory = StringIO()
    
    with ZipFile(in_memory, 'w') as zipfile:
        for obj in queryset:
            try:
                root_node = obj.export_to_elementtree()
                xml_string = ElementTree.tostring(root_node, encoding="utf-8")
                pretty = pretty_xml(xml_string).encode('utf-8')
                resource_filename = 'resource-{0}.xml'.format(obj.storage_object.id)
                zipfile.writestr(resource_filename, pretty)
    
            except Exception:
                raise Http404(_('Could not export resource "%(name)s" with primary key %(key)s.') \
                  % {'name': force_unicode(obj), 'key': escape(obj.storage_object.id)})

        zipfile.close()  

        response = http.HttpResponse(mimetype='application/zip')
        response['Content-Disposition'] = 'attachment; filename=%s' % (zipfilename)
        
        in_memory.seek(0)      
        response.write(in_memory.read())  

        return response
Esempio n. 2
0
 def assert_import_equals_export(self, _roundtrip):
     _result = test_utils.import_xml(_roundtrip)
     with open(_roundtrip) as _import_file:
         _import_xml = _import_file.read()
         register_namespace('', SCHEMA_NAMESPACE)
         _import_xml = tostring(fromstring(_import_xml))
     _export_xml = tostring(_result[0].export_to_elementtree())
 # cfedermann: uncomment these lines to dump import/export XML to file.
 #
 #with open('/tmp/_import.xml', 'w') as _out:
 #    _out.write(pretty_xml(_import_xml).encode('utf-8'))
 #with open('/tmp/_export.xml', 'w') as _out:
 #    _out.write(pretty_xml(_export_xml).encode('utf-8'))
     _pretty_import = pretty_xml(_import_xml).strip().encode('utf-8')
     _pretty_export = pretty_xml(_export_xml).strip().encode('utf-8')
     diff = '\n'.join(unified_diff(_pretty_import.split('\n'), _pretty_export.split('\n')))
     self.assertEqual(_pretty_import, _pretty_export,
          msg='For file {0}, export differs from import:\n{1}'.format(_roundtrip, diff))
Esempio n. 3
0
    def exportxml(self, request, object_id, extra_context=None):
        """
        Export the XML description for one single resource
        """
        model = self.model
        opts = model._meta

        obj = self.get_object(request, unquote(object_id))

        if not self.has_change_permission(request, obj):
            raise PermissionDenied

        if obj is None:
            raise Http404(_('%(name)s object with primary key %(key)s does not exist.') \
             % {'name': force_unicode(opts.verbose_name), 'key': escape(object_id)})

        if obj.storage_object is None:
            raise Http404(_('%(name)s object with primary key %(key)s does not have a StorageObject attached.') \
              % {'name': force_unicode(opts.verbose_name), 'key': escape(object_id)})
        elif obj.storage_object.deleted:
            raise Http404(_('%(name)s object with primary key %(key)s does not exist anymore.') \
              % {'name': force_unicode(opts.verbose_name), 'key': escape(object_id)})

        from xml.etree import ElementTree
        from metashare.xml_utils import pretty_xml
        from django import http

        try:
            root_node = obj.export_to_elementtree()
            xml_string = ElementTree.tostring(root_node, encoding="utf-8")
            pretty = pretty_xml(xml_string).encode('utf-8')
            resource_filename = 'resource-{0}.xml'.format(object_id)
        
            response = http.HttpResponse(pretty, mimetype='text/xml')
            response['Content-Disposition'] = 'attachment; filename=%s' % (resource_filename)
            return response

        except Exception:
            raise Http404(_('Could not export resource "%(name)s" with primary key %(key)s.') \
              % {'name': force_unicode(opts.verbose_name), 'key': escape(object_id)})
Esempio n. 4
0
 def update_storage(self):
     """
     Updates the metadata XML if required and serializes it and this storage
     object to the storage folder.
     """
     # for internal resources, no serialization is done
     if self.publication_status is INTERNAL:
         return
     
     # check if the storage folder for this storage object instance exists
     if self._storage_folder() and not exists(self._storage_folder()):
         # If not, create the storage folder.
         mkdir(self._storage_folder())
     
     # update the checksum, if a downloadable file exists
     if self.master_copy:
         self._compute_checksum()
     
     # flag to indicate if rebuilding of resource.zip is required
     update_zip = False
     
     self.digest_last_checked = datetime.now()        
     
     # create current version of metadata XML
     _metadata = pretty_xml(tostring(
       # pylint: disable-msg=E1101
       self.resourceinfotype_model_set.all()[0].export_to_elementtree()))
     
     # check if there exists a metadata XML file; this is not the case if
     # the publication status just changed from internal to ingested
     _xml_exists = os.path.isfile(
       '{0}/metadata-{1:04d}.xml'.format(self._storage_folder(), self.revision))
       
     # check if metadata has changed; if yes, increase revision for ingested
     # and published resources and save metadata to storage folder
     if self.metadata != _metadata or not _xml_exists:
         if self.metadata != _metadata:
             self.metadata = _metadata
             self.modified = datetime.now()
         if self.publication_status in (INGESTED, PUBLISHED):
             self.revision += 1
             # serialize metadata
             with open('{0}/metadata-{1:04d}.xml'.format(
               self._storage_folder(), self.revision), 'wb') as _out:
                 _out.write(unicode(self.metadata).encode('utf-8'))
             update_zip = True
         LOGGER.debug(u"\nMETADATA: {0}\n".format(self.metadata))
     
     # check if global storage object serialization has changed; if yes,
     # save it to storage folder
     _dict_global = { }
     for item in GLOBAL_STORAGE_ATTS:
         _dict_global[item] = getattr(self, item)
     _global_storage = \
       dumps(_dict_global, cls=DjangoJSONEncoder, sort_keys=True, separators=(',',':'))
     if self.global_storage != _global_storage:
         self.global_storage = _global_storage
         if self.publication_status in (INGESTED, PUBLISHED):
             with open('{0}/storage-global.json'.format(
               self._storage_folder()), 'wb') as _out:
                 _out.write(unicode(self.global_storage).encode('utf-8'))
             update_zip = True
     
     # create new digest zip if required, but only for master copies
     if update_zip and self.copy_status == MASTER:
         _zf_name = '{0}/resource.zip'.format(self._storage_folder())
         _zf = zipfile.ZipFile(_zf_name, mode='w', compression=ZIP_DEFLATED)
         try:
             _zf.write(
               '{0}/metadata-{1:04d}.xml'.format(self._storage_folder(), self.revision),
               arcname='metadata.xml')
             _zf.write(
               '{0}/storage-global.json'.format(self._storage_folder()),
               arcname='storage-global.json')
         finally:
             _zf.close()
         # update zip digest checksum
         self.digest_checksum = compute_checksum(_zf_name)
         # update last modified timestamp
         self.digest_modified = datetime.now()
         
     # check if local storage object serialization has changed; if yes,
     # save it to storage folder
     _dict_local = { }
     for item in LOCAL_STORAGE_ATTS:
         _dict_local[item] = getattr(self, item)
     _local_storage = \
       dumps(_dict_local, cls=DjangoJSONEncoder, sort_keys=True, separators=(',',':'))
     if self.local_storage != _local_storage:
         self.local_storage = _local_storage
         if self.publication_status in (INGESTED, PUBLISHED):
             with open('{0}/storage-local.json'.format(
               self._storage_folder()), 'wb') as _out:
                 _out.write(unicode(self.local_storage).encode('utf-8'))
     
     # save storage object if required; this is always required since at 
     # least self.digest_last_checked has changed
     self.save()
Esempio n. 5
0
                continue
    
        conn.close()
    
    # Disable verbose debug output for the import process...
    settings.DEBUG = False
    
    SUCCESSFUL_EXPORTS = 0
    ERRONEOUS_EXPORTS = 0
    RESOURCE_NO = 0
    from metashare.repository.models import resourceInfoType_model
    from metashare.xml_utils import pretty_xml
    with ZipFile(sys.argv[1], 'w') as out:
        for resource in resourceInfoType_model.objects.all():
            try:
                RESOURCE_NO += 1
                root_node = resource.export_to_elementtree()
                xml_string = ElementTree.tostring(root_node, encoding="utf-8")
                pretty = pretty_xml(xml_string).encode('utf-8')
                resource_filename = 'resource-{0}.xml'.format(RESOURCE_NO)
                out.writestr(resource_filename, pretty)
                SUCCESSFUL_EXPORTS += 1
            
            except Exception:
                ERRONEOUS_EXPORTS += 1
                print 'Could not export resource id={0}!'.format(resource.id)
                print traceback.format_exc()
    
    print "Done. Successfully exported {0} files from the database, errors " \
      "occured in {1} cases.".format(SUCCESSFUL_EXPORTS, ERRONEOUS_EXPORTS)