Beispiel #1
0
 def test_get_latest_timestamp_with_jumbled_list(self):
     """Testing get_latest_timestamp with unsorted date time
     list.
      """
     self.assertEqual(
         dates.get_latest_timestamp(
             ['1453204800', '1466337600', '1466424000']), '1466424000')
Beispiel #2
0
def comment_diff_fragments(
        request, review_request_id, comment_ids,
        template_name='reviews/load_diff_comment_fragments.js',
        comment_template_name='reviews/diff_comment_fragment.html',
        error_template_name='diffviewer/diff_fragment_error.html'):
    """
    Returns the fragment representing the parts of a diff referenced by the
    specified list of comment IDs. This is used to allow batch lazy-loading
    of these diff fragments based on filediffs, since they may not be cached
    and take time to generate.
    """
    comments = get_list_or_404(Comment, pk__in=comment_ids.split(","))
    latest_timestamp = get_latest_timestamp([comment.timestamp
                                             for comment in comments])

    if get_modified_since(request, latest_timestamp):
        return HttpResponseNotModified()

    context = RequestContext(request, {
        'comment_entries': [],
        'container_prefix': request.GET.get('container_prefix'),
        'queue_name': request.GET.get('queue'),
    })

    had_error = False

    for comment in comments:
        try:
            content = render_to_string(comment_template_name,
                                       RequestContext(request, {
                'comment': comment,
                'chunks': list(get_file_chunks_in_range(context,
                                                        comment.filediff,
                                                        comment.interfilediff,
                                                        comment.first_line,
                                                        comment.num_lines))
            }))
        except Exception, e:
            content = exception_traceback_string(request, e,
                                                 error_template_name, {
                'comment': comment,
                'file': {
                    'depot_filename': comment.filediff.source_file,
                    'index': None,
                    'filediff': comment.filediff,
                },
            })

            # It's bad that we failed, and we'll return a 500, but we'll
            # still return content for anything we have. This will prevent any
            # caching.
            had_error = True

        context['comment_entries'].append({
            'comment': comment,
            'html': content,
        })
Beispiel #3
0
 def test_get_latest_timestamp_with_jumbled_list(self):
     """Testing get_latest_timestamp with unsorted date time
     list.
      """
     self.assertEqual(dates.get_latest_timestamp([
         '1453204800',
         '1466337600',
         '1466424000'
     ]), '1466424000')
Beispiel #4
0
def comment_diff_fragments(
        request,
        review_request_id,
        comment_ids,
        template_name='reviews/load_diff_comment_fragments.js',
        comment_template_name='reviews/diff_comment_fragment.html',
        error_template_name='diffviewer/diff_fragment_error.html',
        local_site_name=None):
    """
    Returns the fragment representing the parts of a diff referenced by the
    specified list of comment IDs. This is used to allow batch lazy-loading
    of these diff fragments based on filediffs, since they may not be cached
    and take time to generate.
    """
    # While we don't actually need the review request, we still want to do this
    # lookup in order to get the permissions checking.
    review_request, response = \
        _find_review_request(request, review_request_id, local_site_name)

    if not review_request:
        return response

    comments = get_list_or_404(Comment, pk__in=comment_ids.split(","))
    latest_timestamp = get_latest_timestamp(
        [comment.timestamp for comment in comments])

    if get_modified_since(request, latest_timestamp):
        return HttpResponseNotModified()

    context = RequestContext(
        request, {
            'comment_entries': [],
            'container_prefix': request.GET.get('container_prefix'),
            'queue_name': request.GET.get('queue'),
        })

    had_error, context['comment_entries'] = \
        build_diff_comment_fragments(comments,
                                     context,
                                     comment_template_name,
                                     error_template_name)

    page_content = render_to_string(template_name, context)

    if had_error:
        return HttpResponseServerError(page_content)

    response = HttpResponse(page_content)
    set_last_modified(response, comment.timestamp)
    response['Expires'] = http_date(time.time() + 60 * 60 * 24 * 365)  # 1 year
    return response
Beispiel #5
0
def comment_diff_fragments(
    request,
    review_request_id,
    comment_ids,
    template_name='reviews/load_diff_comment_fragments.js',
    comment_template_name='reviews/diff_comment_fragment.html',
    error_template_name='diffviewer/diff_fragment_error.html',
    local_site_name=None):
    """
    Returns the fragment representing the parts of a diff referenced by the
    specified list of comment IDs. This is used to allow batch lazy-loading
    of these diff fragments based on filediffs, since they may not be cached
    and take time to generate.
    """
    # While we don't actually need the review request, we still want to do this
    # lookup in order to get the permissions checking.
    review_request, response = \
        _find_review_request(request, review_request_id, local_site_name)

    if not review_request:
        return response

    comments = get_list_or_404(Comment, pk__in=comment_ids.split(","))
    latest_timestamp = get_latest_timestamp([comment.timestamp
                                             for comment in comments])

    if get_modified_since(request, latest_timestamp):
        return HttpResponseNotModified()

    context = RequestContext(request, {
        'comment_entries': [],
        'container_prefix': request.GET.get('container_prefix'),
        'queue_name': request.GET.get('queue'),
    })

    had_error, context['comment_entries'] = \
        build_diff_comment_fragments(comments,
                                     context,
                                     comment_template_name,
                                     error_template_name)

    page_content = render_to_string(template_name, context)

    if had_error:
        return HttpResponseServerError(page_content)

    response = HttpResponse(page_content)
    set_last_modified(response, comment.timestamp)
    response['Expires'] = http_date(time.time() + 60 * 60 * 24 * 365) # 1 year
    return response
Beispiel #6
0
def comment_diff_fragments(
    request,
    review_request_id,
    comment_ids,
    template_name="reviews/load_diff_comment_fragments.js",
    comment_template_name="reviews/diff_comment_fragment.html",
    error_template_name="diffviewer/diff_fragment_error.html",
):
    """
    Returns the fragment representing the parts of a diff referenced by the
    specified list of comment IDs. This is used to allow batch lazy-loading
    of these diff fragments based on filediffs, since they may not be cached
    and take time to generate.
    """
    comments = get_list_or_404(Comment, pk__in=comment_ids.split(","))
    latest_timestamp = get_latest_timestamp([comment.timestamp for comment in comments])

    if get_modified_since(request, latest_timestamp):
        return HttpResponseNotModified()

    context = RequestContext(
        request,
        {
            "comment_entries": [],
            "container_prefix": request.GET.get("container_prefix"),
            "queue_name": request.GET.get("queue"),
        },
    )

    had_error, context["comment_entries"] = build_diff_comment_fragments(
        comments, context, comment_template_name, error_template_name
    )

    page_content = render_to_string(template_name, context)

    if had_error:
        return HttpResponseServerError(page_content)

    response = HttpResponse(page_content)
    set_last_modified(response, comment.timestamp)
    response["Expires"] = http_date(time.time() + 60 * 60 * 24 * 365)  # 1 year
    return response
Beispiel #7
0
    def get_last_activity_timestamp(review_request):
        review_timestamp = 0

        if request.user.is_authenticated():
            try:
                last_draft_review = Review.objects.filter(
                    review_request=review_request, user=request.user, public=False
                ).latest()
                review_timestamp = last_draft_review.timestamp
            except Review.DoesNotExist:
                pass

        # Find out if we can bail early. Generate an ETag for this.
        timestamps = [review_request.last_updated]
        draft = review_request.get_draft()

        if draft:
            timestamps.append(draft.last_updated)

        return get_latest_timestamp(timestamps)
Beispiel #8
0
 def test_get_latest_timestamp_with_empty_list(self):
     """Testing get_latest_timestamp without any timestamps in the
      list
      """
     self.assertEqual(dates.get_latest_timestamp([]), None)
Beispiel #9
0
 def test_get_latest_timestamp_with_empty_list(self):
     """Testing get_latest_timestamp without any timestamps in the list"""
     self.assertIsNone(get_latest_timestamp([]))
Beispiel #10
0
def review_detail(request, review_request_id,
                  template_name="reviews/review_detail.html"):
    """
    Main view for review requests. This covers the review request information
    and all the reviews on it.
    """
    review_request = get_object_or_404(ReviewRequest, pk=review_request_id)

    reviews = review_request.get_public_reviews()
    review = review_request.get_pending_review(request.user)

    if request.user.is_authenticated():
        # If the review request is public and pending review and if the user
        # is logged in, mark that they've visited this review request.
        if review_request.public and review_request.status == "P":
            visited, visited_is_new = ReviewRequestVisit.objects.get_or_create(
                user=request.user, review_request=review_request)
            visited.timestamp = datetime.now()
            visited.save()


    # Unlike review above, this covers replies as well.
    review_timestamp = 0

    if request.user.is_authenticated():
        try:
            last_draft_review = Review.objects.filter(
                review_request=review_request,
                user=request.user,
                public=False).latest()
            review_timestamp = last_draft_review.timestamp
        except Review.DoesNotExist:
            pass

    # Find out if we can bail early. Generate an ETag for this.
    timestamps = [review_request.last_updated]
    draft = review_request.get_draft()

    if draft:
        timestamps.append(draft.last_updated)

    last_activity_time = get_latest_timestamp(timestamps)

    etag = "%s:%s:%s:%s" % (request.user, last_activity_time, review_timestamp,
                            settings.AJAX_SERIAL)

    if etag_if_none_match(request, etag):
        return HttpResponseNotModified()

    repository = review_request.repository
    changedescs = review_request.changedescs.filter(public=True)

    entries = []

    for temp_review in reviews:
        temp_review.ordered_comments = \
            temp_review.comments.order_by('filediff', 'first_line')

        entries.append({
            'review': temp_review,
            'timestamp': temp_review.timestamp,
        })

    for changedesc in changedescs:
        fields_changed = []

        for name, info in changedesc.fields_changed.items():
            multiline = False

            if 'added' in info or 'removed' in info:
                change_type = 'add_remove'

                # We don't hard-code URLs in the bug info, since the
                # tracker may move, but we can do it here.
                if (name == "bugs_closed" and
                    review_request.repository.bug_tracker):
                    bug_url = review_request.repository.bug_tracker
                    for field in info:
                        for i, buginfo in enumerate(info[field]):
                            try:
                                full_bug_url = bug_url % buginfo[0]
                                info[field][i] = (buginfo[0], full_bug_url)
                            except TypeError:
                                logging.warning("Invalid bugtracker url format")

            elif 'old' in info or 'new' in info:
                change_type = 'changed'
                multiline = (name == "description" or name == "testing_done")

                # Branch text is allowed to have entities, so mark it safe.
                if name == "branch":
                    if 'old' in info:
                        info['old'][0] = mark_safe(info['old'][0])

                    if 'new' in info:
                        info['new'][0] = mark_safe(info['new'][0])
            elif name == "screenshot_captions":
                change_type = 'screenshot_captions'
            else:
                # No clue what this is. Bail.
                continue

            fields_changed.append({
                'title': fields_changed_name_map.get(name, name),
                'multiline': multiline,
                'info': info,
                'type': change_type,
            })

        entries.append({
            'changeinfo': fields_changed,
            'changedesc': changedesc,
            'timestamp': changedesc.timestamp,
        })

    entries.sort(key=lambda item: item['timestamp'])

    response = render_to_response(template_name, RequestContext(request, {
        'draft': draft,
        'review_request': review_request,
        'review_request_details': draft or review_request,
        'entries': entries,
        'review': review,
        'request': request,
        'upload_diff_form': UploadDiffForm(repository),
        'upload_screenshot_form': UploadScreenshotForm(),
        'scmtool': repository.get_scmtool(),
        'PRE_CREATION': PRE_CREATION,
    }))
    set_etag(response, etag)

    return response
Beispiel #11
0
 def test_get_latest_timestamp_with_empty_list(self):
     """Testing get_latest_timestamp without any timestamps in the list"""
     self.assertIsNone(get_latest_timestamp([]))
Beispiel #12
0
 def test_get_latest_timestamp_with_empty_list(self):
     """Testing get_latest_timestamp without any timestamps in the
      list
      """
     self.assertEqual(dates.get_latest_timestamp([]), None)