summaryrefslogtreecommitdiffstats
path: root/agenda/views.py
blob: 1f2ceb3dc4ede515d190f94ba58cca1fbb3870b4 (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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# Create your views here.
#from __future__ import with_statement
from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404
from django.db.models import Q
from infopoint.agenda.models import * 
from datetime import date
from django.utils.translation import ugettext as _
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth import authenticate, login
from django.contrib.auth.models import Group,User
from django.contrib.comments.models import Comment
from infopoint.agenda.templatetags import *
from infopoint.agenda.models import *
from infopoint.agenda.forms import EventForm, NewEventForm, NewEventRestrictedForm, RedesForm, GroupAddEventForm, TodoForm, TodoCompletedForm, TodoCreateForm, TodoRelatedCreateForm, DateForm, AttendanceForm, ConfirmForm 
from django.contrib.formtools.preview import FormPreview
from django.views.generic import create_update
from django.template import RequestContext
from django.forms import ModelForm
from django.shortcuts import render_to_response
from django.utils.safestring import mark_safe
import calendar as therealcalendar
import copy


fuckthiscalendar = therealcalendar.Calendar()

today = date.today()
def find_recurrences(event,year,month):
	recurrences = [] 
	alldays =  list(fuckthiscalendar.itermonthdays2(int(year), int(month)))
	if event.recurrence == '2':
		weekday = event.start_date.isoweekday()-1
		days_list = filter(lambda day:day[1]==weekday,alldays)		
		for day,weekday in days_list:
			fakeevent = copy.copy(event) 
			fakeevent.start_date = fakeevent.start_date.replace(day=day+1)
			if (fakeevent.start_date>fakeevent.end_period):
				recurrences.append(fakeevent)
	return recurrences

def calendarshape(request, year, month):
	"""
	taken from http://journal.redflavor.com/creating-a-flexible-monthly-calendar-in-django
	"""
	next_events = Event.events.all().order_by('start_date').filter(start_date__year=year, start_date__month=month,public=True,recurrence='0')
	year = int(year)
	month = int(month)
	nothing, last_day = therealcalendar.monthrange(year,month)
	recurrent_events = Event.events.all().order_by('start_date').filter(start_period__lte=(date(int(year),int(month),last_day)),end_period__gte=(date(int(year),int(month),1)),recurrence__in=['1','2','3'])
	all_events = list(next_events)
	for recurrent_event in recurrent_events:
		all_events += find_recurrences(recurrent_event,year,month)
	all_days = map(lambda ev:ev.start_date.day, all_events)
	
	cal = EventShapeCalendar(all_events).formatmonth(year, month)
	previous, next = month-1,month+1
	prevyear, postyear = year, year
	if int(month) == 1:
		previous = 12
		prevyear = year-1
		
	if int(month) == 12:
		next = 1
		postyear = year+1

	return render_to_response('agenda/calendarshape.html', {'prevyear':prevyear, 'postyear':postyear, 'month':month,'previous':previous,'next':next, 'calendar': mark_safe(cal),'today': today })


def justonegroup(request,object_id):
	group = Group.objects.select_related().get(pk=object_id)
	todolist = Todo.todos.all().filter(network=group,done=False).order_by('due_date')
	wishlist = Wish.objects.all().filter(network=group,done=False).order_by('due_date')
	group_events = Event.events.next_events().filter(redes=object_id).distinct()
	newtodo = TodoRelatedCreateForm()
	todos = Todo.todos.pending_todos().filter(network=object_id).filter(public=True)
	return render_to_response('agenda/one_group.html', {'wishlist':wishlist, 'todolist':todolist,'group': group, 'group_events': group_events,'todos': todos, 'newtodo': newtodo },context_instance=RequestContext(request))

def mismensajes(request):
	user = request.user
	todolist = Todo.todos.all().filter(done=False).order_by('due_date')
	wishlist = Wish.objects.all().filter(done=False).order_by('due_date')
	newtodo = TodoCreateForm()
	if  request.user.is_authenticated():
		missing_pixies = Event.events.missing_pixies().filter(redes__user=user.id).distinct()
		group_todos = Todo.todos.pending_todos().filter(network__user=request.user).order_by('due_date')
		attending = user.event_set.all().filter(end_date__gte=datetime.now())
		my_mentored_events = Event.events.next_events().filter(pixie=user).select_related()
		return render_to_response('agenda/user_page.html', {'wishlist':wishlist, 'attending': attending,'todolist':todolist,'group_todos': group_todos, 'my_mentored_events': my_mentored_events,'newtodo': newtodo, 'missing_pixies': missing_pixies },context_instance=RequestContext(request))   
        else:   
                event = Event.calendar.filter(public=True)
                return render_to_response('agenda/event_list.html', {'event':event })

def render (request):
	event = Event.events.today_events().filter(public=True)
	
        if  request.user.is_authenticated():
		event = Event.events.today_events()
                return render_to_response('agenda/event_list.html', {'event': event},context_instance=RequestContext(request))
        else:   
                return render_to_response('agenda/event_list.html', {'event': event })


def orphan_events (request):
	event = missing_pixies = Event.events.missing_pixies().distinct()
	wishlist = Wish.objects.all().filter(done=False).order_by('due_date')
        if  request.user.is_authenticated():
		todolist = Todo.todos.all().filter(done=False).order_by('due_date')
                return render_to_response('agenda/event_list.html', {'todolist':todolist,'wishlist': wishlist,'event': event},context_instance=RequestContext(request))
        else:   
		event = Event.events.next_events().filter(public=True)
                return render_to_response('agenda/event_list.html', {'event': event })

def calendar_shape(request):
	event = Event.events.all().order_by('start_date')
	wishlist = Wish.objects.all().filter(done=False).order_by('due_date')
	todolist = Todo.todos.all().filter(done=False).order_by('due_date')
	return render_to_response('agenda/event_list.html', {'todolist':todolist,'wishlist': wishlist,'event': event},context_instance=RequestContext(request))

def calendar (request):
	event = Event.events.next_events().order_by('start_date')
	wishlist = Wish.objects.all().filter(done=False).order_by('due_date')
        if  request.user.is_authenticated():
		todolist = Todo.todos.all().filter(done=False).order_by('due_date')
                return render_to_response('agenda/event_list.html', {'todolist':todolist,'wishlist': wishlist,'event': event},context_instance=RequestContext(request))
        else:   
		event = Event.events.next_events().filter(public=True)
                return render_to_response('agenda/event_list.html', {'event': event })

def wishlist (request):
	wishlist = Wish.objects.all().filter(done=False).order_by('due_date')
	done = Wish.objects.all().filter(done=True).order_by('due_date')[:10]
	return render_to_response('agenda/wishlist.html', {'wishlist': wishlist, 'done': done }, context_instance=RequestContext(request))



def logout_view(request):
	logout(request)
	return HttpResponseRedirect('/agenda/')

def random(request):
	event = Event.events.next_events().order_by('?')[:1]

	return render_to_response('agenda/random.html', {'event': event },context_instance=RequestContext(request))

def get_map(object_id):
	try:
		map = open( '/var/www/media/maps/event_'+str(object_id)+'.map' )
	except:
		return HttpResponseRedirect('/agenda/map/'+object_id)	
	info = str(map.read())
	return info

def event(request,object_id):
        event = get_object_or_404(Event, pk=object_id)
	try: 
		attendance = Attendance.objects.get(event=object_id,person=request.user.id)
		invite = 'confirm'

	except Attendance.DoesNotExist:
		invite = 'True'
		attendance = 'none'
	newtodo = TodoRelatedCreateForm()
	completed_todo = TodoCompletedForm()
	try: 
		map_coordinates = str(get_map(object_id)).encode()
	except:
		return HttpResponseRedirect('/agenda/map/'+object_id)
	addtogroup = GroupAddEventForm()
	pending_todos = Todo.todos.pending_todos().filter(event=object_id)
	return render_to_response('agenda/one_event.html', {'event': event,'today': today , 'invite': invite, 'attendance': attendance, 'newtodo': newtodo, 'addtogroup': addtogroup, 'pending_todos': pending_todos, 'completed_todo': completed_todo, 'map_coordinates': map_coordinates },context_instance=RequestContext(request))

def add_new_event(request):
	form = NewEventRestrictedForm()
	general_form = EventForm()
	mydate = DateForm()
	return render_to_response('agenda/add_new_event.html', {'general_form':general_form,'form': form, 'mydate': mydate  },context_instance=RequestContext(request))

def submit_new_event(request):
	if request.method == 'POST':
		
		request.POST['start_date'] = str(request.POST['start_date_0']+" "+request.POST['start_date_1'])

		
		form = NewEventRestrictedForm(request.POST)

		#form.start_date = str(request.POST['start_date_0']+" "+request.POST['start_date_1'])
		#request.POST['start_date_0']+" "+request.POST['start_date_1'] = form.start_date
		#form.end_date = str(request.POST['end_date_0']+" "+request.POST['end_date_1'])
		if form.is_valid():
			new_event = form.save(commit=False)
			#form.save_m2m()
			form.save()

			return HttpResponseRedirect('/agenda/'+new_event.id)
		return render_to_response('agenda/add_new_event.html', {'form': form  },context_instance=RequestContext(request))
	return render_to_response('agenda/add_new_event.html', {'form': form, 'mydate': mydate  },context_instance=RequestContext(request))

def adoptevent(request,object_id):
	if request.method == 'POST':
		event = get_object_or_404(Event, pk=object_id)
		event.pixie.add(request.user)
		return HttpResponseRedirect('/agenda/'+object_id)


def submittodo(request):
	if request.method == 'POST':
		f = TodoForm(request.POST) 
		f.save()
		user = request.user
		user.message_set.create(message=str(f.title+' has been saved'))
		return render_to_response('agenda/user_events.html', {'f': f},context_instance=RequestContext(request))

def closealltodo(request,todo_id):
	todo = get_object_or_404(Todo, pk=todo_id)
	todo.done = True
	todo.save()
	request.user.message_set.create(message=todo.title+_(' has been closed as completed'))
	return HttpResponseRedirect('/agenda/me/')

def closetodo(request,todo_id,event_id):
	if request.method == 'POST':
		todo = get_object_or_404(Todo, pk=todo_id)
		todo.done = True
		todo.save()
		request.user.message_set.create(message=todo.title+' has been closed as completed')
		return HttpResponseRedirect('/agenda/map/'+event_id)

def closewishitem(request,wish_id):
	wish = get_object_or_404(Wish, pk=wish_id)
	wish.done = True
	wish.save()
	return HttpResponseRedirect('/agenda/wishlist/')

def closegrouptodo(request,todo_id,object_id):
	if request.method == 'POST':
		todo = get_object_or_404(Todo, pk=todo_id)
		todo.done = True
		todo.save()
		return HttpResponseRedirect('/group/'+object_id)


def addtodo(request):
	newtodo = TodoCreateForm()
	return render_to_response('agenda/add_todo.html', {'f': newtodo },context_instance=RequestContext(request))

def addeventtogroup(request,event_id):
	event = get_object_or_404(Event, pk=event_id)
	addtogroup = GroupAddEventForm()
	return render_to_response('agenda/add_event_to_group.html', {'f': addtogroup, 'event': event },context_instance=RequestContext(request))


def addeventtogroupsave(request,event_id):
	f = RedesForm(request.POST)
	try:
		f.save()
	except:
		pass
	return HttpResponseRedirect('/agenda/map/'+event_id)
	#return render_to_response('agenda/one_event.html', {'f': f, 'event': event},context_instance=RequestContext(request))

def addtodotoevent(request,event_id):
	event = get_object_or_404(Event, pk=event_id)
	newtodo = TodoRelatedCreateForm()
	return render_to_response('agenda/add_todo_to_event.html', {'f': newtodo, 'event': event },context_instance=RequestContext(request))

def saveeventtodo(request,event_id):
	event = get_object_or_404(Event, pk=event_id)
	f = TodoForm(request.POST)
	f.save()
	return HttpResponseRedirect('/agenda/map/'+event_id)
	#return render_to_response('agenda/one_event.html', {'f': f, 'event': event},context_instance=RequestContext(request))

def savegrouptodo(request,object_id):
	group = get_object_or_404(Group, pk=object_id)
	f = TodoForm(request.POST)
	f.save()
	return HttpResponseRedirect('/group/'+object_id)

def location(request, object_id):
	"""
	This view helps you to get information about one of the places of the center
	The information can be technical, about the programme, and about everything you
	want
	"""
	location = get_object_or_404(Location, pk=object_id)
	wishlist = Wish.objects.all().filter(location=location,done=False).order_by('due_date')

	if request.user.is_authenticated():
		todolist = Todo.todos.all().filter(location=location,done=False).order_by('due_date')
		location_events = Event.events.next_events().filter(location=location.id).distinct()
		return render_to_response('agenda/location_events.html', {'wishlist':wishlist,'todolist':todolist,'location_events':location_events,'location': location},context_instance=RequestContext(request))
	else: 
		location_events = Event.events.next_events().filter(public=True,redes=location.redes.id).distinct()
		return render_to_response('agenda/location_events.html', {'location': location,'wishlist':wishlist,'location_events':location_events  })

def attendance(request, object_id):
	if request.user.is_authenticated():

		event = get_object_or_404(Event, pk=object_id)
		person = request.user.id()
		attendance = request.POST['attendance']
		try:
			unapersona = Attendance.objects.get(event=object_id,person=person)
			unapersona.attendance = request.POST['attendance']
			attendance.save()
			return render_to_response('agenda/one_event.html', {'event': event },context_instance=RequestContext(request))

	    	except Attendance.DoesNotExist:
			attendance = Attendance(event=event,person=person,attendance=1)
			attendance.save()
		
			return render_to_response('agenda/wanna_join.html', {'event': event },context_instance=RequestContext(request))
	else:   
		return HttpResponseRedirect('django.contrib.auth.views.login')

        # Always return an HttpResponseRedirect after successfully dealing
        # with POST data. This prevents data from being posted twice if a
        # user hits the Back button.

def confirmation_pending(request):
	object_id=request.user.id
	if request.user.is_authenticated():
		user = get_object_or_404(User, pk=object_id)
		attendance = user.attendance_set.all()
		confirmed = user.attendance.filter(attendance='1')
		pending = user.attendance.filter(attendance='NULL')
		rejected = user.attendance.filter(attendance='0')
		return render_to_response('agenda/user_events.html', {'user': user, 'attendance': attendance, 'confirmed': confirmed, 'pending': pending, 'rejected': rejected},context_instance=RequestContext(request))

	else:   
		return HttpResponseRedirect('django.contrib.auth.views.login')

def attend(request,object_id):
	event = Event.calendar.get(pk=object_id)
	return render_to_response('agenda/one_event.html', {'event': event},context_instance=RequestContext(request))


def one_comment(request,object_id):
	comment = Comment.objects.get(pk=object_id)
	return render_to_response('agenda/one_comment.html', {'comment': comment},context_instance=RequestContext(request))