示例#1
0
def charge_training(request):
	trainer: User = request.user
	try:
		charges = {}
		for key, value in request.POST.items():
			if is_valid_field(key):
				attribute, separator, index = key.partition("__")
				index = int(index)
				if index not in charges:
					charges[index] = TrainingSession()
					charges[index].trainer = trainer
				if attribute == "chosen_user":
					charges[index].trainee = User.objects.get(id=to_int_or_negative(value))
				if attribute == "chosen_tool":
					charges[index].tool = Tool.objects.get(id=to_int_or_negative(value))
					if not trainer.is_staff and trainer.is_tool_superuser and charges[index].tool not in trainer.superuser_for_tools.all():
						raise Exception("The trainer is not authorized to train on this tool")
				if attribute == "chosen_project":
					charges[index].project = Project.objects.get(id=to_int_or_negative(value))
				if attribute == "duration":
					charges[index].duration = int(value)
				if attribute == "charge_type":
					charges[index].type = int(value)
				if attribute == "qualify":
					charges[index].qualified = (value == "on")
		for c in charges.values():
			c.full_clean()
			check_billing_to_project(c.project, c.trainee, c.tool)
	except ProjectChargeException as e:
		return HttpResponseBadRequest(e.msg)
	except User.DoesNotExist:
		return HttpResponseBadRequest("Please select a trainee from the list")
	except Tool.DoesNotExist:
		return HttpResponseBadRequest("Please select a tool from the list")
	except Project.DoesNotExist:
		return HttpResponseBadRequest("Please select a project from the list")
	except Exception as e:
		training_logger.exception(e)
		return HttpResponseBadRequest('An error occurred while processing the training charges. None of the charges were committed to the database. Please review the form for errors and omissions then submit the form again.')
	else:
		for c in charges.values():
			if c.qualified:
				qualify(c.trainer, c.trainee, c.tool)
			c.save()
		dictionary = {
			'title': 'Success!',
			'content': 'Training charges were successfully saved.',
			'redirect': reverse('landing'),
		}
		return render(request, 'display_success_and_redirect.html', dictionary)
示例#2
0
def charge_training(request):
    try:
        charges = {}
        for key, value in request.POST.items():
            if is_valid_field(key):
                attribute, separator, index = key.partition("__")
                index = int(index)
                if index not in charges:
                    charges[index] = TrainingSession()
                    charges[index].trainer = request.user
                if attribute == "chosen_user":
                    charges[index].trainee = User.objects.get(id=value)
                if attribute == "chosen_tool":
                    charges[index].tool = Tool.objects.get(id=value)
                if attribute == "chosen_project":
                    charges[index].project = Project.objects.get(id=value)
                if attribute == "duration":
                    charges[index].duration = int(value)
                if attribute == "charge_type":
                    charges[index].type = int(value)
                if attribute == "qualify":
                    charges[index].qualified = (value == "on")
        for c in charges.values():
            c.full_clean()
    except Exception as e:
        training_logger.exception(e)
        return HttpResponseBadRequest(
            'An error occurred while processing the training charges. None of the charges were committed to the database. Please review the form for errors and omissions then submit the form again.'
        )
    else:
        for c in charges.values():
            if c.qualified:
                qualify(c.trainer, c.trainee, c.tool)
            c.save()
        dictionary = {
            'title': 'Success!',
            'content': 'Training charges were successfully saved.',
            'redirect': '/',
        }
        return render(request, 'display_success_and_redirect.html', dictionary)