Пример #1
0
    def has_changes(self):
        """
        Helper method to check if this OLX bundle has any pending changes,
        including any deleted blocks.

        Returns a tuple of (
            has_unpublished_changes,
            has_unpublished_deletes,
        )
        Where has_unpublished_changes is true if there is any type of change,
        including deletes, and has_unpublished_deletes is only true if one or
        more blocks has been deleted since the last publish.
        """
        if not self.draft_name:
            return (False, False)
        cached_result = self.cache.get(('has_changes', ))
        if cached_result is not None:
            return cached_result
        draft_files = get_bundle_files_cached(self.bundle_uuid,
                                              draft_name=self.draft_name)

        has_unpublished_changes = False
        has_unpublished_deletes = False

        for file_ in draft_files:
            if getattr(file_, 'modified', False):
                has_unpublished_changes = True
                break

        if not has_unpublished_changes:
            # Check if any links have changed:
            old_links = set(
                get_bundle_direct_links_with_cache(self.bundle_uuid).items())
            new_links = set(
                get_bundle_direct_links_with_cache(
                    self.bundle_uuid, draft_name=self.draft_name).items())
            has_unpublished_changes = new_links != old_links

        published_file_paths = {
            f.path
            for f in get_bundle_files_cached(self.bundle_uuid)
        }
        draft_file_paths = {f.path for f in draft_files}
        for file_path in published_file_paths:
            if file_path not in draft_file_paths:
                has_unpublished_changes = True
                if file_path.endswith('/definition.xml'):
                    # only set 'has_unpublished_deletes' if the actual main definition XML
                    # file was deleted, not if only some asset file was deleted, etc.
                    has_unpublished_deletes = True
                    break

        result = (has_unpublished_changes, has_unpublished_deletes)
        self.cache.set(('has_changes', ), result)
        return result
Пример #2
0
def definition_for_include(parsed_include, parent_definition_key):
    """
    Given a parsed <xblock-include /> element as a XBlockInclude tuple, get the
    definition (OLX file) that it is pointing to.

    Arguments:

    parsed_include: An XBlockInclude tuple

    parent_definition_key: The BundleDefinitionLocator for the XBlock whose OLX
        contained the <xblock-include /> (i.e. the parent).

    Returns: a BundleDefinitionLocator
    """
    if parsed_include.link_id:
        links = get_bundle_direct_links_with_cache(
            parent_definition_key.bundle_uuid,
            # And one of the following will be set:
            bundle_version=parent_definition_key.bundle_version,
            draft_name=parent_definition_key.draft_name,
        )
        try:
            link = links[parsed_include.link_id]
        except KeyError:
            raise BundleFormatException("Link not found: {}".format(
                parsed_include.link_id))
        return BundleDefinitionLocator(
            bundle_uuid=link.bundle_uuid,
            block_type=parsed_include.block_type,
            olx_path="{}/{}/definition.xml".format(
                parsed_include.block_type, parsed_include.definition_id),
            bundle_version=link.version,
        )
    else:
        return BundleDefinitionLocator(
            bundle_uuid=parent_definition_key.bundle_uuid,
            block_type=parsed_include.block_type,
            olx_path="{}/{}/definition.xml".format(
                parsed_include.block_type, parsed_include.definition_id),
            bundle_version=parent_definition_key.bundle_version,
            draft_name=parent_definition_key.draft_name,
        )