Commit 4eed82b9 authored by fred's avatar fred

revamp focus handling to use a special object

parent fe207bb8
......@@ -2,7 +2,7 @@ from django.template import Library
from django.conf import settings
from django.core.urlresolvers import reverse
from emissions.models import Emission, Episode, NewsItem
from emissions.models import Emission, Episode, NewsItem, SoundFile, Focus
register = Library()
......@@ -33,3 +33,64 @@ def context_title(object):
if isinstance(object, Episode):
return object.emission.title
return None
@register.filter
def has_focus(object):
if isinstance(object, NewsItem):
object_type = 'newsitem'
elif isinstance(object, Emission):
object_type = 'emission'
elif isinstance(object, Episode):
object_type = 'episode'
elif isinstance(object, SoundFile):
object_type = 'soundfile'
else:
return False
try:
Focus.objects.get(**{object_type: object})
except Focus.DoesNotExist:
return False
return True
@register.filter
def unset_focus_url(object):
if isinstance(object, NewsItem):
object_type = 'newsitem'
elif isinstance(object, Emission):
object_type = 'emission'
elif isinstance(object, Episode):
object_type = 'episode'
elif isinstance(object, SoundFile):
object_type = 'soundfile'
return reverse('focus-unset', kwargs={
'object_type': object_type,
'object_id': object.id})
@register.filter
def set_focus_url(object):
if isinstance(object, NewsItem):
object_type = 'newsitem'
elif isinstance(object, Emission):
object_type = 'emission'
elif isinstance(object, Episode):
object_type = 'episode'
elif isinstance(object, SoundFile):
object_type = 'soundfile'
return reverse('focus-set', kwargs={
'object_type': object_type,
'object_id': object.id})
@register.filter
def can_focus(object):
if isinstance(object, NewsItem):
return True
elif isinstance(object, Emission):
return True
elif isinstance(object, Episode):
return True
elif isinstance(object, SoundFile):
return True
return False
......@@ -523,5 +523,6 @@ a [class^="icon-"], a [class*=" icon-"] {
.icon-bar-chart:before { content: "\f080 "; }
.icon-search:before { content: "\f002 "; }
.icon-signout:before { content: "\f08b "; }
.icon-pushpin:before { content: "\f08d "; }
.icon-circle-arrow-down:after { content: "  \f0ab"; }
......@@ -52,6 +52,17 @@
<a href="{{ object|online_url }}" target="blank" class="icon-eye-open">Voir en ligne</a>
{% endif %}
{% endblock %}
{% block manage-focus %}
{% if object|can_focus %}
{% if perms.emissions.add_focus %}
{% if object|has_focus %}
<a class="icon-pushpin" href="{{ object|unset_focus_url }}">Retirer le focus</a>
{% else %}
<a class="icon-pushpin" href="{{ object|set_focus_url }}">Mettre en focus</a>
{% endif %}
{% endif %}
{% endif %}
{% endblock %}
{% block more-user-links %}
{% endblock %}
......
......@@ -82,6 +82,29 @@ Aucune actu.
</div>
{% endfor %}
{% if perms.emissions.add_focus %}
<div class="newsitems">
<h3>Focus</h3>
<ul class="episode-list">
{% for focus in focused_items %}
<li>
{% if focus.emission %}
<a href="{% url 'emission-view' slug=focus.emission.slug %}">{{ focus.emission.title }}</a>
{% elif focus.episode %}
<a href="{% url 'episode-view' slug=focus.episode.slug emission_slug=focus.episode.emission.slug %}">{{ focus.episode.title }}</a>
{% elif focus.newsitem %}
<a href="{% url 'newsitem-view' slug=focus.newsitem.slug %}">{{ focus.newsitem.title }}</a>
{% elif focus.soundfile %}
<a href="{% url 'episode-view' slug=focus.soundfile.episode.slug emission_slug=focus.soundfile.episode.emission.slug %}">{{ focus.episode.title }}</a>
{% endif %}
</li>
{% endfor %}
</ul>
</div>
{% endif %}
{% endblock %}
{% block page-end %}
......
......@@ -37,6 +37,11 @@ urlpatterns = patterns('',
url(r'^news/$', 'panikdb.views.news', name='news-list'),
url(r'^focus/set/(?P<object_type>[\w,-]+)/(?P<object_id>\d+)$',
'panikdb.views.focus_set', name='focus-set'),
url(r'^focus/unset/(?P<object_type>[\w,-]+)/(?P<object_id>\d+)$',
'panikdb.views.focus_unset', name='focus-unset'),
url(r'^accounts/logout/', 'django.contrib.auth.views.logout_then_login', name='logout'),
url(r'^accounts/', include('registration.backends.default.urls')),
......
import datetime
from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse
from django.template import loader, Context
from django.views.generic.base import TemplateView
from django.views.generic.base import TemplateView, RedirectView
from django.views.generic.list import ListView
from emissions.models import Emission, NewsCategory, NewsItem, SoundFile, Diffusion
from emissions.models import *
from emissions.utils import period_program
import emissions.views
......@@ -18,6 +20,8 @@ class Home(TemplateView):
context = super(Home, self).get_context_data(**kwargs)
context['emissions'] = self.request.user.emissions.all().order_by('title')
context['news_categories'] = self.request.user.news_categories.all().order_by('title')
if self.request.user.has_perm('emissions.add_focus'):
context['focused_items'] = Focus.objects.select_related()
return context
......@@ -79,3 +83,71 @@ class NewsletterCreateView(newsletter.views.NewsletterCreateView):
return initial
newsletter_create = NewsletterCreateView.as_view()
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)
f = Focus()
f.emission = emission
f.save()
return reverse('emission-view', kwargs={'slug': emission.slug})
if object_type == 'episode':
episode = Episode.objects.get(id=object_id)
f = Focus()
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)
f = Focus()
f.newsitem = newsitem
f.save()
return reverse('newsitem-view', kwargs={'slug': newsitem.slug})
if object_type == 'soundfile':
soundfile = SoundFile.objects.get(id=object_id)
f = Focus()
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)
Focus.objects.get(emission=emission).delete()
return reverse('emission-view', kwargs={'slug': emission.slug})
if object_type == 'episode':
episode = Episode.objects.get(id=object_id)
Focus.objects.get(episode=episode).delete()
return reverse('episode-view', kwargs={
'emission_slug': episode.emission.slug, 'slug': episode.slug})
if object_type == 'newsitem':
newsitem = NewsItem.objects.get(id=object_id)
Focus.objects.get(newsitem=newsitem).delete()
return reverse('newsitem-view', kwargs={'slug': newsitem.slug})
if object_type == 'soundfile':
soundfile = SoundFile.objects.get(id=object_id)
Focus.objects.get(soundfile=soundfile).delete()
return reverse('episode-view', kwargs={
'slug': soundfile.episode.slug,
'emission_slug': soundfile.episode.emission.slug,
})
focus_unset = FocusUnsetView.as_view()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment