def pt_artifact_meta_id_json(request, api_ver, project_id, uuid): project_id = int(project_id) uuid = uuid.lower() if not pt_is_valid_uuid(uuid): return pt_rest_bad_req("Invalid artifact UUID %s" % uuid) if not ArtifactMetaModel.pt_artifacts_enabled(): return pt_rest_err( http.client.NOT_IMPLEMENTED, "Artifacts management is not configured, set the ARTIFACTS_STORAGE_DIR setting" ) try: artifact = ArtifactMetaModel.objects.get(uuid=uuid) except ArtifactMetaModel.DoesNotExist: artifact = None if request.method == "POST": if artifact is None: artifact = ArtifactMetaModel(uuid=uuid) return artifact.pt_update(request.POST, request.FILES) if artifact is None: return pt_rest_not_found("Artifact with UUID %s is not found" % uuid) if request.method == "PATCH": data = json.loads(request.body.decode("utf-8")) return artifact.pt_update(data) elif request.method == "DELETE": return artifact.pt_delete() elif request.method == "GET": return pt_rest_ok(ArtifactMetaSerializer(artifact).data) return pt_rest_method_not_allowed(request)
def pt_artifact_content_id(request, project_id, uuid): if not ArtifactMetaModel.pt_artifacts_enabled(): return pt_rest_err( http.client.NOT_IMPLEMENTED, "Artifacts management is not configured, set the ARTIFACTS_STORAGE_DIR setting" ) uuid = str(uuid).lower() if not pt_is_valid_uuid(uuid): return pt_rest_bad_req("Invalid artifact content UUID %s" % uuid) try: artifact = ArtifactMetaModel.objects.get(uuid=uuid) except ArtifactMetaModel.DoesNotExist: return Http404 return artifact.pt_get_http_content()
def pt_update(self, json_data, file_data=None): j = PTJson(json_data, obj_name="artifact json", exception_type=SuspiciousOperation) try: self.description = j.get_str('description', defval=self.description) self.ttl_days = j.get_int('ttl_days', defval=self.ttl_days) self.uploaded_dt = timezone.now() self.filename = j.get_str('filename', defval=self.filename, require=False if self.filename else True) self.mime = j.get_str('mime', defval=self.mime) self.expires_dt = timezone.now() + timedelta(days=self.ttl_days) self.inline = j.get_bool('inline', defval=self.inline) self.compression = j.get_bool('compression', defval=self.compression) linked_uuids = j.get_list('linked_uuids', []) unlinked_uuids = j.get_list('unlinked_uuids', []) except SuspiciousOperation as e: return pt_rest_bad_req(str(e)) if not self.mime: self.mime = mimetypes.guess_type(self.filename)[0] if not self.mime: self.mime = 'application/octet-stream' if file_data: data = file_data.get('file', None) if data is None: return pt_rest_bad_req("can't get file data") ret = self._pt_save_file(data.read()) if ret is not None: return ret for uuid in linked_uuids: if not pt_is_valid_uuid(uuid): return pt_rest_bad_req( "trying to link resource with invalid UUID format: %s" % uuid) for uuid in unlinked_uuids: if not pt_is_valid_uuid(uuid): return pt_rest_bad_req( "trying to unlink resource with invalid UUID format: %s" % uuid) exists = self.id self.save() for uuid in linked_uuids: try: al = ArtifactLinkModel.objects.get(artifact=self, linked_uuid=uuid) if al.deleted: al.deleted = False al.save() continue except ArtifactLinkModel.DoesNotExist as e: al = ArtifactLinkModel(artifact=self, linked_uuid=uuid) al.save() for uuid in unlinked_uuids: try: al = ArtifactLinkModel.objects.get(artifact=self, linked_uuid=uuid) except ArtifactLinkModel.DoesNotExist as e: continue al.deleted = True al.save() return pt_rest_ok(message="Artifact has been %s, uuid: %s" % ("updated" if exists else "created", self.uuid), uuid=self.uuid)