Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def perform_get_or_create(self, request, *args, **kwargs):
"""Perform "get_or_create" - return existing object if found."""
self.define_contributor(request)
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
process = serializer.validated_data.get("process")
process_input = request.data.get("input", {})
fill_with_defaults(process_input, process.input_schema)
checksum = get_data_checksum(process_input, process.slug, process.version)
data_qs = Data.objects.filter(
checksum=checksum,
process__persistence__in=[
Process.PERSISTENCE_CACHED,
Process.PERSISTENCE_TEMP,
],
)
data_qs = get_objects_for_user(request.user, "view_data", data_qs)
if data_qs.exists():
data = data_qs.order_by("created").last()
serializer = self.get_serializer(data)
return Response(serializer.data)
def _get_data(self, user, ids):
"""Return data objects queryset based on provided ids."""
queryset = get_objects_for_user(
user, "view_data", Data.objects.filter(id__in=ids)
)
actual_ids = queryset.values_list("id", flat=True)
missing_ids = list(set(ids) - set(actual_ids))
if missing_ids:
raise exceptions.ParseError(
"Data objects with the following ids not found: {}".format(
", ".join(map(str, missing_ids))
)
)
for data in queryset:
collection = data.collection
if collection and not user.has_perm("edit_collection", obj=collection):
if user.is_authenticated:
raise exceptions.PermissionDenied()
else:
def duplicate(self, request, *args, **kwargs):
"""Duplicate (make copy of) ``Data`` objects."""
if not request.user.is_authenticated:
raise exceptions.NotFound
inherit_collection = request.data.get("inherit_collection", False)
ids = self.get_ids(request.data)
queryset = get_objects_for_user(
request.user, "view_data", Data.objects.filter(id__in=ids)
)
actual_ids = queryset.values_list("id", flat=True)
missing_ids = list(set(ids) - set(actual_ids))
if missing_ids:
raise exceptions.ParseError(
"Data objects with the following ids not found: {}".format(
", ".join(map(str, missing_ids))
)
)
duplicated = queryset.duplicate(
contributor=request.user, inherit_collection=inherit_collection,
)
serializer = self.get_serializer(duplicated, many=True)
return Response(serializer.data)
__("Handling get missing data location"),
extra={"data_id": data_id, "packet": obj},
)
try:
data = Data.objects.get(pk=data_id)
except Data.DoesNotExist:
logger.error(
"Data object does not exist (handle_missing_data_locations).",
extra={"data_id": data_id,},
)
self._abort_processing(obj)
return
missing_data = []
dependencies = (
Data.objects.filter(
children_dependency__child=data,
children_dependency__kind=DataDependency.KIND_IO,
)
.exclude(location__isnull=True)
.distinct()
)
for parent in dependencies:
file_storage = parent.location
if not file_storage.has_storage_location(STORAGE_LOCAL_CONNECTOR):
from_location = file_storage.default_storage_location
if from_location is None:
logger.error(
"No storage location exists (handle_get_missing_data_locations).",
extra={"data_id": data_id, "file_storage_id": file_storage.id},
)
def validate_data(data_pk, type_):
""""Check that `Data` objects exist and is of right type."""
data_qs = Data.objects.filter(pk=data_pk).values('process__type')
if not data_qs.exists():
raise ValidationError(
"Referenced `Data` object does not exist (id:{})".format(data_pk))
data = data_qs.first()
if not data['process__type'].startswith(type_):
raise ValidationError(
"Data object of type `{}` is required, but type `{}` is given. "
"(id:{})".format(type_, data['process__type'], data_pk))
return Response({'process': ['Invalid process slug "{}" - object does not exist.'.format(process_slug)]},
status=status.HTTP_400_BAD_REQUEST)
request.data['process'] = process.pk
# perform "get_or_create" if requested - return existing object
# if found
if kwargs.pop('get_or_create', False):
process_input = request.data.get('input', {})
# use default values if they are not given
for field_schema, fields, path in iterate_schema(process_input, process.input_schema):
if 'default' in field_schema and field_schema['name'] not in fields:
dict_dot(process_input, path, field_schema['default'])
checksum = get_data_checksum(process_input, process.slug, process.version)
data_qs = Data.objects.filter(
checksum=checksum,
process__persistence__in=[Process.PERSISTENCE_CACHED, Process.PERSISTENCE_TEMP],
)
data_qs = get_objects_for_user(request.user, 'view_data', data_qs)
if data_qs.exists():
data = data_qs.order_by('created').last()
serializer = self.get_serializer(data)
return Response(serializer.data)
# create the objects
resp = super(ResolweCreateDataModelMixin, self).create(request, *args, **kwargs)
# run manager
manager.communicate()
return resp