views.py 6.55 KB
Newer Older
1 2
import datetime

fred's avatar
fred committed
3
from django.contrib.auth.decorators import login_required
4 5
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse
6
from django.template import loader, Context
7
from django.views.generic.base import TemplateView, RedirectView
fred's avatar
fred committed
8
from django.views.generic.list import ListView
fred's avatar
fred committed
9

10
from emissions.models import *
11
from emissions.utils import period_program
fred's avatar
fred committed
12

13
import emissions.views
14
import newsletter.views
15

fred's avatar
fred committed
16 17 18 19 20
class Home(TemplateView):
    template_name = 'home.html'

    def get_context_data(self, **kwargs):
        context = super(Home, self).get_context_data(**kwargs)
21
        context['emissions'] = self.request.user.emissions.all().order_by('title')
fred's avatar
fred committed
22
        context['news_categories'] = self.request.user.news_categories.all().order_by('title')
23
        if self.request.user.has_perm('emissions.add_focus'):
24
            context['focused_items'] = Focus.objects.select_related().filter(current=True)
25

fred's avatar
fred committed
26 27 28
        return context

home = login_required(Home.as_view())
29 30 31 32 33 34


class EmissionListView(emissions.views.EmissionListView):
    pass

emissions = login_required(EmissionListView.as_view())
fred's avatar
fred committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49



class NewsListView(ListView):
    model = NewsCategory

    def get_queryset(self):
        return NewsCategory.objects.order_by('title')

    def get_context_data(self, **kwargs):
        context = super(NewsListView, self).get_context_data(**kwargs)
        context['unsorted_newsitems'] = NewsItem.objects.filter(category__isnull=True).order_by('title')
        return context

news = login_required(NewsListView.as_view())
50 51 52 53 54 55 56


class NewsletterCreateView(newsletter.views.NewsletterCreateView):
    def get_initial(self):
        initial = super(NewsletterCreateView, self).get_initial()

        newsletter_body = loader.get_template('newsletter/body.html')
57
        newsletter_subject = loader.get_template('newsletter/subject.txt')
58 59 60 61 62 63 64
        context = Context()

        date = datetime.datetime.today()
        if date.weekday() > 0:
            date = date + datetime.timedelta(days=7-date.weekday())

        date = datetime.datetime(*date.timetuple()[:3])
65
        date_end = date + datetime.timedelta(days=7)
66 67

        context['date'] = date
68
        context['date_end'] = date_end
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

        context['soundfiles'] = SoundFile.objects.filter(
                podcastable=True, fragment=False).select_related().filter(
                        creation_timestamp__gte=date-datetime.timedelta(days=7))

        context['diffusions'] = list(Diffusion.objects.select_related().filter(
                datetime__gte=date,
                datetime__lte=date+datetime.timedelta(days=7)).order_by('datetime'))

        # remove rebroadcasts made during the same week
        seen_episodes = {}
        for diff in context['diffusions'][:]:
            if diff.episode.id in seen_episodes:
                context['diffusions'].remove(diff)
            seen_episodes[diff.episode.id] = True

85
        initial['subject'] = newsletter_subject.render(context)
86 87 88 89
        initial['text'] = newsletter_body.render(context)
        return initial

newsletter_create = NewsletterCreateView.as_view()
90 91 92 93 94 95 96 97 98 99


class FocusSetView(RedirectView):
    permanent = False

    def get_redirect_url(self, object_type, object_id):
        if not self.request.user.has_perm('emissions.add_focus'):
            raise PermissionDenied()
        if object_type == 'emission':
            emission = Emission.objects.get(id=object_id)
fred's avatar
fred committed
100 101 102 103 104
            try:
                f = Focus.objects.get(emission=emission)
            except Focus.DoesNotExist:
                f = Focus()
            f.current = True
105 106 107 108 109
            f.emission = emission
            f.save()
            return reverse('emission-view', kwargs={'slug': emission.slug})
        if object_type == 'episode':
            episode = Episode.objects.get(id=object_id)
fred's avatar
fred committed
110 111 112 113 114
            try:
                f = Focus.objects.get(episode=episode)
            except Focus.DoesNotExist:
                f = Focus()
            f.current = True
115 116 117 118 119 120
            f.episode = episode
            f.save()
            return reverse('episode-view', kwargs={
                'emission_slug': episode.emission.slug, 'slug': episode.slug})
        if object_type == 'newsitem':
            newsitem = NewsItem.objects.get(id=object_id)
fred's avatar
fred committed
121 122 123 124 125
            try:
                f = Focus.objects.get(newsitem=newsitem)
            except Focus.DoesNotExist:
                f = Focus()
            f.current = True
126 127 128 129 130
            f.newsitem = newsitem
            f.save()
            return reverse('newsitem-view', kwargs={'slug': newsitem.slug})
        if object_type == 'soundfile':
            soundfile = SoundFile.objects.get(id=object_id)
fred's avatar
fred committed
131 132 133 134 135
            try:
                f = Focus.objects.get(soundfile=soundfile)
            except Focus.DoesNotExist:
                f = Focus()
            f.current = True
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
            f.soundfile = soundfile
            f.save()
            return reverse('episode-view', kwargs={
                'slug': soundfile.episode.slug,
                'emission_slug': soundfile.episode.emission.slug,
                })

focus_set = FocusSetView.as_view()


class FocusUnsetView(RedirectView):
    permanent = False

    def get_redirect_url(self, object_type, object_id):
        if not self.request.user.has_perm('emissions.delete_focus'):
            raise PermissionDenied()
        if object_type == 'emission':
            emission = Emission.objects.get(id=object_id)
fred's avatar
fred committed
154 155 156
            focus = Focus.objects.get(emission=emission)
            focus.current = False
            focus.save()
157 158 159
            return reverse('emission-view', kwargs={'slug': emission.slug})
        if object_type == 'episode':
            episode = Episode.objects.get(id=object_id)
fred's avatar
fred committed
160 161 162
            focus = Focus.objects.get(episode=episode)
            focus.current = False
            focus.save()
163 164 165 166
            return reverse('episode-view', kwargs={
                'emission_slug': episode.emission.slug, 'slug': episode.slug})
        if object_type == 'newsitem':
            newsitem = NewsItem.objects.get(id=object_id)
fred's avatar
fred committed
167 168 169
            focus = Focus.objects.get(newsitem=newsitem)
            focus.current = False
            focus.save()
170 171 172
            return reverse('newsitem-view', kwargs={'slug': newsitem.slug})
        if object_type == 'soundfile':
            soundfile = SoundFile.objects.get(id=object_id)
fred's avatar
fred committed
173 174 175
            focus = Focus.objects.get(soundfile=soundfile)
            focus.current = False
            focus.save()
176 177 178 179 180 181
            return reverse('episode-view', kwargs={
                'slug': soundfile.episode.slug,
                'emission_slug': soundfile.episode.emission.slug,
                })

focus_unset = FocusUnsetView.as_view()