def test_update_ucr(self):
        linked_report_info = create_linked_ucr(self.domain_link,
                                               self.report.get_id)
        self.report.title = "New title"
        self.report.save()

        update_linked_ucr(self.domain_link, linked_report_info.report.get_id)

        report = ReportConfiguration.get(linked_report_info.report.get_id)
        self.assertEqual("New title", report.title)
        self.assertEqual(self.report.get_id, report.report_meta.master_id)
        self.assertNotEqual(self.report.config_id, report.config_id)
Exemplo n.º 2
0
    def test_delete_master_deletes_linked(self):
        linked_report_info = create_linked_ucr(self.domain_link,
                                               self.report.get_id)
        soft_delete(self.report)
        update_linked_ucr(self.domain_link, linked_report_info.report.get_id)
        report = ReportConfiguration.get(linked_report_info.report.get_id)
        self.assertTrue(is_deleted(report))

        self.report.config.deactivate()
        update_linked_ucr(self.domain_link, linked_report_info.report.get_id)
        report = ReportConfiguration.get(linked_report_info.report.get_id)
        self.assertTrue(report.config.is_deactivated)
Exemplo n.º 3
0
    def _release_report(self, domain_link, model):
        report_id = model['detail']['report_id']
        found = False
        for linked_report in get_report_configs_for_domain(domain_link.linked_domain):
            if linked_report.report_meta.master_id == report_id:
                found = True
                update_linked_ucr(domain_link, linked_report.get_id)

        if not found:
            report = ReportConfiguration.get(report_id)
            if report.report_meta.created_by_builder:
                view = 'edit_report_in_builder'
            else:
                view = 'edit_configurable_report'
            url = get_url_base() + reverse(view, args=[domain_link.master_domain, report_id])
            return self._error_tuple(
                _('Could not find report. <a href="{}">Click here</a> and click "Link Report" to link this '
                  + 'report.').format(url),
                text=_('Could not find report. Please check that the report has been linked.'),
            )
Exemplo n.º 4
0
    def _release_report(self, domain_link, model, user_id):
        report_id = model['detail']['report_id']
        linked_report = get_downstream_report(domain_link.linked_domain,
                                              report_id)

        if not linked_report:
            try:
                linked_report_info = create_linked_ucr(domain_link, report_id)
                linked_report = linked_report_info.report
            except DomainLinkError as e:
                return self._error_tuple(str(e))

        # have no hit an error case, so update the ucr
        update_linked_ucr(domain_link, linked_report.get_id)
        domain_link.update_last_pull(
            MODEL_REPORT,
            user_id,
            model_detail=ReportLinkDetail(
                report_id=linked_report.get_id).to_json(),
        )
Exemplo n.º 5
0
    def test_remote_link_ucr(self, fake_ucr_getter):

        fake_ucr_getter.return_value = {
            "report": self.report,
            "datasource": self.data_source,
        }

        # Create
        linked_report_info = create_linked_ucr(self.domain_link,
                                               self.report.get_id)
        self.assertEqual(
            1,
            len(ReportConfiguration.by_domain(self.domain_link.linked_domain)))
        self.assertEqual(self.report.get_id,
                         linked_report_info.report.report_meta.master_id)

        # Update
        self.report.title = "Another new title"
        self.report.save()

        update_linked_ucr(self.domain_link, linked_report_info.report.get_id)
        report = ReportConfiguration.get(linked_report_info.report.get_id)
        self.assertEqual("Another new title", report.title)
    def test_remote_link_ucr(self, fake_ucr_getter):
        create_domain(self.domain)
        self.addCleanup(delete_all_domains)

        couch_user = WebUser.create(self.domain, "test", "foobar", None, None)
        django_user = couch_user.get_django_user()
        self.addCleanup(delete_all_users)

        api_key, _ = HQApiKey.objects.get_or_create(user=django_user)
        auth_headers = {'HTTP_AUTHORIZATION': 'apikey test:%s' % api_key.key}
        self.domain_link.save()

        url = reverse('linked_domain:ucr_config',
                      args=[self.domain, self.report.get_id])
        headers = auth_headers.copy()
        headers[REMOTE_REQUESTER_HEADER] = self.domain_link.linked_domain
        resp = self.client.get(url, **headers)

        fake_ucr_getter.return_value = json.loads(resp.content)

        # Create
        linked_report_info = create_linked_ucr(self.domain_link,
                                               self.report.get_id)
        self.assertEqual(
            1,
            len(ReportConfiguration.by_domain(self.domain_link.linked_domain)))
        self.assertEqual(self.report.get_id,
                         linked_report_info.report.report_meta.master_id)

        # Update
        self.report.title = "Another new title"
        self.report.save()

        update_linked_ucr(self.domain_link, linked_report_info.report.get_id)
        report = ReportConfiguration.get(linked_report_info.report.get_id)
        self.assertEqual("Another new title", report.title)
Exemplo n.º 7
0
def push_models(master_domain, models, linked_domains, build_apps, username):
    domain_links_by_linked_domain = {
        link.linked_domain: link
        for link in get_linked_domains(master_domain)
    }
    user = CouchUser.get_by_username(username)
    errors_by_domain = defaultdict(list)
    successes_by_domain = defaultdict(list)
    for linked_domain in linked_domains:
        if linked_domain not in domain_links_by_linked_domain:
            errors_by_domain[linked_domain].append(
                _("Project space {} is no longer linked to {}. No content "
                  "was released to it.").format(master_domain, linked_domain))
            continue
        domain_link = domain_links_by_linked_domain[linked_domain]
        for model in models:
            try:
                found = False
                updated_app = False
                built_app = False
                if model['type'] == MODEL_APP:
                    app_id = model['detail']['app_id']
                    for linked_app in get_apps_in_domain(linked_domain,
                                                         include_remote=False):
                        if is_linked_app(
                                linked_app) and linked_app.family_id == app_id:
                            found = True
                            if toggles.MULTI_MASTER_LINKED_DOMAINS.enabled(
                                    linked_domain):
                                errors_by_domain[linked_domain].append(
                                    textwrap.dedent(
                                        _("""
                                    Could not update {} because multi master flag is in use
                                """.strip()).format(model['name'])))
                                continue
                            app = update_linked_app(linked_app, app_id,
                                                    user.user_id)
                            updated_app = True
                            if build_apps:
                                build = app.make_build()
                                build.is_released = True
                                build.save(increment_version=False)
                                built_app = True
                elif model['type'] == MODEL_REPORT:
                    report_id = model['detail']['report_id']
                    for linked_report in get_report_configs_for_domain(
                            linked_domain):
                        if linked_report.report_meta.master_id == report_id:
                            found = True
                            update_linked_ucr(domain_link,
                                              linked_report.get_id)
                elif (model['type'] == MODEL_CASE_SEARCH and
                      not toggles.SYNC_SEARCH_CASE_CLAIM.enabled(linked_domain)
                      ):
                    errors_by_domain[linked_domain].append(
                        textwrap.dedent(
                            _("""
                        Could not update {} because case claim flag is not on
                    """.strip()).format(model['name'])))
                    continue
                else:
                    found = True
                    update_model_type(domain_link,
                                      model['type'],
                                      model_detail=model['detail'])
                    domain_link.update_last_pull(model['type'],
                                                 user._id,
                                                 model_details=model['detail'])
                if found:
                    successes_by_domain[linked_domain].append(
                        _("{} was updated").format(model['name']))
                else:
                    errors_by_domain[linked_domain].append(
                        _("Could not find {}").format(model['name']))
            except Exception as e:  # intentionally broad
                if model[
                        'type'] == MODEL_APP and updated_app and build_apps and not built_app:
                    # Updating an app can be a 2-step process, make it clear which one failed
                    errors_by_domain[linked_domain].append(
                        textwrap.dedent(
                            _("""
                        Updated {} but could not make and release build: {}
                    """.strip()).format(model['name'], str(e))))
                else:
                    errors_by_domain[linked_domain].append(
                        textwrap.dedent(
                            _("""
                        Could not update {}: {}
                    """.strip()).format(model['name'], str(e))))
                notify_exception(
                    None, "Exception pushing linked domains: {}".format(e))

    subject = _("Linked project release complete.")
    if errors_by_domain:
        subject += _(" Errors occurred.")

    error_domain_count = len(errors_by_domain)
    success_domain_count = len(linked_domains) - error_domain_count
    message = _("""
Release complete. {} project(s) succeeded. {}

The following content was released:
{}

The following linked project spaces received content:
    """).format(
        success_domain_count,
        _("{} project(s) encountered errors.").format(error_domain_count)
        if error_domain_count else "",
        "\n".join(["- " + m['name'] for m in models]))
    for linked_domain in linked_domains:
        if linked_domain not in errors_by_domain:
            message += _("\n- {} updated successfully").format(linked_domain)
        else:
            message += _("\n- {} encountered errors:").format(linked_domain)
            for msg in errors_by_domain[linked_domain] + successes_by_domain[
                    linked_domain]:
                message += "\n   - " + msg
    send_mail_async.delay(subject, message, settings.DEFAULT_FROM_EMAIL,
                          [user.email or user.username])