Ejemplo n.º 1
0
    def create_renderer(self, context, renderer_settings, diff_file, *args, **kwargs):
        """Creates the renderer for the diff.

        This calculates all the state and data needed for rendering, and
        constructs a DiffRenderer with that data. That renderer is then
        returned, ready for rendering.

        If there's an error in looking up the necessary information, this
        may raise a UserVisibleError (best case), or some other form of
        Exception.
        """
        return get_diff_renderer(
            diff_file, extra_context=context, template_name=self.template_name, **renderer_settings
        )
Ejemplo n.º 2
0
    def create_renderer(self, context, renderer_settings, diff_file, *args,
                        **kwargs):
        """Creates the renderer for the diff.

        This calculates all the state and data needed for rendering, and
        constructs a DiffRenderer with that data. That renderer is then
        returned, ready for rendering.

        If there's an error in looking up the necessary information, this
        may raise a UserVisibleError (best case), or some other form of
        Exception.
        """
        return get_diff_renderer(diff_file,
                                 extra_context=context,
                                 template_name=self.template_name,
                                 **renderer_settings)
Ejemplo n.º 3
0
def view_diff_fragment(
        request,
        diffset_or_id,
        filediff_id,
        base_url,
        interdiffset_or_id=None,
        chunkindex=None,
        template_name='diffviewer/diff_file_fragment.html',
        error_template_name='diffviewer/diff_fragment_error.html'):
    """View which renders a specific fragment from a diff."""

    def get_requested_diff_file(get_chunks=True):
        files = get_diff_files(diffset, filediff, interdiffset,
                               request=request)

        if get_chunks:
            populate_diff_chunks(files, highlighting, request=request)

        if files:
            assert len(files) == 1
            file = files[0]

            if 'index' in request.GET:
                file['index'] = request.GET.get('index')

            return file

        return None

    # Depending on whether we're invoked from a URL or from a wrapper
    # with precomputed diffsets, we may be working with either IDs or
    # actual objects. If they're objects, just use them as-is. Otherwise,
    # if they're IDs, we want to grab them both (if both are provided)
    # in one go, to save on an SQL query.
    diffset_ids = []
    diffset = None
    interdiffset = None

    if isinstance(diffset_or_id, DiffSet):
        diffset = diffset_or_id
    else:
        diffset_ids.append(diffset_or_id)

    if interdiffset_or_id:
        if isinstance(interdiffset_or_id, DiffSet):
            interdiffset = interdiffset_or_id
        else:
            diffset_ids.append(interdiffset_or_id)

    if diffset_ids:
        diffsets = DiffSet.objects.filter(pk__in=diffset_ids)

        if len(diffsets) != len(diffset_ids):
            raise Http404

        for temp_diffset in diffsets:
            if temp_diffset.pk == diffset_or_id:
                diffset = temp_diffset
            elif temp_diffset.pk == interdiffset_or_id:
                interdiffset = temp_diffset
            else:
                assert False

    filediff = get_object_or_404(FileDiff, pk=filediff_id, diffset=diffset)

    # Store this so we don't end up causing an SQL query later when looking
    # this up.
    filediff.diffset = diffset

    highlighting = get_enable_highlighting(request.user)

    try:
        lines_of_context = request.GET.get('lines-of-context', '')
        lines_of_context = [int(i) for i in lines_of_context.split(',', 1)]
    except (TypeError, ValueError):
        lines_of_context = None

    if chunkindex is not None:
        try:
            chunkindex = int(chunkindex)
        except (TypeError, ValueError):
            chunkindex = None

    if lines_of_context:
        collapseall = True
    elif chunkindex:
        # If we're currently expanding part of a chunk, we want to render
        # the entire chunk without any lines collapsed. In the case of showing
        # a range of lines, we're going to get all chunks and then only show
        # the range. This is so that we won't have separate cached entries for
        # each range.
        collapseall = False
    else:
        collapseall = get_collapse_diff(request)

    try:
        diff_file = get_requested_diff_file()

        if diff_file:
            renderer = get_diff_renderer(
                diff_file,
                chunk_index=chunkindex,
                highlighting=highlighting,
                collapse_all=collapseall,
                lines_of_context=lines_of_context,
                extra_context={
                    'base_url': base_url,
                },
                template_name=template_name)

            return renderer.render_to_response()

        raise UserVisibleError(
            _(u"Internal error. Unable to locate file record for filediff %s")
            % filediff.id)
    except Exception, e:
        return exception_traceback(
            request, e, error_template_name,
            extra_context={'file': get_requested_diff_file(False)})
Ejemplo n.º 4
0
    def create_renderer(self, context, diffset_or_id, filediff_id,
                        interdiffset_or_id=None, chunkindex=None,
                        *args, **kwargs):
        """Creates the renderer for the diff.

        This calculates all the state and data needed for rendering, and
        constructs a DiffRenderer with that data. That renderer is then
        returned, ready for rendering.

        If there's an error in looking up the necessary information, this
        may raise a UserVisibleError (best case), or some other form of
        Exception.
        """
        # Depending on whether we're invoked from a URL or from a wrapper
        # with precomputed diffsets, we may be working with either IDs or
        # actual objects. If they're objects, just use them as-is. Otherwise,
        # if they're IDs, we want to grab them both (if both are provided)
        # in one go, to save on an SQL query.
        self.diffset = None
        self.interdiffset = None

        diffset_ids = []

        if isinstance(diffset_or_id, DiffSet):
            self.diffset = diffset_or_id
        else:
            diffset_ids.append(diffset_or_id)

        if interdiffset_or_id:
            if isinstance(interdiffset_or_id, DiffSet):
                self.interdiffset = interdiffset_or_id
            else:
                diffset_ids.append(interdiffset_or_id)

        if diffset_ids:
            diffsets = DiffSet.objects.filter(pk__in=diffset_ids)

            if len(diffsets) != len(diffset_ids):
                raise Http404

            for temp_diffset in diffsets:
                if temp_diffset.pk == diffset_or_id:
                    self.diffset = temp_diffset
                elif temp_diffset.pk == interdiffset_or_id:
                    self.interdiffset = temp_diffset
                else:
                    assert False

        self.highlighting = get_enable_highlighting(self.request.user)
        self.filediff = get_object_or_404(FileDiff, pk=filediff_id,
                                          diffset=self.diffset)

        # Store this so we don't end up causing an SQL query later when looking
        # this up.
        self.filediff.diffset = self.diffset

        try:
            lines_of_context = self.request.GET.get('lines-of-context', '')
            lines_of_context = [int(i) for i in lines_of_context.split(',', 1)]
        except (TypeError, ValueError):
            lines_of_context = None

        if chunkindex is not None:
            try:
                chunkindex = int(chunkindex)
            except (TypeError, ValueError):
                chunkindex = None

        if lines_of_context:
            collapseall = True
        elif chunkindex is not None:
            # If we're currently expanding part of a chunk, we want to render
            # the entire chunk without any lines collapsed. In the case of
            # showing a range of lines, we're going to get all chunks and then
            # only show the range. This is so that we won't have separate
            # cached entries for each range.
            collapseall = False
        else:
            collapseall = get_collapse_diff(self.request)

        self.diff_file = self._get_requested_diff_file()

        if not self.diff_file:
            raise UserVisibleError(
                _('Internal error. Unable to locate file record for '
                  'filediff %s')
                % self.filediff.pk)

        return get_diff_renderer(
            self.diff_file,
            chunk_index=chunkindex,
            highlighting=self.highlighting,
            collapse_all=collapseall,
            lines_of_context=lines_of_context,
            extra_context=context,
            template_name=self.template_name)
Ejemplo n.º 5
0
def view_diff_fragment(
        request,
        diffset_or_id,
        filediff_id,
        base_url,
        interdiffset_or_id=None,
        chunkindex=None,
        template_name='diffviewer/diff_file_fragment.html',
        error_template_name='diffviewer/diff_fragment_error.html'):
    """View which renders a specific fragment from a diff."""
    def get_requested_diff_file(get_chunks=True):
        files = get_diff_files(diffset,
                               filediff,
                               interdiffset,
                               request=request)

        if get_chunks:
            populate_diff_chunks(files, highlighting, request=request)

        if files:
            assert len(files) == 1
            file = files[0]

            if 'index' in request.GET:
                file['index'] = request.GET.get('index')

            return file

        return None

    # Depending on whether we're invoked from a URL or from a wrapper
    # with precomputed diffsets, we may be working with either IDs or
    # actual objects. If they're objects, just use them as-is. Otherwise,
    # if they're IDs, we want to grab them both (if both are provided)
    # in one go, to save on an SQL query.
    diffset_ids = []
    diffset = None
    interdiffset = None

    if isinstance(diffset_or_id, DiffSet):
        diffset = diffset_or_id
    else:
        diffset_ids.append(diffset_or_id)

    if interdiffset_or_id:
        if isinstance(interdiffset_or_id, DiffSet):
            interdiffset = interdiffset_or_id
        else:
            diffset_ids.append(interdiffset_or_id)

    if diffset_ids:
        diffsets = DiffSet.objects.filter(pk__in=diffset_ids)

        if len(diffsets) != len(diffset_ids):
            raise Http404

        for temp_diffset in diffsets:
            if temp_diffset.pk == diffset_or_id:
                diffset = temp_diffset
            elif temp_diffset.pk == interdiffset_or_id:
                interdiffset = temp_diffset
            else:
                assert False

    filediff = get_object_or_404(FileDiff, pk=filediff_id, diffset=diffset)

    # Store this so we don't end up causing an SQL query later when looking
    # this up.
    filediff.diffset = diffset

    highlighting = get_enable_highlighting(request.user)

    try:
        lines_of_context = request.GET.get('lines-of-context', '')
        lines_of_context = [int(i) for i in lines_of_context.split(',', 1)]
    except (TypeError, ValueError):
        lines_of_context = None

    if chunkindex is not None:
        try:
            chunkindex = int(chunkindex)
        except (TypeError, ValueError):
            chunkindex = None

    if lines_of_context:
        collapseall = True
    elif chunkindex:
        # If we're currently expanding part of a chunk, we want to render
        # the entire chunk without any lines collapsed. In the case of showing
        # a range of lines, we're going to get all chunks and then only show
        # the range. This is so that we won't have separate cached entries for
        # each range.
        collapseall = False
    else:
        collapseall = get_collapse_diff(request)

    try:
        diff_file = get_requested_diff_file()

        if diff_file:
            renderer = get_diff_renderer(diff_file,
                                         chunk_index=chunkindex,
                                         highlighting=highlighting,
                                         collapse_all=collapseall,
                                         lines_of_context=lines_of_context,
                                         extra_context={
                                             'base_url': base_url,
                                         },
                                         template_name=template_name)

            return renderer.render_to_response()

        raise UserVisibleError(
            _(u"Internal error. Unable to locate file record for filediff %s")
            % filediff.id)
    except Exception, e:
        return exception_traceback(
            request,
            e,
            error_template_name,
            extra_context={'file': get_requested_diff_file(False)})
Ejemplo n.º 6
0
    def create_renderer(self,
                        context,
                        diffset_or_id,
                        filediff_id,
                        interdiffset_or_id=None,
                        chunkindex=None,
                        *args,
                        **kwargs):
        """Creates the renderer for the diff.

        This calculates all the state and data needed for rendering, and
        constructs a DiffRenderer with that data. That renderer is then
        returned, ready for rendering.

        If there's an error in looking up the necessary information, this
        may raise a UserVisibleError (best case), or some other form of
        Exception.
        """
        # Depending on whether we're invoked from a URL or from a wrapper
        # with precomputed diffsets, we may be working with either IDs or
        # actual objects. If they're objects, just use them as-is. Otherwise,
        # if they're IDs, we want to grab them both (if both are provided)
        # in one go, to save on an SQL query.
        self.diffset = None
        self.interdiffset = None

        diffset_ids = []

        if isinstance(diffset_or_id, DiffSet):
            self.diffset = diffset_or_id
        else:
            diffset_ids.append(diffset_or_id)

        if interdiffset_or_id:
            if isinstance(interdiffset_or_id, DiffSet):
                self.interdiffset = interdiffset_or_id
            else:
                diffset_ids.append(interdiffset_or_id)

        if diffset_ids:
            diffsets = DiffSet.objects.filter(pk__in=diffset_ids)

            if len(diffsets) != len(diffset_ids):
                raise Http404

            for temp_diffset in diffsets:
                if temp_diffset.pk == diffset_or_id:
                    self.diffset = temp_diffset
                elif temp_diffset.pk == interdiffset_or_id:
                    self.interdiffset = temp_diffset
                else:
                    assert False

        self.highlighting = get_enable_highlighting(self.request.user)
        self.filediff = get_object_or_404(FileDiff,
                                          pk=filediff_id,
                                          diffset=self.diffset)

        # Store this so we don't end up causing an SQL query later when looking
        # this up.
        self.filediff.diffset = self.diffset

        try:
            lines_of_context = self.request.GET.get('lines-of-context', '')
            lines_of_context = [int(i) for i in lines_of_context.split(',', 1)]
        except (TypeError, ValueError):
            lines_of_context = None

        if chunkindex is not None:
            try:
                chunkindex = int(chunkindex)
            except (TypeError, ValueError):
                chunkindex = None

        if lines_of_context:
            collapseall = True
        elif chunkindex is not None:
            # If we're currently expanding part of a chunk, we want to render
            # the entire chunk without any lines collapsed. In the case of showing
            # a range of lines, we're going to get all chunks and then only show
            # the range. This is so that we won't have separate cached entries for
            # each range.
            collapseall = False
        else:
            collapseall = get_collapse_diff(self.request)

        self.diff_file = self._get_requested_diff_file()

        if not self.diff_file:
            raise UserVisibleError(
                _('Internal error. Unable to locate file record for '
                  'filediff %s') % self.filediff.pk)

        return get_diff_renderer(self.diff_file,
                                 chunk_index=chunkindex,
                                 highlighting=self.highlighting,
                                 collapse_all=collapseall,
                                 lines_of_context=lines_of_context,
                                 extra_context=context,
                                 template_name=self.template_name)