def new_idea(request): if request.method == 'POST': #if form has been submitted form = IdeaForm(request.POST) # a form bound to the post data if form.is_valid(): # all validation rules pass title = save_idea(form) return HttpResponseRedirect('/ideas/idea/' + title.replace(' ','_') + '/') else: form = IdeaForm(initial={'status': 'idea'}) # unbound form return render(request, 'new_idea.html', { 'form': form, 'idea': None, 'edit': False })
def idea_submit(request): if request.method == "POST": if request.POST.has_key("part1"): idea_form = IdeaForm(initial={'description': request.POST['description']}) else: idea_form = IdeaForm(request.POST) if idea_form.is_valid(): title = idea_form.cleaned_data["title"] description = idea_form.cleaned_data["description"] tags = idea_form.cleaned_data["tags"] saveObj = idea_form.save(commit=False) saveObj.author = request.user saveObj.save() return HttpResponseRedirect("/") else: idea_form = IdeaForm() return render_to_response('idea/idea_submit.html', locals(), context_instance=RequestContext(request))
def edit_idea(request, title_nospaces): if request.method == 'POST': # if form has been submitted form = IdeaForm(request.POST) # a form bound to the post data # delete old idea before saving so we don't end up # with two objects with the same title title_withspaces = title_nospaces.replace('_', ' ') old_idea = Idea.objects.get(title__exact=title_withspaces) saved_idea = old_idea old_idea.delete() print('Idea \'{}\' deleted!'.format(title_withspaces)) if form.is_valid(): # all validation rules pass print('Form is valid') title = save_idea(form) return HttpResponseRedirect('/ideas/idea/' + title.replace(' ','_') + '/') else: # idea with same title still exists idea = saved_idea idea.save() else: form = IdeaForm() # unbound form title_withspaces = title_nospaces.replace('_', ' ') idea = Idea.objects.get(title__exact=title_withspaces) # fill out form with the data from the object # ripe for the editing! form = IdeaForm(initial={ 'title': idea.title, 'status': idea.status, 'short_desc': idea.short_desc, 'long_desc': idea.long_desc, 'links': get_field_string(idea.links), 'tags': get_field_string(idea.tags), 'sources': get_field_string(idea.sources), 'images': get_field_string(idea.images), 'date_formed': idea.date_formed}) return render(request, 'new_idea.html', { 'form': form, 'idea': idea, 'edit': True })
def view_posts_draft(request): """ Displays a list of ideas, the form to add / update ideas, and writing posts """ idea = None idea_id = request.GET.get('idea_id', 0) if idea_id: idea = get_object_or_404(Idea, pk=idea_id) if request.method == 'POST': form = IdeaForm(request.POST, instance=idea, request=request) if form.is_valid(): form.save() return HttpResponseRedirect(reverse('posts_draft')) else: form = IdeaForm(instance=idea) ideas_list = Idea.objects.select_related().filter(status=1) \ .order_by('-rating_ratio') news_list = News.objects.filter(status=1) \ .order_by('-updated_at', '-created_at') \ .select_related('category', 'last_version', 'created_by', \ 'updated_by', 'last_version__author') \ .prefetch_related('versions', 'versions__author') last_revisions = Version.objects.select_related().filter(news__status=1) \ .order_by('-created_at')[:10] last_comments = ThreadedComment.objects.filter(object_pk__in=news_list) \ .select_related().order_by('-created_at')[:10] return render(request, 'posts_draft.html', { 'ideas_list': ideas_list, 'news_list': news_list, 'last_revisions': last_revisions, 'form': form, 'idea_id': idea_id })