def get(self, request, case_id, organisation_id, *args, **kwargs): action = request.GET.get("action") organisation = self._client.get_organisation(organisation_id, case_id) case = self._client.get_case(case_id) roles = self._client.get_case_roles(exclude=[ CASE_ROLE_APPLICANT, CASE_ROLE_AWAITING_APPROVAL, CASE_ROLE_REJECTED, CASE_ROLE_PREPARING, ]) contacts = organisation["contacts"] notify_key = ("NOTIFY_COMPANY_ROLE_CHANGED_V2" if action in ("approve", "change") else "NOTIFY_COMPANY_ROLE_DENIED_V2") notification_template = self._client.get_notification_template( notify_key) values = { "case_name": case["name"], "case_number": case["reference"], "notice": "", "company_name": organisation["name"], "login_url": public_login_url(), "previous_role": organisation.get("case_role", {}).get("name"), "reason": "", } values = self._client.create_notify_context(values) context = { "case": case, "data_substitute": "new_role", "editable_fields": { # Leaving one for future reference # 'case': {'title': 'Case Name'}, }, "values": values, "organisation": organisation, "contacts": contacts, "contact_id": contacts[0]["id"] if len(contacts) == 1 else None, "action": action, "roles": roles, "parsed_template": parse_notify_template(notification_template["body"], values), "notification_template": notification_template, "organisation_type": request.GET.get("organisation_type") or CASE_ROLE_AWAITING_APPROVAL, } return render(request, self.template_name, context)
def test_create_submission_with_notification(self, notifier_client): self.submission_type = SubmissionType.objects.get(name="General") self.submission.type = self.submission_type self.submission.save() payload = { "submission_type": "General", "submission_status_id": 7, "status_context": "received", } notifier_client().send_email_notification.return_value = {} self.client.force_authenticate(user=self.user_1, token=self.user_1.auth_token) response = self.post_form(self.url, payload) self.assertEqual(response.status_code, status.HTTP_200_OK) # Build footer base_footer = SystemParameter.get("NOTIFY_BLOCK_FOOTER") email = f"{self.case.reference}@{SystemParameter.get('TRADE_REMEDIES_EMAIL_DOMAIN')}" footer = "\n".join([base_footer, f"Contact: {email}"]) notify_data = { "company": self.organisation.name, "case_name": self.case.name, "case_title": self.case.name, "case_number": self.case.reference, "case_type": self.case.type.name, "investigation_type": self.case.type.name, "dumped_or_subsidised": self.case.dumped_or_subsidised(), "product": "", "full_name": self.user_1.contact.name.strip(), "country": "N/A", "organisation_name": titlecase(self.organisation.name), "notice_url": self.submission.url or "N/A", # TODO: Remove "notice_of_initiation_url": self.submission.url or "N/A", "login_url": public_login_url(), "submission_type": "General", "company_name": titlecase(self.submission.organisation.name) if self.submission.organisation else "", "deadline": "", "footer": footer, "email": email, "guidance_url": SystemParameter.get("LINK_HELP_BOX_GUIDANCE"), } notifier_client().send_email_notification.assert_called_once_with( email_address=self.user_1.email, personalisation=notify_data, reference=None, template_id="d6fb3018-2338-40c9-aa6d-f1195f5f65de", # /PS-IGNORE ) response_data = response.data["response"] self.assertTrue(response_data["success"]) self.assertEqual(response_data["result"]["submission"]["id"], str(self.submission.id)) self.assertEqual(response_data["result"]["submission"]["type"]["name"], "General")
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 notify_owners(self, organisation, template_id, context, notified_by): audit_kwargs = { "audit_type": AUDIT_TYPE_NOTIFY, "user": notified_by, } owners = organisation.organisationuser_set.filter( user__is_active=True, user__groups__name="Organisation Owner") for owner in owners: user = owner.user context["full_name"] = user.name context["organisation_name"] = organisation.name audit_kwargs["model"] = user.contact context["login_url"] = public_login_url() send_mail(user.contact.email, context, template_id, audit_kwargs=audit_kwargs)
def post(self, request, submission_id, *args, **kwargs): submission = Submission.objects.get(id=submission_id) case = submission.case organisation = submission.organisation caserole = OrganisationCaseRole.objects.get(case=case, organisation=organisation) json_data = submission.deficiency_notice_params or { } # this has most of what we need submission.deficiency_notice_params = json_data auth_contact_details = None values = { "case_number": case.reference, "case_name": case.name, "login_url": public_login_url(), "company_name": titlecase(organisation.name), "full_name": submission.contact.name, "role": caserole.role.name, }
def notify_deficiency(self, sent_by, contact=None, context=None, template_id=None): """ Notify the contact about a deficiency to this submission using the given template. If no template is provided, the type's default is used falling back to the default deficiency template. """ contact = contact or self.contact template_id = "NOTIFY_SUBMISSION_DEFICIENCY" if context.get("submission_type", "") == "Application": template_id = "NOTIFY_APPLICATION_INSUFFICIENT_V2" notify_template_id = SystemParameter.get(template_id) product = self.case.product_set.first() product_name = product.name if product else "" case_name = self.case.name company_name = titlecase(self.organisation.name) # set the due date on this submission self.set_due_date(force=True) values = { "company": company_name, "investigation_type": self.case.type.name, "product": product_name, "full_name": contact.name.strip() if contact else "N/A", "organisation_name": company_name, "case_number": self.case.reference, "case_name": case_name, "tra_contact_name": "us", "submission_type": self.type.name, "login_url": public_login_url(), "deadline": self.due_at.strftime(settings.FRIENDLY_DATE_FORMAT) if self.due_at else "N/A", } if context: values.update(context) audit_kwargs = { "audit_type": AUDIT_TYPE_NOTIFY, "user": sent_by, "case": self.case, "model": contact, } send_mail(contact.email, values, notify_template_id, audit_kwargs=audit_kwargs) self.deficiency_sent_at = timezone.now() self.sent_at = timezone.now() self.save()
def notify(self, sent_by, contact=None, context=None, template_id=None, new_status=None): """ Notify the contact about this submission using the given template :param core.User sent_by: The user performing an action on the submission. :param contacts.Contact contact: An optional contact to be notified. Defaults to the submission's designated contact. :param dict context: An optional dictionary of parameters to be made available to the template. :param str template_id: An optional string representing the key of a Notify template in the System Parameters. Defaults to NOTIFY_QUESTIONNAIRE :param str new_status: An optional status that the submission will be moved to after sending the notification. This value should correspond to the property of the SubmissionType excluding the `_status` suffix eg: - `sent` -> self.type.sent_status - `received` -> self.type.received_status Defaults to None ie the submission status will not change. """ contact = contact or self.contact template_id = template_id or "NOTIFY_QUESTIONNAIRE" if template_id == "NOTIFY_APPLICATION_SUCCESSFUL": template_id = "NOTIFY_APPLICATION_SUCCESSFUL_V2" notify_template_id = SystemParameter.get(template_id) export_sources = self.case.exportsource_set.filter( deleted_at__isnull=True) export_countries = [src.country.name for src in export_sources] product = self.case.product_set.first() case_name = self.case.name company_name = titlecase(self.organisation.name) values = { "company": self.organisation.name, "investigation_type": self.case.type.name, "product": product.name if product else "", "case_name": case_name, "case_number": self.case.reference, "full_name": contact.name.strip() if contact else "N/A", "country": ", ".join(export_countries) if export_countries else "N/A", "organisation_name": company_name, "company_name": company_name, "login_url": public_login_url(), "submission_type": self.type.name, "deadline": self.due_at.strftime(settings.FRIENDLY_DATE_FORMAT) if self.due_at else "", "dumped_or_subsidised": self.case.dumped_or_subsidised(), "case_title": case_name, # TODO: merge the two identicals "notice_url": self.case.latest_notice_of_initiation_url, # TODO: remove "notice_of_initiation_url": self.case.latest_notice_of_initiation_url, } if context: if template_id == "NOTIFY_QUESTIONNAIRE": context[ "footer"] = "Investigations Team\r\nTrade Remedies\r\nDepartment for International Trade" # /PS-IGNORE values.update(context) if template_id == "NOTIFY_AD_HOC_EMAIL": values[ "footer"] = "Investigations Team\r\nTrade Remedies\r\nDepartment for International Trade\r\nContact: [email protected]" # /PS-IGNORE audit_kwargs = { "audit_type": AUDIT_TYPE_NOTIFY, "user": sent_by, "case": self.case, "model": contact, } send_mail(contact.email, values, notify_template_id, audit_kwargs=audit_kwargs) if new_status: self.status = getattr(self.type, f"{new_status}_status") if new_status == "sent": self.sent_at = timezone.now() self.set_due_date() self.save()
def post(self, request, case_id, organisation_id, action, *args, **kwargs): values = {key: request.data.get(key) for key in request.data.keys()} contact_id = values.pop("contact_id") organisation = Organisation.objects.get(id=organisation_id) case = Case.objects.get(id=case_id) values["case_number"] = case.reference values["case_name"] = case.name values["login_url"] = public_login_url() values["company_name"] = titlecase(organisation.name) user_granted_access = None if action in ("approve", "change"): role_key = values.pop("organisation_type", None) role = CaseRole.objects.get(key=role_key) values["role"] = role.contributor_or_interested() if action == "change": previous_role = case.organisationcaserole_set.filter( organisation=organisation).first() values[ "previous_role"] = previous_role.role.contributor_or_interested( ) values["new_role"] = values["role"] try: contact = Contact.objects.select_related( "userprofile", "organisation").get(id=contact_id, casecontact__case=case, casecontact__organisation=organisation) except Contact.DoesNotExist: contact = Contact.objects.select_related( "userprofile", "organisation").get(id=contact_id) CaseContact.objects.get_or_create(contact=contact, case=case, organisation=organisation) caserole, created = organisation.assign_case(case, role) user_granted_access = caserole.created_by # assign the user registering access to the case case.assign_user(user_granted_access, created_by=request.user, organisation=organisation) message = f"Organisation {organisation.name} approved as {role.name}" else: role = CaseRole.objects.get(key="rejected") values["new_role"] = "rejected" caserole, created = organisation.assign_case(case, role) user_granted_access = caserole.created_by contact = Contact.objects.select_related( "userprofile", "organisation").get(id=contact_id) message = f"Organisation {organisation.name} was rejected" values["full_name"] = contact.name # Send the message if action == "approve" or values.get("previous_role") != values.get( "new_role"): organisation.notify_approval_status(action, contact, values, case, request.user) if action == "approve": audit_log( audit_type=AUDIT_TYPE_EVENT, user=request.user, model=case, case=case, milestone=True, data={ "action": "assign_user", "message": f"User {user_granted_access} " f"was granted access to the case for {organisation}", }, ) return ResponseSuccess({"result": values})