Skip to content

Commit

Permalink
fix #49 avoid infinite links with a configuration limit
Browse files Browse the repository at this point in the history
  • Loading branch information
llazzaro committed Jul 11, 2015
1 parent 6f749b3 commit 17b56a2
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 23 deletions.
2 changes: 2 additions & 0 deletions schedule/conf/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ def get_events(request, calendar):

# URL to redirect to to after an occurrence is canceled
OCCURRENCE_CANCEL_REDIRECT = get_config('OCCURRENCE_CANCEL_REDIRECT', None)

SCHEDULER_PREVNEXT_LIMIT_SECONDS = get_config('SCHEDULER_PREVNEXT_LIMIT_SECONDS', 62208000) # two years
13 changes: 4 additions & 9 deletions schedule/templates/schedule/_prevnext.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
{% load scheduletags staticfiles %}
<div class="prevnext">
<a href="{% prev_url target slug period %}">
<img align="top" border="0" src="{% static "schedule/img/left_mod.png" %}"/>
</a>
&nbsp; <b>{{period_name}}</b> &nbsp;
<a href="{% next_url target slug period %}">
<img align="top" border="0" src="{% static "schedule/img/right_mod.png" %}"/>
</a>
<div class="row row-centered">
{% prev_url target calendar period %}
&nbsp; <b>{{period_name}}</b> &nbsp;
{% next_url target calendar period %}
</div>

4 changes: 2 additions & 2 deletions schedule/templates/schedule/calendar_day.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
</a>
</div>
<div class="tablewrapper">
<div class="calendarname">{{ calendar.name }}</div>
{% prevnext "day_calendar" calendar.slug periods.day "l, F d, Y" %}
<h1>{{ calendar.name }}</h1>
{% prevnext "day_calendar" calendar periods.day "l, F d, Y" %}
<div class="now">
<a href="{% url "day_calendar" calendar.slug %}">
{% trans "Today" %}
Expand Down
2 changes: 1 addition & 1 deletion schedule/templates/schedule/calendar_month.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{% include "schedule/_dialogs.html" %}
<div class="tablewrapper">
<div class="calendarname">{{ calendar.name }}</div>
{% prevnext "month_calendar" calendar.slug periods.month "F Y"%}
{% prevnext "month_calendar" calendar periods.month "F Y"%}
<div class="now">
<a href="{% url "month_calendar" calendar.slug %}">
{% trans "This month" %}
Expand Down
2 changes: 1 addition & 1 deletion schedule/templates/schedule/calendar_tri_month.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% block body %}
<div class="tablewrapper">
<div class="calendarname">{{ calendar.name }}</div>
{% prevnext "tri_month_calendar" calendar.slug periods.month "F Y"%}
{% prevnext "tri_month_calendar" calendar periods.month "F Y"%}
<div class="now">
<a href="{% url "tri_month_calendar" calendar.slug %}">
{% trans "This month" %}
Expand Down
2 changes: 1 addition & 1 deletion schedule/templates/schedule/calendar_week.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<div class="tablewrapper">
<div class="calendarname">{{ calendar.name }}</div>
{% prevnext "week_calendar" calendar.slug periods.week "\W\e\ek W, M Y" %}
{% prevnext "week_calendar" calendar periods.week "\W\e\ek W, M Y" %}
<div class="now">
<a href="{% url "week_calendar" calendar.slug %}">
{% trans "This week" %}
Expand Down
2 changes: 1 addition & 1 deletion schedule/templates/schedule/calendar_year.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% block body %}
<div class="tablewrapper">
<div class="calendarname">{{ calendar.name }}</div>
{% prevnext "year_calendar" calendar.slug periods.year "Y" %}
{% prevnext "year_calendar" calendar periods.year "Y" %}
<table align="center">
<tr>
{% for month in periods.year.get_months %}
Expand Down
31 changes: 23 additions & 8 deletions schedule/templatetags/scheduletags.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
from django.utils.dateformat import format
from django.utils.html import conditional_escape
from django.utils.safestring import mark_safe
from schedule.conf.settings import CHECK_EVENT_PERM_FUNC, CHECK_CALENDAR_PERM_FUNC
from django.utils import timezone

from schedule.conf.settings import CHECK_EVENT_PERM_FUNC, CHECK_CALENDAR_PERM_FUNC, SCHEDULER_PREVNEXT_LIMIT_SECONDS
from schedule.models import Calendar
from schedule.periods import weekday_names, weekday_abbrs

Expand Down Expand Up @@ -196,25 +198,38 @@ def querystring_for_date(date, num=6, autoescape=None):


@register.simple_tag
def prev_url(target, slug, period):
return '%s%s' % (
def prev_url(target, calendar, period):
now = timezone.now()
delta = now - period.prev().start
slug = calendar.slug
if delta.total_seconds() > SCHEDULER_PREVNEXT_LIMIT_SECONDS:
return ''

return '<a href="%s%s"><span class="glyphicon glyphicon-circle-arrow-left" /></a>' % (
reverse(target, kwargs=dict(calendar_slug=slug)),
querystring_for_date(period.prev().start, autoescape=True))


@register.simple_tag
def next_url(target, slug, period):
return '%s%s' % (
def next_url(target, calendar, period):
now = timezone.now()
slug = calendar.slug

delta = period.next().start - now
if delta.total_seconds() > SCHEDULER_PREVNEXT_LIMIT_SECONDS:
return ''

return '<a href="%s%s"><span class="glyphicon glyphicon-circle-arrow-right" /></a>' % (
reverse(target, kwargs=dict(calendar_slug=slug)),
querystring_for_date(next(period).start, autoescape=True))
querystring_for_date(period.next().start, autoescape=True))


@register.inclusion_tag("schedule/_prevnext.html")
def prevnext(target, slug, period, fmt=None):
def prevnext(target, calendar, period, fmt=None):
if fmt is None:
fmt = settings.DATE_FORMAT
context = {
'slug': slug,
'calendar': calendar,
'period': period,
'period_name': format(period.start, fmt),
'target': target,
Expand Down

0 comments on commit 17b56a2

Please sign in to comment.