Ejemplo n.º 1
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"
Ejemplo 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"
Ejemplo n.º 3
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
Ejemplo n.º 4
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