Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
class GetFacetsOperation(Operation):
def __init__(self, query):
"""
@param FacetQuery query: The query we wish to get
"""
if query is None:
raise ValueError("Invalid query")
super(GetFacetsOperation, self).__init__()
self._query = query
def get_command(self, store, conventions, cache=None):
return GetFacetsCommand(self._query)
class GetMultiFacetsOperation(Operation):
def __init__(self, queries):
if queries is None or len(queries) == 0:
raise ValueError("Invalid queries")
super(GetMultiFacetsOperation, self).__init__()
self._queries = queries
def get_command(self, store, conventions, cache=None):
return self._GetMultiFacetsCommand(self._queries)
class _GetMultiFacetsCommand(RavenCommand):
def __init__(self, queries):
super(GetMultiFacetsOperation._GetMultiFacetsCommand, self).__init__(is_read_request=True)
requests = {}
for q in queries:
if not q:
self._document_id = document_id
self.name = name
self._change_vector = change_vector
def create_request(self, server_node):
self.url = "{0}/databases/{1}/attachments?id={2}&name={3}".format(server_node.url, server_node.database,
Utils.quote_key(self._document_id),
Utils.quote_key(self.name))
if self._change_vector is not None:
self.headers = {"If-Match": "\"{0}\"".format(self._change_vector)}
def set_response(self, response):
pass
class PatchByQueryOperation(Operation):
def __init__(self, query_to_update, options=None):
"""
@param query_to_update: query that will be performed
:type IndexQuery or str
@param options: various Operation options e.g. AllowStale or MaxOpsPerSec
:type QueryOperationOptions
@return: json
:rtype: dict of operation_id
"""
if query_to_update is None:
raise ValueError("Invalid query")
super(PatchByQueryOperation, self).__init__()
if isinstance(query_to_update, str):
query_to_update = IndexQuery(query=query_to_update)
self._query_to_update = query_to_update
if options is None:
class Operation(object):
__slots__ = ['__operation']
def __init__(self):
self.__operation = "Operation"
@property
def operation(self):
return self.__operation
@abstractmethod
def get_command(self, store, conventions, cache=None):
raise NotImplementedError
class DeleteAttachmentOperation(Operation):
def __init__(self, document_id, name, change_vector=None):
super(DeleteAttachmentOperation, self).__init__()
self._document_id = document_id
self._name = name
self._change_vector = change_vector
def get_command(self, store, conventions, cache=None):
return self._DeleteAttachmentCommand(self._document_id, self._name, self._change_vector)
class _DeleteAttachmentCommand(RavenCommand):
def __init__(self, document_id, name, change_vector=None):
super(DeleteAttachmentOperation._DeleteAttachmentCommand, self).__init__(method="DELETE")
if not document_id:
raise ValueError("Invalid document_id")
if not name:
self.data = {"Type": str(self._attachment_type), "ChangeVector": self._change_vector}
def set_response(self, response):
if response is None:
return None
if response.status_code == 200:
attachment_details = {"content_type": response.headers.get("Content-Type", None),
"change_vector": Utils.get_change_vector_from_header(response),
"hash": response.headers.get("Attachment-Hash", None),
"size": response.headers.get("Attachment-Size", 0)}
return {"response": response, "details": attachment_details}
class PatchOperation(Operation):
def __init__(self, document_id, change_vector, patch, patch_if_missing=None,
skip_patch_if_change_vector_mismatch=False):
"""
@param str document_id: The id of the document
@param str change_vector: The change_vector
@param PatchRequest patch: The patch that going to be applied on the document
@param PatchRequest patch_if_missing: The default patch to applied
@param bool skip_patch_if_change_vector_mismatch: If True will skip documents that mismatch the change_vector
"""
super(PatchOperation, self).__init__()
self._document_id = document_id
self._change_vector = change_vector
self._patch = patch
self._patch_if_missing = patch_if_missing
self._skip_patch_if_change_vector_mismatch = skip_patch_if_change_vector_mismatch
@param PatchRequest patch_if_missing: The default patch to applied
@param bool skip_patch_if_change_vector_mismatch: If True will skip documents that mismatch the change_vector
"""
super(PatchOperation, self).__init__()
self._document_id = document_id
self._change_vector = change_vector
self._patch = patch
self._patch_if_missing = patch_if_missing
self._skip_patch_if_change_vector_mismatch = skip_patch_if_change_vector_mismatch
def get_command(self, store, conventions, cache=None):
return PatchCommand(self._document_id, self._change_vector, self._patch, self._patch_if_missing,
self._skip_patch_if_change_vector_mismatch)
class PutAttachmentOperation(Operation):
def __init__(self, document_id, name, stream, content_type=None, change_vector=None):
"""
@param document_id: The id of the document
@param name: Name of the attachment
@param stream: The attachment as bytes (ex.open("file_path", "rb"))
@param content_type: The type of the attachment (ex.image/png)
@param change_vector: The change vector of the document
"""
super(PutAttachmentOperation, self).__init__()
self._document_id = document_id
self._name = name
self._stream = stream
self._content_type = content_type
self._change_vector = change_vector
from typing import Optional, Iterable, List
from pyravendb.data.timeseries import TimeSeriesRange
from pyravendb.commands.raven_commands import *
from pyravendb.tools.utils import Utils
from .operations import Operation
from datetime import datetime
import sys
class GetTimeSeriesOperation(Operation):
def __init__(self, document_id: str, ranges: Iterable[TimeSeriesRange] or TimeSeriesRange,
start: int = 0, page_size: int = sys.maxsize):
"""
Build the time series get operations.
"""
super().__init__()
if not document_id:
raise ValueError("Invalid document Id, please provide a valid value and try again")
if not ranges:
raise ValueError("Ranges property Cannot be empty or None, please put a valid value and try again")
if isinstance(ranges, TimeSeriesRange):
ranges = [ranges]
self._ranges = ranges
self._document_id = document_id
self.url += path
self.data = self._query_to_delete.to_json()
def set_response(self, response):
if response is None:
raise exceptions.ErrorResponseException("Could not find index {0}".format(self.index_name))
if response.status_code != 200 and response.status_code != 202:
try:
raise exceptions.ErrorResponseException(response.json()["Error"])
except ValueError:
raise response.raise_for_status()
return {"operation_id": response.json()["OperationId"]}
class GetAttachmentOperation(Operation):
def __init__(self, document_id, name, attachment_type, change_vector):
if document_id is None:
raise ValueError("Invalid document_id")
if name is None:
raise ValueError("Invalid name")
if attachment_type != AttachmentType.document and change_vector is None:
raise ValueError("change_vector",
"Change Vector cannot be null for attachment type {0}".format(attachment_type))
super(GetAttachmentOperation, self).__init__()
self._document_id = document_id
self._name = name
self._attachment_type = attachment_type
self._change_vector = change_vector
def to_json(self):
_dict = {"Timestamp": Utils.datetime_to_string(self.timestamp), "Values": self.values}
if self.tag:
_dict["Tag"] = self.tag
return _dict
class RemoveOperation:
def __init__(self, from_date: datetime = None, to_date: datetime = None):
self.from_date = from_date if from_date else datetime.min
self.to_date = to_date if to_date else datetime.max
def to_json(self):
return {"From": Utils.datetime_to_string(self.from_date), "To": Utils.datetime_to_string(self.to_date)}
class TimeSeriesBatchOperation(Operation):
def __init__(self, document_id: str, operation: TimeSeriesOperation):
super().__init__()
self._document_id = document_id
self._operation = operation
def get_command(self, store, conventions, cache=None):
return self._TimeSeriesBatchCommand(self._document_id, self._operation)
class _TimeSeriesBatchCommand(RavenCommand):
def __init__(self, document_id: str, operation: TimeSeriesOperation):
super().__init__(method="POST")
if not document_id:
raise ValueError("Invalid document_id")
@param change_vector: The change vector of the document
"""
super(PutAttachmentOperation, self).__init__()
self._document_id = document_id
self._name = name
self._stream = stream
self._content_type = content_type
self._change_vector = change_vector
def get_command(self, store, conventions, cache=None):
return PutAttachmentCommand(self._document_id, self._name, self._stream, self._content_type,
self._change_vector)
class GetFacetsOperation(Operation):
def __init__(self, query):
"""
@param FacetQuery query: The query we wish to get
"""
if query is None:
raise ValueError("Invalid query")
super(GetFacetsOperation, self).__init__()
self._query = query
def get_command(self, store, conventions, cache=None):
return GetFacetsCommand(self._query)
class GetMultiFacetsOperation(Operation):
def __init__(self, queries):
self.url += path
self.data = {"Query": self._query_to_update.to_json()}
def set_response(self, response):
if response is None:
raise exceptions.ErrorResponseException("Invalid Response")
try:
response = response.json()
if "Error" in response:
raise exceptions.ErrorResponseException(response["Error"])
return {"operation_id": response["OperationId"]}
except ValueError:
raise response.raise_for_status()
class DeleteByQueryOperation(Operation):
def __init__(self, query_to_delete, options=None):
"""
@param query_to_delete: query that will be performed
:type IndexQuery or str
@param options: various Operation options e.g. AllowStale or MaxOpsPerSec
:type QueryOperationOptions
:rtype: dict of operation_id
"""
if not query_to_delete:
raise ValueError("Invalid query")
super(DeleteByQueryOperation, self).__init__()
if isinstance(query_to_delete, str):
query_to_delete = IndexQuery(query=query_to_delete)
self._query_to_delete = query_to_delete
self._options = options if options is not None else QueryOperationOptions()