From 16f510696ae641a247afe2cd7e2d650189eded31 Mon Sep 17 00:00:00 2001 From: kimarakov Date: Mon, 1 Jan 2018 11:13:00 -0800 Subject: [PATCH] Remove unnecessary django-annoying dependency The dependency does not add any useful abstractions for this project. Better to reduce the number of dependencies than to add one for a simple one off function. Allows users of django-scheduler to require fewer packages. --- requirements.txt | 1 - schedule/settings.py | 24 ++++++++++---------- schedule/utils.py | 54 ++++++++++++++++---------------------------- setup.py | 1 - 4 files changed, 32 insertions(+), 48 deletions(-) diff --git a/requirements.txt b/requirements.txt index 1821f00..4acb8bd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,5 @@ coverage>=3.7.1 coveralls>=0.5 -django-annoying>=0.8.0 Django>=1.11 flake8 icalendar>=3.8.4 diff --git a/schedule/settings.py b/schedule/settings.py index f8aef7e..71d6132 100644 --- a/schedule/settings.py +++ b/schedule/settings.py @@ -1,14 +1,14 @@ -from annoying.functions import get_config +from django.conf import settings # 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 = get_config('SHOW_CANCELLED_OCCURRENCES', False) +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 occurrence -CHECK_EVENT_PERM_FUNC = get_config('CHECK_EVENT_PERM_FUNC', None) +CHECK_EVENT_PERM_FUNC = getattr(settings, 'CHECK_EVENT_PERM_FUNC', None) if not CHECK_EVENT_PERM_FUNC: def check_event_permission(ob, user): @@ -17,17 +17,17 @@ def check_event_permission(ob, user): CHECK_EVENT_PERM_FUNC = check_event_permission # Callable used to check if a user has edit permissions to occurrence -CHECK_OCCURRENCE_PERM_FUNC = get_config('CHECK_OCCURRENCE_PERM_FUNC', None) +CHECK_OCCURRENCE_PERM_FUNC = getattr(settings, 'CHECK_OCCURRENCE_PERM_FUNC', None) if not CHECK_OCCURRENCE_PERM_FUNC: def check_occurrence_permission(ob, user): return CHECK_EVENT_PERM_FUNC(ob.event, user) CHECK_OCCURRENCE_PERM_FUNC = check_occurrence_permission -CALENDAR_VIEW_PERM = get_config('CALENDAR_VIEW_PERM', False) +CALENDAR_VIEW_PERM = getattr(settings, 'CALENDAR_VIEW_PERM', False) # Callable used to check if a user has edit permissions to calendar -CHECK_CALENDAR_PERM_FUNC = get_config('CHECK_CALENDAR_PERM_FUNC', None) +CHECK_CALENDAR_PERM_FUNC = getattr(settings, 'CHECK_CALENDAR_PERM_FUNC', None) if not CHECK_CALENDAR_PERM_FUNC: def check_calendar_permission(ob, user): @@ -35,13 +35,13 @@ def check_calendar_permission(ob, user): CHECK_CALENDAR_PERM_FUNC = check_calendar_permission -CALENDAR_VIEW_PERM = get_config('CALENDAR_VIEW_PERM', False) +CALENDAR_VIEW_PERM = getattr(settings, 'CALENDAR_VIEW_PERM', False) # 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 = get_config('GET_EVENTS_FUNC', None) +GET_EVENTS_FUNC = getattr(settings, 'GET_EVENTS_FUNC', None) if not GET_EVENTS_FUNC: def get_events(request, calendar): return calendar.event_set.prefetch_related('occurrence_set', 'rule') @@ -49,11 +49,11 @@ def get_events(request, calendar): GET_EVENTS_FUNC = get_events # URL to redirect to to after an occurrence is canceled -OCCURRENCE_CANCEL_REDIRECT = get_config('OCCURRENCE_CANCEL_REDIRECT', None) +OCCURRENCE_CANCEL_REDIRECT = getattr(settings, 'OCCURRENCE_CANCEL_REDIRECT', None) -SCHEDULER_PREVNEXT_LIMIT_SECONDS = get_config('SCHEDULER_PREVNEXT_LIMIT_SECONDS', 62208000) # two years +SCHEDULER_PREVNEXT_LIMIT_SECONDS = getattr(settings, 'SCHEDULER_PREVNEXT_LIMIT_SECONDS', 62208000) # two years -USE_FULLCALENDAR = get_config('USE_FULLCALENDAR', False) +USE_FULLCALENDAR = getattr(settings, 'USE_FULLCALENDAR', False) # This name is used when a new event is created through selecting in fullcalendar -EVENT_NAME_PLACEHOLDER = get_config('EVENT_NAME_PLACEHOLDER', 'Event Name') +EVENT_NAME_PLACEHOLDER = getattr(settings, 'EVENT_NAME_PLACEHOLDER', 'Event Name') diff --git a/schedule/utils.py b/schedule/utils.py index e40c051..c3496d1 100644 --- a/schedule/utils.py +++ b/schedule/utils.py @@ -1,7 +1,6 @@ import heapq from functools import wraps -from annoying.functions import get_object_or_None from django.conf import settings from django.http import HttpResponseNotFound, HttpResponseRedirect from django.utils import timezone @@ -93,37 +92,31 @@ def get_additional_occurrences(self, start, end): return [occ for _, occ in list(self.lookup.items()) if (occ.start < end and occ.end >= start and not occ.cancelled)] +def get_kwarg_or_param(request, kwargs, key): + value = None + try: + value = kwargs[key] + except KeyError: + if request.method == 'GET': + value = request.GET.get(key) + elif request.method == 'POST': + value = request.POST.get(key) + return value + + def get_occurrence(request, *args, **kwargs): from schedule.models import Occurrence - occurrence = None - if 'occurrence_id' in kwargs: - occurrence = get_object_or_None(Occurrence, id=kwargs['occurrence_id']) - elif request.GET: - occurrence = get_object_or_None( - Occurrence, - id=request.GET.get('occurrence_id')) - elif request.POST: - occurrence = get_object_or_None( - Occurrence, - id=request.POST.get('occurrence_id')) - return occurrence + occurrence_id = get_kwarg_or_param(request, kwargs, 'occurrence_id') + return Occurrence.objects.filter(pk=occurrence_id).first() if occurrence_id else None def get_event(occurrence, request, *args, **kwargs): from schedule.models import Event - event = None if occurrence: event = occurrence.event - elif 'event_id' in kwargs: - event = get_object_or_None(Event, id=kwargs['event_id']) - elif request.GET: - event = get_object_or_None( - Event, - id=request.GET.get('event_id')) - elif request.POST: - event = get_object_or_None( - Event, - id=request.POST.get('event_id')) + else: + event_id = get_kwarg_or_param(request, kwargs, 'event_id') + event = Event.objects.filter(pk=event_id).first() if event_id else None return event @@ -132,16 +125,9 @@ def get_calendar(event, request, *args, **kwargs): calendar = None if event: calendar = event.calendar - elif 'calendar_slug' in kwargs: - calendar = get_object_or_None(Calendar, slug=kwargs['calendar_slug']) - elif request.GET: - calendar = get_object_or_None( - Calendar, - slug=request.GET.get('calendar_slug')) - elif request.POST: - calendar = get_object_or_None( - Calendar, - slug=request.POST.get('calendar_slug')) + else: + calendar_slug = get_kwarg_or_param(request, kwargs, 'calendar_slug') + calendar = Calendar.objects.filter(slug=calendar_slug).first() if calendar_slug else None return calendar diff --git a/setup.py b/setup.py index 931ec7f..a5b22d1 100755 --- a/setup.py +++ b/setup.py @@ -42,7 +42,6 @@ 'python-dateutil>=2.1', 'pytz>=2013.9', 'icalendar>=3.8.4', - 'django-annoying>=0.8.0', ], license='BSD', test_suite='runtests.runtests',