def get_workflow_url(workflow, widget_name="current"): """Get URL to workflow object.""" url = "workflows/{workflow_id}#{widget_name}".format( workflow_id=workflow.id, widget_name=widget_name, ) return urljoin(get_url_root(), url)
def handle_export_post(**kwargs): """Handle export post""" check_import_export_headers() objects = request.json.get("objects") current_time = request.json.get("current_time") user = get_current_user() if user.system_wide_role == 'No Access': raise Forbidden() if not objects or not current_time: raise BadRequest("Export failed due incorrect request data") try: filename = get_export_filename(objects, current_time) ie = import_export.create_import_export_entry( job_type="Export", status="In Progress", title=filename, start_at=datetime.utcnow(), ) deferred.defer(run_export, objects, ie.id, user.id, get_url_root(), _queue="ggrcImport") return make_import_export_response(ie.log_json()) except Exception as e: logger.exception("Export failed due incorrect request data: %s", e.message) raise BadRequest("Export failed due incorrect request data")
def _get_cycle_url(self, widget_name): return urljoin( get_url_root(), "workflows/{workflow_id}#{widget_name}/cycle/{cycle_id}".format( workflow_id=self.workflow.id, cycle_id=self.id, widget_name=widget_name))
def test_handle_one_comment(self, send_email_mock): """Test handling of mapped comment.""" with factories.single_commit(): person = factories.PersonFactory(email="*****@*****.**") obj = factories.ProductFactory(title="Product1") comment = factories.CommentFactory( description=u"One <a href=\"mailto:[email protected]\"></a>", ) comment.modified_by_id = person.id comment.created_at = datetime.datetime(2018, 1, 10, 7, 31, 42) url = urljoin(get_url_root(), utils.view_url_for(obj)) people_mentions.handle_comment_mapped(obj, [comment]) expected_title = (u"[email protected] mentioned you " u"on a comment within Product1") expected_body = ( u"[email protected] mentioned you on a comment within Product1 " u"at 01/09/2018 23:31:42 PST:\n" u"One <a href=\"mailto:[email protected]\"></a>\n" ) body = settings.EMAIL_MENTIONED_PERSON.render(person_mention={ "comments": [expected_body], "url": url, }) send_email_mock.assert_called_once_with(u"*****@*****.**", expected_title, body)
def test_comment_imported(self, send_email_mock): """Test sending mention email after import an object with comments.""" with factories.single_commit(): factories.PersonFactory(email="*****@*****.**") obj = factories.ProductFactory(title="Product4") obj_slug = obj.slug url = urljoin(get_url_root(), utils.view_url_for(obj)) first_comment = u"One <a href=\"mailto:[email protected]\"></a>" second_comment = u"Two <a href=\"mailto:[email protected]\"></a>" import_data = OrderedDict([("object_type", "Product"), ("Code*", obj_slug), ("comments", first_comment + u";;" + second_comment)]) with freeze_time("2018-01-10 07:31:42"): response = self.import_data(import_data) self._check_csv_response(response, {}) expected_title = (u"[email protected] mentioned you on " u"a comment within Product4") body = settings.EMAIL_MENTIONED_PERSON.render( person_mention={ "comments": [ (u"[email protected] mentioned you on a comment within Product4 " u"at 01/09/2018 23:31:42 PST:\n" + first_comment + u"\n"), (u"[email protected] mentioned you on a comment within Product4 " u"at 01/09/2018 23:31:42 PST:\n" + second_comment + u"\n"), ], "url": url, }) send_email_mock.assert_called_once_with(u"*****@*****.**", expected_title, body)
def test_comment_imported(self, send_email_mock): """Test sending mention email after import an object with comments.""" with factories.single_commit(): factories.PersonFactory(email="*****@*****.**") obj = factories.ProductFactory(title="Product4") obj_slug = obj.slug url = urljoin(get_url_root(), utils.view_url_for(obj)) first_comment = u"One <a href=\"mailto:[email protected]\"></a>" second_comment = u"Two <a href=\"mailto:[email protected]\"></a>" import_data = OrderedDict( [ ("object_type", "Product"), ("Code*", obj_slug), ("comments", first_comment + u";;" + second_comment) ] ) with freeze_time("2018-01-10 07:31:42"): response = self.import_data(import_data) self._check_csv_response(response, {}) expected_title = (u"[email protected] mentioned you on " u"a comment within Product4") body = settings.EMAIL_MENTIONED_PERSON.render(person_mention={ "comments": [ (u"[email protected] mentioned you on a comment within Product4 " u"at 01/09/2018 23:31:42 PST:\n" + first_comment + u"\n"), (u"[email protected] mentioned you on a comment within Product4 " u"at 01/09/2018 23:31:42 PST:\n" + second_comment + u"\n"), ], "url": url, }) send_email_mock.assert_called_once_with(u"*****@*****.**", expected_title, body)
def test_review_posted(self, model, send_email_mock): """Test mentions in request review comment {}.""" with factories.single_commit(): factories.PersonFactory(email="*****@*****.**") obj = factories.get_model_factory(model.__name__)() url = urljoin(get_url_root(), utils.view_url_for(obj)) with freeze_time("2018-01-10 07:31:42"): resp, _ = generate_review_object( obj, email_message= u"Test <a href=\"mailto:[email protected]\"></a>", ) self.assertEqual(201, resp.status_code) expected_title = (u"[email protected] mentioned you on " u"a comment within {title}").format(title=obj.title) expected_body = ( u"[email protected] mentioned you on a comment within {title} " u"at 01/09/2018 23:31:42 PST:\n" u"<p>Review requested from</p>" u"<p>[email protected]</p>" u"<p>with a comment:" u" Test <a href=\"mailto:[email protected]\"></a></p>\n" ).format(title=obj.title) body = settings.EMAIL_MENTIONED_PERSON.render( person_mention={ "comments": [expected_body], "url": url, }) send_email_mock.assert_called_once_with(u"*****@*****.**", expected_title, body)
def handle_export_post(**kwargs): """Handle export post""" check_import_export_headers() request_json = request.json objects = request_json.get("objects") exportable_objects = request_json.get("exportable_objects", []) current_time = request.json.get("current_time") user = get_current_user() if user.system_wide_role == 'No Access': raise Forbidden() if not objects or not current_time: raise BadRequest( app_errors.INCORRECT_REQUEST_DATA.format(job_type="Export")) try: filename = get_export_filename(objects, current_time, exportable_objects) ie = import_export.create_import_export_entry( job_type="Export", status="In Progress", title=filename, start_at=datetime.utcnow(), ) deferred.defer(run_export, objects, ie.id, user.id, get_url_root(), exportable_objects, _queue="ggrcImport") return make_import_export_response(ie.log_json()) except Exception as e: logger.exception(e.message) raise BadRequest( app_errors.INCORRECT_REQUEST_DATA.format(job_type="Export"))
def test_handle_one_comment(self, send_email_mock): """Test handling of mapped comment.""" with factories.single_commit(): person = factories.PersonFactory(email="*****@*****.**") obj = factories.ProductFactory(title="Product1") comment = factories.CommentFactory( description=u"One <a href=\"mailto:[email protected]\"></a>", ) comment.modified_by_id = person.id comment.created_at = datetime.datetime(2018, 1, 10, 7, 31, 42) url = urljoin(get_url_root(), utils.view_url_for(obj)) people_mentions.handle_comment_mapped(obj, [comment]) expected_title = (u"[email protected] mentioned you " u"on a comment within Product1") expected_body = ( u"[email protected] mentioned you on a comment within Product1 " u"at 01/09/2018 23:31:42 PST:\n" u"One <a href=\"mailto:[email protected]\"></a>\n") body = settings.EMAIL_MENTIONED_PERSON.render( person_mention={ "comments": [expected_body], "url": url, }) send_email_mock.assert_called_once_with(u"*****@*****.**", expected_title, body)
def test_using_request_url_when_custom_url_root_setting_undefined(self): """Url root should be read from request if not set in environment.""" with patch("ggrc.utils.CUSTOM_URL_ROOT", None): with patch("ggrc.utils.request") as fake_request: fake_request.url_root = "http://www.foo.com/" result = get_url_root() self.assertEqual(result, "http://www.foo.com/")
def test_using_custom_url_root_setting_if_defined(self): """Url root should be read from environment if defined there.""" with patch("ggrc.utils.CUSTOM_URL_ROOT", "http://www.default-root.com/"): with patch("ggrc.utils.request") as fake_request: fake_request.url_root = "http://www.foo.com/" result = get_url_root() self.assertEqual(result, "http://www.default-root.com/")
def get_active_cycle_tasks_url(due_date): """Get CycleTask notification url.""" base = urljoin(get_url_root(), u"dashboard#!task&query=") active_filter = (u'(("Task Status" IN ("Finished","Declined")' u' and "Needs Verification"="Yes") or (' u'"Task Status" IN ("Assigned","In Progress")' u')) and "Task Due Date"={due_date}').format( due_date=due_date) return base + urllib.quote(active_filter.encode('utf-8'))
def _get_cycle_url(self, widget_name): return urljoin( get_url_root(), "workflows/{workflow_id}#{widget_name}/cycle/{cycle_id}".format( workflow_id=self.workflow.id, cycle_id=self.id, widget_name=widget_name ) )
def test_using_request_url_when_custom_url_root_setting_empty_string(self): """Url root should be read from request if set to empty string in environ. """ with patch("ggrc.utils.CUSTOM_URL_ROOT", ""): with patch("ggrc.utils.request") as fake_request: fake_request.url_root = "http://www.foo.com/" result = get_url_root() self.assertEqual(result, "http://www.foo.com/")
def get_active_cycle_tasks_url(due_date): """Get CycleTask notification url.""" base = urljoin(get_url_root(), u"dashboard#!task&query=") active_filter = ( u'(("Task Status" IN ("Finished","Declined")' u' and "Needs Verification"="Yes") or (' u'"Task Status" IN ("Assigned","In Progress")' u')) and "Task Due Date"={due_date}' ).format(due_date=due_date) return base + urllib.quote(active_filter.encode('utf-8'))
def get_cycle_task_url(cycle_task): url = ("/workflows/{workflow_id}#current_widget/cycle/{cycle_id}" "/cycle_task_group/{cycle_task_group_id}" "/cycle_task_group_object_task/{cycle_task_id}").format( workflow_id=cycle_task.cycle_task_group.cycle.workflow.id, cycle_id=cycle_task.cycle_task_group.cycle.id, cycle_task_group_id=cycle_task.cycle_task_group.id, cycle_task_id=cycle_task.id, ) return urljoin(get_url_root(), url)
def _get_cycle_url(self, widget_name): """Returns url for Cycle with filtering by slug""" query = u'"cycle slug"="{slug}"'.format(slug=self.slug) query = urllib.quote(query.encode('utf-8')) return urljoin( get_url_root(), u'workflows/{workflow_id}#{widget_name}&query={query}'.format( workflow_id=self.workflow.id, widget_name=widget_name, query=query, ))
def send_email(template, send_to, filename="", ie_id=None): """ Send email """ subject = template["title"].format(filename=filename) url = urljoin(utils.get_url_root(), template["url"]) if ie_id is not None: url = "{}#!&job_id={}".format(url, str(ie_id)) data = {"body": template["body"], "url": url, "title": subject} body = settings.EMAIL_IMPORT_EXPORT.render(import_export=data) common.send_email(send_to, subject, body)
def get_object_url(obj): """Get url for the object info page. Args: obj (db.Model): Object for which we want to info page url. Returns: string: Url for the object info page. """ # pylint: disable=protected-access url = "{}/{}".format(obj._inflector.table_plural, obj.id) return urlparse.urljoin(utils.get_url_root(), url)
def test_several_mentions_imported(self, send_email_mock): """Test sending mention email after import an object with comments with mentions of different persons.""" with factories.single_commit(): factories.PersonFactory(email="*****@*****.**") factories.PersonFactory(email="*****@*****.**") obj = factories.ProductFactory(title="Product5") obj_slug = obj.slug first_comment = u"One <a href=\"mailto:[email protected]\"></a>" second_comment = u"Two <a href=\"mailto:[email protected]\"></a>" \ u"<a href=\"mailto:[email protected]\"></a>" import_data = OrderedDict([("object_type", "Product"), ("Code*", obj_slug), ("comments", first_comment + u";;" + second_comment)]) with freeze_time("2018-01-10 07:31:42"): response = self.import_data(import_data) self._check_csv_response(response, {}) obj = all_models.Product.query.filter_by(title="Product5").one() url = urljoin(get_url_root(), utils.view_url_for(obj)) expected_title = (u"[email protected] mentioned you on " u"a comment within Product5") first_body = settings.EMAIL_MENTIONED_PERSON.render( person_mention={ "comments": [ (u"[email protected] mentioned you on a comment within Product5 " u"at 01/09/2018 23:31:42 PST:\n" + first_comment + u"\n"), (u"[email protected] mentioned you on a comment within Product5 " u"at 01/09/2018 23:31:42 PST:\n" + second_comment + u"\n"), ], "url": url, }) first_call = mock.call(u"*****@*****.**", expected_title, first_body) second_body = settings.EMAIL_MENTIONED_PERSON.render( person_mention={ "comments": [(u"[email protected] mentioned you on a comment within Product5 " u"at 01/09/2018 23:31:42 PST:\n" + second_comment + u"\n")], "url": url, }) second_call = mock.call(u"*****@*****.**", expected_title, second_body) send_email_mock.assert_has_calls([second_call, first_call])
def test_several_mentions_imported(self, send_email_mock): """Test sending mention email after import an object with comments with mentions of different persons.""" with factories.single_commit(): factories.PersonFactory(email="*****@*****.**") factories.PersonFactory(email="*****@*****.**") obj = factories.ProductFactory(title="Product5") obj_slug = obj.slug first_comment = u"One <a href=\"mailto:[email protected]\"></a>" second_comment = u"Two <a href=\"mailto:[email protected]\"></a>" \ u"<a href=\"mailto:[email protected]\"></a>" import_data = OrderedDict( [ ("object_type", "Product"), ("Code*", obj_slug), ("comments", first_comment + u";;" + second_comment) ] ) with freeze_time("2018-01-10 07:31:42"): response = self.import_data(import_data) self._check_csv_response(response, {}) obj = all_models.Product.query.filter_by(title="Product5").one() url = urljoin(get_url_root(), utils.view_url_for(obj)) expected_title = (u"[email protected] mentioned you on " u"a comment within Product5") first_body = settings.EMAIL_MENTIONED_PERSON.render(person_mention={ "comments": [ (u"[email protected] mentioned you on a comment within Product5 " u"at 01/09/2018 23:31:42 PST:\n" + first_comment + u"\n"), (u"[email protected] mentioned you on a comment within Product5 " u"at 01/09/2018 23:31:42 PST:\n" + second_comment + u"\n"), ], "url": url, }) first_call = mock.call(u"*****@*****.**", expected_title, first_body) second_body = settings.EMAIL_MENTIONED_PERSON.render(person_mention={ "comments": [( u"[email protected] mentioned you on a comment within Product5 " u"at 01/09/2018 23:31:42 PST:\n" + second_comment + u"\n" )], "url": url, }) second_call = mock.call(u"*****@*****.**", expected_title, second_body) send_email_mock.assert_has_calls([second_call, first_call])
def unsubscribe_url(user_id): """Generate a user-specific URL for unsubscribing from notifications. Args: id (int): user's id Returns: url (string): unsubscribe URL """ url = urlparse.urljoin( get_url_root(), "_notifications/unsubscribe/{}".format(user_id) ) return url
def test_relation_comment_posted(self, send_email_mock): """Test sending mention email after posting a relationship to comment.""" with factories.single_commit(): author_person = factories.PersonFactory(email="*****@*****.**") factories.PersonFactory(email="*****@*****.**") obj = factories.ProductFactory(title="Product3") obj_id = obj.id comment = factories.CommentFactory( description= u"One <a href=\"mailto:[email protected]\"></a>", ) comment_id = comment.id comment.created_at = datetime.datetime(2018, 07, 10, 8, 31, 42) comment.modified_by_id = author_person.id url = urljoin(get_url_root(), utils.view_url_for(obj)) author_person = all_models.Person.query.filter_by( email="*****@*****.**").one() api = api_helper.Api() api.set_user(author_person) response = api.post( all_models.Relationship, { "relationship": { "source": { "id": obj_id, "type": obj.type, }, "destination": { "id": comment_id, "type": comment.type }, "context": None }, }) self.assertEqual(response.status_code, 201) expected_title = (u"[email protected] mentioned you on " u"a comment within Product3") expected_body = ( u"[email protected] mentioned you on a comment within Product3 " u"at 07/10/2018 01:31:42 PDT:\n" u"One <a href=\"mailto:[email protected]\"></a>\n") body = settings.EMAIL_MENTIONED_PERSON.render( person_mention={ "comments": [expected_body], "url": url, }) send_email_mock.assert_called_once_with(u"*****@*****.**", expected_title, body)
def handle_start(ie_job, user_id): """Handle import start command""" if ie_job.status == "Not Started": ie_job.status = "Analysis" elif ie_job.status == "Blocked": ie_job.status = "In Progress" else: raise BadRequest("Wrong status") try: db.session.commit() deferred.defer(run_import_phases, ie_job.id, user_id, get_url_root()) return make_import_export_response(ie_job.log_json()) except Exception as e: logger.exception("Import failed: %s", e.message) raise BadRequest("Import failed")
def send_email(template, send_to, filename="", ie_id=None): """ Send email """ subject = template["title"].format(filename=filename) url = urljoin(utils.get_url_root(), template["url"]) if ie_id is not None: url = "{}#!&job_id={}".format(url, str(ie_id)) data = { "body": template["body"], "url": url, "title": subject } body = settings.EMAIL_IMPORT_EXPORT.render(import_export=data) common.send_email(send_to, subject, body)
def get_cycle_task_url(cycle_task, filter_exp=u""): if filter_exp: filter_exp = u"?filter=" + urllib.quote(filter_exp) url = (u"/workflows/{workflow_id}" u"{filter_exp}" u"#current_widget/cycle/{cycle_id}" u"/cycle_task_group/{cycle_task_group_id}" u"/cycle_task_group_object_task/{cycle_task_id}").format( workflow_id=cycle_task.cycle_task_group.cycle.workflow.id, filter_exp=filter_exp, cycle_id=cycle_task.cycle_task_group.cycle.id, cycle_task_group_id=cycle_task.cycle_task_group.id, cycle_task_id=cycle_task.id, ) return urljoin(get_url_root(), url)
def test_relation_comment_posted(self, send_email_mock): """Test sending mention email after posting a relationship to comment.""" with factories.single_commit(): author_person = factories.PersonFactory(email="*****@*****.**") factories.PersonFactory(email="*****@*****.**") obj = factories.ProductFactory(title="Product3") obj_id = obj.id comment = factories.CommentFactory( description=u"One <a href=\"mailto:[email protected]\"></a>", ) comment_id = comment.id comment.created_at = datetime.datetime(2018, 07, 10, 8, 31, 42) comment.modified_by_id = author_person.id url = urljoin(get_url_root(), utils.view_url_for(obj)) author_person = all_models.Person.query.filter_by( email="*****@*****.**" ).one() api = api_helper.Api() api.set_user(author_person) response = api.post(all_models.Relationship, { "relationship": {"source": { "id": obj_id, "type": obj.type, }, "destination": { "id": comment_id, "type": comment.type }, "context": None}, }) self.assertEqual(response.status_code, 201) expected_title = (u"[email protected] mentioned you on " u"a comment within Product3") expected_body = ( u"[email protected] mentioned you on a comment within Product3 " u"at 07/10/2018 01:31:42 PDT:\n" u"One <a href=\"mailto:[email protected]\"></a>\n" ) body = settings.EMAIL_MENTIONED_PERSON.render(person_mention={ "comments": [expected_body], "url": url, }) send_email_mock.assert_called_once_with(u"*****@*****.**", expected_title, body)
def get_cycle_url(cycle, active=True): """Build the URL to the given workflow cycle. Args: cycle: The cycle instance to build the URL for. active: If True (default), the URL is generated for when a cycle is active, otherwise the resulting URL is generated for when a cycle is inactive. Returns: Full workflow cycle URL (as a string). """ widget_name = "current_widget" if active else "history_widget" url = "workflows/{workflow_id}#{widget_name}/cycle/{cycle_id}".format( workflow_id=cycle.workflow.id, cycle_id=cycle.id, widget_name=widget_name) return urljoin(get_url_root(), url)
def test_proposal_put(self, send_email_mock): """Test sending mention email after a change of proposal.""" with factories.single_commit(): author_person = factories.PersonFactory(email="*****@*****.**") factories.PersonFactory(email="*****@*****.**") risk = factories.RiskFactory(title="Risk2") proposal = factories.ProposalFactory( instance=risk, content={"fields": { "title": "Risk3" }}, agenda=u'some agenda', proposed_by=author_person, ) url = urljoin(get_url_root(), utils.view_url_for(risk)) proposal_id = proposal.id proposal = all_models.Proposal.query.get(proposal_id) api = api_helper.Api() with freeze_time("2018-01-10 07:31:42"): data = { "status": proposal.STATES.APPLIED, "apply_reason": u'<a href=\"mailto:[email protected]\"></a>', } response = api.put(proposal, {"proposal": data}) self.assertEqual(200, response.status_code) expected_title = (u"[email protected] mentioned you on " u"a comment within Risk3") expected_body = ( u"[email protected] mentioned you on a comment within Risk3 " u"at 01/09/2018 23:31:42 PST:\n" u"<p>Proposal created by [email protected] has been applied" u" with a comment: " u'<a href="mailto:[email protected]"></a></p>\n') body = settings.EMAIL_MENTIONED_PERSON.render( person_mention={ "comments": [expected_body], "url": url, }) send_email_mock.assert_called_once_with(u"*****@*****.**", expected_title, body)
def handle_comment_mapped(obj, comments): """Send mentions in the comments in the bg task. Args: obj: object for which comments were created, comments: A list of comment objects. """ comments_data = _fetch_comments_data(comments) models.background_task.create_task( name="send_mentions_bg", url=flask.url_for("send_mentions_bg"), parameters={ "comments_data": comments_data, "object_name": obj.title, "href": urljoin(get_url_root(), utils.view_url_for(obj)), }, queued_callback=send_mentions_bg, )
def test_proposal_posted(self, send_email_mock): """Test sending mention email after posting a proposal.""" with factories.single_commit(): factories.PersonFactory(email="*****@*****.**") obj = factories.RiskFactory(title="Risk1") obj_id = obj.id url = urljoin(get_url_root(), utils.view_url_for(obj)) obj = all_models.Risk.query.get(obj_id) obj_content = obj.log_json() obj_content["title"] = "Risk2" with freeze_time("2018-01-10 07:31:42"): api = api_helper.Api() response = api.post( all_models.Proposal, { "proposal": { "instance": { "id": obj_id, "type": obj.type, }, "full_instance_content": obj_content, "agenda": u'<a href=\"mailto:[email protected]\"></a', "context": None, } }) self.assertEqual(201, response.status_code) expected_title = (u"[email protected] mentioned you on " u"a comment within Risk1") expected_body = ( u"[email protected] mentioned you on a comment within Risk1 " u"at 01/09/2018 23:31:42 PST:\n" u"<p>Proposal has been created with comment: " u"<a href=\"mailto:[email protected]\"></a></p>\n") body = settings.EMAIL_MENTIONED_PERSON.render( person_mention={ "comments": [expected_body], "url": url, }) send_email_mock.assert_called_once_with(u"*****@*****.**", expected_title, body)
def handle_start(ie_job, user_id): """Handle import start command""" if ie_job.status == "Not Started": ie_job.status = "Analysis" elif ie_job.status == "Blocked": ie_job.status = "In Progress" else: raise BadRequest(app_errors.WRONG_STATUS) try: ie_job.start_at = datetime.utcnow() db.session.commit() deferred.defer(run_import_phases, ie_job.id, user_id, get_url_root(), _queue="ggrcImport") return make_import_export_response(ie_job.log_json()) except Exception as e: logger.exception(e.message) raise BadRequest(app_errors.JOB_FAILED.format(job_type="Import"))
def test_proposal_put(self, send_email_mock): """Test sending mention email after a change of proposal.""" with factories.single_commit(): author_person = factories.PersonFactory(email="*****@*****.**") factories.PersonFactory(email="*****@*****.**") risk = factories.RiskFactory(title="Risk2") proposal = factories.ProposalFactory( instance=risk, content={"fields": {"title": "Risk3"}}, agenda=u'some agenda', proposed_by=author_person, ) url = urljoin(get_url_root(), utils.view_url_for(risk)) proposal_id = proposal.id proposal = all_models.Proposal.query.get(proposal_id) api = api_helper.Api() with freeze_time("2018-01-10 07:31:42"): data = { "status": proposal.STATES.APPLIED, "apply_reason": u'<a href=\"mailto:[email protected]\"></a>', } response = api.put(proposal, {"proposal": data}) self.assertEqual(200, response.status_code) expected_title = (u"[email protected] mentioned you on " u"a comment within Risk3") expected_body = ( u"[email protected] mentioned you on a comment within Risk3 " u"at 01/09/2018 23:31:42 PST:\n" u"<p>Proposal created by [email protected] has been applied" u" with a comment: " u'<a href="mailto:[email protected]"></a></p>\n' ) body = settings.EMAIL_MENTIONED_PERSON.render(person_mention={ "comments": [expected_body], "url": url, }) send_email_mock.assert_called_once_with(u"*****@*****.**", expected_title, body)
def test_proposal_posted(self, send_email_mock): """Test sending mention email after posting a proposal.""" with factories.single_commit(): factories.PersonFactory(email="*****@*****.**") obj = factories.RiskFactory(title="Risk1") obj_id = obj.id url = urljoin(get_url_root(), utils.view_url_for(obj)) obj = all_models.Risk.query.get(obj_id) obj_content = obj.log_json() obj_content["title"] = "Risk2" with freeze_time("2018-01-10 07:31:42"): api = api_helper.Api() response = api.post(all_models.Proposal, { "proposal": { "instance": { "id": obj_id, "type": obj.type, }, "full_instance_content": obj_content, "agenda": u'<a href=\"mailto:[email protected]\"></a', "context": None, } }) self.assertEqual(201, response.status_code) expected_title = (u"[email protected] mentioned you on " u"a comment within Risk1") expected_body = ( u"[email protected] mentioned you on a comment within Risk1 " u"at 01/09/2018 23:31:42 PST:\n" u"<p>Proposal has been created with comment: " u"<a href=\"mailto:[email protected]\"></a></p>\n" ) body = settings.EMAIL_MENTIONED_PERSON.render(person_mention={ "comments": [expected_body], "url": url, }) send_email_mock.assert_called_once_with(u"*****@*****.**", expected_title, body)
def cycle_task_workflow_cycle_url(cycle_task, filter_exp=u""): """Build the URL to the given task's workflow cycle. Args: cycle_task: The cycle task instance to build the URL for. filter_exp: An optional query filter expression to be included in the URL. Defaults to an empty string. Returns: Full workflow cycle URL (as a string). """ if filter_exp: filter_exp = u"?filter=" + urllib.quote(filter_exp) url = (u"/workflows/{workflow_id}" u"{filter_exp}" u"#current/cycle/{cycle_id}").format( workflow_id=cycle_task.cycle_task_group.cycle.workflow.id, filter_exp=filter_exp, cycle_id=cycle_task.cycle_task_group.cycle.id, ) return urljoin(get_url_root(), url)
def cycle_task_workflow_cycle_url(cycle_task, filter_exp=u""): """Build the URL to the given task's workflow cycle. Args: cycle_task: The cycle task instance to build the URL for. filter_exp: An optional query filter expression to be included in the URL. Defaults to an empty string. Returns: Full workflow cycle URL (as a string). """ if filter_exp: filter_exp = u"?filter=" + urllib.quote(filter_exp) url = (u"/workflows/{workflow_id}" u"{filter_exp}" u"#current_widget/cycle/{cycle_id}").format( workflow_id=cycle_task.cycle_task_group.cycle.workflow.id, filter_exp=filter_exp, cycle_id=cycle_task.cycle_task_group.cycle.id, ) return urljoin(get_url_root(), url)
def test_comment_people_mentioned(self, send_email_mock): """Test handling of mapped comment with mentioned people""" person = factories.PersonFactory(email="*****@*****.**") comment = factories.CommentFactory( description=u"One <a href=\"mailto:[email protected]\"></a>", modified_by_id=person.id, created_at=datetime.datetime(2018, 1, 10, 7, 31, 42)) assessment = factories.AssessmentFactory() response = self.api.put( assessment, { "actions": { "add_related": [{ "id": comment.id, "type": comment.__class__.__name__, }] } }) url = urljoin(get_url_root(), utils.view_url_for(assessment)) self.assert200(response) relationship = _get_relationship("Assessment", response.json["assessment"]["id"]) self.assertIsNotNone(relationship) expected_title = (u"[email protected] mentioned you " u"on a comment within {}".format(assessment.title)) expected_body = ( u"[email protected] mentioned you on a comment within {} " u"at 01/09/2018 23:31:42 PST:\n" u"One <a href=\"mailto:[email protected]\"></a>\n".format( assessment.title)) body = settings.EMAIL_MENTIONED_PERSON.render( person_mention={ "comments": [expected_body], "url": url, }) send_email_mock.assert_called_once_with(u"*****@*****.**", expected_title, body)
def handle_comment_mapped(obj, comments): """Send mentions in the comments in the bg task. Args: obj: object for which comments were created, comments: A list of comment objects. """ comments_data = _fetch_comments_data(comments) if obj.__class__.__name__ == "CycleTaskGroupObjectTask": url = calendar_utils.get_cycle_tasks_url_by_slug(obj.slug) else: url = urljoin(get_url_root(), utils.view_url_for(obj)) models.background_task.create_task( name="send_mentions_bg", url=flask.url_for("send_mentions_bg"), parameters={ "comments_data": comments_data, "object_name": obj.title, "href": url, }, queued_callback=send_mentions_bg, )
def get_assessment_url(assessment): return urlparse.urljoin( utils.get_url_root(), "assessments/{}".format(assessment.id))
def get_cycle_url(cycle): url = "workflows/{workflow_id}#current_widget/cycle/{cycle_id}".format( workflow_id=cycle.workflow.id, cycle_id=cycle.id, ) return urljoin(get_url_root(), url)
def _get_assessment_url(assessment): """Returns string URL for assessment view page.""" return urlparse.urljoin(utils.get_url_root(), utils.view_url_for(assessment))
def get_cycle_tasks_url_by_slug(slug): """Get CycleTask notification url based on slug.""" base = urljoin(get_url_root(), u"dashboard#!task&query=") active_filter = u'"task slug"={slug}'.format(slug=slug) return base + urllib.quote(active_filter.encode('utf-8'))
def get_workflow_url(workflow): """Get URL to workflow object.""" url = "workflows/{}#current".format(workflow.id) return urljoin(get_url_root(), url)
def get_ggrc_object_url(obj): """Builds and returns URL to GGRC object.""" return urlparse.urljoin( ggrc_utils.get_url_root(), ggrc_utils.view_url_for(obj) )
def get_cycle_task_url(): """Get CycleTask notification url.""" return urljoin(get_url_root(), u"dashboard#!task")
def get_object_url(obj): return urlparse.urljoin(utils.get_url_root(), "{}/{}".format(obj._inflector.table_plural, obj.id))
def get_workflow_url(workflow): url = "workflows/{}#current_widget".format(workflow.id) return urljoin(get_url_root(), url)