2013-06-02 21:26:10 +02:00
|
|
|
from django.db import models
|
|
|
|
from django.utils.translation import ugettext as _
|
2013-10-14 19:46:53 +02:00
|
|
|
from django.contrib.auth.models import User, Permission
|
2013-09-26 21:56:32 +02:00
|
|
|
|
2013-06-02 21:26:10 +02:00
|
|
|
|
2013-10-14 19:46:53 +02:00
|
|
|
from django.db.models import Q
|
2013-06-02 21:26:10 +02:00
|
|
|
|
2013-09-15 14:48:40 +02:00
|
|
|
from datetime import datetime
|
2013-09-22 11:11:48 +02:00
|
|
|
from location_field.models import PlainLocationField
|
|
|
|
|
2013-06-02 21:26:10 +02:00
|
|
|
|
|
|
|
class Event ( models.Model ):
|
|
|
|
|
|
|
|
EVENT_TYPES = (
|
|
|
|
( 'Reh', _('Rehearsal') ),
|
|
|
|
( 'Conc', _('Concert') ),
|
2013-06-30 16:55:00 +02:00
|
|
|
( 'Party', _('Party') ),
|
2013-09-28 18:57:19 +02:00
|
|
|
( 'Travel', _('Travel') )
|
2013-06-02 21:26:10 +02:00
|
|
|
)
|
|
|
|
|
2013-06-30 16:55:00 +02:00
|
|
|
type = models.CharField( max_length=6, choices=EVENT_TYPES, default='Reh', verbose_name= _("type") )
|
2013-09-28 18:57:19 +02:00
|
|
|
short_desc = models.CharField( null=True, max_length=100, blank = True, verbose_name= _("Short Description") )
|
2013-06-30 16:55:00 +02:00
|
|
|
location = models.TextField( blank=False, verbose_name=_("location") )
|
2013-09-28 18:57:19 +02:00
|
|
|
map_location = PlainLocationField(blank=True, based_field = location, zoom=7, verbose_name=_("Location on map") )
|
|
|
|
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") )
|
2013-06-30 16:55:00 +02:00
|
|
|
meeting_time = models.TimeField( null=True, blank=True, verbose_name = _("meeting_time") )
|
2013-09-22 11:11:48 +02:00
|
|
|
|
2013-09-28 18:57:19 +02:00
|
|
|
end_date = models.DateField( null=True, blank = True, verbose_name = _("End Date") )
|
|
|
|
|
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):
|
2013-09-28 18:57:19 +02:00
|
|
|
return self.title
|
2013-06-02 21:26:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-09-28 18:57:19 +02:00
|
|
|
@property
|
|
|
|
def title(self):
|
|
|
|
res = self.get_type_display()
|
|
|
|
if ( self.short_desc ):
|
|
|
|
res += " ( " + self.short_desc + " ) "
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
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-06-30 16:55:00 +02:00
|
|
|
|
2013-09-15 14:48:40 +02:00
|
|
|
@staticmethod
|
|
|
|
def hasUserSetParticipationForAllEvents( user ):
|
2013-10-14 19:46:53 +02:00
|
|
|
if not EventParticipation.isMember(user):
|
|
|
|
return True
|
|
|
|
|
2013-09-15 14:48:40 +02:00
|
|
|
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
|
|
|
|
|
2013-10-14 19:46:53 +02:00
|
|
|
@staticmethod
|
|
|
|
def isMember( user ):
|
|
|
|
return user.has_perm('eventplanner.member')
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def isAdmin( user ):
|
|
|
|
return user.has_perm('eventplanner.admin')
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def members():
|
|
|
|
perm = Permission.objects.get( codename='member' )
|
|
|
|
return User.objects.filter(Q(groups__permissions=perm) | Q(user_permissions=perm) ).distinct()
|
2013-06-30 16:55:00 +02:00
|
|
|
|
|
|
|
@staticmethod
|
2013-09-26 21:56:32 +02:00
|
|
|
def get_or_create( user , event ):
|
2013-06-30 16:55:00 +02:00
|
|
|
try:
|
2013-09-26 21:56:32 +02:00
|
|
|
result = EventParticipation.objects.get( event = event, user = user )
|
2013-06-30 16:55:00 +02:00
|
|
|
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
|
|
|
|
2013-06-30 16:55:00 +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")
|
2013-06-30 16:55:00 +02:00
|
|
|
permissions = (
|
2013-10-14 19:46:53 +02:00
|
|
|
("admin", _("Admin") ),
|
|
|
|
("member", _("Member") ),
|
2013-06-30 16:55:00 +02:00
|
|
|
)
|
2013-10-14 19:46:53 +02:00
|
|
|
|
2013-06-18 22:49:01 +02:00
|
|
|
|
|
|
|
|