def post(self, request, task_id=None, case_id=None, model_id=None, content_type=None, *args, **kwargs): """ create / update a Task """ task_id = task_id or request.data.get("id") case_id = case_id or request.data.get("case_id") if task_id: try: task = Task.objects.get(id=task_id) except Task.DoesNotExist: raise NotFoundApiExceptions("Invalid task id") else: # we must be creating a task, so get the key params case = get_case(case_id) model_id = model_id or request.data.get("model_id") content_type = content_type or request.data.get("content_type") _content_type = content_type and get_content_type(content_type) task = Task( case=case, created_by=request.user, model_id=model_id, content_type=_content_type, user_context=[request.user], )
def delete( self, request, case_id=None, note_id=None, model_id=None, document_id=None, content_type=None, *args, **kwargs, ): response = None if note_id and document_id: try: note = Note.objects.get(id=note_id, case__id=case_id) document = Document.objects.get(id=document_id) case = get_case(case_id) # Only delete if the doc is not used in any submissions if len(document.submissions(case)) == 0: note.documents.remove(document) response = document.delete() else: result = { "deleted": False, "result": "Document used in submission" }
def get(request, case_id=None): """Return all unacknowledged notify failures for a case. Generates report document with additional detail (if specified in `detail` query param). :param (HTTPRequest) request: request object. :param (str) case_id: case to report on. :returns (HTTPResponse): Document containing failed notifications logged in the audit trail. """ detail = request.query_params.get("detail") case = get_case(case_id) report = get_notify_fail_report(case=case, detail=bool(detail)) return ResponseSuccess({"result": report})
def post( self, request, organisation_id, user_id, case_id, representing_id=None, submission_id=None, invite_id=None, ): from cases.models import get_case primary = request.data.get("primary") remove = request.data.get("remove") try: user_organisation = Organisation.objects.get(id=organisation_id) if representing_id: representing = Organisation.objects.get(id=representing_id) else: representing = user_organisation except Organisation.DoesNotExist: raise NotFoundApiExceptions("Invalid parameters or access denied") if not request.user.is_tra() and user_id and (user_id != request.user.id): if not request.user.groups.filter( name=SECURITY_GROUP_ORGANISATION_OWNER).exists(): raise InvalidAccess( "Only organisation owners can update other members") case = get_case(case_id) user = User.objects.get( id=user_id, organisationuser__organisation=user_organisation) if not remove: user.assign_to_case(case=case, organisation=representing, created_by=request.user) user.contact.add_to_case( case=case, organisation=representing, primary=bool(primary), ) context = { "case_name": case.name, "case_number": case.reference, "company_name": user_organisation.name, "representing_clause": f" representing {representing.name}", "login_url": public_login_url(), } context[
def post( # noqa: C901 self, request, contact_id=None, organisation_id=None, case_id=None, *args, **kwargs ): self.required_keys = ["contact_name"] if not self.feature_flags("contact_email_read_only"): self.required_keys.append("contact_email") case_id = case_id or request.data.get("case_id") organisation_id = organisation_id or request.data.get("organisation_id") missing_required_fields = self.validate_required_fields(request) errors = {fld: "Required" for fld in missing_required_fields} if not self.feature_flags("contact_email_read_only"): if not is_valid_email(request.data.get("contact_email", "")): errors["email_not_valid"] = "Invalid email format" if errors: raise RequestValidationError(detail=errors) case = None if case_id: case = get_case(case_id)
def post(self, request, case_id, submission_id, contact_id, *args, **kwargs): notify_data = request.data.dict() case = get_case(case_id) submission = Submission.objects.get(id=submission_id, case=case) contact = Contact.objects.select_related("userprofile", "organisation").get(id=contact_id) try: invite = Invitation.objects.get(submission=submission, contact=contact) except Invitation.DoesNotExist: raise NotFoundApiExceptions("Invite not found") send_report = invite.send( sent_by=request.user, context=notify_data, direct=True, template_key="NOTIFY_THIRD_PARTY_INVITE", ) invite.email_sent = True invite.sent_at = timezone.now() invite.approved_by = request.user invite.save() return ResponseSuccess({"result": invite.to_dict()}, http_status=status.HTTP_201_CREATED)
def post( self, request, case_type_id=None, submission_type_id=None, status=None, bundle_id=None, *args, **kwargs, ): case_id = None if bundle_id: bundle = DocumentBundle.objects.get(id=bundle_id) else: if case_type_id: bundle = (DocumentBundle.objects.filter( case_type_id=case_type_id).order_by("-version").first()) elif submission_type_id: bundle = (DocumentBundle.objects.filter( submission_type_id=submission_type_id, case__isnull=True).order_by("-version").first()) else: case_id = request.data.get("case_id") submission_type_id = request.data.get("submission_type_id") bundle = (DocumentBundle.objects.filter( case_id=case_id, submission_type_id=submission_type_id).order_by( "-version").first()) if bundle: bundle = bundle.new_version() else: bundle = DocumentBundle.objects.create( case_type_id=case_type_id, submission_type_id=submission_type_id, case_id=case_id, status="DRAFT", user_context=self.user, ) bundle.created_by = self.user bundle.set_user_context(self.user) if case_id: bundle.set_case_context(get_case(case_id)) bundle.description = request.data.get("description", bundle.description) status = status or request.data.get("status") if status in ("LIVE", "DRAFT"): if status == "LIVE": bundle.make_live(request.user) else: bundle.status = status bundle.save() return ResponseSuccess({"result": bundle.to_dict()})
def post(self, request, case_id, contact_id, organisation_id, *args, **kwargs): try: contact = Contact.objects.select_related("userprofile", "organisation").get( id=contact_id ) except Contact.DoesNotExist: raise NotFoundApiExceptions("Contact does not exist or access is denied") try: organisation = Organisation.objects.get(id=organisation_id) except Organisation.DoesNotExist: raise NotFoundApiExceptions("Organisation does not exist or access is denied") contact.set_primary(case=get_case(case_id), organisation=organisation) return ResponseSuccess({"result": contact.to_dict()}, http_status=status.HTTP_201_CREATED)
def post( self, request, document_id=None, case_id=None, organisation_id=None, submission_id=None, bundle_id=None, *args, **kwargs, ): if document_id is None: document_id = request.data.get("document_id", None) _files = request.FILES.getlist("file", None) _issued = request.data.get("issued") or False _case_document = request.data.get("case_document") _parent_id = request.data.get("parent_id") or False _replace_id = request.data.get("replace_id") or False _bundle_id = bundle_id or request.data.get("bundle_id") _system = request.data.get("system") or bool(_bundle_id) _confidential = request.data.get("confidential") _submission_document_type = request.data.get( "submission_document_type") if _submission_document_type: submission_document_type = SubmissionDocumentType.objects.get( key=_submission_document_type) else: submission_document_type = SubmissionDocumentType.type_by_user( request.user) if _confidential is None: _confidential = True submission_type_id = request.data.get("submission_type_id") if (not _case_document and not _bundle_id and not submission_id and not submission_type_id and not _system and not document_id): raise InvalidRequestParams("Submission id or type id are required") if not _files and not document_id and not request.data.get( "file_name"): raise InvalidRequestParams("No file or documents provided") case = get_case(case_id) submission = None if submission_id: submission = Submission.objects.get_submission(id=submission_id, case=case) elif submission_type_id: submission_type = SubmissionType.objects.get(id=submission_type_id) submission = Submission.objects.create( name=submission_type.name, case=case, organisation=self.organisation, type=submission_type, status=submission_type.default_status, created_by=request.user, user_context=[request.user], ) if not _files and request.data.get("file_name"): _files = [{ "name": request.data.get("file_name"), "size": request.data.get("file_size"), "document_name": request.data.get("document_name"), }] if _files: result = [] for _file in _files: _parent = None if _parent_id: try: _parent = Document.objects.get(id=_parent_id) except Document.DoesNotExist: raise NotFoundApiExceptions( "Parent document is not found") try: document = Document.objects.create_document( file=_file, user=request.user, confidential=_confidential, system=bool(_system), parent=_parent, document=Document.objects.get( id=document_id) if document_id else None, case=case, ) except InvalidFile as e: raise InvalidFileUpload(str(e)) if _replace_id and submission: # We are being asked to replace the given doc in this submission try: # Find the document to replace and get its child _replace_doc = Document.objects.get(id=_replace_id) _child_doc = Document.objects.filter( parent_id=_replace_id).first() replace_submission_document = SubmissionDocument.objects.get( submission=submission, document=_replace_doc) replace_submission_document.set_user_context( request.user) if _child_doc: child_submission_document = SubmissionDocument.objects.get( submission=submission, document=_child_doc) child_submission_document.set_user_context( request.user) # Clone the child doc and add link to parent _child_doc.id = None _child_doc.parent = document _child_doc.save() # Update submission_doc to point to new child doc. child_submission_document.document = _child_doc child_submission_document.save() replace_submission_document.delete() except (Document.DoesNotExist, SubmissionDocument.DoesNotExist) as e: logger.warning( f"Document to replace with id '{_replace_id}' was not found: {e}" ) if submission: if _submission_document_type: submission_document_type = SubmissionDocumentType.objects.get( key=_submission_document_type) else: submission_document_type = SubmissionDocumentType.type_by_user( request.user) submission_document = submission.add_document( document=document, document_type=submission_document_type, issued=_issued, issued_by=request.user, ) result_item = submission_document.to_dict() elif _bundle_id: bundle = DocumentBundle.objects.get(id=_bundle_id) bundle.documents.add(document) document.generic_audit( message=f"Attached to bundle {bundle.name}", audit_type=AUDIT_TYPE_ATTACH, id=str(document.id), ) result_item = document.to_dict() else: result_item = document.to_dict() result.append(result_item) return ResponseSuccess({"result": result}, http_status=status.HTTP_201_CREATED) elif document_id: # We just want to attach a document document = Document.objects.get(id=document_id) if submission_id: submission_document = submission.add_document( document=document, document_type=submission_document_type, issued=_issued, issued_by=request.user, ) if not request.user.is_tra(): submission.received_at = timezone.now() submission.status = submission.type.received_status submission.save() return ResponseSuccess( {"result": submission_document.to_dict()}, http_status=status.HTTP_201_CREATED) document.confidential = _confidential document.save() return ResponseSuccess( {"result": { "document": document.to_dict() }})
def post( # noqa: C901 self, request, case_id=None, note_id=None, model_id=None, document_id=None, content_type=None, *args, **kwargs, ): """ create / update the Notes :param request: :param note_id: :param model_id: :param args: :param kwargs: :return: """ case = get_case(case_id) _file = request.data.get("document", None) _file = json.loads(_file) if isinstance(_file, str) else None document = None if _file: document = Document.objects.create_document( file=_file, user=request.user, case=case, confidential=request.data.get("confidentiality") == "confidential", ) document_id = document.id if document_id: document = Document.objects.get(id=document_id) confidentiality = request.data.get("confidentiality") if confidentiality in ["confidential", "non-confidential"]: document.confidential = confidentiality == "confidential" document.save() if note_id: try: note = Note.objects.get(id=note_id, case__id=case_id) except Note.DoesNotExist: raise NotFoundApiExceptions("Invalid note id") else: model_id = model_id or request.data.get("model_id") content_type = content_type or request.data.get("content_type") if not model_id and not content_type: model_id = case_id _content_type = get_content_type("cases.case") else: _content_type = get_content_type(content_type) note = Note( case=case, created_by=request.user, model_id=model_id, content_type=_content_type, user_context=[request.user], ) note.load_attributes(request.data, ["note", "model_key"]) if document: note.documents.add(document) note.set_user_context([request.user]) note.generic_audit( message=f"Document attached: {document.name}", audit_type=AUDIT_TYPE_ATTACH, id=str(document.id), ) if note.is_dirty(): note.save() return ResponseSuccess({"result": note.to_dict()}, http_status=status.HTTP_201_CREATED)
raise NotFoundApiExceptions("Case not found or access denied") if gov_body: organisations = Organisation.objects.filter( gov_body=True).order_by("name") return ResponseSuccess({ "results": [ organisation.to_embedded_dict() for organisation in organisations ] }) elif organisation_id: try: organisation = Organisation.objects.get(id=organisation_id) org_data = organisation.to_dict(case=case) if case_id: case_role = organisation.get_case_role(get_case(case_id)) org_data["case_role"] = case_role.to_dict( ) if case_role else None return ResponseSuccess({"result": org_data}) except Organisation.DoesNotExist: raise NotFoundApiExceptions( "Organisation does not exist or access is denied") elif is_tra: organisations = Organisation.objects.filter( deleted_at__isnull=True) return ResponseSuccess({ "results": [org.to_dict(fields=fields) for org in organisations] }) else: