def destination(request, page_id, destination_id=None): """ controller to set the (new) destination page for the passed in id similar to edit page, but slightly different processing """ cur_destination = None destination_options = Page.objects.filter(id=destination_id) if len(destination_options): cur_destination = destination_options[0] page_options = Page.objects.filter(id=page_id) if len(page_options): cur_page = page_options[0] if request.method == 'POST': #print "POST!" pageform = PageForm(request.POST, instance=cur_destination, prefix='pages') if pageform.is_valid(): # All validation rules pass existing = check_pageform(pageform, request, cur_destination) if existing: messages.warning(request, 'Found an existing destination with that URL. Setting it as a destination for source page.') new_destination = existing else: #now it's ok to save the changes: new_destination = pageform.save() #all_conversions = Conversion.objects.all() #print all_conversions #look for existing Conversion first! conversion_options = Conversion.objects.filter(source=cur_page).filter(destination=new_destination) print conversion_options if len(conversion_options): messages.warning(request, 'These pages are already associated') cur_conversion = conversion_options[0] else: #associate new_destination and cur_page via new Conversion object conversion = Conversion() conversion.source = cur_page conversion.destination = new_destination #TODO: #consider if notes are useful here #conversion.notes = '?' conversion.save() messages.info(request, 'New destination was added') finished_url = reverse('pages:details', kwargs={'page_id':cur_page.id}) return redirect(finished_url) else: pageform = PageForm(prefix='pages', instance=cur_destination) context = { 'pageform': pageform, } return render(request, 'pages-edit.html', context)