feeds.py 1.84 KB
Newer Older
1
from django.contrib.syndication.views import Feed
2 3
from django.db.models import Q

4 5 6 7
from rp.models import Article

class ArticlesFeed(Feed):
    title = "Revue de presse de la Quadrature"
8
    link = "/feeds/"
9 10
    description = "La revue de presse recense les articles de presse relatifs aux sujets de la Quadrature. Elle est compilée chaque jour par ses bénévoles, à partir de la presse francophone et internationale. Bonne lecture !"

11 12 13
    def __init__(self, **kwargs):
        self.filter_lang = kwargs['filter_lang']

14
    def items(self):
15
        return Article.objects.filter(lang=self.filter_lang) \
16 17 18 19 20
                              .order_by('-created_at')[:25]

    def item_title(self, item):
        return item.title

21 22 23
    def item_status(self, item):
        return item.status

24 25 26 27 28 29 30 31 32 33 34 35 36 37
    def item_description(self, item):
        return item.extracts

    def item_link(self, item):
        return item.url

    def item_pubdate(self, item):
        return item.published_at

    def item_updateddate(self, item):
        return item.updated_at

    def item_categories(self, item):
        return item.tags.all()
38 39 40 41 42 43

class ArticlesTagsFeed(ArticlesFeed):
    def __init__(self, **kwargs):
        pass

    def items(self):
44 45
        return Article.objects.filter(tags__name__in=[self.filter_tag]) \
                              .order_by('-created_at')[:25]
46 47 48

    def get_object(self, request, filter_tag):
        self.filter_tag = filter_tag
49 50 51 52 53 54 55 56 57 58 59 60

class ArticlesSearchFeed(ArticlesFeed):
    def __init__(self, **kwargs):
        pass

    def items(self):
        return Article.objects.filter(Q(title__icontains=self.search_keywords)
                                      | Q(extracts__icontains=self.search_keywords)) \
                                      .order_by('-created_at')[:25]

    def get_object(self, request, search_keywords):
        self.search_keywords = search_keywords