Permalink
Browse files

Refactored all settings manipulation in to a schedule.conf.settings m…

…odule to make it easier to keep them straight. This also has the effect of providing default settings and thus simplifying installation
  • Loading branch information...
1 parent 7561fdd commit 2ca6b1f6181b04fc3186ec9b30e24a1d837f4d3a @winhamwr winhamwr committed Jul 2, 2009
No changes.
View
@@ -0,0 +1,47 @@
+from django.utils.translation import ugettext, ugettext_lazy as _
+from django.conf import settings
+from django.core.exceptions import ImproperlyConfigured
+
+fdow_default = 0 # Sunday
+
+# Look for FIRST_DAY_OF_WEEK as a locale setting
+fdow = ugettext('FIRST_DAY_OF_WEEK')
+try:
+ FIRST_DAY_OF_WEEK = int(fdow)
+except ValueError:
+ # Let's try our settings
+ fdow = settings.get('FIRST_DAY_OF_WEEK', fdow_default)
+ FIRST_DAY_OF_WEEK = int(fdow)
+except ValueError:
+ raise ImproperlyConfigured("FIRST_DAY_OF_WEEK must be an integer between 0 and 6")
+
+
+# whether to display cancelled occurrences
+# (if they are displayed then they have a css class "cancelled")
+# this controls behaviour of Period.classify_occurrence method
+SHOW_CANCELLED_OCCURRENCES = getattr(settings, 'SHOW_CANCELLED_OCCURRENCES',
+ False)
+
+# Callable used to check if a user has edit permissions to event
+# (and occurrence). Used by check_edit_permission decorator
+# if ob==None we check permission to add events
+CHECK_PERMISSION_FUNC = getattr(settings, 'CHECK_PERMISSION_FUNC', None)
+if not CHECK_PERMISSION_FUNC:
+ def check_edit_permission(ob, user):
+ return user.is_authenticated()
+
+ CHECK_PERMISSION_FUNC = check_edit_permission
+
+# Callable used to customize the event list given for a calendar and user
+# (e.g. all events on that calendar, those events plus another calendar's events,
+# or the events filtered based on user permissions)
+# Imports have to be placed within the function body to avoid circular imports
+GET_EVENTS_FUNC = getattr(settings, 'GET_EVENTS_FUNC', None)
+if not GET_EVENTS_FUNC:
+ def get_events(request, calendar):
+ return calendar.event_set.all()
+
+ GET_EVENTS_FUNC = get_events
+
+# URL to redirect to to after an occurrence is canceled
+OCCURRENCE_CANCEL_REDIRECT = getattr(settings, 'OCCURRENCE_CANCEL_REDIRECT', None)
View
@@ -3,25 +3,13 @@
from django.template.defaultfilters import date
from django.utils.translation import ugettext, ugettext_lazy as _
from django.utils.dates import WEEKDAYS, WEEKDAYS_ABBR
-from django.conf import settings
+from schedule.conf.settings import FIRST_DAY_OF_WEEK, SHOW_CANCELLED_OCCURRENCES
from schedule.models import Occurrence
from schedule.utils import OccurrenceReplacer
-# Look for FIRST_DAY_OF_WEEK as a locale setting
-first_day_of_week = ugettext('FIRST_DAY_OF_WEEK')
-if first_day_of_week == 'FIRST_DAY_OF_WEEK':
- # FIRST_DAY_OF_WEEK was not set in the locale, try the settings
- first_day_of_week = getattr(settings, 'FIRST_DAY_OF_WEEK', "0")
-
-try:
- first_day_of_week = int(first_day_of_week)
-except:
- # default to Sunday
- first_day_of_week = 0
-
weekday_names = []
weekday_abbrs = []
-if first_day_of_week == 1:
+if FIRST_DAY_OF_WEEK == 1:
# The calendar week starts on Monday
for i in range(7):
weekday_names.append( WEEKDAYS[i] )
@@ -79,8 +67,8 @@ def get_persisted_occurrences(self):
return self._persisted_occurrences
def classify_occurrence(self, occurrence):
- if occurrence.cancelled and not settings.SHOW_CANCELLED_OCCURRENCES:
- return
+ if occurrence.cancelled and not SHOW_CANCELLED_OCCURRENCES:
+ return
if occurrence.start > self.end or occurrence.end < self.start:
return None
started = False
@@ -128,7 +116,7 @@ def __init__(self, events, date=None, parent_persisted_occurrences=None):
date = datetime.datetime.now()
start, end = self._get_year_range(date)
super(Year, self).__init__(events, start, end, parent_persisted_occurrences)
-
+
def get_months(self):
months = []
month_start = self.start
@@ -138,27 +126,27 @@ def get_months(self):
months.append(month)
month_start = month.next_month()
return months
-
+
def next_year(self):
return self.end
next = next_year
-
+
def prev_year(self):
return datetime.datetime(
self.start.year-1, self.start.month, self.start.day)
prev = prev_year
-
+
def _get_year_range(self, year):
start = datetime.datetime(year.year, datetime.datetime.min.month,
datetime.datetime.min.day)
end = datetime.datetime(year.year+1, datetime.datetime.min.month,
datetime.datetime.min.day)
return start, end
-
+
def __unicode__(self):
return self.start.strftime('%Y')
-
-
+
+
class Month(Period):
"""
@@ -170,7 +158,7 @@ def __init__(self, events, date=None, parent_persisted_occurrences=None,
if date is None:
date = datetime.datetime.now()
start, end = self._get_month_range(date)
- super(Month, self).__init__(events, start, end,
+ super(Month, self).__init__(events, start, end,
parent_persisted_occurrences, occurrence_pool)
def get_weeks(self):
@@ -204,7 +192,7 @@ def get_day(self, daynumber ):
def next_month(self):
return self.end
next = next_month
-
+
def prev_month(self):
return (self.start - datetime.timedelta(days=1)).replace(day=1)
prev = prev_month
@@ -241,14 +229,14 @@ class Week(Period):
"""
The Week period that has functions for retrieving Day periods within it
"""
- def __init__(self, events, date=None, parent_persisted_occurrences=None,
+ def __init__(self, events, date=None, parent_persisted_occurrences=None,
occurrence_pool=None):
if date is None:
date = datetime.datetime.now()
start, end = self._get_week_range(date)
- super(Week, self).__init__(events, start, end,
+ super(Week, self).__init__(events, start, end,
parent_persisted_occurrences, occurrence_pool)
-
+
def prev_week(self):
return self.start - datetime.timedelta(days=7)
prev = prev_week
@@ -274,7 +262,7 @@ def _get_week_range(self, week):
start = datetime.datetime.combine(week, datetime.time.min)
# Adjust the start datetime to Monday or Sunday of the current week
sub_days = 0
- if first_day_of_week == 1:
+ if FIRST_DAY_OF_WEEK == 1:
# The week begins on Monday
sub_days = start.isoweekday() - 1
else:
@@ -300,7 +288,7 @@ def __init__(self, events, date=None, parent_persisted_occurrences=None,
if date is None:
date = datetime.datetime.now()
start, end = self._get_day_range(date)
- super(Day, self).__init__(events, start, end,
+ super(Day, self).__init__(events, start, end,
parent_persisted_occurrences, occurrence_pool)
def _get_day_range(self, date):
@@ -3,6 +3,7 @@
from django import template
from django.core.urlresolvers import reverse
from django.utils.dateformat import format
+from schedule.conf.settings import CHECK_PERMISSION_FUNC
from schedule.models import Calendar
from schedule.periods import weekday_names, weekday_abbrs, Month
@@ -48,7 +49,7 @@ def daily_table( context, day, width, width_slot, height, start=8, end=20, incre
increment - size of a time slot (in minutes)
"""
user = context['request'].user
- context['addable'] = settings.CHECK_PERMISSION_FUNC(None, user)
+ context['addable'] = CHECK_PERMISSION_FUNC(None, user)
width_occ = width - width_slot
day_part = day.get_time_slot(day.start + datetime.timedelta(hours=start), day.start + datetime.timedelta(hours=end))
occurrences = day_part.get_occurrences()
@@ -78,7 +79,7 @@ def options(context, occurrence ):
})
context['view_occurrence'] = occurrence.get_absolute_url()
user = context['request'].user
- if settings.CHECK_PERMISSION_FUNC(occurrence.event, user):
+ if CHECK_PERMISSION_FUNC(occurrence.event, user):
context['edit_occurrence'] = occurrence.get_edit_url()
print context['edit_occurrence']
context['cancel_occurrence'] = occurrence.get_cancel_url()
@@ -240,13 +241,13 @@ def _cook_occurrences(period, occs, width, height):
# calculate position and dimensions
for o in occs:
# number of overlapping occurrences
- o.max = len([n for n in occs if not(n.end<=o.start or n.start>=o.end)])
+ o.max = len([n for n in occs if not(n.end<=o.start or n.start>=o.end)])
for o in occs:
o.cls = o.data['class']
o.real_start = max(o.start, period.start)
o.real_end = min(o.end, period.end)
# number of "columns" is a minimum number of overlaps for each overlapping group
- o.max = min([n.max for n in occs if not(n.end<=o.start or n.start>=o.end)] or [1])
+ o.max = min([n.max for n in occs if not(n.end<=o.start or n.start>=o.end)] or [1])
w = int(width / (o.max))
o.width = w - 2
o.left = w * o.level
@@ -2,7 +2,6 @@
import os
from django.test import TestCase
-from django.conf import settings
from django.core.urlresolvers import reverse
from schedule.forms import GlobalSplitDateTimeWidget
@@ -2,7 +2,6 @@
import os
from django.test import TestCase
-from django.conf import settings
from django.core.urlresolvers import reverse
from schedule.forms import GlobalSplitDateTimeWidget
@@ -31,15 +30,15 @@ def setUp(self):
'end_recurring_period' : datetime.datetime(2008, 5, 5, 0, 0),
'calendar': cal
}
-
-
+
+
def test_recurring_event_get_occurrences(self):
recurring_event = Event(**self.recurring_data)
occurrences = recurring_event.get_occurrences(start=datetime.datetime(2008, 1, 12, 0, 0),
end=datetime.datetime(2008, 1, 20, 0, 0))
self.assertEquals(["%s to %s" %(o.start, o.end) for o in occurrences],
['2008-01-12 08:00:00 to 2008-01-12 09:00:00', '2008-01-19 08:00:00 to 2008-01-19 09:00:00'])
-
+
def test_event_get_occurrences_after(self):
recurring_event=Event(**self.recurring_data)
recurring_event.save()
@@ -48,7 +47,7 @@ def test_event_get_occurrences_after(self):
occurrence = occurrences[0]
occurrence2 = recurring_event.occurrences_after(datetime.datetime(2008,1,5)).next()
self.assertEqual(occurrence, occurrence2)
-
+
def test_get_occurrence(self):
event = Event(**self.recurring_data)
event.save()
@@ -84,7 +83,7 @@ def setUp(self):
self.recurring_event.save()
self.start = datetime.datetime(2008, 1, 12, 0, 0)
self.end = datetime.datetime(2008, 1, 27, 0, 0)
-
+
def test_presisted_occurrences(self):
occurrences = self.recurring_event.get_occurrences(start=self.start,
end=self.end)
@@ -94,7 +93,7 @@ def test_presisted_occurrences(self):
end=self.end)
self.assertTrue(occurrences[0].pk)
self.assertFalse(occurrences[1].pk)
-
+
def test_moved_occurrences(self):
occurrences = self.recurring_event.get_occurrences(start=self.start,
end=self.end)
@@ -117,7 +116,7 @@ def test_moved_occurrences(self):
period_post = Period([self.recurring_event], span_post[0], span_post[1])
self.assertFalse(period_pre.has_occurrences())
self.assertTrue(period_post.has_occurrences())
-
+
def test_cancelled_occurrences(self):
occurrences = self.recurring_event.get_occurrences(start=self.start,
end=self.end)
@@ -130,4 +129,4 @@ def test_cancelled_occurrences(self):
occurrences = self.recurring_event.get_occurrences(start=self.start,
end=self.end)
self.assertFalse(occurrences[2].cancelled)
-
+
@@ -2,7 +2,6 @@
import os
from django.test import TestCase
-from django.conf import settings
from django.core.urlresolvers import reverse
from schedule.forms import GlobalSplitDateTimeWidget
@@ -18,7 +17,7 @@ def setUp(self):
daily.save()
cal = Calendar(name="MyCal")
cal.save()
-
+
self.event1 = Event(**{
'title': 'Weekly Event',
'start': datetime.datetime(2009, 4, 1, 8, 0),
@@ -37,7 +36,7 @@ def setUp(self):
'calendar': cal
})
self.event2.save()
-
+
def test_occurrences_after(self):
eml = EventListManager([self.event1, self.event2])
occurrences = eml.occurrences_after(datetime.datetime(2009,4,1,0,0))
Oops, something went wrong.

0 comments on commit 2ca6b1f

Please sign in to comment.