Example #1
0
    def _get_toggles_to_migrate(self):
        from corehq.toggles import all_toggles, NAMESPACE_DOMAIN
        from toggle.models import Toggle
        from toggle.shortcuts import namespaced_item

        all_user_ids = self._user_ids_in_domain()

        toggles_to_migrate = []
        domain_item = namespaced_item(self.domain, NAMESPACE_DOMAIN)

        for toggle in all_toggles() + all_previews():
            try:
                current_toggle = Toggle.get(toggle.slug)
            except ResourceNotFound:
                continue

            enabled_for = set(current_toggle.enabled_users)

            new_toggle = Toggle(slug=toggle.slug, enabled_users=[])
            if domain_item in enabled_for:
                new_toggle.enabled_users.append(domain_item)

            enabled_users = enabled_for & all_user_ids
            new_toggle.enabled_users.extend(list(enabled_users))

            if new_toggle.enabled_users:
                toggles_to_migrate.append(new_toggle.to_json())

        return toggles_to_migrate
Example #2
0
def toggle_diff(request, domain):
    params = json_request(request.GET)
    other_domain = params.get('domain')
    diff = []
    if Domain.get_by_name(other_domain):
        diff = [{
            'slug': t.slug,
            'label': t.label,
            'url': reverse(ToggleEditView.urlname, args=[t.slug]),
            'tag_name': _('Preview'),
            'tag_css_class': 'default',
            'tag_index': -1,
        } for t in feature_previews.all_previews()
                if _can_copy_toggle(t, request.domain, other_domain)]
        diff.extend([{
            'slug': t.slug,
            'label': t.label,
            'url': reverse(ToggleEditView.urlname, args=[t.slug]),
            'tag_name': t.tag.name,
            'tag_css_class': t.tag.css_class,
            'tag_index': t.tag.index,
        } for t in toggles.all_toggles()
                     if _can_copy_toggle(t, request.domain, other_domain)])
        diff.sort(key=lambda x: (x['tag_index'], x['label']))
    return json_response(diff)
Example #3
0
    def _get_toggles_to_migrate(self):
        from corehq.toggles import all_toggles, NAMESPACE_DOMAIN
        from toggle.models import Toggle
        from toggle.shortcuts import namespaced_item

        all_user_ids = self._user_ids_in_domain()

        toggles_to_migrate = []
        domain_item = namespaced_item(self.domain, NAMESPACE_DOMAIN)

        for toggle in all_toggles() + all_previews():
            try:
                current_toggle = Toggle.get(toggle.slug)
            except ResourceNotFound:
                continue

            enabled_for = set(current_toggle.enabled_users)

            new_toggle = Toggle(slug=toggle.slug, enabled_users=[])
            if domain_item in enabled_for:
                new_toggle.enabled_users.append(domain_item)

            enabled_users = enabled_for & all_user_ids
            new_toggle.enabled_users.extend(list(enabled_users))

            if new_toggle.enabled_users:
                toggles_to_migrate.append(new_toggle.to_json())

        return toggles_to_migrate
Example #4
0
def set_toggle(slug, item, enabled, namespace=None):
    """
    Sets a toggle value explicitly. Should only save anything if the value needed to be changed.
    """
    if toggle_enabled(slug, item, namespace=namespace) != enabled:
        ns_item = namespaced_item(item, namespace)
        try:
            toggle_doc = Toggle.get(slug)
        except ResourceNotFound:
            toggle_doc = Toggle(slug=slug, enabled_users=[])
        if enabled:
            toggle_doc.add(ns_item)
        else:
            toggle_doc.remove(ns_item)
        from corehq.feature_previews import all_previews
        from corehq.toggles import all_toggles, NAMESPACE_DOMAIN
        static_toggles_by_slug = {
            t.slug: t
            for t in all_toggles() + all_previews()
        }
        if namespace == NAMESPACE_DOMAIN and slug in static_toggles_by_slug:
            static_toggle = static_toggles_by_slug[slug]
            if static_toggle.save_fn:
                static_toggle.save_fn(item, enabled)
        update_toggle_cache(slug, item, enabled, namespace)
Example #5
0
def init_app(request, app):
    if app.add_ons:
        return

    # Don't use previews_dict because it doesn't include disabled previews
    previews = {
        p.slug: p.enabled(app.domain)
        for p in feature_previews.all_previews()
    }

    for slug in _ADD_ONS.keys():
        add_on = _ADD_ONS[slug]
        enable = False
        if add_on.has_privilege(request):
            # Enable if it's an enabled preview
            if slug in previews:
                enable = previews[slug]

            # Turn on if it's used anywhere
            enable = enable or any(
                [add_on.used_in_module(m) for m in app.modules])
            enable = enable or any(
                [add_on.used_in_form(f) for m in app.modules for f in m.forms])

            enable = enable or _grandfathered(slug, app)
        app.add_ons[slug] = enable
Example #6
0
def _get_toggles_to_migrate(domain):
    from corehq.toggles import all_toggles, NAMESPACE_DOMAIN
    from toggle.models import Toggle
    from toggle.shortcuts import namespaced_item

    domain_item = namespaced_item(domain, NAMESPACE_DOMAIN)
    usernames = set(get_all_usernames_by_domain(domain))

    for toggle in all_toggles() + all_previews():
        try:
            current_toggle = Toggle.get(toggle.slug)
        except ResourceNotFound:
            continue

        enabled_for = set(current_toggle.enabled_users)

        new_toggle = Toggle(slug=toggle.slug, enabled_users=[])
        if domain_item in enabled_for:
            new_toggle.enabled_users.append(domain_item)

        enabled_users = enabled_for & usernames
        new_toggle.enabled_users.extend(list(enabled_users))

        if new_toggle.enabled_users:
            yield new_toggle.to_json()
Example #7
0
def toggle_diff(request, domain):
    params = json_request(request.GET)
    other_domain = params.get('domain')
    diff = []
    if Domain.get_by_name(other_domain):
        diff = [{'slug': t.slug, 'label': t.label, 'url': reverse(ToggleEditView.urlname, args=[t.slug])}
                for t in feature_previews.all_previews() + all_toggles()
                if t.enabled(request.domain, NAMESPACE_DOMAIN) and not t.enabled(other_domain, NAMESPACE_DOMAIN)]
        diff.sort(key=lambda x: x['label'])
    return json_response(diff)
Example #8
0
def toggle_diff(request, domain):
    params = json_request(request.GET)
    other_domain = params.get('domain')
    diff = []
    if Domain.get_by_name(other_domain):
        diff = [{
            'slug': t.slug,
            'label': t.label,
            'url': reverse(ToggleEditView.urlname, args=[t.slug])
        } for t in feature_previews.all_previews() + all_toggles()
                if t.enabled(request.domain, NAMESPACE_DOMAIN)
                and not t.enabled(other_domain, NAMESPACE_DOMAIN)]
        diff.sort(key=lambda x: x['label'])
    return json_response(diff)
Example #9
0
    def set_flags(self):
        from corehq.toggles import all_toggles, NAMESPACE_DOMAIN
        from corehq.feature_previews import all_previews

        for toggle in all_toggles():
            if toggle.enabled(self.existing_domain, NAMESPACE_DOMAIN):
                self.stdout.write('Setting flag: {}'.format(toggle.slug))
                if not self.no_commit:
                    toggle.set(self.new_domain, True, NAMESPACE_DOMAIN)

        for preview in all_previews():
            if preview.enabled(self.existing_domain):
                self.stdout.write('Setting preview: {}'.format(preview.slug))
                if not self.no_commit:
                    preview.set(self.new_domain, True, NAMESPACE_DOMAIN)
                    if preview.save_fn is not None:
                        preview.save_fn(self.new_domain, True)
Example #10
0
    def set_flags(self):
        from corehq.toggles import all_toggles, NAMESPACE_DOMAIN
        from corehq.feature_previews import all_previews

        for toggle in all_toggles():
            if toggle.enabled(self.existing_domain, NAMESPACE_DOMAIN):
                self.stdout.write('Setting flag: {}'.format(toggle.slug))
                if not self.no_commit:
                    toggle.set(self.new_domain, True, NAMESPACE_DOMAIN)

        for preview in all_previews():
            if preview.enabled(self.existing_domain):
                self.stdout.write('Setting preview: {}'.format(preview.slug))
                if not self.no_commit:
                    preview.set(self.new_domain, True, NAMESPACE_DOMAIN)
                    if preview.save_fn is not None:
                        preview.save_fn(self.new_domain, True)
Example #11
0
    def set_flags(self):
        from corehq.toggles import all_toggles, NAMESPACE_DOMAIN
        from corehq.feature_previews import all_previews
        from corehq.apps.hqwebapp.templatetags.hq_shared_tags import toggle_js_domain_cachebuster

        for toggle in all_toggles():
            if toggle.enabled(self.existing_domain):
                self.stdout.write("Setting flag: {}".format(toggle.slug))
                if not self.no_commmit:
                    toggle.set(self.new_domain, True, NAMESPACE_DOMAIN)

        for preview in all_previews():
            if preview.enabled(self.existing_domain):
                self.stdout.write("Setting preview: {}".format(preview.slug))
                if not self.no_commmit:
                    preview.set(self.new_domain, True, NAMESPACE_DOMAIN)
                    if preview.save_fn is not None:
                        preview.save_fn(self.new_domain, True)

        toggle_js_domain_cachebuster.clear(self.new_domain)
Example #12
0
    def set_flags(self):
        from corehq.toggles import all_toggles, NAMESPACE_DOMAIN
        from corehq.feature_previews import all_previews
        from corehq.apps.hqwebapp.templatetags.hq_shared_tags import toggle_js_domain_cachebuster

        for toggle in all_toggles():
            if toggle.enabled(self.existing_domain):
                self.stdout.write('Setting flag: {}'.format(toggle.slug))
                if not self.no_commmit:
                    toggle.set(self.new_domain, True, NAMESPACE_DOMAIN)

        for preview in all_previews():
            if preview.enabled(self.existing_domain):
                self.stdout.write('Setting preview: {}'.format(preview.slug))
                if not self.no_commmit:
                    preview.set(self.new_domain, True, NAMESPACE_DOMAIN)
                    if preview.save_fn is not None:
                        preview.save_fn(self.new_domain, True)

        toggle_js_domain_cachebuster.clear(self.new_domain)
Example #13
0
def init_app(request, app):
    if app.add_ons:
        return

    # Don't use previews_dict because it doesn't include disabled previews
    previews = {p.slug: p.enabled(app.domain) for p in feature_previews.all_previews()}

    for slug in _ADD_ONS.keys():
        add_on = _ADD_ONS[slug]
        enable = False
        if add_on.has_privilege(request):
            # Enable if it's an enabled preview
            if slug in previews:
                enable = previews[slug]

            # Turn on if it's used anywhere
            enable = enable or any([add_on.used_in_module(m) for m in app.modules])
            enable = enable or any([add_on.used_in_form(f) for m in app.modules for f in m.forms])

            enable = enable or _grandfathered(slug, app)
        app.add_ons[slug] = enable
Example #14
0
def set_toggle(slug, item, enabled, namespace=None):
    """
    Sets a toggle value explicitly. Should only save anything if the value needed to be changed.
    """
    if toggle_enabled(slug, item, namespace=namespace) != enabled:
        ns_item = namespaced_item(item, namespace)
        try:
            toggle_doc = Toggle.get(slug)
        except ResourceNotFound:
            toggle_doc = Toggle(slug=slug, enabled_users=[])
        if enabled:
            toggle_doc.add(ns_item)
        else:
            toggle_doc.remove(ns_item)
        from corehq.feature_previews import all_previews
        from corehq.toggles import all_toggles, NAMESPACE_DOMAIN
        static_toggles_by_slug = {t.slug: t for t in all_toggles() + all_previews()}
        if namespace == NAMESPACE_DOMAIN and slug in static_toggles_by_slug:
            static_toggle = static_toggles_by_slug[slug]
            if static_toggle.save_fn:
                static_toggle.save_fn(item, enabled)
Example #15
0
 def show_toggles(self):
     counts = Counter(t.tag.name for t in all_toggles() + all_previews())
     for tag, count in counts.items():
         self.logger.log("commcare.static_analysis.toggle_count", count,
                         [f"toggle_tag:{tag}"])