Beispiel #1
0
    def _get_mocked_toggles(self):
        from toggle.models import generate_toggle_id
        from toggle.models import Toggle
        from toggle.shortcuts import namespaced_item

        mocked_toggles = {
            toggle.slug: Toggle(_id=generate_toggle_id(toggle.slug),
                                slug=toggle.slug)
            for toggle in random.sample(all_toggles(), 3)
        }
        toggles = mocked_toggles.values()
        domain_item = namespaced_item(self.domain_name, NAMESPACE_DOMAIN)
        toggles[0].enabled_users = [domain_item]
        toggles[1].enabled_users = ['user1', 'other-user', 'user2']
        toggles[2].enabled_users = [
            'user1', domain_item,
            namespaced_item('other_domain', NAMESPACE_DOMAIN)
        ]

        expected_items = {
            toggles[0].slug: [domain_item],
            toggles[1].slug: ['user1', 'user2'],
            toggles[2].slug: ['user1', domain_item],
        }

        return mocked_toggles, expected_items
Beispiel #2
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
Beispiel #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
    def handle(self, *args, **options):
        dry_run = options.get('dry_run', False)

        if dry_run:
            print("\n-------- DRY RUN --------\n")

        slug = CALLCENTER.slug
        try:
            toggle = Toggle.get(slug)
        except ResourceNotFound:
            toggle = Toggle(slug=slug)
        print("Current domains in toggle: {}".format(toggle.enabled_users))

        domains = get_call_center_domains()
        print("Active call center domains: {}".format(domains))

        items = [
            namespaced_item(domain, NAMESPACE_DOMAIN) for domain in domains
        ]
        missing = set(items) - set(toggle.enabled_users)
        print("Domains missing from toggle: {}".format(missing))

        toggle.enabled_users = items

        if not dry_run:
            toggle.save()

            for item in items:
                update_toggle_cache(slug, item, True)
Beispiel #5
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()
    def handle(self, *args, **options):
        dry_run = options.get('dry_run', False)

        if dry_run:
            print("\n-------- DRY RUN --------\n")

        slug = CALLCENTER.slug
        try:
            toggle = Toggle.get(slug)
        except ResourceNotFound:
            toggle = Toggle(slug=slug)
        print("Current domains in toggle: {}".format(toggle.enabled_users))

        domains = get_call_center_domains()
        print("Active call center domains: {}".format(domains))

        items = [namespaced_item(domain, NAMESPACE_DOMAIN) for domain in domains]
        missing = set(items) - set(toggle.enabled_users)
        print("Domains missing from toggle: {}".format(missing))

        toggle.enabled_users = items

        if not dry_run:
            toggle.save()

            for item in items:
                update_toggle_cache(slug, item, True)
Beispiel #7
0
 def setUpClass(cls):
     cls.user_toggle = Toggle(slug='user_toggle',
                              enabled_users=['arthur', 'diana'])
     cls.user_toggle.save()
     cls.domain_toggle = Toggle(
         slug='domain_toggle',
         enabled_users=[namespaced_item('dc', NAMESPACE_DOMAIN)])
     cls.domain_toggle.save()
Beispiel #8
0
 def setUpClass(cls):
     super(PredictablyRandomToggleTests, cls).setUpClass()
     cls.user_toggle = Toggle(slug='user_toggle',
                              enabled_users=['arthur', 'diana'])
     cls.user_toggle.save()
     cls.domain_toggle = Toggle(
         slug='domain_toggle',
         enabled_users=[namespaced_item('dc', NAMESPACE_DOMAIN)])
     cls.domain_toggle.save()
Beispiel #9
0
 def setUpClass(cls):
     super(PredictablyRandomToggleTests, cls).setUpClass()
     cls.user_toggle = Toggle(
         slug='user_toggle',
         enabled_users=['arthur', 'diana'])
     cls.user_toggle.save()
     cls.domain_toggle = Toggle(
         slug='domain_toggle',
         enabled_users=[namespaced_item('dc', NAMESPACE_DOMAIN)])
     cls.domain_toggle.save()
Beispiel #10
0
 def setUpClass(cls):
     super(ShortcutTests, cls).setUpClass()
     cls.users = ['arthur', 'diane']
     cls.user_toggle = Toggle(slug='user_toggle', enabled_users=cls.users)
     cls.user_toggle.save()
     cls.domain = 'dc'
     cls.domain_toggle = Toggle(
         slug='domain_toggle',
         enabled_users=[namespaced_item(cls.domain, NAMESPACE_DOMAIN)])
     cls.domain_toggle.save()
Beispiel #11
0
 def setUpClass(cls):
     super(ShortcutTests, cls).setUpClass()
     cls.users = ['arthur', 'diane']
     cls.user_toggle = Toggle(
         slug='user_toggle',
         enabled_users=cls.users)
     cls.user_toggle.save()
     cls.domain = 'dc'
     cls.domain_toggle = Toggle(
         slug='domain_toggle',
         enabled_users=[namespaced_item(cls.domain, NAMESPACE_DOMAIN)])
     cls.domain_toggle.save()
Beispiel #12
0
    def _get_mocked_toggles(self):
        from toggle.models import generate_toggle_id
        from toggle.models import Toggle
        from toggle.shortcuts import namespaced_item

        mocked_toggles = {
            toggle.slug: Toggle(_id=generate_toggle_id(toggle.slug), slug=toggle.slug)
            for toggle in random.sample(all_toggles(), 3)
        }
        toggles = mocked_toggles.values()
        domain_item = namespaced_item(self.domain_name, NAMESPACE_DOMAIN)
        toggles[0].enabled_users = [domain_item]
        toggles[1].enabled_users = ['user1', 'other-user', 'user2']
        toggles[2].enabled_users = ['user1', domain_item, namespaced_item('other_domain', NAMESPACE_DOMAIN)]

        expected_items = {
            toggles[0].slug: [domain_item],
            toggles[1].slug: ['user1', 'user2'],
            toggles[2].slug: ['user1', domain_item],
        }

        return mocked_toggles, expected_items
Beispiel #13
0
    def update_toggle(self):
        """
        This turns on the special stock handler when EWS is enabled.
        """
        toggle = Toggle.get(STOCK_AND_RECEIPT_SMS_HANDLER.slug)
        toggle_user_key = namespaced_item(self.domain, NAMESPACE_DOMAIN)

        if self.enabled and toggle_user_key not in toggle.enabled_users:
            toggle.enabled_users.append(toggle_user_key)
            toggle.save()
            update_toggle_cache(
                STOCK_AND_RECEIPT_SMS_HANDLER.slug,
                toggle_user_key, True
            )
Beispiel #14
0
    def handle(self, *args, **options):
        domains = Domain.get_all()

        for domain in domains:
            if domain.commtrack_enabled:
                toggle = Toggle.get(LOCATIONS.slug)
                toggle_user_key = namespaced_item(domain.name, NAMESPACE_DOMAIN)

                if toggle_user_key not in toggle.enabled_users:
                    toggle.enabled_users.append(toggle_user_key)
                    toggle.save()
                    update_toggle_cache(LOCATIONS.slug, toggle_user_key, True)

                if not domain.locations_enabled:
                    domain.locations_enabled = True
                    domain.save()
Beispiel #15
0
def set_toggle(request, toggle_slug):
    static_toggle = find_static_toggle(toggle_slug)
    if not static_toggle:
        raise Http404()

    item = request.POST['item']
    enabled = request.POST['enabled'] == 'true'
    namespace = request.POST['namespace']
    if static_toggle.set(item=item, enabled=enabled, namespace=namespace):
        action = ToggleAudit.ACTION_ADD if enabled else ToggleAudit.ACTION_REMOVE
        ToggleAudit.objects.log_toggle_action(
            toggle_slug, request.user.username, [namespaced_item(item, namespace)], action
        )

    if enabled:
        _notify_on_change(static_toggle, [item], request.user.username)

    return HttpResponse(json.dumps({'success': True}), content_type="application/json")
Beispiel #16
0
    def test_toggle_cache(self):
        ns = 'ns'
        toggle = Toggle(slug=self.slug, enabled_users=['mojer', namespaced_item('fizbod', ns)])
        toggle.save()

        self.assertTrue(toggle_enabled(self.slug, 'mojer'))
        self.assertFalse(toggle_enabled(self.slug, 'fizbod'))
        self.assertTrue(toggle_enabled(self.slug, 'fizbod', namespace=ns))

        update_toggle_cache(self.slug, 'mojer', False)
        update_toggle_cache(self.slug, 'fizbod', False, namespace=ns)

        self.assertFalse(toggle_enabled(self.slug, 'mojer'))
        self.assertFalse(toggle_enabled(self.slug, 'fizbod', namespace=ns))

        clear_toggle_cache(self.slug, 'mojer')
        clear_toggle_cache(self.slug, 'fizbod', namespace=ns)

        self.assertTrue(toggle_enabled(self.slug, 'mojer'))
        self.assertTrue(toggle_enabled(self.slug, 'fizbod', namespace=ns))
Beispiel #17
0
    def test_toggle_cache(self):
        ns = 'ns'
        toggle = Toggle(slug=self.slug, enabled_users=['mojer', namespaced_item('fizbod', ns)])
        toggle.save()

        self.assertTrue(toggle_enabled(self.slug, 'mojer'))
        self.assertFalse(toggle_enabled(self.slug, 'fizbod'))
        self.assertTrue(toggle_enabled(self.slug, 'fizbod', namespace=ns))

        update_toggle_cache(self.slug, 'mojer', False)
        update_toggle_cache(self.slug, 'fizbod', False, namespace=ns)

        self.assertFalse(toggle_enabled(self.slug, 'mojer'))
        self.assertFalse(toggle_enabled(self.slug, 'fizbod', namespace=ns))

        clear_toggle_cache(self.slug, 'mojer')
        clear_toggle_cache(self.slug, 'fizbod', namespace=ns)

        self.assertTrue(toggle_enabled(self.slug, 'mojer'))
        self.assertTrue(toggle_enabled(self.slug, 'fizbod', namespace=ns))
Beispiel #18
0
def request_new_domain(request, form, org, domain_type=None, new_user=True):
    now = datetime.utcnow()
    current_user = CouchUser.from_django_user(request.user)

    commtrack_enabled = domain_type == 'commtrack'

    dom_req = RegistrationRequest()
    if new_user:
        dom_req.request_time = now
        dom_req.request_ip = get_ip(request)
        dom_req.activation_guid = uuid.uuid1().hex

    new_domain = Domain(
        name=form.cleaned_data['domain_name'],
        is_active=False,
        date_created=datetime.utcnow(),
        commtrack_enabled=commtrack_enabled,
        locations_enabled=commtrack_enabled,
        creating_user=current_user.username,
        secure_submissions=True,
    )

    if commtrack_enabled:
        toggle_user_key = namespaced_item(new_domain.name, NAMESPACE_DOMAIN)

        # enable commtrack toggle
        toggle = Toggle.get(COMMTRACK.slug)
        toggle.enabled_users.append(toggle_user_key)
        toggle.save()
        update_toggle_cache(COMMTRACK.slug, toggle_user_key, True)

        # enable location toggle
        toggle = Toggle.get(LOCATIONS.slug)
        toggle.enabled_users.append(toggle_user_key)
        toggle.save()
        update_toggle_cache(LOCATIONS.slug, toggle_user_key, True)


    if form.cleaned_data.get('domain_timezone'):
        new_domain.default_timezone = form.cleaned_data['domain_timezone']

    if org:
        new_domain.organization = org
        new_domain.hr_name = request.POST.get('domain_hrname', None) or new_domain.name

    if not new_user:
        new_domain.is_active = True

    # ensure no duplicate domain documents get created on cloudant
    new_domain.save(**get_safe_write_kwargs())

    if not new_domain.name:
        new_domain.name = new_domain._id
        new_domain.save() # we need to get the name from the _id

    create_30_day_trial(new_domain)

    dom_req.domain = new_domain.name

    if request.user.is_authenticated():
        if not current_user:
            current_user = WebUser()
            current_user.sync_from_django_user(request.user)
            current_user.save()
        current_user.add_domain_membership(new_domain.name, is_admin=True)
        current_user.save()
        dom_req.requesting_user_username = request.user.username
        dom_req.new_user_username = request.user.username

    if new_user:
        dom_req.save()
        send_domain_registration_email(request.user.email,
                                       dom_req.domain,
                                       dom_req.activation_guid)
    else:
        send_global_domain_registration_email(request.user, new_domain.name)
    send_new_request_update_email(request.user, get_ip(request), new_domain.name, is_new_user=new_user)