Example #1
0
    def _get_apps(self, restore_state, restore_user):
        app_aware_sync_app = restore_state.params.app

        if app_aware_sync_app:
            apps = [app_aware_sync_app]
        elif (toggles.ROLE_WEBAPPS_PERMISSIONS.enabled(restore_user.domain)
              and restore_state.params.device_id
              and "WebAppsLogin" in restore_state.params.device_id):
            # Only sync reports for apps the user has access to if this is a restore from webapps
            role = restore_user.get_role(restore_user.domain)
            if role:
                allowed_app_ids = [
                    app['_id']
                    for app in get_brief_apps_in_domain(restore_user.domain)
                    if role.permissions.view_web_app(app)
                ]
                apps = get_apps_by_id(restore_user.domain, allowed_app_ids)
            else:
                # If there is no role, allow access to all apps
                apps = get_apps_in_domain(restore_user.domain,
                                          include_remote=False)
        else:
            apps = get_apps_in_domain(restore_user.domain,
                                      include_remote=False)

        return apps
    def __call__(self, restore_state):
        """
        Generates a report fixture for mobile that can be used by a report module
        """
        restore_user = restore_state.restore_user
        if not toggles.MOBILE_UCR.enabled(restore_user.domain) or not _should_sync(restore_state):
            return []

        if toggles.PREVENT_MOBILE_UCR_SYNC.enabled(restore_user.domain):
            return []

        app_aware_sync_app = restore_state.params.app
        if app_aware_sync_app:
            apps = [app_aware_sync_app]
        elif (
                toggles.ROLE_WEBAPPS_PERMISSIONS.enabled(restore_user.domain)
                and restore_state.params.device_id
                and "WebAppsLogin" in restore_state.params.device_id
        ):
            # Only sync reports for apps the user has access to if this is a restore from webapps
            role = restore_user.get_role(restore_user.domain)
            if role:
                allowed_app_ids = [app['_id'] for app in get_brief_apps_in_domain(restore_user.domain)
                                   if role.permissions.view_web_app(app)]
                apps = get_apps_by_id(restore_user.domain, allowed_app_ids)
            else:
                # If there is no role, allow access to all apps
                apps = get_apps_in_domain(restore_user.domain, include_remote=False)
        else:
            apps = get_apps_in_domain(restore_user.domain, include_remote=False)

        report_configs = [
            report_config
            for app_ in apps
            for module in app_.modules if isinstance(module, ReportModule)
            for report_config in module.report_configs
        ]
        if not report_configs:
            return []

        root = E.fixture(id=self.id, user_id=restore_user.user_id)
        reports_elem = E.reports(last_sync=datetime.utcnow().isoformat())
        for report_config in report_configs:
            try:
                reports_elem.append(self.report_config_to_fixture(report_config, restore_user))
            except ReportConfigurationNotFoundError as err:
                logging.exception('Error generating report fixture: {}'.format(err))
                continue
            except UserReportsError:
                if settings.UNIT_TESTING or settings.DEBUG:
                    raise
            except Exception as err:
                logging.exception('Error generating report fixture: {}'.format(err))
                if settings.UNIT_TESTING or settings.DEBUG:
                    raise
        root.append(reports_elem)
        return [root]
Example #3
0
 def test_get_apps_in_domain(self):
     apps = get_apps_in_domain(self.domain)
     self.assertEqual(len(apps), 2)
     normal_app, remote_app = sorted(apps, key=lambda app: app.is_remote_app())
     expected_normal_app, expected_remote_app = sorted(self.apps, key=lambda app: app.is_remote_app())
     self.assert_docs_equal(remote_app, expected_remote_app)
     self.assert_docs_equal(normal_app, expected_normal_app)
 def for_domain(cls, domain, include_parent_properties=True):
     apps = get_apps_in_domain(domain, include_remote=False)
     return cls(domain,
                apps,
                defaults=('name',),
                include_parent_properties=include_parent_properties,
                exclude_invalid_properties=False)
Example #5
0
    def _generate_sample_app(self, domain):
        name = 'Case Fixtures App'
        for app in get_apps_in_domain(domain):
            if app.name == name:
                return False

        factory = AppFactory(domain, name)
        factory.app.comment = "App auto generated with ./manage.py create_case_fixtures"
        adult, adult_form = factory.new_basic_module('adult', 'adult')
        child, child_form = factory.new_basic_module('child', 'child')
        factory.form_opens_case(child_form,
                                'child',
                                is_subcase=True,
                                parent_tag='parent')
        car, car_form = factory.new_basic_module('car', 'car')
        factory.form_opens_case(car_form,
                                'car',
                                is_subcase=True,
                                parent_tag='car')
        maintenance_record, maintenance_record_form = factory.new_basic_module(
            'maintenance_record', 'maintenance_record')
        factory.form_opens_case(maintenance_record_form,
                                'maintenance_record',
                                is_subcase=True,
                                parent_tag='maintenance_record_of_car',
                                is_extension=True)

        factory.app.save()
        return True
Example #6
0
    def __call__(self, user, version, last_sync=None):
        """
        Generates a report fixture for mobile that can be used by a report module
        """
        if not toggles.MOBILE_UCR.enabled(user.domain):
            return []

        report_configs = [
            report_config for app in get_apps_in_domain(user.domain)
            if isinstance(app, Application)
            # TODO: pass app_id to reduce size of fixture
            for module in app.modules if isinstance(module, ReportModule)
            for report_config in module.report_configs
        ]
        if not report_configs:
            return []

        root = ElementTree.Element('fixture', attrib={'id': self.id})
        reports_elem = ElementTree.Element('reports')
        for report_config in report_configs:
            report = ReportConfiguration.get(report_config.report_id)
            try:
                reports_elem.append(
                    self._report_to_fixture(report, report_config.filters,
                                            user))
            except UserReportsError:
                pass
        root.append(reports_elem)
        return [root]
Example #7
0
def get_apps_modules(domain, current_app_id=None, current_module_id=None, app_doc_types=('Application',)):
    """
    Returns a domain's Applications and their modules.

    If current_app_id and current_module_id are given, "is_current" is
    set to True for them. The interface uses this to select the current
    app and module by default.

    Linked and remote apps are omitted. Use the app_doc_types parameter
    to change this behaviour. (Deleted apps are not returned because the
    underlying Couch view doesn't include them.)
    """
    return [
        {
            'app_id': app.id,
            'name': app.name,
            'is_current': app.id == current_app_id,
            'modules': [{
                'module_id': module.id,
                'name': clean_trans(module.name, app.langs),
                'is_current': module.unique_id == current_module_id,
            } for module in app.get_modules()]
        }
        for app in get_apps_in_domain(domain)
        # No linked, deleted or remote apps. (Use app.doc_type not
        # app.get_doc_type() so that the suffix isn't dropped.)
        if app.doc_type in app_doc_types
    ]
Example #8
0
        def _inner(request, domain, *args, **kwargs):
            if toggles.DISABLE_WEB_APPS.enabled_for_request(request):

                apps_in_domain = get_apps_in_domain(domain)
                if (len(apps_in_domain) == 1):
                    app_or_domain_name = apps_in_domain[0].name
                else:
                    app_or_domain_name = domain

                context = {
                    "app_or_domain_name":
                    app_or_domain_name,
                    "is_superuser":
                    hasattr(request, "couch_user")
                    and request.couch_user.is_superuser
                }
                return render(request, "cloudcare/web_apps_disabled.html",
                              context)
            if hasattr(request, "couch_user"):
                if request.couch_user.is_web_user():
                    return require_permission(
                        Permissions.access_web_apps)(view_func)(request,
                                                                domain, *args,
                                                                **kwargs)
                else:
                    assert request.couch_user.is_commcare_user(), \
                        "user was neither a web user or a commcare user!"
                    return login_and_domain_required(view_func)(request,
                                                                domain, *args,
                                                                **kwargs)
            return login_and_domain_required(view_func)(request, domain, *args,
                                                        **kwargs)
 def handle(self, *args, **options):
     toggle_map = dict([(t.slug, t) for t in all_toggles()])
     domains = [row['key'] for row in Domain.get_all(include_docs=False)]
     for domain in domains:
         if toggle_map['rich_text'].enabled(domain) or toggle_map['experimental_ui'].enabled(domain):
             #logger.info('migrating domain {}'.format(domain))
             apps = get_apps_in_domain(domain, include_remote=False)
             for app in apps:
                 app_dirty = False
                 builder = ParentCasePropertyBuilder(app)
                 relationships = builder.get_parent_type_map(app.get_case_types(), allow_multiple_parents=True)
                 for module in app.modules:
                     for form in module.forms:
                         if form.doc_type == 'Form' and form.requires_case():
                             #logger.info('migrating form {}'.format(form.name.get('en', form.name)))
                             base_case_type = form.get_module().case_type
                             self._replace_in_form(form, relationships, base_case_type, 0)
                             prefixes = re.findall(r'#case/\w+/', form.source)
                             prefixes = set(prefixes)
                             for p in prefixes:
                                 if p != "#case/parent/" and p != "#case/grandparent/":
                                     self._form_error(form, "Unknown prefix remaining: {}".format(p))
                             if options['save']:
                                 try:
                                     save_xform(form.get_app(), form, form.source)
                                     app_dirty = True
                                 except:
                                     self._form_error(form, "Form xml invalid")
                 if app_dirty:
                     app.save()
     logger.info('done with cmitfb_migrate_syntax')
 def test_get_apps_in_domain_exclude_remote(self):
     apps = get_apps_in_domain(self.domain, include_remote=False)
     self.assertEqual(len(apps), 1)
     normal_app, = apps
     expected_normal_app, _ = sorted(self.apps,
                                     key=lambda app: app.is_remote_app())
     self.assert_docs_equal(normal_app, expected_normal_app)
Example #11
0
    def __call__(self, user, version, last_sync=None):
        """
        Generates a report fixture for mobile that can be used by a report module
        """
        if not toggles.MOBILE_UCR.enabled(user.domain):
            return []

        report_configs = [
            report_config
            for app in get_apps_in_domain(user.domain) if isinstance(app, Application)
            # TODO: pass app_id to reduce size of fixture
            for module in app.modules if isinstance(module, ReportModule)
            for report_config in module.report_configs
        ]
        if not report_configs:
            return []

        root = ElementTree.Element('fixture', attrib={'id': self.id})
        reports_elem = ElementTree.Element(
            'reports',
            attrib={
                'last_sync': datetime.utcnow().isoformat(),
            },
        )
        for report_config in report_configs:
            try:
                reports_elem.append(self._report_config_to_fixture(report_config, user))
            except UserReportsError:
                pass
            except Exception as err:
                logging.exception('Error generating report fixture: {}'.format(err))
        root.append(reports_elem)
        return [root]
Example #12
0
def get_apps_modules(domain, current_app_id=None, current_module_id=None, app_doc_types=('Application',)):
    """
    Returns a domain's Applications and their modules.

    If current_app_id and current_module_id are given, "is_current" is
    set to True for them. The interface uses this to select the current
    app and module by default.

    Linked and remote apps are omitted. Use the app_doc_types parameter
    to change this behaviour. (Deleted apps are not returned because the
    underlying Couch view doesn't include them.)
    """
    return [
        {
            'app_id': app.id,
            'name': app.name,
            'is_current': app.id == current_app_id,
            'modules': [{
                'module_id': module.id,
                'name': clean_trans(module.name, app.langs),
                'is_current': module.unique_id == current_module_id,
            } for module in app.modules]
        }
        for app in get_apps_in_domain(domain)
        # No linked, deleted or remote apps. (Use app.doc_type not
        # app.get_doc_type() so that the suffix isn't dropped.)
        if app.doc_type in app_doc_types
    ]
Example #13
0
 def test_get_apps_in_domain(self):
     apps = get_apps_in_domain(self.domain)
     self.assertEqual(len(apps), 2)
     normal_app, remote_app = sorted(apps, key=lambda app: app.is_remote_app())
     expected_normal_app, expected_remote_app = sorted(self.apps, key=lambda app: app.is_remote_app())
     self.assert_docs_equal(remote_app, expected_remote_app)
     self.assert_docs_equal(normal_app, expected_normal_app)
Example #14
0
def multimedia_ajax(request, domain, app_id):
    app = get_app(domain, app_id)
    if not is_remote_app(app):
        try:
            multimedia_state = app.check_media_state()
        except ReportConfigurationNotFoundError:
            return JsonResponse(
                {"message": _("One of the Report menus is misconfigured, please try again after they are fixed")},
                status=500)

        context = {
            'multimedia_state': multimedia_state,
            'domain': domain,
            'app': app,
            'is_linked_app': is_linked_app(app),
        }

        if toggles.MULTI_MASTER_LINKED_DOMAINS.enabled_for_request(request):
            missing_paths = {p for p in app.all_media_paths() if p not in app.multimedia_map}
            import_apps = get_apps_in_domain(domain, include_remote=False)
            import_app_counts = {
                a.id: len(missing_paths.intersection(a.multimedia_map.keys()))
                for a in import_apps
            }
            import_apps = [a for a in import_apps if import_app_counts[a.id]]
            context.update({
                'import_apps': import_apps,
                'import_app_counts': import_app_counts,
            })

        return render(request, "app_manager/partials/settings/multimedia_ajax.html", context)
    else:
        raise Http404()
Example #15
0
    def _release_app(self, domain_link, model, user, build_and_release=False):
        if toggles.MULTI_MASTER_LINKED_DOMAINS.enabled(
                domain_link.linked_domain):
            return self._error_tuple(_("Multi master flag is in use"))

        app_id = model['detail']['app_id']
        found = False
        error_prefix = ""
        try:
            for linked_app in get_apps_in_domain(domain_link.linked_domain,
                                                 include_remote=False):
                if is_linked_app(
                        linked_app) and linked_app.family_id == app_id:
                    found = True
                    app = update_linked_app(linked_app, app_id, user.user_id)

            if not found:
                return self._error_tuple(_("Could not find app"))

            if build_and_release:
                error_prefix = _("Updated app but did not build or release: ")
                build = app.make_build()
                build.is_released = True
                build.save(increment_version=False)
        except Exception as e:  # intentionally broad
            return self._error_tuple(error_prefix + str(e))
Example #16
0
    def __call__(self, user, version, last_sync=None, app=None):
        """
        Generates a report fixture for mobile that can be used by a report module
        """
        if not toggles.MOBILE_UCR.enabled(user.domain):
            return []

        apps = [app] if app else (a for a in get_apps_in_domain(user.domain, include_remote=False))
        report_configs = [
            report_config
            for app_ in apps
            for module in app_.modules if isinstance(module, ReportModule)
            for report_config in module.report_configs
        ]
        if not report_configs:
            return []

        root = E.fixture(id=self.id)
        reports_elem = E.reports(last_sync=datetime.utcnow().isoformat())
        for report_config in report_configs:
            try:
                reports_elem.append(self._report_config_to_fixture(report_config, user))
            except BadReportConfigurationError as err:
                logging.exception('Error generating report fixture: {}'.format(err))
                continue
            except UserReportsError:
                if settings.UNIT_TESTING or settings.DEBUG:
                    raise
            except Exception as err:
                logging.exception('Error generating report fixture: {}'.format(err))
                if settings.UNIT_TESTING or settings.DEBUG:
                    raise
        root.append(reports_elem)
        return [root]
 def handle(self, **options):
     logger.setLevel('DEBUG')
     domains = [row['key'] for row in Domain.get_all(include_docs=False)]
     settings = {
         s['id']: s['default']
         for s in get_custom_commcare_settings() if 'default' in s
     }
     deviant_counts = {id: 0 for id in settings.keys()}
     app_count = 0
     for domain in domains:
         for app in get_apps_in_domain(domain, include_remote=False):
             #logger.info("looking at app {}".format(app.id))
             if ('properties' in app.profile):
                 app_count = app_count + 1
                 for id, default in six.iteritems(settings):
                     if (id not in app.profile['properties']):
                         #logger.info("{}: not found".format(id))
                         pass
                     elif (app.profile['properties'][id] != default):
                         #logger.info("{}: {} != {}".format(id, app.profile['properties'][id], default))
                         deviant_counts[id] = deviant_counts[id] + 1
                     else:
                         #logger.info("{}: {} == {}".format(id, app.profile['properties'][id], default))
                         pass
     for id, count in six.iteritems(deviant_counts):
         logger.info("{}\t{}".format(count, id))
     logger.info(
         'done with audit_app_profile_properties, examined {} apps in {} domains'
         .format(app_count, len(domains)))
Example #18
0
 def for_domain(cls, domain, include_parent_properties=True):
     apps = get_apps_in_domain(domain, include_remote=False)
     return cls(domain,
                apps,
                defaults=('name',),
                include_parent_properties=include_parent_properties,
                exclude_invalid_properties=False)
Example #19
0
    def __call__(self, user, version, last_sync=None):
        """
        Generates a report fixture for mobile that can be used by a report module
        """
        if not toggles.MOBILE_UCR.enabled(user.domain):
            return []

        report_configs = [
            report_config
            for app in get_apps_in_domain(user.domain) if isinstance(app, Application)
            # TODO: pass app_id to reduce size of fixture
            for module in app.modules if isinstance(module, ReportModule)
            for report_config in module.report_configs
        ]
        if not report_configs:
            return []

        root = ElementTree.Element('fixture', attrib={'id': self.id})
        reports_elem = ElementTree.Element('reports')
        for report_config in report_configs:
            report = ReportConfiguration.get(report_config.report_id)
            try:
                reports_elem.append(self._report_to_fixture(report, report_config.filters, user))
            except UserReportsError:
                pass
        root.append(reports_elem)
        return [root]
Example #20
0
def get_linked_apps_for_domain(domain):
    linked_apps = []
    apps = get_apps_in_domain(domain, include_remote=False)
    for app in apps:
        if is_linked_app(app):
            linked_apps.append(app)

    return linked_apps
Example #21
0
 def app_to_module_options(self):
     return {
         app._id: [{
             'text': trans(module.name, app.langs),
             'value': module.unique_id,
         } for module in app.modules]
         for app in get_apps_in_domain(self.domain)
     }
Example #22
0
 def module_to_form_options(self):
     return {
         module.unique_id: [
             {"text": trans(form.name, app.langs), "value": form.unique_id} for form in module.get_forms()
         ]
         for app in get_apps_in_domain(self.domain)
         for module in app.modules
     }
Example #23
0
 def app_to_module_options(self):
     return {
         app._id: [{
             'text': trans(module.name, app.langs),
             'value': module.unique_id,
         } for module in app.modules]
         for app in get_apps_in_domain(self.domain)
     }
Example #24
0
 def app_to_case_type_options(self):
     return {
         app._id: [
             {"text": case_type, "value": case_type}
             for case_type in set(module.case_type for module in app.modules if module.case_type)
         ]
         for app in get_apps_in_domain(self.domain)
     }
Example #25
0
def confirm_domain(request, guid=None):
    error = None
    # Did we get a guid?
    if guid is None:
        error = _('An account activation key was not provided.  If you think this '
                  'is an error, please contact the system administrator.')

    # Does guid exist in the system?
    else:
        req = RegistrationRequest.get_by_guid(guid)
        if not req:
            error = _('The account activation key "%s" provided is invalid. If you '
                      'think this is an error, please contact the system '
                      'administrator.') % guid

    if error is not None:
        context = {
            'message_body': error,
            'current_page': {'page_name': 'Account Not Activated'},
        }
        return render(request, 'registration/confirmation_error.html', context)

    requested_domain = Domain.get_by_name(req.domain)

    # Has guid already been confirmed?
    if requested_domain.is_active:
        assert(req.confirm_time is not None and req.confirm_ip is not None)
        messages.success(request, 'Your account %s has already been activated. '
            'No further validation is required.' % req.new_user_username)
        return HttpResponseRedirect(reverse("dashboard_default", args=[requested_domain]))

    # Set confirm time and IP; activate domain and new user who is in the
    req.confirm_time = datetime.utcnow()
    req.confirm_ip = get_ip(request)
    req.save()
    requested_domain.is_active = True
    requested_domain.save()
    requesting_user = WebUser.get_by_username(req.new_user_username)

    send_new_request_update_email(requesting_user, get_ip(request), requested_domain.name, is_confirming=True)

    messages.success(request,
            'Your account has been successfully activated.  Thank you for taking '
            'the time to confirm your email address: %s.'
        % (requesting_user.username))
    track_workflow(requesting_user.email, "Confirmed new project")
    track_confirmed_account_on_hubspot.delay(requesting_user)
    url = reverse("dashboard_default", args=[requested_domain])

    # If user already created an app (via prelogin demo), send them there
    apps = get_apps_in_domain(requested_domain.name, include_remote=False)
    if len(apps) == 1:
        app = apps[0]
        if len(app.modules) == 1 and len(app.modules[0].forms) == 1:
            url = reverse('form_source', args=[requested_domain.name, app.id, 0, 0])

    return HttpResponseRedirect(url)
Example #26
0
    def handle(self, itext_type, log_file, **options):

        hdlr = logging.FileHandler(log_file)
        formatter = logging.Formatter('%(message)s')
        hdlr.setFormatter(formatter)
        logger.addHandler(hdlr)

        domains = [row['key'] for row in Domain.get_all(include_docs=False)]
        domain_data = {}
        errors = []
        for domain in domains:
            sys.stdout.write(".")
            app_data = {}
            for app in get_apps_in_domain(domain, include_remote=False):
                form_data = {}
                for module in app.get_modules():
                    for form in module.get_forms():
                        try:
                            xform = form.wrapped_xform()
                        except Exception:
                            errors.append(
                                ItextFetchError(domain, app.get_id,
                                                form.get_unique_id()))
                            continue
                        total_refs = len(
                            xform.media_references(form=itext_type))
                        if total_refs > 0:
                            form_data[form.unique_id] = total_refs
                if len(form_data) > 0:
                    app_data[app.get_id] = {
                        'data': form_data,
                        'total_refs': sum(form_data.values()),
                        'total_forms': len(form_data),
                    }
            if len(app_data) > 0:
                domain_data[domain] = {
                    'app_data':
                    app_data,
                    'total_apps':
                    len(app_data),
                    'total_refs':
                    sum([a['total_refs'] for a in app_data.values()]),
                    'total_forms':
                    sum([a['total_forms'] for a in app_data.values()]),
                }
        logger.info('DOMAINS USING "{}"'.format(itext_type))
        logger.info('domain name\t# apps\t# forms\t# references')
        for domain, data in domain_data.items():
            logger.info('{}\t{}\t{}\t{}'.format(domain, data['total_apps'],
                                                data['total_forms'],
                                                data['total_refs']))

            logger.info('\n\nERRORS')
        for error in errors:
            logger.info(
                'Error getting form {} in app {} from domain {}'.format(
                    error.form, error.app, error.domain))
Example #27
0
 def test_get_apps_in_domain_exclude_remote(self):
     apps = get_apps_in_domain(self.domain, include_remote=False)
     self.assertEqual(len(apps), 2)
     normal_app = list(filter(
         lambda app: not app.is_remote_app() and app.doc_type != 'LinkedApplication', apps
     ))[0]
     linked_app = list(filter(lambda app: app.doc_type == 'LinkedApplication', apps))[0]
     self.assert_docs_equal(self.normal_app, normal_app)
     self.assert_docs_equal(self.linked_app, linked_app)
Example #28
0
def _get_apps(restore_state):
    app_aware_sync_app = restore_state.params.app

    if app_aware_sync_app:
        apps = [app_aware_sync_app]
    else:
        apps = get_apps_in_domain(restore_state.domain, include_remote=False)

    return apps
Example #29
0
 def get_context_data(self, **kwargs):
     return {
         "has_apps": len(get_apps_in_domain(self.domain)) > 0,
         "domain": self.domain,
         "report": {
             "title": _("Create New Report")
         },
         "tiles": self.tiles,
     }
Example #30
0
 def get_context_data(self, **kwargs):
     return {
         "has_apps": len(get_apps_in_domain(self.domain)) > 0,
         "domain": self.domain,
         "report": {
             "title": _("Create New Report")
         },
         "tiles": self.tiles,
     }
Example #31
0
 def module_to_form_options(self):
     return {
         module.unique_id: [{
             'text': trans(form.name, app.langs),
             'value': form.unique_id,
         } for form in module.get_forms()]
         for app in get_apps_in_domain(self.domain)
         for module in app.modules
     }
Example #32
0
    def __init__(self, domain, *args, **kwargs):
        super(CreateFormExportForm, self).__init__(*args, **kwargs)
        apps = get_apps_in_domain(domain)
        self.fields['application'].choices = ([
            ('', _('Select Application...')),
        ] if len(apps) > 1 else []) + [(app._id, app.name) for app in apps]
        self.fields['module'].choices = [(module.unique_id, module.name)
                                         for app in apps
                                         if hasattr(app, 'modules')
                                         for module in app.modules]
        self.fields['form'].choices = [(form.get_unique_id(), form.name)
                                       for app in apps
                                       for form in app.get_forms()]

        self.helper = FormHelper()
        self.helper.form_id = "create-export-form"
        self.helper.form_class = "form-horizontal"

        self.helper.layout = crispy.Layout(
            crispy.Fieldset(
                _('Select Form'),
                crispy.Field(
                    'application',
                    data_bind='value: appId',
                ),
                crispy.Div(
                    crispy.Field(
                        'module',
                        data_bind=("options: moduleOptions, "
                                   "optionsText: 'text', "
                                   "optionsValue: 'value', "
                                   "value: moduleId"),
                    ),
                    data_bind="visible: appId",
                ),
                crispy.Div(
                    crispy.Field(
                        'form',
                        data_bind=("options: formOptions, "
                                   "optionsText: 'text', "
                                   "optionsValue: 'value', "
                                   "value: formId"),
                    ),
                    data_bind="visible: moduleId",
                ),
            ),
            crispy.Div(
                FormActions(
                    crispy.ButtonHolder(
                        crispy.Submit(
                            'create_export',
                            _('Next'),
                        ), ), ),
                data_bind="visible: formId",
            ),
        )
Example #33
0
 def app_to_case_type_options(self):
     return {
         app._id: [{
             'text': case_type,
             'value': case_type,
         } for case_type in set(
             module.case_type for module in app.modules if module.case_type
         )]
         for app in get_apps_in_domain(self.domain)
     }
Example #34
0
def _get_app_ids_by_form_unique_id(domain):
    apps = get_apps_in_domain(domain, include_remote=False)
    app_ids = {}
    for app in apps:
        for module in app.modules:
            for form in module.get_forms():
                if form.unique_id in app_ids:
                    raise AppManagerException("Could not identify app for form {}".format(form.unique_id))
                app_ids[form.unique_id] = app.get_id
    return app_ids
 def handle(self, *args, **options):
     toggle_map = dict([(t.slug, t) for t in all_toggles()])
     domains = [row['key'] for row in Domain.get_all(include_docs=False)]
     for domain in domains:
         if toggle_map['rich_text'].enabled(domain) or toggle_map['experimental_ui'].enabled(domain):
             logger.info('migrating domain {}'.format(domain))
             apps = get_apps_in_domain(domain, include_remote=False)
             for app in apps:
                 call_command('migrate_app_to_cmitfb', app.id)
             toggle_map['rich_text'].set(domain, False, NAMESPACE_DOMAIN)
             toggle_map['experimental_ui'].set(domain, False, NAMESPACE_DOMAIN)
     logger.info('done with cmitfb_toggle_to_flag')
    def handle(self, itext_type, log_file, **options):

        hdlr = logging.FileHandler(log_file)
        formatter = logging.Formatter('%(message)s')
        hdlr.setFormatter(formatter)
        logger.addHandler(hdlr)

        domains = [row['key'] for row in Domain.get_all(include_docs=False)]
        domain_data = {}
        errors = []
        for domain in domains:
            sys.stdout.write(".")
            app_data = {}
            for app in get_apps_in_domain(domain, include_remote=False):
                form_data = {}
                for module in app.get_modules():
                    for form in module.get_forms():
                        try:
                            xform = form.wrapped_xform()
                        except Exception:
                            errors.append(ItextFetchError(domain, app.get_id, form.get_unique_id()))
                            continue
                        total_refs = len(xform.media_references(form=itext_type))
                        if total_refs > 0:
                            form_data[form.unique_id] = total_refs
                if len(form_data) > 0:
                    app_data[app.get_id] = {
                        'data': form_data,
                        'total_refs': sum(form_data.values()),
                        'total_forms': len(form_data),
                    }
            if len(app_data) > 0:
                domain_data[domain] = {
                    'app_data': app_data,
                    'total_apps': len(app_data),
                    'total_refs': sum([a['total_refs'] for a in app_data.values()]),
                    'total_forms': sum([a['total_forms'] for a in app_data.values()]),
                }
        logger.info('DOMAINS USING "{}"'.format(itext_type))
        logger.info('domain name\t# apps\t# forms\t# references')
        for domain, data in domain_data.items():
            logger.info('{}\t{}\t{}\t{}'.format(
                domain,
                data['total_apps'],
                data['total_forms'],
                data['total_refs']
            ))

            logger.info('\n\nERRORS')
        for error in errors:
            logger.info('Error getting form {} in app {} from domain {}'.format(
                error.form, error.app, error.domain
            ))
Example #37
0
    def test_get_apps_in_domain(self):
        apps = get_apps_in_domain(self.domain)
        self.assertEqual(len(apps), 3)
        normal_app = list(filter(
            lambda app: not app.is_remote_app() and app.doc_type != 'LinkedApplication', apps
        ))[0]
        linked_app = list(filter(lambda app: app.doc_type == 'LinkedApplication', apps))[0]
        remote_app = list(filter(lambda app: app.is_remote_app(), apps))[0]

        self.assert_docs_equal(remote_app, self.remote_app)
        self.assert_docs_equal(normal_app, self.normal_app)
        self.assert_docs_equal(linked_app, self.linked_app)
def turn_on_secure_submissions_for_all_apps(domain):
    for app in get_apps_in_domain(domain):
        save = False
        if app.application_version == APP_V1:
            continue
        if not app.build_version or app.build_version < LooseVersion('2.8'):
            app.build_spec = get_default_build_spec()
            save = True
        if not app.secure_submissions:
            app.secure_submissions = True
            save = True
        if save:
            app.save()
Example #39
0
    def page_context(self):
        measures_by_app_id = defaultdict(list)
        for measure in (MobileRecoveryMeasure.objects.filter(
                domain=self.domain).order_by('pk')):
            measures_by_app_id[measure.app_id].append(measure)

        all_apps = get_apps_in_domain(self.domain, include_remote=False)
        return {
            'measures_by_app':
            sorted(
                ((app.name, measures_by_app_id[app._id]) for app in all_apps),
                key=lambda x: (-1 * len(x[1]), x[0])),
        }
Example #40
0
    def __init__(self, domain, *args, **kwargs):
        super(CreateCaseExportForm, self).__init__(*args, **kwargs)
        apps = get_apps_in_domain(domain)
        self.fields['application'].choices = ([
            ('', _('Select Application...')),
        ] if len(apps) > 1 else []) + [
            (app._id, app.name) for app in apps
        ]
        self.fields['case_type'].choices = [
            (module.case_type, module.case_type)
            for app in apps if hasattr(app, 'modules')
            for module in app.modules
            if module.case_type
        ]

        self.helper = FormHelper()
        self.helper.form_id = "create-export-form"
        self.helper.form_class = "form-horizontal"

        self.helper.layout = crispy.Layout(
            crispy.Fieldset(
                _('Select Case Type'),
                crispy.Field(
                    'application',
                    data_bind='value: appId',
                ),
                crispy.Div(
                    crispy.Field(
                        'case_type',
                        data_bind=(
                            "options: caseTypeOptions, "
                            "optionsText: 'text', "
                            "optionsValue: 'value', "
                            "value: case_type"
                        ),
                    ),
                    data_bind="visible: appId",
                ),
            ),
            crispy.Div(
                FormActions(
                    crispy.ButtonHolder(
                        crispy.Submit(
                            'create_export',
                            _('Next'),
                        ),
                    ),
                ),
                data_bind="visible: case_type",
            ),
        )
 def handle(self, *args, **options):
     toggle_map = dict([(t.slug, t) for t in all_toggles()])
     domains = [row['key'] for row in Domain.get_all(include_docs=False)]
     for domain in domains:
         if toggle_map['rich_text'].enabled(
                 domain) or toggle_map['experimental_ui'].enabled(domain):
             logger.info('migrating domain {}'.format(domain))
             apps = get_apps_in_domain(domain, include_remote=False)
             for app in apps:
                 call_command('migrate_app_to_cmitfb', app.id)
             toggle_map['rich_text'].set(domain, False, NAMESPACE_DOMAIN)
             toggle_map['experimental_ui'].set(domain, False,
                                               NAMESPACE_DOMAIN)
     logger.info('done with cmitfb_toggle_to_flag')
Example #42
0
    def page_context(self):
        measures_by_app_id = defaultdict(list)
        for measure in (MobileRecoveryMeasure.objects
                        .filter(domain=self.domain)
                        .order_by('pk')):
            measures_by_app_id[measure.app_id].append(measure)

        all_apps = get_apps_in_domain(self.domain, include_remote=False)
        return {
            'measures_by_app': sorted((
                (app.name, measures_by_app_id[app._id])
                for app in all_apps
            ), key=lambda x: (-1 * len(x[1]), x[0])),
        }
Example #43
0
    def _get_apps(self, restore_state, restore_user):
        app_aware_sync_app = restore_state.params.app

        if app_aware_sync_app:
            apps = [app_aware_sync_app]
        elif (
                toggles.ROLE_WEBAPPS_PERMISSIONS.enabled(restore_user.domain)
                and restore_state.params.device_id
                and "WebAppsLogin" in restore_state.params.device_id
        ):
            # Only sync reports for apps the user has access to if this is a restore from webapps
            role = restore_user.get_role(restore_user.domain)
            if role:
                allowed_app_ids = [app['_id'] for app in get_brief_apps_in_domain(restore_user.domain)
                                   if role.permissions.view_web_app(app)]
                apps = get_apps_by_id(restore_user.domain, allowed_app_ids)
            else:
                # If there is no role, allow access to all apps
                apps = get_apps_in_domain(restore_user.domain, include_remote=False)
        else:
            apps = get_apps_in_domain(restore_user.domain, include_remote=False)

        return apps
Example #44
0
def get_app_sources(domain):
    apps = get_apps_in_domain(domain, include_remote=False)
    return {
        app._id: {
            "name": app.name,
            "case": [{"text": t, "value": t} for t in app.get_case_types()],
            "form": [
                {
                    "text": u'{} / {}'.format(form.get_module().default_name(), form.default_name()),
                    "value": form.get_unique_id()
                } for form in app.get_forms()
            ]
        }
        for app in apps
    }
Example #45
0
def get_app_sources(domain):
    apps = get_apps_in_domain(domain, include_remote=False)
    return {
        app._id: {
            "name": app.name,
            "case": [{"text": t, "value": t} for t in app.get_case_types()],
            "form": [
                {
                    "text": u'{} / {}'.format(form.get_module().default_name(), form.default_name()),
                    "value": form.get_unique_id()
                } for form in app.get_forms()
            ]
        }
        for app in apps
    }
Example #46
0
def get_uuids_by_instance_id(domain):
    """
    map ReportAppConfig.uuids list to user-defined ReportAppConfig.instance_ids

    This is per-domain, since registering instances (like
    commcare_reports_fixture_instances) is per-domain
    """
    apps = get_apps_in_domain(domain)
    config_ids = defaultdict(list)
    for app in apps:
        if app.mobile_ucr_restore_version in (MOBILE_UCR_MIGRATING_TO_2, MOBILE_UCR_VERSION_2):
            for module in app.modules:
                if module.module_type == 'report':
                    for report_config in module.report_configs:
                        config_ids[report_config.instance_id].append(report_config.uuid)
    return config_ids
Example #47
0
def get_practice_mode_configured_apps(domain, mobile_worker_id=None):

    def is_set(app_or_profile):
        if mobile_worker_id:
            if app_or_profile.practice_mobile_worker_id == mobile_worker_id:
                return True
        else:
            if app_or_profile.practice_mobile_worker_id:
                return True

    def _practice_mode_configured(app):
        if is_set(app):
            return True
        return any(is_set(profile) for _, profile in app.build_profiles.items())

    return [app for app in get_apps_in_domain(domain) if _practice_mode_configured(app)]
Example #48
0
def get_practice_mode_configured_apps(domain, mobile_worker_id=None):

    def is_set(app_or_profile):
        if mobile_worker_id:
            if app_or_profile.practice_mobile_worker_id == mobile_worker_id:
                return True
        else:
            if app_or_profile.practice_mobile_worker_id:
                return True

    def _practice_mode_configured(app):
        if is_set(app):
            return True
        return any(is_set(profile) for profile in app.build_profiles.values())

    return [app for app in get_apps_in_domain(domain) if _practice_mode_configured(app)]
Example #49
0
    def copy_applications(self):
        from corehq.apps.app_manager.dbaccessors import get_apps_in_domain
        from corehq.apps.app_manager.models import ReportModule
        from corehq.apps.app_manager.models import import_app
        apps = get_apps_in_domain(self.existing_domain)
        for app in apps:
            for module in app.modules:
                if isinstance(module, ReportModule):
                    for config in module.report_configs:
                        config.report_id = self.report_map[config.report_id]

            if self.no_commmit:
                new_app = Application.from_source(app.export_json(dump_json=False), self.new_domain)
                new_app['_id'] = 'new-{}'.format(app._id)
            else:
                new_app = import_app(app.to_json(), self.new_domain)
            self.log_copy(app.doc_type, app._id, new_app._id)
Example #50
0
def get_referring_apps(domain, report_id):
    to_ret = []
    apps = get_apps_in_domain(domain)
    for app in apps:
        app_url = reverse('view_app', args=[domain, app.id])
        for module in app.get_report_modules():
            module_url = reverse('view_module',
                                 args=[domain, app.id, module.unique_id])
            for config in module.report_configs:
                if config.report_id == report_id:
                    to_ret.append({
                        "app_url": app_url,
                        "app_name": app.name,
                        "module_url": module_url,
                        "module_name": module.default_name()
                    })
    return to_ret
Example #51
0
 def page_context(self):
     apps = get_apps_in_domain(self.domain, include_remote=False)
     case_types = {t for app in apps for t in app.get_case_types() if t}
     current_values = CaseSearchConfig.objects.get_or_none(pk=self.domain)
     return {
         'case_types': sorted(list(case_types)),
         'values': {
             'enabled': current_values.enabled if current_values else False,
             'fuzzy_properties': {
                 fp.case_type: fp.properties for fp in current_values.fuzzy_properties.all()
             } if current_values else {},
             'ignore_patterns': [{
                 'case_type': rc.case_type,
                 'case_property': rc.case_property,
                 'regex': rc.regex
             } for rc in current_values.ignore_patterns.all()] if current_values else {}
         }
     }
 def handle(self, *args, **options):
     domains = [row['key'] for row in Domain.get_all(include_docs=False)]
     settings = {s['id']: s['default'] for s in get_custom_commcare_settings() if 'default' in s}
     deviant_counts = {id: 0 for id in settings.keys()}
     app_count = 0
     for domain in domains:
         for app in get_apps_in_domain(domain, include_remote=False):
             #logger.info("looking at app {}".format(app.id))
             if ('properties' in app.profile):
                 app_count = app_count + 1
                 for id, default in settings.iteritems():
                     if (id not in app.profile['properties']):
                         #logger.info("{}: not found".format(id))
                         pass
                     elif (app.profile['properties'][id] != default):
                         #logger.info("{}: {} != {}".format(id, app.profile['properties'][id], default))
                         deviant_counts[id] = deviant_counts[id] + 1
                     else:
                         #logger.info("{}: {} == {}".format(id, app.profile['properties'][id], default))
                         pass
     for id, count in deviant_counts.iteritems():
         logger.info("{}\t{}".format(count, id))
     logger.info('done with audit_app_profile_properties, examined {} apps in {} domains'.format(app_count, len(domains)))
Example #53
0
def purge_report_from_mobile_ucr(report_config):
    """
    Called when a report is deleted, this will remove any references to it in
    mobile UCR modules.
    """
    if not toggles.MOBILE_UCR.enabled(report_config.domain):
        return False

    did_purge_something = False
    for app in get_apps_in_domain(report_config.domain):
        save_app = False
        for module in app.modules:
            if module.module_type == 'report':
                valid_report_configs = [
                    app_config for app_config in module.report_configs
                    if app_config.report_id != report_config._id
                ]
                if len(valid_report_configs) != len(module.report_configs):
                    module.report_configs = valid_report_configs
                    save_app = True
        if save_app:
            app.save()
            did_purge_something = True
    return did_purge_something
Example #54
0
 def test_get_apps_in_domain_exclude_remote(self):
     apps = get_apps_in_domain(self.domain, include_remote=False)
     self.assertEqual(len(apps), 1)
     normal_app, = apps
     expected_normal_app, _ = sorted(self.apps, key=lambda app: app.is_remote_app())
     self.assert_docs_equal(normal_app, expected_normal_app)
Example #55
0
 def get_other_case_sharing_apps_in_domain(self):
     apps = get_apps_in_domain(self.app.domain, include_remote=False)
     return [a for a in apps if a.case_sharing and a.id != self.app.id]
Example #56
0
 def obj_get_list(self, bundle, domain, **kwargs):
     return get_apps_in_domain(domain, include_remote=False)
Example #57
0
 def apps(self):
     return get_apps_in_domain(self.domain, include_remote=False)
Example #58
0
 def get_other_case_sharing_apps_in_domain(self):
     from corehq.apps.app_manager.dbaccessors import get_apps_in_domain
     apps = get_apps_in_domain(self.app.domain, include_remote=False)
     return [a for a in apps if a.case_sharing and a.id != self.app.id]
Example #59
0
 def test_app_base(self):
     apps = get_apps_in_domain(self.domain)
     self.assertEqual(len(apps), 2)