summaryrefslogtreecommitdiffstats
path: root/agenda/views.py
blob: 3a4b3ba3dff131a1a1dcea1938e4be1a0618069d (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 infopoint.agenda.models import * 
from datetime import date
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 Event, Attendance,Todo,PendingTodoManager
from infopoint.agenda.forms import TodoCreateForm, TodoForm,TodoRelatedCreateForm,GroupAddEventForm,EventForm, RedesForm,TodoCompletedForm
from django.contrib.formtools.preview import FormPreview
from django.views.generic import create_update
from django.template import RequestContext
from django.forms import ModelForm
import vobject

address = 'Gaona 3'
city = 'Malaga'

today = date.today()

def get_map(type,object_id):
	map = open( '/var/www/media/maps/'+str(type)+'_'+str(object_id)+'.map' )
	info = str(map.read())
	return info

def ical_one_event(request,object_id):
	cal = vobject.iCalendar()
	cal.add('method').value = 'PUBLISH'  # IE/Outlook needs this
	cal.add('calscale').value = 'GREGORIAN'
	for one_event in Event.calendar.filter(pk=object_id):
		cal.add('caldesc').value = 'Infopoint event - '+one_event.title
		cal.add('x-wr-calname').value = one_event.title+' calendar'
		vevent = cal.add('vevent')
	       	vevent.add('summary').value = one_event.title
		vevent.add('dtstart').value = one_event.start_date
		vevent.add('dtend').value = one_event.end_date
		vevent.add('dtstamp').value = one_event.start_date 
		vevent.add('status').value = 'CONFIRMED'
		vevent.add('description').value = one_event.description
		vevent.add('location').value = one_event.location.title
		for user in one_event.atendees.all():
			att = vevent.add('attendee')
			att.value=user.email
			att.rsvp_param='True'
			att.cn_param=user.username
			att.role_param='REQ-PARTICIPANT'
			if user.attendance == 1:
				att.partstat_param='ACCEPTED'
			att.dir_param="http://infopoint/agenda/user/"+str(user.id)+"/busy/"

		for group in one_event.group.all():
			one_group = vevent.add('categorie')
			one_group.value=group.group
			one_group.dir_param="http://infopoint/agenda/group/"+str(group.group.id)+"/cal/"
		for pixie in one_event.pixie.all()[:1]:
			vevent.add('organizer').value = pixie.email
	icalstream = cal.serialize()
	response = HttpResponse(icalstream, mimetype='text/calendar')
	response['Filename'] = 'filename.ics'  # IE needs this
	response['Content-Disposition'] = 'attachment; filename=filename.ics'
	return response

def ical_user_busytime(request,object_id):
	cal = vobject.iCalendar()
	user = User.objects.get(pk=object_id)
	cal.add('x-wr-calname').value = 'infopoint free/busy time for '+user.username
	cal.add('calscale').value = 'GREGORIAN'
	cal.add('URL').value = 'http://infopoint/agenda/user/'+object_id+'/busy'
	for one_event in user.events.all().exclude(title__in=user.event_set.all()):
		freebusystart = one_event.start_date
		freebusyend = one_event.end_date
		vevent = cal.add('VFREEBUSY')
	       	vevent.add('summary').value = 'BUSY'
		vevent.add('dtstart').value = one_event.start_date
		vevent.add('dtend').value = one_event.end_date
		vevent.add('transp').value = 'OPAQUE'
		vevent.add('freebusy').value = [(vevent.dtstart.value,vevent.dtend.value)]
	for one_event in user.event_set.all():
		freebusystart = one_event.start_date
		freebusyend = one_event.end_date
		vevent = cal.add('VFREEBUSY')
	       	vevent.add('summary').value = 'BUSY'
		vevent.add('dtstart').value = one_event.start_date
		vevent.add('dtend').value = one_event.end_date
		vevent.add('transp').value = 'OPAQUE'
		vevent.add('freebusy').value = [(vevent.dtstart.value,vevent.dtend.value)]
	icalstream = cal.serialize()
	response = HttpResponse(icalstream, mimetype='text/calendar')
	response['Filename'] = 'filename.ics'  # IE needs this
	response['Content-Disposition'] = 'attachment; filename=filename.ics'
	return response

def ical_user_events(request,object_id):
	cal = vobject.iCalendar()
	user = User.objects.get(pk=object_id)
	cal.add('method').value = 'PUBLISH'  # IE/Outlook needs this
	cal.add('calscale').value = 'GREGORIAN'
	cal.add('x-wr-calname').value = 'infopoint calendar for '+user.username
	for one_event in user.event_set.all():
		vevent = cal.add('vevent')
	       	vevent.add('summary').value = one_event.title
		vevent.add('dtstart').value = one_event.start_date
		vevent.add('status').value = 'CONFIRMED'
		vevent.add('TRANSP').value = 'OPAQUE'
		vevent.add('dtend').value = one_event.end_date
		vevent.add('description').value = one_event.description
		vevent.add('location').value = one_event.location.title
		for user in one_event.atendees.all():
			att = vevent.add('attendee')
			att.value=user.email
			att.rsvp_param='True'
			att.cn_param=user.username
			att.role_param='REQ-PARTICIPANT'
			att.partstat_param='ACCEPTED'
			att.dir_param="http://infopoint/agenda/user/"+str(user.id)+"/busy/"
		for pixie in one_event.pixie.all()[:1]:
			vevent.add('organizer').value = pixie.email
	icalstream = cal.serialize()
	response = HttpResponse(icalstream, mimetype='text/calendar')
	response['Filename'] = 'filename.ics'  # IE needs this
	response['Content-Disposition'] = 'attachment; filename=filename.ics'
	return response

def ical_next_events(request):
	cal = vobject.iCalendar()
	cal.add('method').value = 'PUBLISH'  # IE/Outlook needs this
	cal.add('calscale').value = 'GREGORIAN'
	cal.add('x-wr-calname').value = 'infopoint calendar'
	for one_event in Event.events.next_events():
		vevent = cal.add('vevent')
	       	vevent.add('summary').value = one_event.title
		vevent.add('dtstart').value = one_event.start_date
		vevent.add('dtend').value = one_event.end_date
		vevent.add('dtstamp').value = one_event.start_date
		vevent.add('description').value = one_event.description
		vevent.add('location').value = one_event.location.title
		for user in one_event.atendees.all():
			att = vevent.add('attendee')
			att.value=user.email
			att.rsvp_param='True'
			att.cn_param=user.username
			att.role_param='REQ-PARTICIPANT'
			att.partstat_param='ACCEPTED'
			att.dir_param="http://infopoint/agenda/user/"+str(user.id)+"/busy/"
		for pixie in one_event.pixie.all()[:1]:
			vevent.add('organizer').value = pixie.email
	icalstream = cal.serialize()
	response = HttpResponse(icalstream, mimetype='text/calendar')
	response['Filename'] = 'filename.ics'  # IE needs this
	response['Content-Disposition'] = 'attachment; filename=filename.ics'
	return response


def justonegroup(request,object_id):
	group = Group.objects.get(pk=object_id)
	newtodo = TodoRelatedCreateForm()
	todos = Todo.todos.pending_todos().filter(network=object_id).filter(public=True)
	map_coordinates = str(get_map('group',object_id)).encode()
	return render_to_response('agenda/one_group.html', {'group': group, 'todos': todos, 'newtodo': newtodo, 'map_coordinates':map_coordinates  },context_instance=RequestContext(request))

def mismensajes(request):
	group_todos = Todo.todos.pending_todos().filter(network__user=request.user).order_by('due_date')
	missing_pixies = Event.events.missing_pixies().filter(redes__user=1).distinct()
	user = request.user
	logentries = user.logentry_set.all()[:5]
	newtodo = TodoCreateForm()
	if  request.user.is_authenticated():
		return render_to_response('agenda/user_page.html', {'logentries': logentries,'group_todos': group_todos, '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', {'log': log })

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

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



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 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()
	map_coordinates = str(get_map('event',object_id)).encode()
	addtogroup = GroupAddEventForm()
	pending_todos = Todo.todos.pending_todos().filter(event=object_id)
	return render_to_response('agenda/one_event.html', {'event': event, 'invite': invite, 'attendance': attendance, 'newtodo': newtodo, 'addtogroup': addtogroup, 'pending_todos': pending_todos, 'completed_todo': completed_todo, 'map_coordinates': map_coordinates, 'address': address, 'city': city },context_instance=RequestContext(request))

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


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

def closetodo(request,todo_id,event_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/'+event_id)

def closegrouptodo(request,todo_id,object_id):
	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/'+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/'+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)
	map_coordinates = str(get_map('location',object_id)).encode()
	if  request.user.is_authenticated():
		return render_to_response('agenda/location_events.html', {'location': location, 'map_coordinates': map_coordinates},context_instance=RequestContext(request))
	else: 
		return render_to_response('agenda/location_events.html', {'location': location  })

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))