Esempio n. 1
0
    def media_resources(self):
        PREFIX = 'jr://file/'
        # you have to call remove_unused_mappings
        # before iterating through multimedia_map
        self.app.remove_unused_mappings()
        if self.app.multimedia_map is None:
            self.app.multimedia_map = {}
        for path, m in self.app.multimedia_map.items():
            unchanged_path = path
            if path.startswith(PREFIX):
                path = path[len(PREFIX):]
            else:
                raise MediaResourceError('%s does not start with jr://file/commcare/' % path)
            path, name = split_path(path)
            # CommCare assumes jr://media/,
            # which is an alias to jr://file/commcare/media/
            # so we need to replace 'jr://file/' with '../../'
            # (this is a hack)
            path = '../../' + path

            if not getattr(m, 'unique_id', None):
                # lazy migration for adding unique_id to map_item
                m.unique_id = HQMediaMapItem.gen_unique_id(m.multimedia_id, unchanged_path)

            yield MediaResource(
                id=self.id_strings.media_resource(m.unique_id, name),
                path=path,
                version=m.version,
                local=None,
                remote=get_url_base() + reverse(
                    'hqmedia_download',
                    args=[m.media_type, m.multimedia_id]
                ) + urllib.quote(name.encode('utf-8')) if name else name
            )
Esempio n. 2
0
def convert_media_path_to_hq_url(path, app):
    media = app.multimedia_map.get(path, None)
    if media is None:
        return None
    else:
        url_base = get_url_base()
        return url_base + HQMediaMapItem.format_match_map(path, media_type=media.media_type, media_id=media.multimedia_id)["url"] + "foo.wav"
Esempio n. 3
0
def convert_media_path_to_hq_url(path, app):
    media = app.multimedia_map.get(path, None)
    if media is None:
        return None
    else:
        url_base = get_url_base()
        return url_base + HQMediaMapItem.format_match_map(path, media_type=media.media_type, media_id=media.multimedia_id)["url"] + "foo.wav"
Esempio n. 4
0
    def media_resources(self):
        PREFIX = 'jr://file/'
        # you have to call remove_unused_mappings
        # before iterating through multimedia_map
        self.app.remove_unused_mappings()
        if self.app.multimedia_map is None:
            self.app.multimedia_map = {}
        for path, m in self.app.multimedia_map.items():
            unchanged_path = path
            if path.startswith(PREFIX):
                path = path[len(PREFIX):]
            else:
                raise MediaResourceError(
                    '%s does not start with jr://file/commcare/' % path)
            path, name = split_path(path)
            # CommCare assumes jr://media/,
            # which is an alias to jr://file/commcare/media/
            # so we need to replace 'jr://file/' with '../../'
            # (this is a hack)
            path = '../../' + path

            if not getattr(m, 'unique_id', None):
                # lazy migration for adding unique_id to map_item
                m.unique_id = HQMediaMapItem.gen_unique_id(
                    m.multimedia_id, unchanged_path)

            yield MediaResource(
                id=self.id_strings.media_resource(m.unique_id, name),
                path=path,
                version=m.version,
                local=None,
                remote=get_url_base() + reverse(
                    'hqmedia_download', args=[m.media_type, m.multimedia_id]) +
                urllib.quote(name.encode('utf-8')) if name else name)
Esempio n. 5
0
    def media_resources(self):
        PREFIX = 'jr://file/'
        # you have to call remove_unused_mappings
        # before iterating through multimedia_map
        self.app.remove_unused_mappings()
        if self.app.multimedia_map is None:
            self.app.multimedia_map = {}
        filter_multimedia = self.app.media_language_map and self.build_profile
        if filter_multimedia:
            media_list = []
            for lang in self.build_profile.langs:
                media_list += self.app.media_language_map[lang].media_refs
            requested_media = set(media_list)
        for path, m in sorted(list(self.app.multimedia_map.items()),
                              key=lambda item: item[0]):
            if filter_multimedia and m.form_media and path not in requested_media:
                continue
            unchanged_path = path
            if path.startswith(PREFIX):
                path = path[len(PREFIX):]
            else:
                raise MediaResourceError('%s does not start with %s' %
                                         (path, PREFIX))
            path, name = split_path(path)
            # CommCare assumes jr://media/,
            # which is an alias to jr://file/commcare/media/
            # so we need to replace 'jr://file/' with '../../'
            # (this is a hack)
            install_path = '../../{}'.format(path)
            local_path = './{}/{}'.format(path, name)

            if not getattr(m, 'unique_id', None):
                # lazy migration for adding unique_id to map_item
                m.unique_id = HQMediaMapItem.gen_unique_id(
                    m.multimedia_id, unchanged_path)

            descriptor = None
            if self.app.build_version and self.app.build_version >= LooseVersion(
                    '2.9'):
                type_mapping = {
                    "CommCareImage": "Image",
                    "CommCareAudio": "Audio",
                    "CommCareVideo": "Video",
                    "CommCareMultimedia": "Text"
                }
                descriptor = "{filetype} File: {name}".format(
                    filetype=type_mapping.get(m.media_type, "Media"),
                    name=name)

            yield MediaResource(
                id=id_strings.media_resource(m.unique_id, name),
                path=install_path,
                version=m.version,
                descriptor=descriptor,
                local=(local_path if self.app.enable_local_resource else None),
                remote=self.app.url_base + reverse(
                    'hqmedia_download', args=[m.media_type, m.multimedia_id]) +
                six.moves.urllib.parse.quote(name.encode('utf-8'))
                if name else name)
Esempio n. 6
0
    def match_zipped(self, zip, replace_existing_media=True, **kwargs):
        unknown_files = []
        matched_audio = []
        matched_images = []
        errors = []
        try:
            for index, path in enumerate(zip.namelist()):
                path = path.strip()
                filename = path.lower()
                basename = os.path.basename(path.lower())
                is_image = False

                if not basename:
                    continue

                if self.match_filename:
                    filename = basename

                media = None
                form_path = None
                data = None

                if filename in self.images:
                    form_path = self.image_paths[self.images.index(filename)]
                    data = zip.read(path)
                    media = CommCareImage.get_by_data(data)
                    is_image = True
                elif filename in self.audio:
                    form_path = self.audio_paths[self.audio.index(filename)]
                    data = zip.read(path)
                    media = CommCareAudio.get_by_data(data)
                else:
                    unknown_files.append(path)

                if media:
                    try:
                        media.attach_data(data,
                            upload_path=path,
                            username=self.username,
                            replace_attachment=replace_existing_media)
                        media.add_domain(self.domain, owner=True)
                        media.update_or_add_license(self.domain,
                                                    type=kwargs.get('license', ''),
                                                    author=kwargs.get('author', ''),
                                                    attribution_notes=kwargs.get('attribution_notes', ''))
                        self.app.create_mapping(media, form_path)
                        match_map = HQMediaMapItem.format_match_map(form_path, media.doc_type, media._id, path)
                        if is_image:
                            matched_images.append(match_map)
                        else:
                            matched_audio.append(match_map)
                    except Exception as e:
                        errors.append("%s (%s)" % (e, filename))
            zip.close()
        except Exception as e:
            logging.error(e)
            errors.append(e.message)

        return matched_images, matched_audio, unknown_files, errors
Esempio n. 7
0
    def media_resources(self):
        PREFIX = 'jr://file/'
        # you have to call remove_unused_mappings
        # before iterating through multimedia_map
        self.app.remove_unused_mappings()
        if self.app.multimedia_map is None:
            self.app.multimedia_map = {}
        filter_multimedia = self.app.media_language_map and self.build_profile
        if filter_multimedia:
            media_list = []
            for lang in self.build_profile.langs:
                media_list += self.app.media_language_map[lang].media_refs
            requested_media = set(media_list)
        for path, m in self.app.multimedia_map.items():
            if filter_multimedia and m.form_media and path not in requested_media:
                continue
            unchanged_path = path
            if path.startswith(PREFIX):
                path = path[len(PREFIX):]
            else:
                raise MediaResourceError('%s does not start with %s' % (path, PREFIX))
            path, name = split_path(path)
            # CommCare assumes jr://media/,
            # which is an alias to jr://file/commcare/media/
            # so we need to replace 'jr://file/' with '../../'
            # (this is a hack)
            install_path = u'../../{}'.format(path)
            local_path = u'./{}/{}'.format(path, name)

            if not getattr(m, 'unique_id', None):
                # lazy migration for adding unique_id to map_item
                m.unique_id = HQMediaMapItem.gen_unique_id(m.multimedia_id, unchanged_path)

            descriptor = None
            if self.app.build_version >= '2.9':
                type_mapping = {"CommCareImage": "Image",
                                "CommCareAudio": "Audio",
                                "CommCareVideo": "Video",
                                "CommCareMultimedia": "Text"}
                descriptor = u"{filetype} File: {name}".format(
                    filetype=type_mapping.get(m.media_type, "Media"),
                    name=name
                )

            yield MediaResource(
                id=id_strings.media_resource(m.unique_id, name),
                path=install_path,
                version=m.version,
                descriptor=descriptor,
                local=(local_path
                       if self.app.enable_local_resource
                       else None),
                remote=self.app.url_base + reverse(
                    'hqmedia_download',
                    args=[m.media_type, m.multimedia_id]
                ) + urllib.quote(name.encode('utf-8')) if name else name
            )
Esempio n. 8
0
    def media_resources(self):
        PREFIX = 'jr://file/'
        multimedia_map = self.app.multimedia_map_for_build(
            build_profile=self.build_profile, remove_unused=True)
        lazy_load_preference = self.app.profile.get(
            'properties', {}).get('lazy-load-video-files')
        for path, m in sorted(list(multimedia_map.items()),
                              key=lambda item: item[0]):
            unchanged_path = path
            if path.startswith(PREFIX):
                path = path[len(PREFIX):]
            else:
                raise MediaResourceError('%s does not start with %s' %
                                         (path, PREFIX))
            path, name = split_path(path)
            # CommCare assumes jr://media/,
            # which is an alias to jr://file/commcare/media/
            # so we need to replace 'jr://file/' with '../../'
            # (this is a hack)
            install_path = '../../{}'.format(path)
            local_path = './{}/{}'.format(path, name)

            load_lazily = (lazy_load_preference == 'true'
                           and m.media_type == "CommCareVideo")
            if not getattr(m, 'unique_id', None):
                # lazy migration for adding unique_id to map_item
                m.unique_id = HQMediaMapItem.gen_unique_id(
                    m.multimedia_id, unchanged_path)

            descriptor = None
            if self.app.build_version and self.app.build_version >= LooseVersion(
                    '2.9'):
                type_mapping = {
                    "CommCareImage": "Image",
                    "CommCareAudio": "Audio",
                    "CommCareVideo": "Video",
                    "CommCareMultimedia": "Text"
                }
                descriptor = "{filetype} File: {name}".format(
                    filetype=type_mapping.get(m.media_type, "Media"),
                    name=name)

            yield MediaResource(
                id=id_strings.media_resource(m.unique_id, name),
                path=install_path,
                version=m.version,
                descriptor=descriptor,
                lazy=load_lazily,
                local=(local_path if self.app.enable_local_resource else None),
                remote=self.app.url_base + reverse(
                    'hqmedia_download', args=[m.media_type, m.multimedia_id]) +
                six.moves.urllib.parse.quote(name.encode('utf-8'))
                if name else name)
Esempio n. 9
0
    def media_resources(self):
        PREFIX = 'jr://file/'
        for path, m in sorted(list(self.app.multimedia_map_for_build(build_profile=self.build_profile).items()),
                              key=lambda item: item[0]):
            unchanged_path = path
            if path.startswith(PREFIX):
                path = path[len(PREFIX):]
            else:
                raise MediaResourceError('%s does not start with %s' % (path, PREFIX))
            path, name = split_path(path)
            # CommCare assumes jr://media/,
            # which is an alias to jr://file/commcare/media/
            # so we need to replace 'jr://file/' with '../../'
            # (this is a hack)
            install_path = '../../{}'.format(path)
            local_path = './{}/{}'.format(path, name)

            if not getattr(m, 'unique_id', None):
                # lazy migration for adding unique_id to map_item
                m.unique_id = HQMediaMapItem.gen_unique_id(m.multimedia_id, unchanged_path)

            descriptor = None
            if self.app.build_version and self.app.build_version >= LooseVersion('2.9'):
                type_mapping = {"CommCareImage": "Image",
                                "CommCareAudio": "Audio",
                                "CommCareVideo": "Video",
                                "CommCareMultimedia": "Text"}
                descriptor = "{filetype} File: {name}".format(
                    filetype=type_mapping.get(m.media_type, "Media"),
                    name=name
                )

            yield MediaResource(
                id=id_strings.media_resource(m.unique_id, name),
                path=install_path,
                version=m.version,
                descriptor=descriptor,
                local=(local_path
                       if self.app.enable_local_resource
                       else None),
                remote=self.app.url_base + reverse(
                    'hqmedia_download',
                    args=[m.media_type, m.multimedia_id]
                ) + six.moves.urllib.parse.quote(name.encode('utf-8')) if name else name
            )
Esempio n. 10
0
    def match_file(self, uploaded_file, replace_existing_media=True, **kwargs):
        errors = []
        try:
            if self.specific_params and self.specific_params['media_type'][0].startswith('CommCare'):
                media_class = getattr(sys.modules[__name__], self.specific_params['media_type'][0])
                form_path = self.specific_params['path'][0]
                replace_existing_media = self.specific_params['replace_attachment'][0]

                filename = uploaded_file.name
                data = uploaded_file.file.read()
                media = media_class.get_by_data(data)
            else:
                filename = uploaded_file.name

                if filename in self.images:
                    form_path = self.image_paths[self.images.index(filename)]
                    data = uploaded_file.file.read()
                    media = CommCareImage.get_by_data(data)
                elif filename in self.audio:
                    form_path = self.audio_paths[self.audio.index(filename)]
                    data = uploaded_file.file.read()
                    media = CommCareAudio.get_by_data(data)
                else:
                    uploaded_file.close()
                    return False, {}, errors
            if media:
                media.attach_data(data,
                    original_filename=filename,
                    username=self.username,
                    replace_attachment=replace_existing_media)
                media.add_domain(self.domain, owner=True, **kwargs)
                media.update_or_add_license(self.domain,
                                            type=kwargs.get('license', ''),
                                            author=kwargs.get('author', ''),
                                            attribution_notes=kwargs.get('attribution_notes', ''))
                self.app.create_mapping(media, form_path)

                return True, HQMediaMapItem.format_match_map(form_path, media.doc_type, media._id, filename), errors
        except Exception as e:
            logging.error(e)
            errors.append(e.message)

        return False, {}, errors
Esempio n. 11
0
    def test_report_module(self):
        from corehq.apps.userreports.tests.utils import get_sample_report_config

        app = Application.new_app('domain', "Untitled Application")

        report_module = app.add_module(ReportModule.new_module(
            'Reports', None))
        report_module.unique_id = 'report_module'
        report = get_sample_report_config()
        report._id = 'd3ff18cd83adf4550b35db8d391f6008'

        report_app_config = ReportAppConfig(
            report_id=report._id,
            header={'en': 'CommBugz'},
            uuid='ip1bjs8xtaejnhfrbzj2r6v1fi6hia4i',
            xpath_description='"report description"',
            use_xpath_description=True)
        report_app_config._report = report
        report_module.report_configs = [report_app_config]
        report_module._loaded = True
        self.assertXmlPartialEqual(
            self.get_xml('reports_module_menu'),
            app.create_suite(),
            "./menu",
        )

        app.multimedia_map = {
            "jr://file/commcare/image/module0_en.png":
            HQMediaMapItem(
                multimedia_id='bb4472b4b3c702f81c0b208357eb22f8',
                media_type='CommCareImage',
                unique_id='fe06454697634053cdb75fd9705ac7e6',
            ),
        }
        report_module.media_image = {
            'en': 'jr://file/commcare/image/module0_en.png',
        }
        self.assertXmlPartialEqual(
            self.get_xml('reports_module_menu_multimedia'),
            app.create_suite(),
            "./menu",
        )

        self.assertXmlPartialEqual(
            self.get_xml('reports_module_select_detail'),
            app.create_suite(),
            "./detail[@id='reports.ip1bjs8xtaejnhfrbzj2r6v1fi6hia4i.select']",
        )
        self.assertXmlPartialEqual(
            self.get_xml(
                'reports_module_summary_detail_use_xpath_description'),
            app.create_suite(),
            "./detail[@id='reports.ip1bjs8xtaejnhfrbzj2r6v1fi6hia4i.summary']",
        )
        self.assertXmlPartialEqual(
            self.get_xml('reports_module_data_detail'),
            app.create_suite(),
            "./detail/detail[@id='reports.ip1bjs8xtaejnhfrbzj2r6v1fi6hia4i.data']",
        )
        self.assertXmlPartialEqual(
            self.get_xml('reports_module_data_entry'),
            app.create_suite(),
            "./entry",
        )
        self.assertIn(
            'reports.ip1bjs8xtaejnhfrbzj2r6v1fi6hia4i=CommBugz',
            app.create_app_strings('default'),
        )

        report_app_config.use_xpath_description = False
        self.assertXmlPartialEqual(
            self.get_xml(
                'reports_module_summary_detail_use_localized_description'),
            app.create_suite(),
            "./detail[@id='reports.ip1bjs8xtaejnhfrbzj2r6v1fi6hia4i.summary']",
        )

        # Tuple mapping translation formats to the expected output of each
        translation_formats = [
            ({
                u'एक': {
                    'en': 'one',
                    'es': 'uno',
                },
                '2': {
                    'en': 'two',
                    'es': 'dos\'',
                    'hin': u'दो',
                },
            }, 'reports_module_data_detail-translated'),
            ({
                u'एक': 'one',
                '2': 'two',
            }, 'reports_module_data_detail-translated-simple'),
            ({
                u'एक': {
                    'en': 'one',
                    'es': 'uno',
                },
                '2': 'two',
            }, 'reports_module_data_detail-translated-mixed'),
        ]
        for translation_format, expected_output in translation_formats:
            report_app_config._report.columns[0]['transform'] = {
                'type': 'translation',
                'translations': translation_format,
            }
            report_app_config._report = ReportConfiguration.wrap(
                report_app_config._report._doc)
            self.assertXmlPartialEqual(
                self.get_xml(expected_output),
                app.create_suite(),
                "./detail/detail[@id='reports.ip1bjs8xtaejnhfrbzj2r6v1fi6hia4i.data']",
            )