# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
+from django.conf import settings
from django.http import HttpResponseRedirect, HttpResponseForbidden
from django.contrib import auth
from django.contrib.auth.forms import AuthenticationForm
from django.newforms import form_for_instance, form_for_model, HiddenInput
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext, Context, loader
-from eventos.models import Palestrante, Trabalho
+from eventos.models import Palestrante, Trabalho, TipoTrabalho, Trilha, Evento
from eventos.forms import RegisterSpeaker
forbidden = \
def talk_details(request, tid):
"""Shows a form to edit a talk
"""
+ # Selected in settings.py (SITE_ID) variable, because an event can
+ # be linked with only one site.
+ event = Evento.objects.get(site__id__exact=settings.SITE_ID)
+
+ # building the form
entity = get_object_or_404(Trabalho, pk=tid)
FormKlass = form_for_instance(entity)
form = FormKlass(request.POST or None)
+
+ # These fields should not be shown to the user.
+ form.fields['palestrante'].widget = HiddenInput()
+ form.fields['evento'].widget = HiddenInput()
+
+ # These fields are event specific
+ trilhas = Trilha.objects.filter(evento=event)
+ form.fields['trilha']._set_queryset(trilhas)
+
+ tipos = TipoTrabalho.objects.filter(evento=event)
+ form.fields['tipo']._set_queryset(tipos)
+
+ # hidding the owner in the other speakers list
+ other = Palestrante.objects.exclude(pk=entity.id)
+ form.fields['outros_palestrantes']._set_queryset(other)
+ if other.count() == 0:
+ # I need set the value to '', otherwise the wise django
+ # newforms will fill the field with the invalid string '[]'
+ form.fields['outros_palestrantes'].initial = ''
+ form.fields['outros_palestrantes'].widget = HiddenInput()
+
if request.POST and form.is_valid():
form.save()
if not hasattr(request.user, 'palestrante_set'):
return forbidden
+ # Selected in settings.py (SITE_ID) variable, because an event can
+ # be linked with only one site.
+ event = Evento.objects.get(site__id__exact=settings.SITE_ID)
+
+ # building the form
entity = request.user.palestrante_set.get()
FormKlass = form_for_model(Trabalho)
form = FormKlass(request.POST or None,
- initial={'palestrante': entity.id})
+ initial={'palestrante': entity.id, 'evento': event.id})
- # This field should not be shown to the user.
+ # These fields should not be shown to the user.
form.fields['palestrante'].widget = HiddenInput()
+ form.fields['evento'].widget = HiddenInput()
+
+ # These fields are event specific
+ trilhas = Trilha.objects.filter(evento=event)
+ form.fields['trilha']._set_queryset(trilhas)
+
+ tipos = TipoTrabalho.objects.filter(evento=event)
+ form.fields['tipo']._set_queryset(tipos)
# hidding the owner in the other speakers list
other = Palestrante.objects.exclude(pk=entity.id)
form.fields['outros_palestrantes']._set_queryset(other)
+ if other.count() == 0:
+ form.fields['outros_palestrantes'].widget = HiddenInput()
if request.POST and form.is_valid():
+ # validation
+ cleaned = form.cleaned_data
+ if cleaned['tipo'].evento.id != event.id:
+ return forbidden
+
+ if cleaned['trilha'].evento.id != event.id:
+ return forbidden
+
instance = form.save()
return HttpResponseRedirect('/speaker/%d/talks/' % entity.id)