views.py 7.11 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
from django.contrib import messages
fred's avatar
fred committed
10

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

14
import emissions.views
15
import newsletter.views
16

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

    def get_context_data(self, **kwargs):
        context = super(Home, self).get_context_data(**kwargs)
22
        context['emissions'] = self.request.user.emissions.all().order_by('title')
fred's avatar
fred committed
23
        context['news_categories'] = self.request.user.news_categories.all().order_by('title')
24
        if self.request.user.has_perm('emissions.add_focus'):
25
            context['focused_items'] = Focus.objects.select_related().filter(current=True)
26 27
            context['recent_episodes'] = Episode.objects.select_related(
                    ).order_by('-last_update_timestamp')
28 29 30 31
            context['recent_newsitems'] = NewsItem.objects.select_related(
                    ).order_by('-last_update_timestamp')
            context['recent_soundfiles'] = SoundFile.objects.select_related(
                    ).order_by('-last_update_timestamp')
32

fred's avatar
fred committed
33 34 35
        return context

home = login_required(Home.as_view())
36 37 38 39 40


class EmissionListView(emissions.views.EmissionListView):
    pass

fred's avatar
fred committed
41
emissions_list = login_required(EmissionListView.as_view())
fred's avatar
fred committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56



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())
57 58 59 60 61 62 63


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

        newsletter_body = loader.get_template('newsletter/body.html')
64
        newsletter_subject = loader.get_template('newsletter/subject.txt')
65 66 67 68 69 70 71
        context = Context()

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

        date = datetime.datetime(*date.timetuple()[:3])
72
        date_end = date + datetime.timedelta(days=7)
73 74

        context['date'] = date
75
        context['date_end'] = date_end
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

        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

92
        initial['subject'] = newsletter_subject.render(context)
93 94 95 96
        initial['text'] = newsletter_body.render(context)
        return initial

newsletter_create = NewsletterCreateView.as_view()
97 98 99 100 101 102 103 104


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()
fred's avatar
fred committed
105
        messages.success(self.request, emissions.views.SUCCESS_MESSAGE)
106 107
        if object_type == 'emission':
            emission = Emission.objects.get(id=object_id)
fred's avatar
fred committed
108 109 110 111 112
            try:
                f = Focus.objects.get(emission=emission)
            except Focus.DoesNotExist:
                f = Focus()
            f.current = True
113 114 115 116 117
            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
118 119 120 121 122
            try:
                f = Focus.objects.get(episode=episode)
            except Focus.DoesNotExist:
                f = Focus()
            f.current = True
123 124 125 126 127 128
            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
129 130 131 132 133
            try:
                f = Focus.objects.get(newsitem=newsitem)
            except Focus.DoesNotExist:
                f = Focus()
            f.current = True
134 135 136 137 138
            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
139 140 141 142 143
            try:
                f = Focus.objects.get(soundfile=soundfile)
            except Focus.DoesNotExist:
                f = Focus()
            f.current = True
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
            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()
fred's avatar
fred committed
160
        messages.success(self.request, emissions.views.SUCCESS_MESSAGE)
161 162
        if object_type == 'emission':
            emission = Emission.objects.get(id=object_id)
fred's avatar
fred committed
163 164 165
            focus = Focus.objects.get(emission=emission)
            focus.current = False
            focus.save()
166 167 168
            return reverse('emission-view', kwargs={'slug': emission.slug})
        if object_type == 'episode':
            episode = Episode.objects.get(id=object_id)
fred's avatar
fred committed
169 170 171
            focus = Focus.objects.get(episode=episode)
            focus.current = False
            focus.save()
172 173 174 175
            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
176 177 178
            focus = Focus.objects.get(newsitem=newsitem)
            focus.current = False
            focus.save()
179 180 181
            return reverse('newsitem-view', kwargs={'slug': newsitem.slug})
        if object_type == 'soundfile':
            soundfile = SoundFile.objects.get(id=object_id)
fred's avatar
fred committed
182 183 184
            focus = Focus.objects.get(soundfile=soundfile)
            focus.current = False
            focus.save()
185 186 187 188 189 190
            return reverse('episode-view', kwargs={
                'slug': soundfile.episode.slug,
                'emission_slug': soundfile.episode.emission.slug,
                })

focus_unset = FocusUnsetView.as_view()