def add_role(request): # A HTTP POST? if request.method == 'POST': form = OpeningForm(request.POST) # Have we been provided with a valid form? if form.is_valid(): #first create a new role in the opening database title=form.cleaned_data['title'] description=form.cleaned_data['description'] new_role=Opening(title=title, description=description) new_role.save() # get the projects the user wants to connect with this role postings=form.cleaned_data['selected_projects'] # consider multiple projects that may be related to such opening. for posting_id in postings: posting=Posting.objects.get(pk=posting_id) if posting: # add the new relation (projects - roles related to the projects) into mainsite_posting_openings print posting.openings.all() posting.openings.add(new_role) else: print "No such project" #get the role types that the role is under role_types=form.cleaned_data['selected_role_types'] #allow one role to be under multiple role types for role_id in role_types: posting=Posting.objects.get(pk=role_id) if posting: #add the new relation into mainsite_posting_openings posting.openings.add (new_role) else: print "No such Role type" # Now call the index() view. # The user will be shown the homepage. return HttpResponseRedirect("/admin/") #add some pop up window for confirmation of save 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 = OpeningForm() # Bad form (or form details), no form supplied... # Render the form with error messages (if any). return render(request, 'change_role.html', {'form': form})
def opening(): form = OpeningForm() if form.validate_on_submit(): if request.method == "POST": arr = [session['id']] for val in ('arrivaltime','departuretime','q1','q2','q3','q3c','q4','q5','q6','q6c','q7','q7c','q8','q8c','q9','q9c','q10','q10c','q11','q11c','q12','q12c','q13','q14','q15','q16','q17','q18','q18c','q19','q20'): arr.append(request.form[val]) cur = mysql.connection.cursor() arr = tuple(arr) cur.execute("SELECT * from opening where l_id=(%s)",(str(session['id']))) exist = cur.fetchone() if exist: flash('You have already submitted your opening form','danger') else: cur.execute(f"INSERT INTO opening VALUES {arr}") mysql.connection.commit() flash(f'Opening Form sent!!!','success') return redirect(url_for('local_home')) if g.loggedin=='localobserver': return render_template('opening.html', title='Opening Form',form=form) return render_template('login.html', title='Welcome')
def edit_role(request, pk): old_role = None if pk: old_role=Opening.objects.get(pk=pk) if old_role: form = OpeningForm(instance=old_role) form.form_submit_action_url = "/admin/edit_role/" + 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_roletypes = [] initial_projects = [] all_roletypes = [] all_projects = [] postings = list(Posting.objects.all()) for posting in postings: print str(posting.pk)+" "+posting.name+" " # add the new relation into mainsite_posting_openings if posting.posting_type == "role_type": all_roletypes.append((posting.pk, posting.name)) else: all_projects.append((posting.pk, posting.name)) # check if the old role is in this posting's opening if old_role in posting.openings.all(): if posting.posting_type == "role_type": initial_roletypes.append(posting.pk) else: initial_projects.append(posting.pk) form.fields['selected_role_types'].choices = all_roletypes form.fields['selected_projects'].choices = all_projects form.fields['selected_role_types'].initial = initial_roletypes form.fields['selected_projects'].initial = initial_projects if request.method == 'POST': form = OpeningForm(request.POST) # Have we been provided with a valid form? if form.is_valid(): #first create a new role in the opening database #and then add a record to the relation title=form.cleaned_data['title'] description=form.cleaned_data['description'] old_role.title=title old_role.description=description old_role.save() postings=map(int, form.cleaned_data['selected_projects']) #deselect_id: the postings that were originally linked to the roles #but not any more deselect_id=set(initial_projects)-set(postings) #newselect_id: the postings that needed to be added to #the relationship table in order to link to this role newselect_id=set(postings)-set(initial_projects) #updateselect_id: don't need to do anything for these category #updateselect_id=set(postings) & set(initial_projects) for posting_id in deselect_id: posting=Posting.objects.get(pk=posting_id) if posting: #remove the relationship posting.openings.remove(old_role) for posting_id in newselect_id: posting=Posting.objects.get(pk=posting_id) if posting: #add new relationship posting.openings.add(old_role) return HttpResponseRedirect("/admin") else: # The supplied form contained errors - just print them to the terminal. print form.errors else: print "posting_form_handler: pk points to a nonexistent object" return HttpResponseRedirect("/admin") return render(request, 'change_role.html', {'form': form, 'is_edit': True})