from datetime import datetime
from django.db import models
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType
from django.utils.translation import ugettext_lazy as _
from knesset.persons.models import Person

class Event(models.Model):
    ''' hold the when, who, what, where and which fields of events
        and allows the users to contribute resources (through links)
        and discuss upcoming events.
    '''
    when = models.DateTimeField()
    when_over = models.DateTimeField(null=True)
    # KNESSET_TODO the end time of a committee meeting is not recorded anywhere,
    # so we are left to guess
    when_over_guessed = models.BooleanField(default=True)
    who = models.ManyToManyField(Person)
    what = models.TextField()
    where = models.TextField()
    which_type   = models.ForeignKey(ContentType,
            verbose_name=_('content type'),
            related_name="event_for_%(class)s", null=True)
    which_pk = models.TextField(_('object ID'), null=True)
    which_object = generic.GenericForeignKey(ct_field="which_type", fk_field="which_pk")

    @property
    def is_future(self):
        return self.when > datetime.now()

    @property
    def which(self):
        return self.which_objects and unicode(self.which_object) or self.what

@@ -22,16 +22,180 @@
 datetime
+, timedelta%0A%0A# TODO x2: python-vobject in fedora %3C- are we tracking distribution locations somewhere?%0A# readme file? buildout should have this too.%0A%0Aimport vobject%0A
 %0Afrom dj
@@ -1393,88 +1393,1403 @@
 def 
-which(self):%0A        return self.which_objects and unicode(self.which_object) or
+summary(self):%0A        %22%22%22 this is used for the title of the event in the calendar view (icalendar) %22%22%22%0A        topic = self.what%5B:30%5D + '...' if len(self.what) %3E= 30 else self.what%0A        if self.which_object:%0A            c = self.which_object%0A            # Today it is only committee%0A            return _('%25(committee)s meeting: %25(topic)s') %25 %7B%0A                        'committee':c.name, 'topic':topic%7D%0A        return topic%0A%0A    @property%0A    def which(self):%0A        return self.which_objects and unicode(self.which_object) or self.what%0A%0A    def add_vevent_to_ical(self, cal):%0A        %22%22%22%0A        adds itself as a vevent to @cal.%0A        cal should be a vobject.iCalendar%0A        %22%22%22%0A        vevent = cal.add('vevent')%0A        vevent.add('dtstart').value = self.when%0A        summary = self.what%0A        if not self.when_over:%0A            # this can happen if you migrated so you have when_over but%0A            # have not run parse_future_committee_meetings yet.%0A            self.when_over = self.when + timedelta(hours=2)%0A            self.when_over_guessed = True%0A            self.save()%0A        if self.when_over_guessed:%0A            summary = u'ATTENTION: The end date is just projected, not available on knesset.gov. Be advised!%5Cn%5Cn' + summary%0A        vevent.add('dtend').value = self.when_over%0A        vevent.add('summary').value = self.summary%0A        vevent.add('description').value =
  sel

use vobject, add Event.add_vevent_to_ical