Take a look at:
https://github.com/llazzaro/django-scheduler

also available on PyPI:
pypi.python.org/pypi/django-scheduler/

It's very similar to what you have done so far. It handles virtual events 
('occurrences') by generating them on the fly for a requested date range. 
Individual occurrences can be saved if needed (ie, someone adds a note, or 
changes the start/end time of a particular occurrence).


On Friday, 19 September 2014 06:01:48 UTC-7, Sanjay Bhangar wrote:
>
> 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 <[email protected] <javascript:>> 
> 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 [email protected] <javascript:>. 
> > To post to this group, send email to [email protected] 
> <javascript:>. 
> > 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 [email protected].
To post to this group, send email to [email protected].
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/336b116d-d140-4a27-b85f-2c811fef6f59%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to