def link_keywords(request, domain): from_domain = domain to_domains = request.POST.getlist("to_domains") keyword_ids = request.POST.getlist("keyword_ids") successes = set() failures = set() for to_domain in to_domains: domain_link = DomainLink.objects.get(master_domain=from_domain, linked_domain=to_domain) for keyword_id in keyword_ids: try: linked_keyword_id = create_linked_keyword(domain_link, keyword_id) domain_link.update_last_pull( 'keyword', request.couch_user._id, model_detail=KeywordLinkDetail(linked_keyword_id=str(linked_keyword_id)).to_json(), ) successes.add(to_domain) except DomainLinkError as err: failures.add(f"{to_domain}: {err}") notify_exception(request, message=str(err)) except Exception as err: failures.add(to_domain) notify_exception(request, message=str(err)) if successes: messages.success( request, _(f"Successfully linked and copied to {', '.join(successes)}. ")) if failures: messages.error(request, _(f"Errors occurred for {', '.join(failures)}.")) return HttpResponseRedirect( reverse(KeywordsListView.urlname, args=[from_domain]) )
def test_create_keyword_link(self): new_keyword_id = create_linked_keyword(self.domain_link, self.keyword.id) new_keyword = Keyword.objects.get(id=new_keyword_id) self.assertEqual(new_keyword.keyword, self.keyword.keyword) new_keyword_action = new_keyword.keywordaction_set.first() self.assertEqual( new_keyword_action.message_content, self.keyword.keywordaction_set.first().message_content, ) self.assertEqual(new_keyword_action.app_id, self.linked_app.get_id)
def test_update_keyword_link(self): new_keyword_id = create_linked_keyword(self.domain_link, self.keyword.id) self.keyword.keyword = "foo" self.keyword.save() keyword_action = self.keyword.keywordaction_set.first() keyword_action.message_content = "bar" keyword_action.save() update_keyword(self.domain_link, new_keyword_id) linked_keyword = Keyword.objects.get(id=new_keyword_id) self.assertEqual(linked_keyword.keyword, "foo") self.assertEqual( linked_keyword.keywordaction_set.first().message_content, "bar")
def _release_keyword(self, domain_link, model, user_id): upstream_id = model['detail']['keyword_id'] try: linked_keyword_id = (Keyword.objects.values_list( 'id', flat=True).get(domain=domain_link.linked_domain, upstream_id=upstream_id)) except Keyword.DoesNotExist: linked_keyword_id = create_linked_keyword(domain_link, upstream_id) update_keyword(domain_link, linked_keyword_id) domain_link.update_last_pull( MODEL_KEYWORD, user_id, model_detail=KeywordLinkDetail( keyword_id=str(linked_keyword_id)).to_json(), )
def test_already_linked_keyword_is_pushed(self): keyword = self._create_new_keyword('keyword') self.addCleanup(keyword.delete) linked_keyword_id = create_linked_keyword(self.domain_link, keyword.id) self.addCleanup(Keyword(id=linked_keyword_id).delete) # after creating the link, update the upstream keyword keyword.keyword = "updated-keyword" keyword.save() model = self._linked_data_view_model( MODEL_KEYWORD, detail=KeywordLinkDetail(keyword_id=str(keyword.id)).to_json()) manager = ReleaseManager(self.domain, self.user.username) errors = manager._release_keyword(self.domain_link, model, 'test-user') self.assertIsNone(errors) downstream_keyword = get_downstream_keyword(self.linked_domain, keyword.id) self.addCleanup(downstream_keyword.delete) self.assertIsNotNone(downstream_keyword) self.assertEqual("updated-keyword", downstream_keyword.keyword)