MyRssFeeds   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 29
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get_queryset() 0 13 3
A get_paginate_by() 0 6 1
1
# coding: utf-8
2
from django.views.generic import TemplateView, ListView
3
from django.core.cache import caches
4
from django.conf import settings
5
6
from th_rss.models import Rss
7
from django_th.models import TriggerService
8
9
import django_th
10
11
12
class MyRssFeed(TemplateView):
13
    """
14
        page to display its RSS from any service
15
    """
16
    template_name = "rss/my_feed.html"
17
18
    def get_context_data(self, **kw):
19
        context = super(MyRssFeed, self).get_context_data(**kw)
20
21
        if 'uuid' in kw:
22
            # get the uuid from the Rss model
23
            rss = Rss.objects.get(uuid=kw['uuid'])
24
            # get its related Trigger where Provider use RSS
25
            trigger = TriggerService.objects.get(id=rss.trigger_id)
26
            # cut 'Service' to get the service name itself
27
            provider = trigger.provider.name.name.split('Service')[1].lower()
28
            cache = caches['django_th']
29
            pattern = 'th_{provider}_{id}'.format(provider=provider,
30
                                                  id=rss.trigger_id)
31
            context['data'] = cache.get(pattern)
32
            context['uuid'] = kw['uuid']
33
            context['last_build_date'] = trigger.date_triggered
34
        context['lang'] = settings.LANGUAGE_CODE
35
        context['version'] = django_th.__version__
36
        return context
37
38
39
class MyRssFeeds(ListView):
40
    """
41
        page to display all existing UUID from all RSS
42
    """
43
    context_object_name = "rss_list"
44
    queryset = TriggerService.objects.all()
45
    template_name = "rss/my_feeds.html"
46
    paginate_by = 3
47
48
    def get_paginate_by(self, queryset):
49
        """
50
            Get the number of items to paginate by,
51
            from the settings
52
        """
53
        return settings.DJANGO_TH.get('paginate_by', self.paginate_by)
54
55
    def get_queryset(self):
56
        # connected ?
57
        if self.request.user.is_authenticated():
58
            # get the Trigger that are MINE and where the CONSUMER is
59
            # the ServiceRss
60
            triggers = self.queryset.filter(user=self.request.user,
61
                                            consumer__name='ServiceRss')
62
            rss = []
63
            for trigger in triggers:
64
                rss.append(Rss.objects.get(trigger_id=trigger.id))
65
            return rss
66
        # otherwise return nothing when user is not connected
67
        return TriggerService.objects.none()
68