Exemple #1
0
    def handle(self, *args, **options):
        domain, app_id = args

        username = self._get_required_option('username', options)
        target_domain = self._get_required_option('to_domain', options)

        name = options['to_name']
        url_base = options['url']
        password = options['password']
        if not password:
            password = getpass(
                "Please enter the password for '{}': ".format(username))

        url = reverse('app_source',
                      kwargs={
                          'domain': domain,
                          'app_id': app_id
                      })
        full_url = '{}{}'.format(url_base, url)
        resp = requests.get(full_url, auth=HTTPDigestAuth(username, password))
        if not resp.status_code == 200:
            return "Command Failed: {}: {}".format(resp.status_code, resp.text)

        app_source = resp.json()
        if not name:
            name = app_source['name']
        import_app(app_source, target_domain, {'name': name})
Exemple #2
0
    def copy_component(self, doc_type, id, new_domain_name, user=None):
        from corehq.apps.app_manager.models import import_app
        from corehq.apps.users.models import UserRole
        str_to_cls = {
            'UserRole': UserRole,
            }
        db = get_db()
        if doc_type in ('Application', 'RemoteApp'):
            new_doc = import_app(id, new_domain_name)
            new_doc.copy_history.append(id)
        else:
            cls = str_to_cls[doc_type]
            new_id = db.copy_doc(id)['id']

            new_doc = cls.get(new_id)
            for field in self._dirty_fields:
                if hasattr(new_doc, field):
                    delattr(new_doc, field)

            if hasattr(cls, '_meta_fields'):
                for field in cls._meta_fields:
                    if not field.startswith('_') and hasattr(new_doc, field):
                        delattr(new_doc, field)

            new_doc.domain = new_domain_name

        if self.is_snapshot and doc_type == 'Application':
            new_doc.prepare_multimedia_for_exchange()

        new_doc.save()
        return new_doc
Exemple #3
0
    def handle(self, domain, app_id, **options):
        username = self._get_required_option('username', options)
        target_domain = self._get_required_option('to_domain', options)

        name = options['to_name']
        url_base = options['url']
        password = options['password']
        headers = {}
        if not password:
            password = getpass("Please enter the password for '{}': ".format(username))

        if options['2fa']:
            otp = getpass("Please enter your 2FA token: ")
            headers['X-COMMCAREHQ-OTP'] = otp

        url = reverse('app_source', kwargs={'domain': domain, 'app_id': app_id})
        full_url = '{}{}'.format(url_base, url)
        print("Making request to: {}".format(full_url))
        resp = requests.get(full_url, auth=HTTPDigestAuth(username, password), headers=headers)
        if not resp.status_code == 200:
            return "Command Failed: {}: {}".format(resp.status_code, resp.text)

        app_source = resp.json()
        if not name:
            name = app_source['name']
        app = import_app(app_source, target_domain, {'name': name})
        return "Created app '{}' at /a/{}/apps/view/{}/".format(app.name, app.domain, app.id)
Exemple #4
0
    def handle(self, domain, app_id, **options):
        username = self._get_required_option('username', options)
        target_domain = self._get_required_option('to_domain', options)

        name = options['to_name']
        url_base = options['url']
        password = options['password']
        headers = {}
        if not password:
            password = getpass(
                "Please enter the password for '{}': ".format(username))

        if options['2fa']:
            otp = getpass("Please enter your 2FA token: ")
            headers['X-COMMCAREHQ-OTP'] = otp

        url = reverse('app_source',
                      kwargs={
                          'domain': domain,
                          'app_id': app_id
                      })
        full_url = '{}{}'.format(url_base, url)
        print("Making request to: {}".format(full_url))
        resp = requests.get(full_url,
                            auth=HTTPDigestAuth(username, password),
                            headers=headers)
        if not resp.status_code == 200:
            return "Command Failed: {}: {}".format(resp.status_code, resp.text)

        app_source = resp.json()
        if not name:
            name = app_source['name']
        app = import_app(app_source, target_domain, {'name': name})
        return "Created app '{}' at /a/{}/apps/view/{}/".format(
            app.name, app.domain, app.id)
    def handle(self, app_id, version, source_domain, target_domain, new_name, **options):
        if options['skip_dynamic_report_check']:
            message = """
                WARNING: You are skipping report-related safety checks.
                If your app uses mobile UCR and contains references to dynamic reports, it will break
                if those references do not use aliases to reference the correct report ids.
                Do you wish to proceed?
            """
            response = input('{} [y/N]'.format(message)).lower()
            if response != 'y':
                raise CommandError('abort')

        old_app = get_app_by_version(source_domain, app_id, version)
        new_app = import_app(old_app.to_json(), target_domain, source_properties={'name': new_name},
                             check_all_reports=not(options['skip_dynamic_report_check']))

        find_and_replace_report_ids(old_app, new_app)

        new_app.save()

        print("App succesfully copied, you can view it at\n{}".format(
            absolute_reverse('view_app', args=[target_domain, new_app.get_id])
        ))

        print("""
            Next, make a build of the new app and then run copy_icds_app_2_of_2
            for all linked apps that use this app as a master app.
        """)
Exemple #6
0
    def copy_component(self, doc_type, id, new_domain_name, user=None):
        from corehq.apps.app_manager.models import import_app
        from corehq.apps.users.models import UserRole
        str_to_cls = {
            'UserRole': UserRole,
            }
        db = get_db()
        if doc_type in ('Application', 'RemoteApp'):
            new_doc = import_app(id, new_domain_name)
            new_doc.copy_history.append(id)
        else:
            cls = str_to_cls[doc_type]
            new_id = db.copy_doc(id)['id']

            new_doc = cls.get(new_id)
            for field in self._dirty_fields:
                if hasattr(new_doc, field):
                    delattr(new_doc, field)

            if hasattr(cls, '_meta_fields'):
                for field in cls._meta_fields:
                    if not field.startswith('_') and hasattr(new_doc, field):
                        delattr(new_doc, field)

            new_doc.domain = new_domain_name

        if self.is_snapshot and doc_type == 'Application':
            new_doc.prepare_multimedia_for_exchange()

        new_doc.save()
        return new_doc
 def testImportApp(self):
     self.assertTrue(self.app._attachments)
     new_app = import_app(self.app.id, self.domain)
     self.assertEqual(set(new_app._attachments.keys()).intersection(self.app._attachments.keys()), set())
     new_forms = list(new_app.get_forms())
     old_forms = list(self.app.get_forms())
     for new_form, old_form in zip(new_forms, old_forms):
         self.assertEqual(new_form.source, old_form.source)
Exemple #8
0
 def _test_import_app(self, app_id_or_source):
     new_app = import_app(app_id_or_source, self.domain)
     self.assertEqual(set(new_app.blobs.keys()).intersection(self.app.blobs.keys()), set())
     new_forms = list(new_app.get_forms())
     old_forms = list(self.app.get_forms())
     for new_form, old_form in zip(new_forms, old_forms):
         self.assertEqual(new_form.source, old_form.source)
         self.assertNotEqual(new_form.unique_id, old_form.unique_id)
 def testImportApp(self):
     self.failUnless(self.app._attachments)
     new_app = import_app(self.app.id, self.domain)
     self.assertEqual(set(new_app._attachments.keys()).intersection(self.app._attachments.keys()), set())
     new_forms = list(new_app.get_forms())
     old_forms = list(self.app.get_forms())
     for new_form, old_form in zip(new_forms, old_forms):
         self.assertEqual(new_form.source, old_form.source)
 def _test_import_app(self, app_id_or_source):
     new_app = import_app(app_id_or_source, self.domain)
     self.assertEqual(set(new_app._attachments.keys()).intersection(self.app._attachments.keys()), set())
     new_forms = list(new_app.get_forms())
     old_forms = list(self.app.get_forms())
     for new_form, old_form in zip(new_forms, old_forms):
         self.assertEqual(new_form.source, old_form.source)
         self.assertNotEqual(new_form.unique_id, old_form.unique_id)
Exemple #11
0
 def load_app(self, filename, dirname=None):
     dirname = dirname or os.path.dirname(os.path.abspath(__file__))
     full_filename = "%s/%s" % (dirname, filename)
     with open(full_filename, "r") as f:
         app_source = f.read()
         app_source = json.loads(app_source)
     app = import_app(app_source, self.domain)
     self.apps.append(app)
     return app
Exemple #12
0
 def load_app(self, filename, dirname=None):
     dirname = dirname or os.path.dirname(os.path.abspath(__file__))
     full_filename = "%s/%s" % (dirname, filename)
     with open(full_filename, "r") as f:
         app_source = f.read()
         app_source = json.loads(app_source)
     app = import_app(app_source, self.domain)
     self.apps.append(app)
     return app
Exemple #13
0
def bug_report(req):
    report = dict([(key, req.POST.get(key, '')) for key in (
        'subject',
        'username',
        'domain',
        'url',
        'now',
        'when',
        'message',
        'app_id',
    )])

    report['datetime'] = datetime.utcnow()

    report['time_description'] = u'just now' if report['now'] else u'earlier: {when}'.format(**report)
    if report['app_id']:
        app = import_app(report['app_id'], BUG_REPORTS_DOMAIN)
        report['copy_url'] = "%s%s" % (get_url_base(), reverse('view_app', args=[BUG_REPORTS_DOMAIN, app.id]))
    else:
        report['copy_url'] = None

    subject = u'CCHQ Bug Report ({domain}): {subject}'.format(**report)
    message = (
        u"username: {username}\n"
        u"domain: {domain}\n"
        u"url: {url}\n"
        u"copy url: {copy_url}\n"
        u"datetime: {datetime}\n"
        u"error occured: {time_description}\n"
        u"Message:\n\n"
        u"{message}\n"
    ).format(**report)

    from django.core.mail.message import EmailMessage
    from django.core.mail import send_mail

    if req.POST.get('five-hundred-report'):
        message = "%s \n\n This messge was reported from a 500 error page! Please fix this ASAP (as if you wouldn't anyway)..." % message
    email = EmailMessage(
        subject,
        message,
        report['username'],
        settings.BUG_REPORT_RECIPIENTS,
        headers = {'Reply-To': report['username']}
    )
    email.send(fail_silently=False)


    if req.POST.get('five-hundred-report'):
        messages.success(req, "Your CommCare HQ Issue Report has been sent. We are working quickly to resolve this problem.")
        return HttpResponseRedirect(reverse('homepage'))

    return HttpResponse()
Exemple #14
0
    def copy_component(self, doc_type, id, new_domain_name, user=None):
        from corehq.apps.app_manager.models import import_app
        from corehq.apps.users.models import UserRole
        from corehq.apps.reminders.models import CaseReminderHandler
        from corehq.apps.fixtures.models import FixtureDataType, FixtureDataItem

        str_to_cls = {
            'UserRole': UserRole,
            'CaseReminderHandler': CaseReminderHandler,
            'FixtureDataType': FixtureDataType,
            'FixtureDataItem': FixtureDataItem,
        }
        db = get_db()
        if doc_type in ('Application', 'RemoteApp'):
            new_doc = import_app(id, new_domain_name)
            new_doc.copy_history.append(id)
            new_doc.case_sharing = False
            # when copying from app-docs that don't have
            # unique_id attribute on Modules
            new_doc.ensure_module_unique_ids(should_save=False)
        else:
            cls = str_to_cls[doc_type]

            if doc_type == 'CaseReminderHandler':
                cur_doc = cls.get(id)
                if not self.reminder_should_be_copied(cur_doc):
                    return None

            new_id = db.copy_doc(id)['id']

            new_doc = cls.get(new_id)

            for field in self._dirty_fields:
                if hasattr(new_doc, field):
                    delattr(new_doc, field)

            if hasattr(cls, '_meta_fields'):
                for field in cls._meta_fields:
                    if not field.startswith('_') and hasattr(new_doc, field):
                        delattr(new_doc, field)

            new_doc.domain = new_domain_name

            if doc_type == 'FixtureDataType':
                new_doc.copy_from = id
                new_doc.is_global = True

        if self.is_snapshot and doc_type == 'Application':
            new_doc.prepare_multimedia_for_exchange()

        new_doc.save()
        return new_doc
 def _test_import_app(self, app_id_or_source):
     new_app = import_app(app_id_or_source, self.domain)
     self.assertEqual(set(new_app.blobs.keys()).intersection(list(self.app.blobs.keys())), set())
     new_forms = list(new_app.get_forms())
     old_forms = list(self.app.get_forms())
     for new_form, old_form in zip(new_forms, old_forms):
         self.assertEqual(new_form.source, old_form.source)
         self.assertNotEqual(new_form.unique_id, old_form.unique_id)
     for new_module, old_module in zip(new_app.get_modules(), self.app.get_modules()):
         if isinstance(old_module, ReportModule):
             old_config_ids = {config.uuid for config in old_module.report_configs}
             new_config_ids = {config.uuid for config in new_module.report_configs}
             self.assertEqual(old_config_ids.intersection(new_config_ids), set())
Exemple #16
0
    def handle(self, *args, **options):
        domain, app_id = args

        username = self._get_required_option('username', options)
        target_domain = self._get_required_option('to_domain', options)

        name = options['to_name']
        url_base = options['url']
        password = options['password']
        if not password:
            password = getpass("Please enter the password for '{}': ".format(username))

        url = reverse('app_source', kwargs={'domain': domain, 'app_id': app_id})
        full_url = '{}{}'.format(url_base, url)
        resp = requests.get(full_url, auth=HTTPDigestAuth(username, password))
        if not resp.status_code == 200:
            return "Command Failed: {}: {}".format(resp.status_code, resp.text)

        app_source = resp.json()
        if not name:
            name = app_source['name']
        import_app(app_source, target_domain, {'name': name})
Exemple #17
0
 def _test_import_app(self, app_id_or_source):
     new_app = import_app(app_id_or_source, self.domain)
     self.assertEqual(set(new_app.blobs.keys()).intersection(list(self.app.blobs.keys())), set())
     new_forms = list(new_app.get_forms())
     old_forms = list(self.app.get_forms())
     for new_form, old_form in zip(new_forms, old_forms):
         self.assertEqual(new_form.source, old_form.source)
         self.assertNotEqual(new_form.unique_id, old_form.unique_id)
     for new_module, old_module in zip(new_app.get_modules(), self.app.get_modules()):
         if isinstance(old_module, ReportModule):
             old_config_ids = {config.uuid for config in old_module.report_configs}
             new_config_ids = {config.uuid for config in new_module.report_configs}
             self.assertEqual(old_config_ids.intersection(new_config_ids), set())
Exemple #18
0
    def copy_component(self, doc_type, id, new_domain_name, user=None):
        from corehq.apps.app_manager.models import import_app
        from corehq.apps.users.models import UserRole
        from corehq.apps.reminders.models import CaseReminderHandler
        from corehq.apps.fixtures.models import FixtureDataType, FixtureDataItem

        str_to_cls = {
            'UserRole': UserRole,
            'CaseReminderHandler': CaseReminderHandler,
            'FixtureDataType': FixtureDataType,
            'FixtureDataItem': FixtureDataItem,
        }
        if doc_type in ('Application', 'RemoteApp'):
            new_doc = import_app(id, new_domain_name)
            new_doc.copy_history.append(id)
            new_doc.case_sharing = False
            # when copying from app-docs that don't have
            # unique_id attribute on Modules
            new_doc.ensure_module_unique_ids(should_save=False)
        else:
            cls = str_to_cls[doc_type]
            db = cls.get_db()
            if doc_type == 'CaseReminderHandler':
                cur_doc = cls.get(id)
                if not self.reminder_should_be_copied(cur_doc):
                    return None

            new_id = db.copy_doc(id)['id']

            new_doc = cls.get(new_id)

            for field in self._dirty_fields:
                if hasattr(new_doc, field):
                    delattr(new_doc, field)

            if hasattr(cls, '_meta_fields'):
                for field in cls._meta_fields:
                    if not field.startswith('_') and hasattr(new_doc, field):
                        delattr(new_doc, field)

            new_doc.domain = new_domain_name

            if doc_type == 'FixtureDataType':
                new_doc.copy_from = id
                new_doc.is_global = True

        if self.is_snapshot and doc_type == 'Application':
            new_doc.prepare_multimedia_for_exchange()

        new_doc.save()
        return new_doc
Exemple #19
0
    def handle(self, app_id, version, source_domain, target_domain, new_name,
               **options):
        old_app = get_app_by_version(source_domain, app_id, version)
        new_app = import_app(old_app.to_json(),
                             target_domain,
                             source_properties={'name': new_name})

        old_to_new = get_old_to_new_config_ids(old_app, new_app)
        for form in new_app.get_forms():
            for old_id, new_id in old_to_new:
                form.source = form.source.replace(old_id, new_id)

        new_app.save()
        print("App succesfully copied, you can view it at\n{}".format(
            absolute_reverse('view_app', args=[target_domain,
                                               new_app.get_id])))
Exemple #20
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_commit:
                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)
    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)
Exemple #22
0
    def test_case_integration(self):
        # load the app
        with open(
                os.path.join(os.path.dirname(__file__), "data",
                             "app_with_cases.json")) as f:
            app_json = json.loads(f.read())
            app = import_app(app_json, self.domain)

        # the first form opens the case
        session, responses = start_session(self.domain, self.contact, app,
                                           app.get_module(0),
                                           app.get_module(0).get_form(0))

        [answer] = responses
        self.assertEqual("what's the case name?", answer)
        q_and_a(self, "some case", "thanks, you're done!", self.domain)

        def _get_case(session):
            session = XFormsSession.get(session.get_id)
            self.assertTrue(session.submission_id)
            instance = XFormInstance.get(session.submission_id)
            case_id = instance.xpath("form/case/@case_id")
            self.assertTrue(case_id)
            return CommCareCase.get(case_id)

        # check the case
        case = _get_case(session)
        self.assertEqual("some case", case.name)
        self.assertFalse(case.closed)
        self.assertFalse(hasattr(case, "feeling"))

        # the second form updates the case
        # NOTE: this currently fails for several reasons, the most
        # notable being that there needs to be a server running configured
        # to hit the test DB, and that there's no authentication built in
        session, responses = start_session(self.domain,
                                           self.contact,
                                           app,
                                           app.get_module(0),
                                           app.get_module(0).get_form(1),
                                           case_id=case.get_id)

        [answer] = responses
        self.assertEqual("how you feeling, some case?", answer)
        q_and_a(self, "groovy", "thanks, you're done!", self.domain)
    def test_multi_master_copy_master(self, *args):
        '''
        This tests that when a master app A is copied to A' and the linked app is pulled from A',
        the linked app's form unique ids remain consistent, and form and multimedia versions
        do NOT increment just because of the copy.
        '''
        self.delete_modules(self.master1)

        # Add single module and form, with image, to master, and pull linked app.
        master1_module = self.master1.add_module(
            Module.new_module('Module for master', None))
        master1_module.new_form('Form for master', 'en',
                                get_simple_form('Form-for-master'))
        image_path = 'jr://file/commcare/photo.jpg'
        self.master1.create_mapping(CommCareImage(_id='123'), image_path)
        self.master1.get_module(0).set_icon('en', image_path)
        self._make_master1_build(True)
        self.linked_app.family_id = self.master1.get_id
        self.linked_app.save()
        self._pull_linked_app(self.master1.get_id)
        build1 = self._make_linked_build()

        # Make a copy of master and pull it.
        master_copy = import_app(self.master1.get_id, self.master1.domain)
        self._make_build(master_copy, True)
        self._pull_linked_app(master_copy.get_id)
        build2 = self._make_linked_build()

        # Verify form XMLNS, form version, and multimedia version all match.
        # Verify that form unique ids in linked app match ids in master app pulled from
        form1 = build1.get_module(0).get_form(0)
        form2 = build2.get_module(0).get_form(0)
        self.assertEqual(form1.xmlns, form2.xmlns)
        self.assertEqual(form1.unique_id,
                         self.master1.get_module(0).get_form(0).unique_id)
        self.assertEqual(form2.unique_id,
                         master_copy.get_module(0).get_form(0).unique_id)
        self.assertNotEqual(build1.version, build2.version)
        self.assertEqual(form1.get_version(), build1.version)
        self.assertEqual(form2.get_version(), build2.version)
        map_item1 = build1.multimedia_map[image_path]
        map_item2 = build2.multimedia_map[image_path]
        self.assertEqual(map_item1.unique_id, map_item2.unique_id)
        self.assertEqual(map_item1.version, map_item2.version)
Exemple #24
0
    def handle(self, app_id, version, source_domain, target_domain, new_name,
               **options):
        old_app = get_app_by_version(source_domain, app_id, version)
        new_app = import_app(old_app.to_json(),
                             target_domain,
                             source_properties={'name': new_name})

        find_and_replace_report_ids(old_app, new_app)

        new_app.save()

        print("App succesfully copied, you can view it at\n{}".format(
            absolute_reverse('view_app', args=[target_domain,
                                               new_app.get_id])))

        print("""
            Next, make a build of the new app and then run copy_icds_app_2_of_2
            for all linked apps that use this app as a master app.
        """)
Exemple #25
0
    def testPruneAutoGeneratedBuilds(self, mock):
        # Build #1, manually generated
        app = import_app(self._yesno_source, self.domain)
        for module in app.modules:
            module.get_or_create_unique_id()
        app.save()
        build1 = app.make_build()
        build1.save()
        self.assertFalse(build1.is_auto_generated)

        # Build #2, auto-generated
        app.save()
        make_async_build(app, 'someone')
        build_ids = get_built_app_ids_for_app_id(app.domain, app.id)
        self.assertEqual(len(build_ids), 2)
        self.assertEqual(build_ids[0], build1.id)
        build2 = get_app(app.domain, build_ids[1])
        self.assertTrue(build2.is_auto_generated)

        # First prune: delete nothing because the auto build is the most recent
        prune_auto_generated_builds(self.domain, app.id)
        self.assertEqual(len(get_built_app_ids_for_app_id(app.domain, app.id)),
                         2)

        # Build #3, manually generated
        app.save()
        build3 = app.make_build()
        build3.save()

        # Release the auto-generated build and prune again, should still delete nothing
        build2.is_released = True
        build2.save()
        prune_auto_generated_builds(self.domain, app.id)
        self.assertEqual(len(get_built_app_ids_for_app_id(app.domain, app.id)),
                         3)

        # Un-release the auto-generated build and prune again, which should delete it
        build2.is_released = False
        build2.save()
        prune_auto_generated_builds(self.domain, app.id)
        build_ids = get_built_app_ids_for_app_id(app.domain, app.id)
        self.assertEqual(len(build_ids), 2)
        self.assertNotIn(build2.id, build_ids)
Exemple #26
0
 def test_case_integration(self):
     # load the app
     with open(os.path.join(os.path.dirname(__file__), "data", "app_with_cases.json")) as f:
         app_json = json.loads(f.read())
         app = import_app(app_json, self.domain)
         
     # the first form opens the case
     session, responses = start_session(self.domain, self.contact, app, 
                                        app.get_module(0), 
                                        app.get_module(0).get_form(0))
     
     [answer] = responses 
     self.assertEqual("what's the case name?", answer)
     q_and_a(self, "some case", "thanks, you're done!", self.domain)
     
     def _get_case(session):
         session = XFormsSession.get(session.get_id)
         self.assertTrue(session.submission_id)
         instance = XFormInstance.get(session.submission_id)
         case_id = instance.xpath("form/case/@case_id")
         self.assertTrue(case_id)
         return CommCareCase.get(case_id)
     
     # check the case
     case = _get_case(session)
     self.assertEqual("some case", case.name)
     self.assertFalse(case.closed)
     self.assertFalse(hasattr(case, "feeling"))
     
     # the second form updates the case
     # NOTE: this currently fails for several reasons, the most
     # notable being that there needs to be a server running configured
     # to hit the test DB, and that there's no authentication built in
     session, responses = start_session(self.domain, self.contact, app, 
                                        app.get_module(0), 
                                        app.get_module(0).get_form(1),
                                        case_id=case.get_id)
     
     [answer] = responses 
     self.assertEqual("how you feeling, some case?", answer)
     q_and_a(self, "groovy", "thanks, you're done!", self.domain)
     
     
Exemple #27
0
    def test_basic_form_playing(self):
        # load the app
        with open(
                os.path.join(os.path.dirname(__file__), "data",
                             "demo_app.json")) as f:
            app_json = json.loads(f.read())
            app = import_app(app_json, self.domain)

        # start form session
        session, responses = start_session(self.domain, self.contact, app,
                                           app.get_module(0),
                                           app.get_module(0).get_form(0))

        [answer] = responses
        self.assertEqual("what is your name?", answer)

        # check state of model
        self.assertEqual(session.start_time, session.modified_time)
        self.assertEqual("http://www.commcarehq.org/tests/smsforms",
                         session.form_xmlns)
        self.assertFalse(session.end_time)
        self.assertEqual(False, session.completed)
        self.assertEqual(self.domain, session.domain)
        self.assertEqual(self.contact.get_id, session.user_id)
        self.assertEqual(app.get_id, session.app_id)
        self.assertFalse(session.submission_id)

        # play through the form, checking answers
        q_and_a(self, "sms contact", "how old are you, sms contact?",
                self.domain)
        q_and_a(self, "29", "what is your gender? 1:male, 2:female",
                self.domain)
        q_and_a(self, "2", "thanks for submitting!", self.domain)

        # check the instance
        session = XFormsSession.get(session.get_id)
        self.assertTrue(session.submission_id)
        instance = XFormInstance.get(session.submission_id)
        self.assertEqual("sms contact", instance.xpath("form/name"))
        self.assertEqual("29", instance.xpath("form/age"))
        self.assertEqual("f", instance.xpath("form/gender"))
        self.assertEqual(self.domain, instance.domain)
    def testPruneAutoGeneratedBuilds(self, mock):
        # Build #1, manually generated
        app = import_app(self._yesno_source, self.domain)
        for module in app.modules:
            module.get_or_create_unique_id()
        app.save()
        build1 = app.make_build()
        build1.save()
        self.assertFalse(build1.is_auto_generated)

        # Build #2, auto-generated
        app.save()
        make_async_build(app, 'someone')
        build_ids = get_built_app_ids_for_app_id(app.domain, app.id)
        self.assertEqual(len(build_ids), 2)
        self.assertEqual(build_ids[0], build1.id)
        build2 = get_app(app.domain, build_ids[1])
        self.assertTrue(build2.is_auto_generated)

        # First prune: delete nothing because the auto build is the most recent
        prune_auto_generated_builds(self.domain, app.id)
        self.assertEqual(len(get_built_app_ids_for_app_id(app.domain, app.id)), 2)

        # Build #3, manually generated
        app.save()
        build3 = app.make_build()
        build3.save()

        # Release the auto-generated build and prune again, should still delete nothing
        build2.is_released = True
        build2.save()
        prune_auto_generated_builds(self.domain, app.id)
        self.assertEqual(len(get_built_app_ids_for_app_id(app.domain, app.id)), 3)

        # Un-release the auto-generated build and prune again, which should delete it
        build2.is_released = False
        build2.save()
        prune_auto_generated_builds(self.domain, app.id)
        build_ids = get_built_app_ids_for_app_id(app.domain, app.id)
        self.assertEqual(len(build_ids), 2)
        self.assertNotIn(build2.id, build_ids)
Exemple #29
0
 def test_basic_form_playing(self):
     # load the app
     with open(os.path.join(os.path.dirname(__file__), "data", "demo_app.json")) as f:
         app_json = json.loads(f.read())
         app = import_app(app_json, self.domain)
         
     # start form session
     session, responses = start_session(self.domain, self.contact, app, 
                                        app.get_module(0), 
                                        app.get_module(0).get_form(0))
     
     [answer] = responses 
     self.assertEqual("what is your name?", answer)
     
     # check state of model
     self.assertEqual(session.start_time, session.modified_time)
     self.assertEqual("http://www.commcarehq.org/tests/smsforms", session.form_xmlns)
     self.assertFalse(session.end_time)
     self.assertEqual(False, session.completed)
     self.assertEqual(self.domain, session.domain)
     self.assertEqual(self.contact.get_id, session.user_id)
     self.assertEqual(app.get_id, session.app_id)
     self.assertFalse(session.submission_id)
     
     # play through the form, checking answers
     q_and_a(self, "sms contact", "how old are you, sms contact?", self.domain)
     q_and_a(self, "29", "what is your gender? 1:male, 2:female", self.domain)
     q_and_a(self, "2", "thanks for submitting!", self.domain)
     
     # check the instance
     session = XFormsSession.get(session.get_id)
     self.assertTrue(session.submission_id)
     instance = XFormInstance.get(session.submission_id)
     self.assertEqual("sms contact", instance.xpath("form/name"))
     self.assertEqual("29", instance.xpath("form/age"))
     self.assertEqual("f", instance.xpath("form/gender"))
     self.assertEqual(self.domain, instance.domain)
    def test(self, mock):
        add_build(version='2.7.0', build_number=20655)
        domain = 'form-versioning-test'

        # set up inital app
        app = Application.new_app(domain, 'Foo')
        app.modules.append(Module(forms=[Form(), Form()]))
        app.build_spec = BuildSpec.from_string('2.7.0/latest')
        app.get_module(0).get_form(0).source = BLANK_TEMPLATE.format(xmlns='xmlns-0.0')
        app.get_module(0).get_form(1).source = BLANK_TEMPLATE.format(xmlns='xmlns-1')
        app.save()

        # make a build
        build1 = app.make_build()
        build1.save()

        # modify first form
        app.get_module(0).get_form(0).source = BLANK_TEMPLATE.format(xmlns='xmlns-0.1')
        app.save()

        # make second build
        build2 = app.make_build()
        build2.save()

        # modify first form
        app.get_module(0).get_form(0).source = BLANK_TEMPLATE.format(xmlns='xmlns-0.2')
        app.save()
        app.save()
        app.save()

        # make third build
        build3 = app.make_build()
        build3.save()

        self.assertEqual(self.get_form_versions(build1), [1, 1])
        self.assertEqual(self.get_form_versions(build2), [2, 1])
        self.assertEqual(self.get_form_versions(build3), [5, 1])

        # revert to build2
        app = app.make_reversion_to_copy(build2)
        app.save()

        # make reverted build
        build4 = app.make_build()
        build4.save()

        self.assertEqual(self.get_form_versions(build4), [6, 1])

        # copy app
        xxx_app = import_app(app.export_json(dump_json=False), domain)

        # make build of copy
        xxx_build1 = xxx_app.make_build()
        xxx_build1.save()

        # modify first form of copy app
        xxx_app.get_module(0).get_form(0).source = BLANK_TEMPLATE.format(xmlns='xmlns-0.xxx.0')
        xxx_app.save()

        # make second build of copy
        xxx_build2 = xxx_app.make_build()
        xxx_build2.save()

        self.assertEqual(self.get_form_versions(xxx_build1), [1, 1])
        self.assertEqual(self.get_form_versions(xxx_build2), [2, 1])
 def testBuildImportedApp(self):
     app = import_app(self._yesno_source, self.domain)
     copy = app.make_build()
     copy.save()
 def testBuildImportedApp(self):
     app = import_app(self._yesno_source, self.domain)
     copy = app.make_build()
     copy.save()
     self._check_has_build_files(copy)
     self._check_legacy_odk_files(copy)
Exemple #33
0
def bug_report(req):
    report = dict([(key, req.POST.get(key, ''))
                   for key in ('subject', 'username', 'domain', 'url',
                               'message', 'app_id', 'cc')])

    report['user_agent'] = req.META['HTTP_USER_AGENT']
    report['datetime'] = datetime.utcnow()

    if report['app_id']:
        app = import_app(report['app_id'], BUG_REPORTS_DOMAIN)
        report['copy_url'] = "%s%s" % (
            get_url_base(),
            reverse('view_app', args=[BUG_REPORTS_DOMAIN, app.id]))
    else:
        report['copy_url'] = None

    try:
        couch_user = CouchUser.get_by_username(report['username'])
        full_name = couch_user.full_name
        email = couch_user.get_email()
    except Exception:
        full_name = None
        email = None
    report['full_name'] = full_name
    report['email'] = email or report['username']

    matching_subscriptions = Subscription.objects.filter(
        is_active=True,
        subscriber__domain=report['domain'],
    )

    if len(matching_subscriptions) >= 1:
        report['software_plan'] = matching_subscriptions[0].plan_version
    else:
        report['software_plan'] = u'domain has no active subscription'

    subject = u'{subject} ({domain})'.format(**report)
    message = (u"username: {username}\n"
               u"full name: {full_name}\n"
               u"domain: {domain}\n"
               u"software plan: {software_plan}\n"
               u"url: {url}\n"
               u"copy url: {copy_url}\n"
               u"datetime: {datetime}\n"
               u"User Agent: {user_agent}\n"
               u"Message:\n\n"
               u"{message}\n").format(**report)
    cc = report['cc'].strip().split(",")
    cc = filter(None, cc)

    if full_name and not any([c in full_name for c in '<>"']):
        reply_to = u'"{full_name}" <{email}>'.format(**report)
    else:
        reply_to = report['email']

    # if the person looks like a commcare user, fogbugz can't reply
    # to their email, so just use the default
    if settings.HQ_ACCOUNT_ROOT in reply_to:
        reply_to = settings.SERVER_EMAIL

    if req.POST.get('five-hundred-report'):
        message = "%s \n\n This messge was reported from a 500 error page! Please fix this ASAP (as if you wouldn't anyway)..." % message
    email = EmailMessage(subject=subject,
                         body=message,
                         to=settings.BUG_REPORT_RECIPIENTS,
                         headers={'Reply-To': reply_to},
                         cc=cc)

    uploaded_file = req.FILES.get('report_issue')
    if uploaded_file:
        filename = uploaded_file.name
        content = uploaded_file.read()
        email.attach(filename=filename, content=content)

    # only fake the from email if it's an @dimagi.com account
    if re.search('@dimagi\.com$', report['username']):
        email.from_email = report['username']
    else:
        email.from_email = settings.CCHQ_BUG_REPORT_EMAIL

    email.send(fail_silently=False)

    if req.POST.get('five-hundred-report'):
        messages.success(
            req,
            "Your CommCare HQ Issue Report has been sent. We are working quickly to resolve this problem."
        )
        return HttpResponseRedirect(reverse('homepage'))

    return HttpResponse()
Exemple #34
0
 def testImportApp(self):
     self.failUnless(self.app._attachments)
     new_app = import_app(self.app.id, self.domain)
     self.failUnlessEqual(
         set(new_app._attachments.keys()).intersection(
             self.app._attachments.keys()), set())
Exemple #35
0
def bug_report(req):
    report = dict([(key, req.POST.get(key, '')) for key in (
        'subject',
        'username',
        'domain',
        'url',
        'message',
        'app_id',
        'cc'
    )])

    report['user_agent'] = req.META['HTTP_USER_AGENT']
    report['datetime'] = datetime.utcnow()

    if report['app_id']:
        app = import_app(report['app_id'], BUG_REPORTS_DOMAIN)
        report['copy_url'] = "%s%s" % (get_url_base(), reverse('view_app', args=[BUG_REPORTS_DOMAIN, app.id]))
    else:
        report['copy_url'] = None

    try:
        couch_user = CouchUser.get_by_username(report['username'])
        full_name = couch_user.full_name
        email = couch_user.get_email()
    except Exception:
        full_name = None
        email = None
    report['full_name'] = full_name
    report['email'] = email or report['username']

    matching_subscriptions = Subscription.objects.filter(
        is_active=True,
        subscriber__domain=report['domain'],
    )

    if len(matching_subscriptions) >= 1:
        report['software_plan'] = matching_subscriptions[0].plan_version
    else:
        report['software_plan'] = u'domain has no active subscription'

    subject = u'{subject} ({domain})'.format(**report)
    message = (
        u"username: {username}\n"
        u"full name: {full_name}\n"
        u"domain: {domain}\n"
        u"software plan: {software_plan}\n"
        u"url: {url}\n"
        u"copy url: {copy_url}\n"
        u"datetime: {datetime}\n"
        u"User Agent: {user_agent}\n"
        u"Message:\n\n"
        u"{message}\n"
        ).format(**report)
    cc = report['cc'].strip().split(",")
    cc = filter(None, cc)

    if full_name and not any([c in full_name for c in '<>"']):
        reply_to = u'"{full_name}" <{email}>'.format(**report)
    else:
        reply_to = report['email']

    # if the person looks like a commcare user, fogbugz can't reply
    # to their email, so just use the default
    if settings.HQ_ACCOUNT_ROOT in reply_to:
        reply_to = settings.SERVER_EMAIL

    if req.POST.get('five-hundred-report'):
        message = "%s \n\n This messge was reported from a 500 error page! Please fix this ASAP (as if you wouldn't anyway)..." % message
    email = EmailMessage(
        subject=subject,
        body=message,
        to=settings.BUG_REPORT_RECIPIENTS,
        headers={'Reply-To': reply_to},
        cc=cc
    )

    uploaded_file = req.FILES.get('report_issue')
    if uploaded_file:
        filename = uploaded_file.name
        content = uploaded_file.read()
        email.attach(filename=filename, content=content)

    # only fake the from email if it's an @dimagi.com account
    if re.search('@dimagi\.com$', report['username']):
        email.from_email = report['username']
    else:
        email.from_email = settings.CCHQ_BUG_REPORT_EMAIL

    email.send(fail_silently=False)

    if req.POST.get('five-hundred-report'):
        messages.success(req,
            "Your CommCare HQ Issue Report has been sent. We are working quickly to resolve this problem.")
        return HttpResponseRedirect(reverse('homepage'))

    return HttpResponse()
Exemple #36
0
 def testImportApp(self):
     self.failUnless(self.app._attachments)
     new_app = import_app(self.app.id, self.domain)
     self.failUnlessEqual(set(new_app._attachments.keys()).intersection(self.app._attachments.keys()), set())
    def test(self, mock):
        add_build(version='2.7.0', build_number=20655)
        domain = 'form-versioning-test'

        # set up inital app
        factory = AppFactory(domain, 'Foo')
        m0, f0 = factory.new_basic_module("bar", "bar")
        f0.source = get_simple_form(xmlns='xmlns-0.0')
        f1 = factory.new_form(m0)
        f1.source = get_simple_form(xmlns='xmlns-1')
        app = factory.app
        app.build_spec = BuildSpec.from_string('2.7.0/latest')
        app.save()

        # make a build
        build1 = app.make_build()
        build1.save()

        # modify first form
        app.get_module(0).get_form(0).source = get_simple_form(
            xmlns='xmlns-0.1')
        app.save()

        # make second build
        build2 = app.make_build()
        build2.save()

        # modify first form
        app.get_module(0).get_form(0).source = get_simple_form(
            xmlns='xmlns-0.2')
        app.save()
        app.save()
        app.save()

        # make third build
        build3 = app.make_build()
        build3.save()

        self.assertEqual(self.get_form_versions(build1), [1, 1])
        self.assertEqual(self.get_form_versions(build2), [2, 1])
        self.assertEqual(self.get_form_versions(build3), [5, 1])

        # revert to build2
        app = app.make_reversion_to_copy(build2)
        app.save()

        # make reverted build
        build4 = app.make_build()
        build4.save()

        self.assertEqual(self.get_form_versions(build4), [6, 1])

        # copy app
        xxx_app = import_app(app.export_json(dump_json=False), domain)

        # make build of copy
        xxx_build1 = xxx_app.make_build()
        xxx_build1.save()

        # modify first form of copy app
        xxx_app.get_module(0).get_form(0).source = get_simple_form(
            xmlns='xmlns-0.xxx.0')
        xxx_app.save()

        # make second build of copy
        xxx_build2 = xxx_app.make_build()
        xxx_build2.save()

        self.assertEqual(self.get_form_versions(xxx_build1), [1, 1])
        self.assertEqual(self.get_form_versions(xxx_build2), [2, 1])
Exemple #38
0
    def test(self, mock):
        add_build(version='2.7.0', build_number=20655)
        domain = 'form-versioning-test'

        # set up inital app
        app = Application.new_app(domain, 'Foo')
        app.modules.append(Module(forms=[Form(), Form()]))
        app.build_spec = BuildSpec.from_string('2.7.0/latest')
        app.get_module(0).get_form(0).source = BLANK_TEMPLATE.format(
            xmlns='xmlns-0.0')
        app.get_module(0).get_form(1).source = BLANK_TEMPLATE.format(
            xmlns='xmlns-1')
        app.save()

        # make a build
        build1 = app.make_build()
        build1.save()

        # modify first form
        app.get_module(0).get_form(0).source = BLANK_TEMPLATE.format(
            xmlns='xmlns-0.1')
        app.save()

        # make second build
        build2 = app.make_build()
        build2.save()

        # modify first form
        app.get_module(0).get_form(0).source = BLANK_TEMPLATE.format(
            xmlns='xmlns-0.2')
        app.save()
        app.save()
        app.save()

        # make third build
        build3 = app.make_build()
        build3.save()

        self.assertEqual(self.get_form_versions(build1), [1, 1])
        self.assertEqual(self.get_form_versions(build2), [2, 1])
        self.assertEqual(self.get_form_versions(build3), [5, 1])

        # revert to build2
        app = app.make_reversion_to_copy(build2)
        app.save()

        # make reverted build
        build4 = app.make_build()
        build4.save()

        self.assertEqual(self.get_form_versions(build4), [6, 1])

        # copy app
        xxx_app = import_app(app.export_json(dump_json=False), domain)

        # make build of copy
        xxx_build1 = xxx_app.make_build()
        xxx_build1.save()

        # modify first form of copy app
        xxx_app.get_module(0).get_form(0).source = BLANK_TEMPLATE.format(
            xmlns='xmlns-0.xxx.0')
        xxx_app.save()

        # make second build of copy
        xxx_build2 = xxx_app.make_build()
        xxx_build2.save()

        self.assertEqual(self.get_form_versions(xxx_build1), [1, 1])
        self.assertEqual(self.get_form_versions(xxx_build2), [2, 1])
Exemple #39
0
def bug_report(req):
    report = dict([(key, req.POST.get(key, '')) for key in (
        'subject',
        'username',
        'domain',
        'url',
        'now',
        'when',
        'message',
        'app_id',
    )])

    report['user_agent'] = req.META['HTTP_USER_AGENT']
    report['datetime'] = datetime.utcnow()

    report['time_description'] = u'just now' if report['now'] else u'earlier: {when}'.format(**report)
    if report['app_id']:
        app = import_app(report['app_id'], BUG_REPORTS_DOMAIN)
        report['copy_url'] = "%s%s" % (get_url_base(), reverse('view_app', args=[BUG_REPORTS_DOMAIN, app.id]))
    else:
        report['copy_url'] = None

    try:
        couch_user = CouchUser.get_by_username(report['username'])
        full_name = couch_user.full_name
    except Exception:
        full_name = None
    report['full_name'] = full_name

    subject = u'{subject} ({domain})'.format(**report)
    message = (
        u"username: {username}\n"
        u"full name: {full_name}\n"
        u"domain: {domain}\n"
        u"url: {url}\n"
        u"copy url: {copy_url}\n"
        u"datetime: {datetime}\n"
        u"error occured: {time_description}\n"
        u"User Agent: {user_agent}\n"
        u"Message:\n\n"
        u"{message}\n"
        ).format(**report)

    if full_name and not any([c in full_name for c in '<>"']):
        reply_to = '"{full_name}" <{username}>'.format(**report)
    else:
        reply_to = report['username']

    # if the person looks like a commcare user, fogbugz can't reply
    # to their email, so just use the default
    if settings.HQ_ACCOUNT_ROOT in reply_to:
        reply_to = settings.EMAIL_HOST_USER

    if req.POST.get('five-hundred-report'):
        message = "%s \n\n This messge was reported from a 500 error page! Please fix this ASAP (as if you wouldn't anyway)..." % message
    email = EmailMessage(
        subject,
        message,
        report['username'],
        settings.BUG_REPORT_RECIPIENTS,
        headers={'Reply-To': reply_to}
    )
    email.send(fail_silently=False)

    if req.POST.get('five-hundred-report'):
        messages.success(req,
            "Your CommCare HQ Issue Report has been sent. We are working quickly to resolve this problem.")
        return HttpResponseRedirect(reverse('homepage'))

    return HttpResponse()
Exemple #40
0
def bug_report(req):
    report = dict([(key, req.POST.get(key, '')) for key in (
        'subject',
        'username',
        'domain',
        'url',
        'message',
        'app_id',
    )])

    report['user_agent'] = req.META['HTTP_USER_AGENT']
    report['datetime'] = datetime.utcnow()

    if report['app_id']:
        app = import_app(report['app_id'], BUG_REPORTS_DOMAIN)
        report['copy_url'] = "%s%s" % (
            get_url_base(),
            reverse('view_app', args=[BUG_REPORTS_DOMAIN, app.id]))
    else:
        report['copy_url'] = None

    try:
        couch_user = CouchUser.get_by_username(report['username'])
        full_name = couch_user.full_name
    except Exception:
        full_name = None
    report['full_name'] = full_name

    subject = u'{subject} ({domain})'.format(**report)
    message = (u"username: {username}\n"
               u"full name: {full_name}\n"
               u"domain: {domain}\n"
               u"url: {url}\n"
               u"copy url: {copy_url}\n"
               u"datetime: {datetime}\n"
               u"User Agent: {user_agent}\n"
               u"Message:\n\n"
               u"{message}\n").format(**report)

    if full_name and not any([c in full_name for c in '<>"']):
        reply_to = '"{full_name}" <{username}>'.format(**report)
    else:
        reply_to = report['username']

    # if the person looks like a commcare user, fogbugz can't reply
    # to their email, so just use the default
    if settings.HQ_ACCOUNT_ROOT in reply_to:
        reply_to = settings.SERVER_EMAIL

    if req.POST.get('five-hundred-report'):
        message = "%s \n\n This messge was reported from a 500 error page! Please fix this ASAP (as if you wouldn't anyway)..." % message
    email = EmailMessage(subject=subject,
                         body=message,
                         to=settings.BUG_REPORT_RECIPIENTS,
                         headers={'Reply-To': reply_to})

    # only fake the from email if it's an @dimagi.com account
    if re.search('@dimagi\.com$', report['username']):
        email.from_email = report['username']

    email.send(fail_silently=False)

    if req.POST.get('five-hundred-report'):
        messages.success(
            req,
            "Your CommCare HQ Issue Report has been sent. We are working quickly to resolve this problem."
        )
        return HttpResponseRedirect(reverse('homepage'))

    return HttpResponse()
Exemple #41
0
 def testBuildImportedApp(self, mock):
     app = import_app(self._yesno_source, self.domain)
     copy = app.make_build()
     copy.save()
     self._check_has_build_files(copy, self.min_paths)
     self._check_legacy_odk_files(copy)