def _increment(self, txn, record, params): self.log.debug("increment called") targetPath = params.getValue("param[@name='target-path']") keyPath = params.getValue("param[@name='key-path']") # We get the key data that we should look for in the related documents. targetID = record.getValue(keyPath) try: self.log.debug("increment retrieving record " + targetID) target = XMLFragment(self.getRecord(txn, targetID)) self.log.debug("increment record retrieved") result = target.getSingle(targetPath) # See whether we should update the count or add the element if result != None: count = int(result.getContent()) result.setContent(str(count + 1)) else: nodeName = targetPath.split("/")[-1] target.getRootElement().newChild(None, nodeName, "1") self.log.debug("increment saving modified record") self.updateRecord(txn, targetID, target.serialize()) except NotFoundError: self.log.warn("increment called for non-existent target")
def _prepareDocument(self, record): doc = record if isinstance(record, str): doc = XMLFragment(record) root = doc.getRootElement() # Add the date if it's not already there. if 0 == len(doc.xpathEval("//pubDate")): date = time.time() # ISO8601 Date # TODO this shouldn't really use localtime as the ISO8601 date should be GMT. Just not sure how to # convert the time using XSL-T otherwise. pubDate = root.newChild( None, "pubDate", time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime(date)) + "-07:00" ) pubDate.setProp("seconds", str(date)) # Set the id for the post # TODO: maybe this would be better as meta-data or should at least # be in a namespace # This call will also mark the database as modified # TODO: see if there's a better way. recordID = self.getNextID() root.setProp("id", str(recordID)) return (doc, recordID)
def _cascadeDeletes(self, txn, record, params): targetRoot = params.getValue("param[@name='target-root']") fromKey = params.getValue("param[@name='from-key']") targetKey = params.getValue("param[@name='target-key']") # We get the key data that we should look for in the related documents. relation = record.getValue("/%s/%s" % (record.getRootElement().name, fromKey)) if relation != "": # Now build the query to select the ids for the target documents query = "/%s[%s=%s]/@id" % (targetRoot, targetKey, relation) related = XMLFragment(self.xpathQuery(txn, query)) content = related.getRootElement().content.strip() if content != "": for id in content.split("\n"): try: self.deleteRecord(txn, id) except NotFoundError: self.log.warn("_cascadeDeletes: Deleting %s, but it's already gone" % id)
def _applyTriggers(self, txn, action, record): if isinstance(record, str): record = XMLFragment(record) rootName = record.getRootElement().name self.log.debug("_applyTriggers: handling %s for type %s" % (action, rootName)) # See if there are any reference triggers for this type of item and this # action query = "/datatypes/datatype/relation[@from-type = '%s']/trigger[on = '%s']" query = query % (rootName, action) self.log.debug("_applyTriggers: looking for triggers with query %s" % query) self._runTriggers(txn, query, record) # Handle any references for actions that should occur for the same # datatype query = "/datatypes/datatype[@root='%s']/trigger[on='%s']" query = query % (rootName, action) self.log.debug("_applyTriggers: looking for triggers with query %s" % query) self._runTriggers(txn, query, record)