Friday, September 19, 2014

Re: Retrieving recurring events using Django queries

hey Stodge,

Interesting question :)

My guess is there are probably a number of approaches one can take. Of
course, processing all your entries in python "on the fly" would
probably get to be quite an intensive operation.

One possible idea maybe to create a "cache" table of sorts where you
pre-calculate and store all possible dates for a CalendarEntry.

Something like:

class EventDate(models.Model):
date = models.DateField(..)
calendarentry = models.ForeignKey(CalendarEntry)

Then, in the post_save of CalendarEntry, you write some code to
calculate all possible dates for the event and populate the EventDate
table. The problem is:
a> If the CalendarEntry does not have an end_date, you obviously
cannot calculate all dates until the end of eternity - you would have
to come up with a reasonable "end date" beyond which queries are not
possible and only populate EventDate until that point (perhaps
updating it for the future with a cron job or some such .. )
b> Generating the EventDates and the associated database INSERTs
could get quite intensive, especially, for say, a daily event for the
next 5 years, etc. If that gets to be a problem, you may need to do
this out of the request-response cycle, using for eg. django-celery.

Once you have that table, of course, querying would be something like
CalendarEntry.objects.filter(eventdate__date=date) , etc..

This is likely not the most elegant solution - not sure if there's a
better way to do it, or some efficient way to do the query directly in
the database without the overhead of maintaining this 'cache' table.

Would love to hear if anyone has an elegant solution to this - else
what I've outlined above should work, though it doesn't feel 100%
right, especially the part about needing some arbitrary cut-off date
to prevent an infinite generation of EventDate entries ..

Please do share what you land up coming up with.

All the best.
-Sanjay

On Fri, Sep 19, 2014 at 6:13 PM, Stodge <stodge@gmail.com> wrote:
> I have the following to model a calendar entry (recurring event). I need the
> ability to retrieve all events from the database between a start and end
> date. This has to include one off events and also recurring "virtual" events
> that occur because of an event in the database. Do you think this is
> possible using Django queries? Or do I need to retrieve the objects from the
> DB and then calculate the recurring events in the range in plain python
> using something like dateutil? I've been racking my brains but I can't seem
> to work out how to do it. Any suggestions appreciated.
>
> class CalendarEntry(models.Model):
>
> REPEAT_CHOICES = (
> ('NONE', _('None')),
> ('DAILY', _('Daily')),
> ('WEEKDAY', _('Every weekday')),
> ('WEEKLY', _('Weekly')),
> ('BIWEEKLY', _('Fortnightly')),
> ('MONTHLY', _('Monthly')),
> ('YEARLY', _('Yearly')),
> )
>
> # Mappings between content and an event entry.
> content_type = models.ForeignKey(ContentType, verbose_name='content
> type')
> object_id = models.PositiveIntegerField()
> content_object = generic.GenericForeignKey('content_type', 'object_id')
> field = models.CharField(max_length=64)
>
> # Basic event information.
> start_date = TimestampGMT(blank=True, null=True)
> end_date = TimestampGMT(blank=True, null=True)
>
> # Recurrence information.
> all_day = models.BooleanField()
> repeat = models.CharField(max_length=15, choices=REPEAT_CHOICES,
> default='NEVER')
> end_repeat = models.DateTimeField(_("end repeat"), null=True,
> blank=True)
>
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscribe@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/293df7ca-1192-49e2-8d94-d133bb1d5057%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAG3W7ZGzvqtbYk_8_aHiZseq%2B1eg5tTcRxm%3DvR3j8ZSURjqAEw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment