Skip to main content

django-eventtools is a lightweight library designed to handle repeating and one-off event occurrences for display on a website.

Installation

Download the source from https://pypi.python.org/pypi/django-eventtools/ and run python setup.py install, or:

> pip install django-eventtools

Django 1.8 or higher is required.

Setup

Given the following models:

from django.db import models

from eventtools.models import BaseEvent, BaseOccurrence


class Event(BaseEvent):
    title = models.CharField(max_length=100)


class Occurrence(BaseOccurrence):
    event = models.ForeignKey(Event)

Usage

Create a sample event & occurrences

>>> from datetime import datetime
>>> from dateutil import rrule
>>> from myapp.models import Event
>>> event = Event.objects.create(title='Test event')
>>> once_off = Occurrence.objects.create(
        event=event,
        start=datetime(2016, 1, 1, 12, 0),
        end=datetime(2016, 1, 1, 2, 0))
>>> christmas = Occurrence.objects.create(
        event=event,
        start=datetime(2015, 12, 25, 7, 0),
        end=datetime(2015, 12, 25, 22, 0),
        repeat.rrule.YEARLY)
>>> daily = Occurrence.objects.create(
        event=event,
        start=datetime(2016, 1, 1, 7, 0),
        end=datetime(2016, 1, 1, 8, 0),
        repeat.rrule.DAILY)

Event and Occurrence instances, and their associated querysets, all support the all_occurrences method, which takes two optional arguments - from_date and to_date, which may be dates or datetimes. from_date defaults to the current day, to_date to None. The method returns a python generator yielding tuples in the format (start, end, data) - for example:

>>> Event.objects.all().all_occurrences()
>>> event.all_occurrences(from_date=datetime(2015, 1, 1, 10, 0))
>>> event.occurrence_set.all().all_occurrences(to_date=date(2016, 1, 1))
>>> occurrence.all_occurrences(from_date=date(2016, 1, 1),
                               to_date=date(2016, 12, 31))

A next_occurrence method is also provided, taking the same arguments, but returning a single occurrence tuple.

>>> event.next_occurrence()
>>> event.next_occurrence(from_date=date(2016, 1, 1))

Queryset filtering

Querysets can also be filtered, but due to uncertainty with repetitions, from_date filtering is only an approximation (to_date filtering is accurate). If you need a queryset filtered exactly, pass exact=True - this will filter using occurrences but still return a queryset - but be careful with this as it may be very slow. For example

>>> Event.objects.for_period(from_date=date(2015, 1, 1),
                             to_date=date(2015, 12, 31))
>>> event.occurrence_set.for_period(from_date=date(2015, 1, 1), exact=True)

Adding additional data to occurrence tuples

If you need more information about the occurrence than just a pair of dates, add an occurrence_data property to your BaseOccurrence subclass - i.e.

class Occurrence(BaseOccurrence):
    event = models.ForeignKey(Event)

    @property
    def occurrence_data(self):
        return {
            'event_title': self.event.title,
            'image': self.event.image,
        }

The result will be appended to the occurrence tuple, i.e. (start, end, data)

Running tests

Assuming virtualenvwrapper is installed:

> cd path-to/django-eventtools
> mkvirtualenv test
> ./setup.py install
> ./runtests.py

Todo

✔ Handle from_date more efficiently in BaseOccurrence.all_occurrences - Test for_period methods with exact=False - Python 3 tests (tox) - Docs

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-eventtools-0.9.2.tar.gz (8.4 kB view details)

Uploaded Source

File details

Details for the file django-eventtools-0.9.2.tar.gz.

File metadata

File hashes

Hashes for django-eventtools-0.9.2.tar.gz
Algorithm Hash digest
SHA256 d73e6eaf5934d0bda223bb31d06c21ac560bcd67d96197956e882f87489cc4d7
MD5 7b04bed69440140d6c7bae9c53fe1def
BLAKE2b-256 b23fd826b7fe8ac3ce9d7b27b0037b6398c4110011ca727c697be47aab96a4e2

See more details on using hashes here.

Release history Release notifications | RSS feed

1.0.3

2 files

1.0.2

1 file

1.0.1

1 file

1.0.0

1 file

0.9.11

1 file

0.9.10

1 file

0.9.9

1 file

0.9.8

1 file

0.9.7

1 file

0.9.6

1 file

0.9.5

1 file

0.9.4

1 file

0.9.3

1 file

This release

0.9.2 This release

1 file

0.9.1

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page