Beispiel #1
0
def index(request):
	context = RequestContext(request)

	experiments = Experiment.objects.filter(status="open")
	sortBy = None
	selected = None

	if(request.GET):
		# get filters
		filters = RequirementsForm(data=request.GET)

		if filters.is_valid():
			# apply filters to query
			if filters.cleaned_data.get("ageMin") is not None and filters.cleaned_data.get("ageMin") != "":
				experiments = experiments.exclude(requirement__ageMin__lt=filters.cleaned_data.get("ageMin"))

			if filters.cleaned_data.get("ageMax") is not None and filters.cleaned_data.get("ageMax") != "":
				experiments = experiments.exclude(requirement__ageMax__gt=filters.cleaned_data.get("ageMax"))
			
			if filters.cleaned_data.get("sex") is not None and filters.cleaned_data.get("sex") != "":
				experiments = experiments.filter(Q(requirement__sex=filters.cleaned_data.get("sex")) | Q(requirement__sex="") | Q(requirement__sex__isnull=True))
			
			if filters.cleaned_data.get("firstLanguage") is not None and filters.cleaned_data.get("firstLanguage") != "":
				experiments = experiments.filter(Q(requirement__firstLanguage=filters.cleaned_data.get("firstLanguage")) | Q(requirement__firstLanguage="") | Q(requirement__firstLanguage__isnull=True))

			if filters.cleaned_data.get("educationLevel") is not None and filters.cleaned_data.get("educationLevel") != "":
				experiments = experiments.filter(Q(requirement__educationLevel=filters.cleaned_data.get("educationLevel")) | Q(requirement__educationLevel="") | Q(requirement__educationLevel__isnull=True))

			if filters.cleaned_data.get("location") is not None and filters.cleaned_data.get("location") != "":
				experiments = experiments.filter(Q(requirement__location=filters.cleaned_data.get("location")) | Q(requirement__location="") | Q(requirement__location__isnull=True))
		else:
			print filters.errors

		# get sort by
		sortBy = request.GET.get("sortBy")
		if sortBy is not None and sortBy != "" and sortBy == "reward":
			experiments = experiments.order_by("-rewardAmount")
			selected = "reward"
		elif sortBy is not None and sortBy != "" and sortBy == "location":
			experiments = experiments.order_by("location")
			selected = "location"
		elif sortBy is not None and sortBy != "" and sortBy == "dateEnd":
			experiments = experiments.order_by("date_end")
			selected = "dateEnd"

	else:
		filters = RequirementsForm()

	if sortBy is None:
		experiments = experiments.order_by("date_end")

	prepareExperiments(experiments)

	return render_to_response('labRatsApp/index.html', {'experiments' : experiments, 'filters': filters, 'selected': selected}, context)
Beispiel #2
0
def modifyExperiment(request,expId):
	context = RequestContext(request)
	print expId
	print request.user.username
	currentUser = User.objects.get(username=request.user.username)
	currentExperiment = Experiment.objects.get(experimentID=expId)
	try:
		currentRequirements = Requirement.objects.get(experiment=expId)
	except:
		currentRequirements = None
	#print currentExperiment.user.user.username
	if request.method == 'POST':
	
		experiment_form = ExperimentForm(data=request.POST, instance=currentExperiment)
		requirements_form = RequirementsForm(data=request.POST, instance=currentRequirements)
		if experiment_form.is_valid() and requirements_form.is_valid():
			
			currentExperiment = experiment_form.save(commit=False)
			currentExperiment.user.user = request.user
			currentExperiment.save()

			currentRequirements = requirements_form.save(commit=False)
			currentRequirements.experiment = currentExperiment
			currentRequirements.save()
			return HttpResponseRedirect('/labRatsApp/profile/'+request.user.username+"/")
		else:	
			print experiment_form.errors, requirements_form.errors


	else:

	
		user = User.objects.filter(username = request.user.username)[0]
		LabUser = LabRatUser.objects.filter(user = request.user)[0]

		if LabUser.userType == "rat":
			return HttpResponse("You are not an experimenter")
		else:
			experiment_form = ExperimentForm(instance=currentExperiment)
			requirements_form = RequirementsForm(instance=currentRequirements)
	return render_to_response('labRatsApp/modifyExperiment.html', {'expId': expId, 'experiment_form' : experiment_form, 'requirements_form' : requirements_form, 'username':request.user.username}, context)
Beispiel #3
0
def createExperiment(request):
	context = RequestContext(request)

	create = False
	if request.method == 'POST':
		if request.user.username is not None:
			user = User.objects.filter(username = request.user.username)[0]  	
			LabUser = LabRatUser.objects.filter(user = request.user)[0]

			if user.is_active:
				experiment_form = ExperimentForm(data=request.POST)
				requirements_form = RequirementsForm(data=request.POST)
				if experiment_form.is_valid() and requirements_form.is_valid():
					experiment = experiment_form.save(commit=False)
					experiment.user = LabUser
					experiment.save()
					requirements = requirements_form.save(commit=False)
					requirements.experiment = experiment
					requirements.save()
					create = True
					return HttpResponseRedirect('/labRatsApp/profile/'+request.user.username+"/")
				else:	
					print experiment_form.errors, requirements_form.errors
			else:
				return HttpResponse("Your labRats account is disabled.")
		else:
			return HttpResponse("Invalid username is None")

		
	else:
		user = User.objects.filter(username = request.user.username)[0]
		LabUser = LabRatUser.objects.filter(user = request.user)[0]
		if LabUser.userType == "rat":
			return HttpResponseForbidden("You are not an experimenter")
		else:
			experiment_form = ExperimentForm()
			requirements_form = RequirementsForm()
	
	return render_to_response('labRatsApp/createExperiment.html', {'experiment_form' : experiment_form, 'requirements_form' : requirements_form, 'username':request.user.username}, context)