def add_intra_task(request, primkey=None): #Get Parent Task if primkey: #Need to figure out the try, except block here parenttask = Task.objects.get(pk=primkey) parentlabel = "\nParent task: " + parenttask.subject else: parentlabel = "\nThis is a top level task." parenttask = None userprofile = request.user.get_profile() department = userprofile.dept title = "Add Intradepartmental Task" info = parentlabel if request.method == 'POST': form = IntraTaskForm(department, request.POST) if form.is_valid(): newTask = form.save(commit=False) #Set the origin & target departments & approve the task. newTask.origindept = userprofile.dept newTask.targetdept = userprofile.dept newTask.taskcreator = userprofile newTask.taskstatus = 'O' newTask.parenttask = parenttask #For many to many relationships to be created, the object MUST first exist in the database. newTask.save() #UNCOMMENT THE BELOW LINE IF MANYTOMANY DATA IS BEING SAVED DIRECTLY FROM THE FORM #form.save_m2m() #Get the TaskForce from the form cores = form.cleaned_data['cores'] coords = form.cleaned_data['coords'] supercoords = form.cleaned_data['supercoords'] #Set the TaskForce for the Task for user in coords: newTask.taskforce.add(user) for user in supercoords: newTask.taskforce.add(user) for user in cores: newTask.taskforce.add(user) newTask.save() return HttpResponse ("Task Saved") else: return render_to_response ('tasks/task.html', {'form': form, 'title':title, 'info':info }, context_instance=RequestContext(request)) else: form = IntraTaskForm(department) context = {'form': form, 'title':title } return render_to_response('tasks/task.html', context, context_instance=RequestContext(request))
def edit_task(request, primkey): task = Task.objects.get(pk=primkey) userprofile = request.user.get_profile() department = userprofile.dept #Get Parent Task if task.parenttask: parentlabel = "\nParent task: " + task.parenttask.subject else: parentlabel = "\nThis is a top level task." #___________----INTRADEPARTMENTAL TASK EDIT----__________________ if ((task.isxdepartmental == False) and (task.origindept == department)): title = "Edit Intradepartmental Task" info = parentlabel if request.method == 'POST': form = IntraTaskForm(department, request.POST, instance=task) if form.is_valid(): form.save() #Get the TaskForce from the form cores = form.cleaned_data['cores'] coords = form.cleaned_data['coords'] supercoords = form.cleaned_data['supercoords'] #Set the TaskForce for the Task task.taskforce.clear() for user in coords: task.taskforce.add(user) for user in supercoords: task.taskforce.add(user) for user in cores: task.taskforce.add(user) task.save() return HttpResponse ("Task Saved") else: #Render the form along with all its errors. return render_to_response ('tasks/task.html', {'form': form, 'title':title , 'info':info }, context_instance=RequestContext(request)) else: form = IntraTaskForm(department, instance=task) context = {'form': form, 'title':title } return render_to_response('tasks/task.html', context, context_instance=RequestContext(request)) #___________----CROSSDEPARTMENTAL TASK EDIT----__________________ #TODO: #For the target core's beenfit, display the ASSIGNED SUBDEPARTMENT in the Approval Page. #As of now, he has the freedom to assign the task to users from other subdepartments too. elif (task.isxdepartmental == True) and (core_check(request.user)): if request.method == 'POST': #For the originating department's core if (task.taskstatus == 'U') and (task.origindept == department): title = "Edit Crossdepartmental Task" info = "Allowed only until approved by the target department's core." + parentlabel form = CrossTaskForm(department, request.POST, instance=task) if form.is_valid(): form.save() #Set the Target Department for subdept in task.targetsubdepts.all(): task.targetdept = subdept.dept task.save() return HttpResponse ("Task Saved") else: #Render the form again with all its errors. return render_to_response ('tasks/task.html', {'form': form, 'title':title, 'info': info }, context_instance=RequestContext(request)) #For the target department's core: if task.targetdept == department: #Getting the targetsubdept. There is only one. The loop will run only once. for subdept in task.targetsubdepts: targetsubdept = subdept title = "Edit/Approve Crossdepartmental Task" info = "Submitting the task here automatically approves & assigns it to the selected workforce.\n<b>Target Subdepartment Requested for the Task: " + targetsubdept + "</b>" + parentlabel form = IntraTaskForm(department, request.POST, instance=task) if form.is_valid(): form.save() #Get the TaskForce from the form cores = form.cleaned_data['cores'] coords = form.cleaned_data['coords'] supercoords = form.cleaned_data['supercoords'] #Set the TaskForce for the Task task.taskforce.clear() for user in coords: task.taskforce.add(user) for user in supercoords: task.taskforce.add(user) for user in cores: task.taskforce.add(user) #Approve the task. task.taskstatus = 'O' task.save() return HttpResponse ("Task Saved/Approved.") else: #Render the form again with all its errors. return render_to_response ('tasks/task.html', {'form': form, 'title':title , 'info': info }, context_instance=RequestContext(request)) else: return HttpResponse ("Pack.") else: #For the originating department's core if (task.taskstatus == 'U') and (task.origindept == department): title = "Edit Crossdepartmental Task" info = "Allowed only until approved by the target department's core." form = CrossTaskForm (department, instance=task) context = {'form': form, 'title':title , 'info': info } return render_to_response('tasks/task.html', context, context_instance=RequestContext(request)) #For the target department's core: if task.targetdept == department: #Getting the targetsubdept. There is only one. The loop will run only once. for subdept in task.targetsubdepts: targetsubdept = subdept title = "Edit/Approve Crossdepartmental Task" info = "Submitting the task here automatically approves & assigns it to the selected workforce.\n<b>Target Subdepartment Requested for the Task: " + targetsubdept + "</b>" form = IntraTaskForm(department, instance=task) context = {'form': form, 'title':title , 'info': info } return render_to_response('tasks/task.html', context, context_instance=RequestContext(request)) else: return HttpResponse ("Pack.") else: return HttpResponse ("Pack.")