summaryrefslogtreecommitdiffstats
path: root/agenda/models.py
blob: 06ad0725571c7105b9241d2bc9f4e0f362144161 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
from django.db import models
from django.conf import settings
from infopoint.agenda import * 
from infopoint.sites.models import Site
import datetime
from time import gmtime, strftime
from django.contrib.contenttypes import generic
from django.contrib import databrowse
from django.contrib.auth.models import User,Group
from django.contrib.comments.models import Comment,ContentType,User
from django import forms
from django.forms import ModelForm
from django.template.loader import render_to_string
from datetime import datetime
from django.utils.translation import ugettext as _

from calendar import HTMLCalendar
from datetime import date
from itertools import groupby

from django.utils.html import conditional_escape as esc
from django.contrib import admin

# Create your models here.

RECURRENCE_CHOICES = (
    ('0', 'No recurrence'),
    ('1', 'Everyday'),
    ('2', 'Each week'),
    ('3', 'Monthly'),
)

TIMESPAN_CHOICES = (
    ('1', 'Future events'),
    ('2', 'Past events'),
    ('3', 'All events'),
    ('4', 'Today'),
)


class EventShapeCalendar(HTMLCalendar):
    def __init__(self, events):
	super(EventShapeCalendar, self).__init__()
	self.events = self.group_by_day(events)
    def formatday(self, day, weekday):
    	if day != 0:
		cssclass = self.cssclasses[weekday]
		if date.today() == date(self.year, self.month, day):
			cssclass += ' today'
		if day in self.events:
			cssclass += ' filled'
			body = ['<ul>']
			for event in self.events[day]:
				body.append('<li>')
				body.append(esc(event.start_date.hour))
				if event.start_date.minute != 0:
					body.append(':%s' % event.start_date.minute)
				body.append(' hs. <a href="%s">' % event.get_absolute_url())
				body.append(esc(event.title))
				body.append('</a></li>')
			body.append('</ul>')
			return self.day_cell(cssclass, '%d %s' % (day, ''.join(body)))
		return self.day_cell(cssclass, day)
	return self.day_cell('noday', '&nbsp;')

    def formatmonth(self, year, month):
	self.year, self.month = year, month
	return super(EventShapeCalendar, self).formatmonth(year, month)

    def group_by_day(self, events):
	field = lambda event: event.start_date.day
	return dict(
		[(day, list(items)) for day, items in groupby(events, field)]	        )

    def day_cell(self, cssclass, body):
	return '<td class="%s">%s</td>' % (cssclass, body)

class Location(models.Model):
	"""
	This is the class for the different spaces we will handle.
	the more information we get here, the less we will have to answer
	later
	"""
	title = models.CharField(max_length=50)
	description = models.TextField()
	size = models.IntegerField(help_text=_('how many people fits here?'))
	color = models.CharField(max_length=6, null=True, blank=True)
	picture = models.FileField(upload_to='locations/', null=True,blank=True)
	gallery = models.URLField(null=True,blank=True)
	is_in = models.ForeignKey('self',null=True,blank=True)
	places = models.Manager()
	#    	inlines = ('AgendaInline',)
	def __unicode__(self):
		return self.title

	def colored_location(self):
		return '<span style="color: #%s;">%s</span>' % (self.color, self.title)
		colored_location.allow_tags = True
	@models.permalink
	def get_absolute_url(self):
		return ('infopoint.agenda.views.location', [str(self.id)])


	
class EventManager(models.Manager):
	def past_events(self):
		return self.exclude(start_date__gte=datetime.now())

	def next_events(self):
		return self.filter(start_date__gte=datetime.now())

	def missing_pixies(self):
		return self.filter(start_date__gte=datetime.now()).filter(pixie__isnull=True)
	def today_events(self):
		today_start = strftime("%Y-%m-%d 0:0:0", gmtime())
		today_end = strftime("%Y-%m-%d 23:59:59", gmtime())
		return self.exclude(start_date__gte=today_end).filter(end_date__gte=today_start)



class Event(models.Model):
	"""
	The Main Organizers class, where all the data reunites again.
	it has some dificulties, anyway. But is a pretty table!
	Fields:
	Event(id, title, start_date, end_date, priority, location_id, link)

	"""
	title = models.CharField(max_length=100)
	public = models.BooleanField(null=True,blank=True,default=True)
	confirmed = models.BooleanField(null=True,blank=True,default=True)
	description = models.TextField(null=True,blank=True)
	start_date = models.DateTimeField(help_text=_('event start'))
	end_date = models.DateTimeField(help_text=_('event end'))
	location = models.ForeignKey(Location,blank=True)
	link = models.URLField(blank=True)
	site = models.ManyToManyField(Site,blank=True)
	calendar = models.Manager()
	events = EventManager()
	pixie = models.ManyToManyField(User,verbose_name=_('mentor'),related_name='mentor',blank=True)
	picture = models.FileField(upload_to='events/', null=True,blank=True)
	redes = models.ManyToManyField(Group, through='Network',null=True,blank=True, related_name='masredes')
	atendees = models.ManyToManyField(User, through='Attendance', null=True,blank=True)
	save_as = True
        recurrence = models.CharField(max_length=1, choices=RECURRENCE_CHOICES,default='0')
	start_period = models.DateTimeField(null=True,blank=True,help_text=_('start period for the recurrence of the event'))
	end_period = models.DateTimeField(null=True,blank=True,help_text=_('event period'))
	def __unicode__(self):
		return self.title
	class Meta:
		ordering = ['start_date']
	@models.permalink
	def get_absolute_url(self):
		return ('infopoint.agenda.views.event', [str(self.id)])

class Network(models.Model):
	"""
	Relation between Groups and events
	"""
	event = models.ForeignKey(Event,related_name='groupevent')
	group = models.ForeignKey(Group,related_name='networkevent')
	creator = models.ForeignKey(User)
	last_update = models.DateTimeField(auto_now=True)
	def __unicode__(self):
		return self.group.name
	class Meta:
		unique_together = (('event', 'group'),)


class PendingTodoManager(models.Manager):
	def completed_todos(self):
		return self.filter(done=True)

	def pending_todos(self):
		return self.filter(done=False).order_by('due_date')



class Wish(models.Model):
	"""
	This is the list of things we need.
	"""
	title = models.CharField(max_length=100)
	public = models.BooleanField(null=True,blank=True,default=True,help_text=_('Is this todo public?'))
	description = models.TextField(null=True,blank=True)
	due_date = models.DateField(help_text=_('When do we need it'),null=True,blank=True)
	done = models.BooleanField(blank=True,null=True)
	network = models.ManyToManyField(Group, null=True,blank=True)
	event = models.ManyToManyField(Event, null=True,blank=True,  related_name='wishlist')
	location = models.ManyToManyField(Location, null=True,blank=True,  related_name='wishlistlocation')
	creator = models.ForeignKey(User, related_name='wish_creator')
	def __unicode__(self):
		return self.title
	class Meta:
		verbose_name_plural = 'wishlist'

class Todo(models.Model):
	"""
	All the tasks that have to be done in the house, even if you dont know how to do them
	"""
	title = models.CharField(max_length=100)
	public = models.BooleanField(null=True,blank=True,default=True)
	description = models.TextField(null=True,blank=True)
	due_date = models.DateField(help_text=_('Limit date'),null=True,blank=True)
	done = models.BooleanField(blank=True,null=True)
	todos = PendingTodoManager()
	network = models.ManyToManyField(Group, null=True,blank=True)
	event = models.ManyToManyField(Event, null=True,blank=True,  related_name='todo')
	location = models.ManyToManyField(Location, null=True,blank=True,  related_name='todolocation')
	creator = models.ForeignKey(User, related_name='todocreator')
	def __unicode__(self):
		return self.title


class Attendance(models.Model):
	"""
	This object links the user table and the events table, 
	to check assistance to events
	Attendance(id, person_id, event_id, attendance, update, reject_reason)
	"""
        person = models.ForeignKey(User)
	event = models.ForeignKey(Event)
        attendance = models.BooleanField(help_text=_('Confirm your assistance to the event'),blank=True,null=True)
        update = models.DateTimeField(auto_now=True, verbose_name=_('Last update'))
        reject_reason = models.CharField(max_length=64,blank=True,null=True, help_text=_("Let others know why you can't come to the event (optional)")) 
	class Meta:
		unique_together = (('person', 'event'),)


        def __unicode__(self):
                return self.person