def posting(request): # A HTTP POST? if request.method == 'POST': form = PostingForm(request.POST) request_dict={} # Have we been provided with a valid form? if form.is_valid(): posting_rank=form.cleaned_data['rank']; possible_ranks=Posting.objects.filter(rank=posting_rank); if possible_ranks: print "Rank Error!"; else: form.save(commit=True) # Now call the index() view. # The user will be shown the homepage. return index(request) else: # The supplied form contained errors - just print them to the terminal. print form.errors else: # If the request was not a POST, display the form to enter details. form = PostingForm() # Bad form (or form details), no form supplied... # Render the form with error messages (if any). return render(request, 'admin_portal/[Temp] templates and css/change_posting.html', {'form': form})
def create(request): if request.POST: form = PostingForm(request.POST) if form.is_valid() and request.user.is_authenticated(): obj = form.save(commit=False) obj.username = request.user obj.save() return HttpResponseRedirect('/')
def post(request): if request.POST: form = PostingForm(request.POST) if form.is_valid(): posting = form.save(request.user) return render_to_response("postings/posting_form_reply.html",{ "posting": posting, "content_type": form.cleaned_data["content_type"], "object_id": form.cleaned_data["object_id"], }, context_instance=RequestContext(request)) else: return HttpResponse(status=400, content="Form was not valid") return HttpResponseRedirect(request.META.get('HTTP_REFERER','/'))
def post(): if current_user.is_authenticated: form = PostingForm() if request.method == 'GET': return render_template("post.html", form=form) if request.method == 'POST' and form.validate(): data = request.form add_post = Post(title=data['title'], article=data['content'],\ Tags1=data['tag1'],Tags2=data['tag2'], Tags3=data['tag3'], create_time=datetime.datetime.now()) db.session.add(add_post) db.session.commit() flash(u'您的文章已经提交成功') return redirect(url_for('index'))
def new_posting(request, template_name="jobs/new_posting.html"): """ Interface to create a new job posting. A GET request will present an empty form. A POST request will allow previewing and submitting the job posting. """ if request.method == 'POST': form = PostingForm(request.POST) if 'submit' in request.POST and form.is_valid(): posting = form.save() send_new_posting_notification(posting) return HttpResponseRedirect(posting.get_edit_url()) if form.is_valid(): preview = form.save(commit=False) # create a dummy posting instance return render_to_response(template_name, {'form': form, 'preview': preview}) else: form = PostingForm() return render_to_response(template_name, {'form': form})
def edit_posting_handler(request, posting_type, pk): if request.method == "POST": form = PostingForm(request.POST, request.FILES) #since existing posting objects passed validation at time of creation, #image/file upload fields and their alt descriptions should be optional form.fields['colored_icon'].required = False form.fields['uncolored_icon'].required = False form.fields['photo_one'].required = False form.fields['photo_two'].required = False form.fields['photo_three'].required = False form.fields['photo_one_alt_text'].required = False form.fields['photo_two_alt_text'].required = False form.fields['photo_three_alt_text'].required = False #although the field was deleted in the GET request, #it must be nonrequired to pass validation when adding a role type if posting_type == "role_type": form.fields['short_project_description'].required = False #del form.fields['short_project_description'] if form.is_valid(): project = get_object_or_404(Posting, pk=pk) #TODO: Remove deprecated check if not project: print "edit_posting_handler: primary key for editing project does not point to an existing project" return render(request, 'generic_error.html', {error_text: 'You have tried to edit a nonexistent posting.'}) #return HttpResponseRedirect("/admin") project.posting_type = posting_type project.name = form.cleaned_data['name'] #TODO(lpei): Remove this, only for testing purposes if not project.short_name: project.short_name = generate_short_name(project.name) print project.short_name project.tagline = form.cleaned_data['tagline'] project.icon_color = form.cleaned_data['icon_color'] #since file uploads may be optional, #we have to check that they exist! if 'colored_icon' in request.FILES: project.colored_icon = request.FILES['colored_icon'] if 'uncolored_icon' in request.FILES: project.uncolored_icon = request.FILES['uncolored_icon'] if 'photo_one' in request.FILES: project.photo_one = request.FILES['photo_one'] if 'photo_two' in request.FILES: project.photo_two = request.FILES['photo_two'] if 'photo_three' in request.FILES: project.photo_three = request.FILES['photo_three'] project.photo_one_alt_text = form.cleaned_data['photo_one_alt_text'] project.photo_two_alt_text = form.cleaned_data['photo_two_alt_text'] project.photo_three_alt_text = form.cleaned_data['photo_three_alt_text'] project.description = form.cleaned_data['description'] if posting_type == "project": project.short_project_description = form.cleaned_data['short_project_description'] else: project.short_project_description = "" #add each role to the many set project.openings.clear() for role_pk_val in form.cleaned_data['role_multiselect']: role = Opening.objects.get(pk=role_pk_val) if role: project.openings.add(role) else: print "edit_posting_handler: role specified by primary key does not exist" #save the role updates project.save() return HttpResponseRedirect("/admin") else: print "edit_posting_handler: form was not valid" #print form.errors #since the form is only created with request.POST and request.FILES #any empty nonrequired fields must be filled in with the existing #object in the database before we redisplay it, else we get errors #like nonattached files for nonedited photos posting = get_object_or_404(Posting, pk=pk) #TODO: remove deprecated check if not posting: return render(request, 'generic_error.html', {error_text: 'You have tried to edit a nonexistent posting.'}) form.instance = posting form_submit_action_url = "/admin/edit_" + posting_type + "/" + pk + "/" return render(request, 'edit_posting.html', {'form': form, 'form_submit_action_url':form_submit_action_url, 'posting_type': posting_type}) elif request.method == "GET": posting_object = get_object_or_404(Posting, pk=pk) #TODO: revise check condition if posting_object and posting_object.posting_type == posting_type: form = PostingForm(instance = posting_object) if posting_type == "role_type": del form.fields['short_project_description'] #IMPORTANT: the submit action url must be set correctly! form_submit_action_url = "/admin/edit_" + posting_type + "/" + pk + "/" #additionally, since there is no direct mapping from #the model's openings set to the form's MultipleChoiceField, #we'll need to generate the initial checked choices initial_choices = [] for role in posting_object.openings.all(): initial_choices.append(role.pk) form.fields['role_multiselect'].initial = initial_choices all_role_choices = [(role.pk, role.title) for role in Opening.objects.all()] form.fields['role_multiselect'].choices = all_role_choices return render(request, 'edit_posting.html', {'form': form, 'form_submit_action_url':form_submit_action_url, 'posting_type': posting_type}) else: print "edit_posting_handler: pk points to a nonexistent object" return render(request, 'generic_error.html', {error_text: 'You have tried to edit a nonexistent posting.'}) #return HttpResponseRedirect("/admin") else: #impossible case return HttpResponse(response)
def add_posting_handler(request, posting_type): if request.method == "POST": form = PostingForm(request.POST, request.FILES) #although the field was deleted in the GET request, #it must be nonrequired to pass validation when adding a role type if posting_type == "role_type": form.fields['short_project_description'].required = False #del form.fields['short_project_description'] if form.is_valid(): project = Posting() project.posting_type = posting_type project.name = form.cleaned_data['name'] project.short_name = generate_short_name(project.name) project.tagline = form.cleaned_data['tagline'] project.icon_color = form.cleaned_data['icon_color'] #internally assigned to be the last! project.rank = len(Posting.objects.all()) #passing validation guarantees existence of these files (their required attribute is set) project.colored_icon = request.FILES['colored_icon'] project.uncolored_icon = request.FILES['uncolored_icon'] project.photo_one = request.FILES['photo_one'] project.photo_two = request.FILES['photo_two'] project.photo_three = request.FILES['photo_three'] project.photo_one_alt_text = form.cleaned_data['photo_one_alt_text'] project.photo_two_alt_text = form.cleaned_data['photo_two_alt_text'] project.photo_three_alt_text = form.cleaned_data['photo_three_alt_text'] project.description = form.cleaned_data['description'] if posting_type == "project": project.short_project_description = form.cleaned_data['short_project_description'] else: project.short_project_description = "" #because this is a new addition, we'll need to save the object first so that the many to many field can be used project.save() #add each role to the many set project.openings.clear() for role_pk_val in form.cleaned_data['role_multiselect']: role = Opening.objects.get(pk=role_pk_val) if role: project.openings.add(role) else: print "add_posting_handler: role specified by primary key does not exist" #save the role updates project.save() return HttpResponseRedirect("/admin") else: print "add_posting_handler: invalid form" #form_url = "/admin/add_" + posting_type + "/" #return HttpResponseRedirect(form_url) form_submit_action_url = "/admin/add_" + posting_type + "/" return render(request, 'add_posting.html', {'form': form, 'form_submit_action_url':form_submit_action_url, 'posting_type': posting_type}) elif request.method == "GET": form = PostingForm() if posting_type == "role_type": del form.fields['short_project_description'] form_submit_action_url = "/admin/add_" + posting_type + "/" return render(request, 'add_posting.html', {'form': form, 'form_submit_action_url':form_submit_action_url, 'posting_type': posting_type}) else: #impossible case return HttpResponse(response)