示例#1
0
 def put_document(self, uri, document, check_etag):
     document = document.decode('utf-8')
     self._normalize_document_path(uri)
     etag = make_random_etag(uri)
     result = self._database.put(uri, document, check_etag, etag)
     result.addCallback(self._cb_put, uri, "%s@%s" % (uri.user.username, uri.user.domain))
     result.addErrback(self._eb_not_found)
     return result
示例#2
0
 def put_document(self, uri, document, check_etag):
     document = document.decode('utf-8')
     self._normalize_document_path(uri)
     etag = make_random_etag(uri)
     result = self._database.put(uri, document, check_etag, etag)
     result.addCallback(self._cb_put, uri,
                        "%s@%s" % (uri.user.username, uri.user.domain))
     result.addErrback(self._eb_not_found)
     return result
示例#3
0
    def _put_document(self, trans, uri, document, check_etag):
        username, domain = uri.user.username, uri.user.domain
        self._normalize_document_path(uri)
        doc_type = self.app_mapping[uri.application_id]
        document_path = uri.doc_selector.document_path
        query = """SELECT etag FROM %(table)s
                   WHERE username = %%(username)s AND domain = %%(domain)s
                   AND doc_type= %%(doc_type)s AND doc_uri=%%(document_path)s""" % {
            "table": Config.xcap_table
        }
        params = {
            "username": username,
            "domain": domain,
            "doc_type": doc_type,
            "document_path": document_path
        }
        trans.execute(query, params)
        result = trans.fetchall()
        if len(result) > 1:
            raise MultipleResultsError(params)
        elif not result:
            check_etag(None, False)
            ## the document doesn't exist, create it
            etag = make_random_etag(uri)
            query = """INSERT INTO %(table)s (username, domain, doc_type, etag, doc, doc_uri)
 VALUES (%%(username)s, %%(domain)s, %%(doc_type)s, %%(etag)s, %%(document)s, %%(document_path)s)""" % {
                "table": Config.xcap_table
            }
            params = {
                "username": username,
                "domain": domain,
                "doc_type": doc_type,
                "etag": etag,
                "document": document,
                "document_path": document_path
            }
            # may raise IntegrityError here, if the document was created in another connection
            # will be catched by repeat_on_error
            trans.execute(query, params)
            return StatusResponse(201, etag)
        else:
            old_etag = result[0][0]
            ## first check the etag of the existing resource
            check_etag(old_etag)
            ## the document exists, replace it
            etag = make_random_etag(uri)
            query = """UPDATE %(table)s
                       SET doc = %%(document)s, etag = %%(etag)s
                       WHERE username = %%(username)s AND domain = %%(domain)s
                       AND doc_type = %%(doc_type)s AND etag = %%(old_etag)s
                       AND doc_uri = %%(document_path)s""" % {
                "table": Config.xcap_table
            }
            params = {
                "document": document,
                "etag": etag,
                "username": username,
                "domain": domain,
                "doc_type": doc_type,
                "old_etag": old_etag,
                "document_path": document_path
            }
            trans.execute(query, params)
            # the request may not update anything (e.g. if etag was changed by another connection
            # after we did SELECT); if so, we should retry
            updated = getattr(trans._connection, 'affected_rows', lambda: 1)()
            if not updated:
                raise UpdateFailed
            assert updated == 1, updated
            return StatusResponse(200, etag, old_etag=old_etag)
示例#4
0
    def _put_document(self, trans, uri, document, check_etag):
        username, domain = uri.user.username, uri.user.domain
        self._normalize_document_path(uri)
        doc_type = self.app_mapping[uri.application_id]
        document_path = uri.doc_selector.document_path
        query = """SELECT etag FROM %(table)s
                   WHERE username = %%(username)s AND domain = %%(domain)s
                   AND doc_type= %%(doc_type)s AND doc_uri=%%(document_path)s""" % {
            "table": Config.xcap_table
        }
        params = {"username": username, "domain": domain, "doc_type": doc_type, "document_path": document_path}
        trans.execute(query, params)
        result = trans.fetchall()
        if len(result) > 1:
            raise MultipleResultsError(params)
        elif not result:
            check_etag(None, False)
            ## the document doesn't exist, create it
            etag = make_random_etag(uri)
            query = """INSERT INTO %(table)s (username, domain, doc_type, etag, doc, doc_uri)
 VALUES (%%(username)s, %%(domain)s, %%(doc_type)s, %%(etag)s, %%(document)s, %%(document_path)s)""" % {
                "table": Config.xcap_table
            }
            params = {
                "username": username,
                "domain": domain,
                "doc_type": doc_type,
                "etag": etag,
                "document": document,
                "document_path": document_path,
            }
            # may raise IntegrityError here, if the document was created in another connection
            # will be catched by repeat_on_error
            trans.execute(query, params)
            return StatusResponse(201, etag)
        else:
            old_etag = result[0][0]
            ## first check the etag of the existing resource
            check_etag(old_etag)
            ## the document exists, replace it
            etag = make_random_etag(uri)
            query = """UPDATE %(table)s
                       SET doc = %%(document)s, etag = %%(etag)s
                       WHERE username = %%(username)s AND domain = %%(domain)s
                       AND doc_type = %%(doc_type)s AND etag = %%(old_etag)s
                       AND doc_uri = %%(document_path)s""" % {
                "table": Config.xcap_table
            }
            params = {
                "document": document,
                "etag": etag,
                "username": username,
                "domain": domain,
                "doc_type": doc_type,
                "old_etag": old_etag,
                "document_path": document_path,
            }
            trans.execute(query, params)
            # the request may not update anything (e.g. if etag was changed by another connection
            # after we did SELECT); if so, we should retry
            updated = getattr(trans._connection, "affected_rows", lambda: 1)()
            if not updated:
                raise UpdateFailed
            assert updated == 1, updated
            return StatusResponse(200, etag, old_etag=old_etag)