def add_rehearsal(request, band_id):
    context = {}
    band = bands_manager.get_band(band_id)
    context['band'] = band

    if request.method == 'POST':
        form = RehearsalEntryForm(request.POST)
        context['form'] = form

        if form.is_valid():
            date_start = form.cleaned_data['date_start']
            time_start = form.cleaned_data['time_start']
            time_end = form.cleaned_data['time_end']
            place = form.cleaned_data['place']
            costs = form.cleaned_data['costs']
            try:
                if not band.is_member(request.user):
                    raise Exception("You have no permission to add songs to this band's setlist cause you are not a member of it.")
                bands_manager.add_rehearsal_entry(band_id, date_start, time_start, time_end, place, costs, request.user)
                request.flash['success'] = "Rehearsal added successfully!"
                return redirect('/band/%d/events' % band.id)
            except Exception as exc:
                context['error_msg'] = "Error ocurred: %s" % exc.message
                # 500
    else:
        context['form'] = RehearsalEntryForm()
    return render_to_response('band/events/rehearsal/create.html', context, context_instance=RequestContext(request))
def edit_rehearsal(request, band_id, entry_id):
    context = {}

    try:
        band = bands_manager.get_band(band_id)
        context['band'] = band
        rehearsal = bands_manager.get_rehearsal(entry_id)

        if not band.is_member(request.user):
            raise Exception("You have no permission to edit this band's events cause you are not a member of it.")

        if request.method == 'POST':
            form = RehearsalEntryForm(request.POST)
            context['form'] = form
            if form.is_valid():
                date_start = form.cleaned_data['date_start']
                time_start = form.cleaned_data['time_start']
                time_end = form.cleaned_data['time_end']
                place = form.cleaned_data['place']
                costs = form.cleaned_data['costs']

                rehearsal = bands_manager.update_rehearsal_entry(entry_id, date_start, time_start, time_end, place, costs)
                request.flash['success'] = "Rehearsal updated successfully!"
                return redirect('/band/%d/events' % band.id)
        else: # GET
            if rehearsal.band != band:
                raise Exception("There is no rehearsal for this band with the given Id.")

            data = {'date_start' : rehearsal.date_start,
                    'time_start' : rehearsal.time_start,
                    'time_end' : rehearsal.time_end,
                    'place' : rehearsal.place,
                    'costs' : rehearsal.costs,
                    }
            form = RehearsalEntryForm(data)

        context['rehearsal'] = rehearsal
        context['form'] = form
    except Exception as exc:
        # 404
        context['error_msg'] = "Error ocurred: %s" % exc.message

    return render_to_response('band/events/rehearsal/edit.html', context, context_instance=RequestContext(request))