from django.db import models from django.utils.translation import ugettext as _ from django.contrib.auth.models import User from datetime import datetime from location_field.models import PlainLocationField class Event ( models.Model ): EVENT_TYPES = ( ( 'Reh', _('Rehearsal') ), ( 'Conc', _('Concert') ), ( 'GenReh', _('General Rehearsal') ), ( 'Party', _('Party') ), ) title = models.CharField( max_length=40, verbose_name = _("titel") ) type = models.CharField( max_length=6, choices=EVENT_TYPES, default='Reh', verbose_name= _("type") ) location = models.TextField( blank=False, verbose_name=_("location") ) map_location = PlainLocationField( based_field = location, zoom=7 ) desc = models.TextField( blank=True, verbose_name=_("description")) date = models.DateField( verbose_name= _("date") ) time = models.TimeField( null=True, blank=True, verbose_name = _("time") ) meeting_time = models.TimeField( null=True, blank=True, verbose_name = _("meeting_time") ) participants = models.ManyToManyField( User, through='EventParticipation', verbose_name=_("participants") ) def __unicode__(self): return self.title + " ( " + self.get_type_display() + " ) " def save(self, *args, **kwargs): # Call the "real" save() method super(Event, self).save(*args, **kwargs) # Create a "Don't Know" participation for each Musician for u in User.objects.all(): if not u in self.participants.all(): EventParticipation.objects.create( event=self, user = u, status='?', comment = '' ) @property def displaytime(self): if self.meeting_time is None or self.meeting_time == "": return self.time else: return self.meeting_time class EventParticipation( models.Model ): OPTIONS = ( ('?' , _('?' )), ('Yes', _('Yes')), ('No' , _('No' )) ) event = models.ForeignKey( Event, verbose_name=_("event") ) user = models.ForeignKey( User, verbose_name=_("user") ) status = models.CharField ( max_length=3, choices = OPTIONS, default='?', verbose_name=_("status") ) comment = models.CharField ( max_length=64, blank=True, verbose_name=_("comment") ) def get_username(self): return self.user.username @staticmethod def hasUserSetParticipationForAllEvents( user ): futurePart = EventParticipation.objects.filter( event__date__gte = datetime.now() ) maybeObjects = futurePart.filter( user = user ).filter( status = '?' ) if len( maybeObjects ) > 0: return False else: return True @staticmethod def get_or_create( user , event ): try: result = EventParticipation.objects.get( event = event, user = user ) except EventParticipation.DoesNotExist: result = EventParticipation.objects.create( event = event, user = user, status='?', comment = '' ) return result class Meta: unique_together = ("event", "user") permissions = ( ("change_others_participation", _("Can modify participation status of other users") ), )