Example #1
0
    def post(self, request, *args, **kwargs):
        try:
            # Explicitly setting the request.post to be mutable so that we can add additional values to it.
            #once done, reset the mutable property. dont need to do this if a form is a multi-part form.
            mutable = request.POST._mutable
            request.POST._mutable = True
            #Set added by updated by user on form post data
            self.set_added_by_update_by_users_on_view(request, GlobalConstants.ADD_ACTION)
            request.POST._mutable = mutable
            projectform = ProjectForm(request.POST)

            project_members_response = {}
            project_members_list = [project_member for project_member in request.POST.getlist('project_members') if
                                    project_member != ""]
            project_members_values_list = [int(project_member_value) for project_member_value in
                                           request.POST.getlist('project_members_value') if project_member_value != ""]

            if projectform.is_valid():
                #First check if the project resources has no freely typed internal user name
                if len(project_members_list) != len(project_members_values_list):
                    if project_members_values_list:
                        actual_project_members = InternalResource.objects.filter(id__in=project_members_values_list)
                        for member in project_members_list:
                            if member not in actual_project_members:
                                project_members_response[str(project_members_list[member])] = str(
                                    project_members_list[member] + " is not a valid Internal Resource")
                                # else:
                                #     project_members_response[member]
                    else:
                        project_members_response[str(project_members_list[0])] = str(
                            project_members_list[0] + " is not a valid Internal Resource")
                    context = self.get_context_data()
                    context['project_members_response'] = project_members_response
                    context['form'] = projectform
                    return self.render_to_response(context)
                else:
                    self.object = projectform.save()
                    super(AddProject, self).form_valid(projectform)
                    for member in project_members_values_list:
                        member = InternalResource.objects.get(id=int(member))
                        project_resource = ProjectResources(project_id=self.object, resource_id=member,
                                                            addedBy=request.user,
                                                            updatedBy=request.user,
                                                            is_active=True)
                        project_resource.save()
                    return HttpResponseRedirect(self.get_success_url())
            else:
                actual_project_members = InternalResource.objects.filter(id__in=project_members_values_list)
                for member in project_members_list:
                    if member not in actual_project_members:
                        project_members_response[member] = ["0", member + "-"]
                    else:
                        project_members_response[member] = [actual_project_members[member].id, ""]

                return self.form_invalid(form=projectform, project_members_response=project_members_response)

        except Exception as e:
            logger.error("Exception while adding Project: " + str(e))
Example #2
0
    def post(self, request, *args, **kwargs):
        try:
            self.object = self.get_object()
            #Explicitly setting the request.post to be mutable so that we can add additional values to it.
            #once done, reset the mutable property. dont need to do this if a form is a multi-part form.
            mutable = request.POST._mutable
            request.POST._mutable = True
            #Set added by updated by user on form post data
            self.set_added_by_update_by_users_on_view(request, GlobalConstants.EDIT_ACTION)
            request.POST._mutable = mutable

            #get a list of all the existing project resources some new resources might have been added
            #and others might have been removed so, we go through the list received and
            #list in DB and compare the values and update accordingly
            resource_list = ProjectResources.objects.values('resource_id_id').filter(project_id_id=self.object.id)
            project_members = request.POST.getlist('project_members_value')

            tmp_resource_list = []
            #deleting any resource that was removed
            for project_resource in resource_list:
                if str(project_resource['resource_id_id']) not in project_members:
                    proj_resource = ProjectResources.objects.get(resource_id_id=project_resource['resource_id_id'],
                                                                 project_id_id=self.object.id)
                    proj_resource.delete()

                    #Creating a temp list to store the retrieved resource ID's, so that addition can be easier.
                tmp_resource_list += str(project_resource['resource_id_id'])

            #adding any newly added resource
            for project_member in project_members:
                if str(project_member) not in tmp_resource_list:
                    if str(project_member) != "":
                        project_member = InternalResource.objects.get(id=int(project_member))
                        project_resource = ProjectResources(project_id=self.object, resource_id=project_member,
                                                            addedBy=request.user,
                                                            updatedBy=request.user,
                                                            is_active=True)
                        project_resource.save()

            #all done, now update the main project and return.
            #possible bug, what happens if the project changes fail to save?
            return super(UpdateProject, self).post(request, *args, **kwargs)
        except Exception as e:
            logger.error("Exception while updating Project: " + str(e))