Example #1
0
    def get_media_references(self, request=None):
        """
            Use this to check all Application media against the stored multimedia_map.
        """
        from corehq.apps.app_manager.models import Application
        if not isinstance(self, Application):
            raise NotImplementedError("Sorry, this method is only supported for CommCare HQ Applications.")

        from corehq.apps.hqmedia.utils import get_application_media
        all_media, form_errors = get_application_media(self)

        # Because couchdbkit is terrible?
        multimedia_map = self.multimedia_map

        missing_refs = False

        references = {}
        for section, media in all_media.items():
            references[section] = {}
            for media_type, paths in media.items():
                maps = []
                missing = 0
                matched = 0
                errors = 0
                for path in paths:
                    match_map = None
                    try:
                        media_item = multimedia_map[path]
                        match_map = HQMediaMapItem.format_match_map(path,
                            media_item.media_type, media_item.multimedia_id)
                        matched += 1
                    except KeyError:
                        match_map = HQMediaMapItem.format_match_map(path)
                        missing += 1
                    except AttributeError:
                        errors += 1
                        if request:
                            messages.error(request, _("Encountered an AttributeError for media: %s" % path))
                    except UnicodeEncodeError:
                        errors += 1
                        if request:
                            messages.error(request, _("This application has unsupported text in one "
                                                      "of it's media file label fields: %s" % path))
                    if match_map:
                        maps.append(match_map)
                    if errors > 0 or missing > 0:
                        missing_refs = True

                references[section][media_type] = {
                    'maps': maps,
                    'missing': missing,
                    'matched': matched,
                    'errors': errors,
                }

        return {
            "references": references,
            "form_errors": form_errors,
            "missing_refs": missing_refs,
        }
Example #2
0
    def get_media_references(self, request=None):
        """
            DEPRECATED METHOD: Use self.all_media instead.
            Use this to check all Application media against the stored multimedia_map.
        """
        #todo this should get updated to use self.all_media
        from corehq.apps.app_manager.models import Application
        if not isinstance(self, Application):
            raise NotImplementedError(
                "Sorry, this method is only supported for CommCare HQ Applications."
            )

        from corehq.apps.hqmedia.utils import get_application_media
        all_media, form_errors = get_application_media(self)

        # Because couchdbkit is terrible?
        multimedia_map = self.multimedia_map

        missing_refs = False

        references = {}
        for section, media in all_media.items():
            references[section] = {}
            for media_type, paths in media.items():
                maps = []
                missing = 0
                matched = 0
                errors = 0
                for path in paths:
                    match_map = None
                    try:
                        media_item = multimedia_map[path]
                        match_map = HQMediaMapItem.format_match_map(
                            path, media_item.media_type,
                            media_item.multimedia_id)
                        matched += 1
                    except KeyError:
                        match_map = HQMediaMapItem.format_match_map(path)
                        missing += 1
                    except AttributeError:
                        errors += 1
                        if request:
                            messages.error(
                                request,
                                _("Encountered an AttributeError for media: %s"
                                  % path))
                    except UnicodeEncodeError:
                        errors += 1
                        if request:
                            messages.error(
                                request,
                                _("This application has unsupported text in one "
                                  "of it's media file label fields: %s" %
                                  path))
                    if match_map:
                        maps.append(match_map)
                    if errors > 0 or missing > 0:
                        missing_refs = True

                references[section][media_type] = {
                    'maps': maps,
                    'missing': missing,
                    'matched': matched,
                    'errors': errors,
                }

        return {
            "references": references,
            "form_errors": form_errors,
            "missing_refs": missing_refs,
        }