This repository has been archived by the owner on Dec 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
feeds.py
107 lines (91 loc) · 4.07 KB
/
feeds.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from django.contrib.syndication.views import Feed
from django.core.urlresolvers import reverse
from django.shortcuts import get_object_or_404
from source.articles.models import Article
from source.articles.views import CATEGORY_MAP, SECTION_MAP
from source.code.models import Code
from taggit.models import Tag
class ArticleFeed(Feed):
def get_object(self, request, *args, **kwargs):
self.section = kwargs.get('section', None)
self.category = kwargs.get('category', None)
self.tags = None
self.tag_slugs = kwargs.get('tag_slugs', None)
self.tag_slug_list = []
if self.tag_slugs:
self.tag_slug_list = self.tag_slugs.split('+')
# need to fail if any item in slug list references nonexistent tag
self.tags = [get_object_or_404(Tag, slug=tag_slug) for tag_slug in self.tag_slug_list]
return ''
def title(self, obj):
if self.section:
return "Source: %s" % SECTION_MAP[self.section]['name']
elif self.category:
return "Source: Articles in the category %s" % CATEGORY_MAP[self.category]['name']
elif self.tag_slugs:
return "Source: Articles tagged with '%s'" % "+".join([tag.name for tag in self.tags])
return "Source"
def link(self, obj):
if self.section:
return reverse('article_list_by_section', kwargs={'section': self.section})
elif self.category:
return reverse('article_list_by_category', kwargs={'category': self.category})
elif self.tag_slugs:
return reverse('article_list_by_tag', kwargs={'tag_slugs': self.tag_slugs})
return reverse('homepage')
def description(self, obj):
identifier = 'from Source'
if self.section:
identifier = "in the %s section" % SECTION_MAP[self.section]['name']
elif self.category:
identifier = "in the %s category" % CATEGORY_MAP[self.category]['name']
elif self.tag_slugs:
identifier = "tagged with '%s'" % "+".join([tag.name for tag in self.tags])
return "Recent articles %s" % identifier
def item_title(self, item):
return item.title
def item_description(self, item):
return item.summary
def items(self, obj):
queryset = Article.live_objects.all()
if self.section:
queryset = queryset.filter(article_type__in=SECTION_MAP[self.section]['article_types'])
elif self.category:
queryset = queryset.filter(article_type=self.category)
elif self.tag_slugs:
for tag_slug in self.tag_slug_list:
queryset = queryset.filter(tags__slug=tag_slug)
return queryset[:20]
class CodeFeed(Feed):
def get_object(self, request, *args, **kwargs):
self.tags = None
self.tag_slugs = kwargs.get('tag_slugs', None)
self.tag_slug_list = []
if self.tag_slugs:
self.tag_slug_list = self.tag_slugs.split('+')
# need to fail if any item in slug list references nonexistent tag
self.tags = [get_object_or_404(Tag, slug=tag_slug) for tag_slug in self.tag_slug_list]
return ''
def title(self, obj):
identifier = ""
if self.tags:
identifier = " tagged '%s'" % "+".join([tag.name for tag in self.tags])
return "Source: Code%s" % identifier
def link(self, obj):
if self.tag_slugs:
return reverse('code_list_by_tag', kwargs={'tag_slugs': self.tag_slugs})
return reverse('code_list')
def description(self, obj):
identifier = " from Source"
if self.tag_slugs:
identifier = " tagged '%s'" % "+".join([tag.name for tag in self.tags])
return "Recent code index pages%s" % identifier
def item_title(self, item):
return item.name
def item_description(self, item):
return item.description
def items(self, obj):
queryset = Code.live_objects.order_by('-created')
for tag_slug in self.tag_slug_list:
queryset = queryset.filter(tags__slug=tag_slug)
return queryset[:20]