blechreiz-website/eventplanner/models.py

96 lines
3.3 KiB
Python
Raw Normal View History

2013-06-02 21:26:10 +02:00
from django.db import models
from django.utils.translation import ugettext as _
2013-09-26 21:56:32 +02:00
from django.contrib.auth.models import User
2013-06-02 21:26:10 +02:00
2013-09-15 14:48:40 +02:00
from datetime import datetime
from location_field.models import PlainLocationField
2013-06-02 21:26:10 +02:00
class Event ( models.Model ):
EVENT_TYPES = (
( 'Reh', _('Rehearsal') ),
( 'Conc', _('Concert') ),
( 'GenReh', _('General Rehearsal') ),
( 'Party', _('Party') ),
2013-06-02 21:26:10 +02:00
)
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") )
2013-09-27 11:39:28 +02:00
map_location = PlainLocationField( based_field = location, zoom=7 )
desc = models.TextField( blank=True, verbose_name=_("description"))
2013-06-30 11:01:12 +02:00
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") )
2013-09-26 21:56:32 +02:00
participants = models.ManyToManyField( User, through='EventParticipation', verbose_name=_("participants") )
2013-06-02 21:26:10 +02:00
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
2013-09-26 21:56:32 +02:00
for u in User.objects.all():
if not u in self.participants.all():
EventParticipation.objects.create( event=self, user = u, status='?', comment = '' )
2013-09-15 14:48:40 +02:00
@property
def displaytime(self):
if self.meeting_time is None or self.meeting_time == "":
return self.time
else:
return self.meeting_time
2013-06-02 21:26:10 +02:00
class EventParticipation( models.Model ):
OPTIONS = ( ('?' , _('?' )),
('Yes', _('Yes')),
('No' , _('No' ))
)
2013-06-30 11:01:12 +02:00
event = models.ForeignKey( Event, verbose_name=_("event") )
2013-09-26 21:56:32 +02:00
user = models.ForeignKey( User, verbose_name=_("user") )
2013-06-30 11:01:12 +02:00
status = models.CharField ( max_length=3, choices = OPTIONS, default='?', verbose_name=_("status") )
comment = models.CharField ( max_length=64, blank=True, verbose_name=_("comment") )
2013-06-02 21:26:10 +02:00
2013-09-26 21:56:32 +02:00
def get_username(self):
return self.user.username
2013-09-15 14:48:40 +02:00
@staticmethod
def hasUserSetParticipationForAllEvents( user ):
futurePart = EventParticipation.objects.filter( event__date__gte = datetime.now() )
2013-09-26 21:56:32 +02:00
maybeObjects = futurePart.filter( user = user ).filter( status = '?' )
2013-09-15 14:48:40 +02:00
if len( maybeObjects ) > 0:
return False
else:
return True
@staticmethod
2013-09-26 21:56:32 +02:00
def get_or_create( user , event ):
try:
2013-09-26 21:56:32 +02:00
result = EventParticipation.objects.get( event = event, user = user )
except EventParticipation.DoesNotExist:
2013-09-26 21:56:32 +02:00
result = EventParticipation.objects.create( event = event, user = user, status='?', comment = '' )
2013-06-18 22:49:01 +02:00
return result
2013-06-02 21:26:10 +02:00
class Meta:
2013-09-26 21:56:32 +02:00
unique_together = ("event", "user")
permissions = (
("change_others_participation", _("Can modify participation status of other users") ),
)
2013-06-18 22:49:01 +02:00