def deleteResource(self, resource=None, resource_id=None): """ Remove a resource from the database. """ if resource_id: resource = self.xmldb.getResource(id=resource_id) # create backup entry into the global trash folder if self.env.config.getbool('seishub', 'use_trash_folder', False): data = resource.document.data # ensure we return a UTF-8 encoded string not an Unicode object if isinstance(data, unicode): data = data.encode('utf-8') # set XML declaration inclusive UTF-8 encoding string if not data.startswith('<xml'): data = addXMLDeclaration(data, 'utf-8') path = os.path.join(self.env.getInstancePath(), 'data', 'trash', resource.package.package_id, resource.resourcetype.resourcetype_id) if not os.path.exists(path): os.makedirs(path) # add current datetime to filename to prevent overwriting resources # with the same filename (e.g. resource deleted/created several # times) filename = resource.name filename += datetime.datetime.now().strftime('__%Y%m%d%H%M%S') file = os.path.join(path, filename) try: fp = open(file, 'wb') fp.write(data) fp.close() except: pass # remove indexed data: self.index_catalog.flushResource(resource) res = self.xmldb.deleteResource(resource) if not res: msg = "Error deleting a resource: No resource was found with " + \ "the given parameters." raise NotFoundError(msg) return res
def render_GET(self, request): """ Process a resource query request. A query at the root of a resource type folder returns a list of all available XML resource objects of this resource type. Direct request on a XML resource results in the content of a XML document. Before returning a XML document, we add a valid XML declaration header and encode it as UTF-8 string. @see: U{http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1} for all possible error codes. """ # global anonymous access allowed if request.env.auth.getUser('anonymous').permissions != 755: self._checkPermissions(request, 755) data = self.res.document.data # ensure we return a UTF-8 encoded string not an Unicode object if isinstance(data, unicode): data = data.encode('utf-8') # set XML declaration inclusive UTF-8 encoding string if not data.startswith('<xml'): data = addXMLDeclaration(data, 'utf-8') # handle output/format conversion data = self._format(request, data) # set last-modified time dt = UTCDateTime(self.res.document.meta.getDatetime()) try: request.setLastModified(dt.timestamp) except: pass # # cache control - 10 seconds # now = UTCDateTime() # request.setHeader("Cache-Control", "max-age = 10") # request.setHeader("Expires", http.datetimeToString(now.timestamp + 10)) # return data return data
# -*- coding: utf-8 -*- from seishub.core.core import implements from seishub.core.db.orm import Serializable, Relation, db_property, LazyAttribute from seishub.core.exceptions import InvalidParameterError from seishub.core.registry.package import ResourceTypeWrapper from seishub.core.util.text import hash, validate_id from seishub.core.util.xml import toUnicode, parseXMLDeclaration, addXMLDeclaration from seishub.core.util.xmlwrapper import IXmlDoc, XmlTreeDoc from seishub.core.xmldb.defaults import resource_tab, document_tab, \ document_meta_tab from seishub.core.xmldb.interfaces import IResource, IXmlDocument, IDocumentMeta XML_DECLARATION_LENGTH = len(addXMLDeclaration("")) class DocumentMeta(Serializable): """contains document specific metadata; such as: size, datetime, hash, user_id """ implements (IDocumentMeta) db_table = document_meta_tab db_mapping = { '_id':'id', 'uid':'uid', 'datetime':'datetime', 'size':'size',
def render_GET(self, request): property = request.postpath[-1] # check for valid properties if property == '.index': res = request.env.catalog.getResource(self.package_id, self.resourcetype_id, self.name, self.revision) # dictionary of indexes index_dict = request.env.catalog.getIndexData(res) # create a XML document root = etree.Element("seishub") for label, values_dict in index_dict.iteritems(): sub = etree.SubElement(root, label) for _pos, values in values_dict.iteritems(): for value in values: if not value: continue if not isinstance(value, basestring): value = unicode(value) etree.SubElement(sub, "value").text = value data = etree.tostring(root, pretty_print=True, encoding='utf-8') format_prefix = 'index' elif property == '.meta': res = request.env.catalog.getResource(self.package_id, self.resourcetype_id, self.name, self.revision) meta = res.document.meta # create a XML document root = etree.Element("seishub") etree.SubElement(root, "package").text = self.package_id etree.SubElement(root, "resourcetype").text = self.resourcetype_id etree.SubElement(root, "name").text = self.name etree.SubElement(root, "document_id").text = str(res.document._id) etree.SubElement(root, "resource_id").text = str(res._id) etree.SubElement(root, "revision").text = \ str(res.document.revision) etree.SubElement(root, "uid").text = unicode(meta.uid) etree.SubElement(root, "datetime").text = \ unicode(meta.datetime.isoformat()) etree.SubElement(root, "size").text = unicode(meta.size) etree.SubElement(root, "hash").text = unicode(meta.hash) data = etree.tostring(root, pretty_print=True, encoding='utf-8') format_prefix = 'meta' else: raise NotFoundError("Property %s is not defined." % property) # ensure we return a utf-8 encoded string not an unicode object if isinstance(data, unicode): data = data.encode('utf-8') # set XML declaration inclusive UTF-8 encoding string if not data.startswith('<xml'): data = addXMLDeclaration(data, 'utf-8') # parse request headers for output type format = request.args.get('format', [None])[0] or \ request.args.get('output', [None])[0] # handle output/format conversion if format: format = '%s.%s' % (format_prefix, format) # fetch a XSLT document object reg = request.env.registry xslt = reg.stylesheets.get(package_id='seishub', resourcetype_id='stylesheet', type=format) if len(xslt): xslt = xslt[0] data = xslt.transform(data) # set additional content-type if given in XSLT if xslt.content_type: request.setHeader('content-type', xslt.content_type + '; charset=UTF-8') else: msg = "There is no stylesheet for requested format %s." request.env.log.debug(msg % format) return data
# -*- coding: utf-8 -*- from seishub.core.core import implements from seishub.core.db.orm import Serializable, Relation, db_property, LazyAttribute from seishub.core.exceptions import InvalidParameterError from seishub.core.registry.package import ResourceTypeWrapper from seishub.core.util.text import hash, validate_id from seishub.core.util.xml import toUnicode, parseXMLDeclaration, addXMLDeclaration from seishub.core.util.xmlwrapper import IXmlDoc, XmlTreeDoc from seishub.core.xmldb.defaults import resource_tab, document_tab, \ document_meta_tab from seishub.core.xmldb.interfaces import IResource, IXmlDocument, IDocumentMeta XML_DECLARATION_LENGTH = len(addXMLDeclaration("")) class DocumentMeta(Serializable): """contains document specific metadata; such as: size, datetime, hash, user_id """ implements(IDocumentMeta) db_table = document_meta_tab db_mapping = { '_id': 'id', 'uid': 'uid', 'datetime': 'datetime', 'size': 'size', 'hash': 'hash'