Changes in eventos/views.py
[cascardo/ema.git] / eventos / views.py
index 275958c..de4dbc0 100644 (file)
@@ -23,7 +23,7 @@ from django.contrib.auth.models import User, Group
 from django.forms import HiddenInput, ModelForm
 from django.shortcuts import render_to_response, get_object_or_404
 from django.template import RequestContext, Context, loader
-from eventos.models import Palestrante, Trabalho, TipoTrabalho, Trilha, Evento
+from eventos.models import Palestrante, Trabalho, TipoTrabalho, Trilha, Evento, Improve
 from eventos.forms import RegisterSpeaker
 from django.db.models import Q
 
@@ -39,6 +39,10 @@ class TalkForm(ModelForm):
     class Meta:
         model = Trabalho
 
+class ImproveForm(ModelForm):
+    class Meta:
+        model = Improve
+
 def login(request):
     """This is a function that will be used as a front-end to the
     django's login system. It receives username and password fields
@@ -158,11 +162,6 @@ def talk_details(request, tid):
     entity = get_object_or_404(Trabalho, pk=tid)
     form = TalkForm(request.POST or None, instance=entity)
 
-    # avoiding smart people trying to se talks of other speakers.
-    speaker = request.user.palestrante_set.get()
-    if speaker.id != entity.palestrante.id:
-        return forbidden
-
     # These fields should not be shown to the user.
     form.fields['palestrante'].widget = HiddenInput()
     form.fields['evento'].widget = HiddenInput()
@@ -183,6 +182,12 @@ def talk_details(request, tid):
         form.fields['outros_palestrantes'].initial = ''
         form.fields['outros_palestrantes'].widget = HiddenInput()
 
+    # avoiding smart people trying to se talks of other speakers.
+    speaker = request.user.palestrante_set.get()
+    if speaker.id != entity.palestrante.id \
+            and speaker not in entity.outros_palestrantes.all():
+        return forbidden
+
     if request.POST and form.is_valid():
         form.save()
 
@@ -251,3 +256,43 @@ def talk_add(request):
     c = {'form': form}
     return render_to_response('eventos/talk-add.html', Context(c),
                               context_instance=RequestContext(request))
+
+def list_all_talks(request):
+    event = Evento.objects.get(site__id__exact=settings.SITE_ID)
+    trilhas = Trilha.objects.filter(evento=event)
+
+    improve = []
+    for t in trilhas:
+        talks = Trabalho.objects.filter(trilha=t)
+        aux = {'trilha':t.nome, 'talks':talks}
+        improve.append(aux)
+
+    c = {'improve': improve,}
+    return render_to_response('eventos/improve.html', Context(c),
+                              context_instance=RequestContext(request))
+
+def talk_improve(request, tid):
+    if not request.user:
+        return forbidden
+
+    talk = get_object_or_404(Trabalho, pk=tid)
+    improve = Improve.objects.filter(trabalho=talk)
+
+    # building the form
+    form = ImproveForm(request.POST or None)
+    form.fields['trabalho'].widget = HiddenInput(attrs={'value':talk.id})
+    form.fields['usuario'].widget = HiddenInput(attrs={'value':request.user.id})
+
+    if request.POST and form.is_valid():
+        event = Evento.objects.get(site__id__exact=settings.SITE_ID)
+        # validation
+        cleaned = form.cleaned_data
+        if cleaned['trabalho'].evento.id != event.id:
+            return forbidden
+
+        instance = form.save()
+        return HttpResponseRedirect('/improve/%d/' % talk.id)
+
+    c = {'talk': talk, 'form': form, 'improve': improve}
+    return render_to_response('eventos/talk_improve.html', Context(c),
+                              context_instance=RequestContext(request))