• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import django.http
2from django.contrib.syndication import views
3from autotest_lib.frontend.afe import models
4
5
6# Copied from django/contrib/syndication/views.py.  The default view doesn't
7# give the feed any way to access the request object, and we need to access it
8# to get the server hostname.  So we're forced to copy the code here and modify
9# it to pass in the request.
10from django.http import HttpResponse, Http404
11
12# name changed from feed to feed_view
13def feed_view(request, url, feed_dict=None):
14    if not feed_dict:
15        raise Http404, "No feeds are registered."
16
17    try:
18        slug, param = url.split('/', 1)
19    except ValueError:
20        slug, param = url, ''
21
22    try:
23        f = feed_dict[slug]
24    except KeyError:
25        raise Http404, "Slug %r isn't registered." % slug
26
27    try:
28        # this line is changed from the Django library version to pass
29        # in request instead of request.path
30        feedgen = f(slug, request).get_feed(param)
31    except views.FeedDoesNotExist:
32        raise Http404, "Invalid feed parameters. Slug %r is valid, but other parameters, or lack thereof, are not." % slug
33
34    response = HttpResponse(mimetype=feedgen.mime_type)
35    feedgen.write(response, 'utf-8')
36    return response
37# end copied code
38
39class JobFeed(views.Feed):
40    """\
41    Common feed functionality.
42    """
43    link  =  "/results"
44    title_template = "feeds/job_feed_title.html"
45    description_template = "feeds/job_feed_description.html"
46
47    NUM_ITEMS = 20
48
49    def __init__(self, slug, request):
50        super(JobFeed, self).__init__(slug, request)
51        server_hostname = django.http.get_host(request)
52        self.full_link = 'http://' + server_hostname + self.link
53
54    def title(self, obj):
55        return "Automated Test Framework %s Jobs" % obj.capitalize()
56
57    def get_object(self, bits):
58        # bits[0] should be a job status
59        return bits[0]
60
61    def items(self, obj):
62        item_list = models.HostQueueEntry.objects.filter(
63            status__iexact=obj).select_related()
64        return item_list.order_by('-id')[:self.NUM_ITEMS]
65
66    def item_link(self, obj):
67        return  '%s/%s-%s' % (self.full_link, obj.job.id, obj.job.owner)
68