Example #1
0
def list(request):
    # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        
        tests=Detail.objects.all()
        for test in tests:
			if test.is_active==True:
				testset=test.test_id
        if form.is_valid():
            newdoc = Document(docfile = request.FILES['docfile'], user=User.objects.get(username=request.user.username), test_set=Detail.objects.get(test_id=testset))
            newdoc.save()

            # Redirect to the document list after POST
            return HttpResponseRedirect(reverse('uploads.views.list'))
    else:
        form = DocumentForm() # A empty, unbound form

    # Load documents for the list page
    documents = Document.objects.all()

    # Render list page with the documents and the form
    return render_to_response(
        'list.html',
        {'documents': documents, 'form': form},
        context_instance=RequestContext(request))
Example #2
0
def model_form_modify(request):
    command = list(request.POST.keys())
    if request.method == 'POST':
        if len(command) >= 1:
            if 'update' in command:
                id = request.POST.get('update')
                images = Document.objects.filter(id=id)
                #### confirm the data is deleted
            elif 'upload' in command:
                id = request.POST.get('upload')
                images = Document.objects.filter(id=id)
                images.delete()
            elif 'delete' in command:
                id = request.POST.get('delete')
                images = Document.objects.filter(id=id)
                #### confirm the data is deleted
                images.delete()
                return redirect('products')
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return redirect('products')
    else:
        form = DocumentForm()
    return render(request, 'upload.html', locals())
Example #3
0
def model_form_upload(request):
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return redirect('products')
    else:
        form = DocumentForm()
    return render(request, 'upload.html', locals())
Example #4
0
def model_form_upload(request):
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return redirect('article_list')
    else:
        form = DocumentForm()
    return render(request, 'uploads/model_form_upload.html', {'form': form})
Example #5
0
def create_profile(request):
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        employee_id=""

        if form.is_valid():
            image_validator, origin_image, image_drawn = form.cleaned_data['photo']
            #def the uploaded image has 1 face and is valid
            if (image_validator==1):
                record = Document()
                record.photo=origin_image
                record.firstname=form.cleaned_data['firstname']
                record.lastname=form.cleaned_data['lastname']
                record.department=form.cleaned_data['department']

                record.save()
                employee_id=record.employee_id

            buffered = BytesIO()


        #face_no, image_drawn = draw_face(image.file.read())

            image_drawn.save(buffered, format="JPEG")
            b64_img = base64.b64encode((buffered.getvalue()))
            one_face= image_validator==1
            #multi_face = image_validator>1
            no_face = image_validator==0

    


            return render(request, 'view_form.html', {
        'form': form, 'image_drawn':b64_img,'one_face':one_face, 'no_face': no_face, 'employee_id': employee_id
    })

    else:
        form = DocumentForm()
    return render(request, 'create_form.html', {
        'form': form
    })
Example #6
0
def list(request):
    # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Document(docfile = request.FILES['docfile'])
            newdoc.save()

            # Redirect to the document list after POST
            return HttpResponseRedirect('/')
    else:
        form = DocumentForm() # A empty, unbound form

    # Load documents for the list page
    documents = Document.objects.all()

    # Render list page with the documents and the form
    return render_to_response(
        'uploads/list.html',
        {'documents': documents, 'form': form},
        context_instance=RequestContext(request)
    )
Example #7
0
def model_form_upload(request):
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = form.save(commit=False)
            newdoc.profile_user = request.user
            newdoc.keep_owner(request.user)
            newdoc.save()
            return redirect('home')
    else:
        form = DocumentForm()
    return render(request, 'model_form_upload.html', {'form': form})
Example #8
0
def Task1bPhaseBSubmitWebInterface(request):
	user=request.user
	dictionary=createPage(user) #Given the logged in user returns the active testset, attempts of the user, released testsets and initializes a form.
	errors=[]
	if request.method=='POST': #If the user submits data...
		form = DocumentForm(request.POST, request.FILES)
		form.fields['system_name'].queryset=system.objects.filter(user=request.user)
		if form.is_valid():
			c=form.cleaned_data
			systems=system.objects.get(system=c['system_name'])
			try: 
				user_data=json.loads(request.FILES['docfile'].read())
			except:
				dictionary['errors']=["Error while decoding the JSON"]
				return render_to_response("Task1B-B.html", dictionary, RequestContext(request))
			#Having the valid user JSON make whatever checks you want	
			test_ids=[]
		        active=Detail1b.objects.filter(phase="B").filter(finished__gte=datetime.now()).filter(started__lte=datetime.now())
			actives=golden_question_1b.objects.filter(testset=Detail1b.objects.filter(phase="A").filter(test_id=active[0].test_id))
			#actives=golden_question_1b.objects.filter(testset=Detail1b.objects.filter(phase="A")).filter(question_id=active[0].test_id)

			for question in actives:
				test_ids.append(question.question_id)
			#Check that the user submits data for every question
			if not len(user_data['questions'])==active[0].number_of_questions:
				dictionary['errors']=["Error! The number of the questions in the JSON is different from the number of questions in the active test set."]
				return render_to_response("Task1B-B.html", dictionary, RequestContext(request))	
			for question in user_data['questions']:
				#Check that the PMIDs submitted belong to active PMIDs
				try: 
					if not question['id'] in test_ids:
						dictionary['errors']=["Error! Question id: %s. The question id doesn't belong to the active test set." %question['id']] 
						return render_to_response("Task1B-B.html", dictionary, RequestContext(request))
				except:
					dictionary['errors']=["Error! Question id not found. Please make sure you submit the correct JSON format."]
					return render_to_response("Task1B-B.html", dictionary, RequestContext(request))		
				if not (("exact_answer" in question) or ("ideal_answer" in question)):
					dictionary['errors']=["Error! Question id: %s. No exact or ideal answer for this question." %question['id']]
                                        return render_to_response("Task1B-B.html", dictionary, RequestContext(request))
				#Factoid Questions: a maximum of 5 entities
				if golden_question_1b.objects.get(question_id=question['id']).type=="factoid":
					try: 
						if len(question['exact_answer'])>5:
							dictionary['errors']=["Error! Question id: %s. In the exact answer of this factoid question there are more than 5 entity names, which is not allowed." %question['id']]
							return render_to_response("Task1B-B.html", dictionary, RequestContext(request))
					except:
						pass		
				#List question: 100 elements of 100 characters each
				if golden_question_1b.objects.get(question_id=question['id']).type=="list":
					try:
						if len(question['exact_answer'])>100:
							dictionary['errors']=["Error! Question id: %s. In the exact answer of this list question there are more than 100 elements, which is not allowed." %question['id']]
							return render_to_response("Task1B-B.html", dictionary, RequestContext(request))	
						for item in question['exact_answer']:
							for inner_item in item:
								if len(inner_item)>400:
									dictionary['errors']=["Error! Question id: %s. In the exact answer of this list question the element %s has more than 100 characters, which is not allowed." %(question['id'], inner_item)]
									return render_to_response("Task1B-B.html", dictionary, RequestContext(request))
					except:
						pass				
				#Yes/no question stricktly "yes" or "no" answer
				if golden_question_1b.objects.get(question_id=question['id']).type=="yesno":
					try:
						if not (question['exact_answer']=="yes" or question['exact_answer']=="no"):
							dictionary['errors']=["Error! Question id: %s. In the exact answer of this yes/no question the answer should be either 'yes' or 'no'. Note that there are no capitals or punctuation." %question['id']]
							return render_to_response("Task1B-B.html", dictionary, RequestContext(request))
					except:
						pass
				try:
					if len(question['ideal_answer'].split())>350:
						dictionary['errors']=["Error! Question id: %s. In the ideal answer of this question the words are more than 200" %question['id']]
						return render_to_response("Task1B-B.html", dictionary, RequestContext(request))
				except:
					pass					
					
			#Having checked everything save the file.
			pt="/home/bioasq/public_html/webexample/uploads/users/task2b/phaseB/testset%d/%s.json" %(active[0].test_id, systems.system)
			try:
				f=open(pt, 'w')
				myfile=File(f)
				print>>myfile, json.dumps(user_data)
				myfile.close()
			except:
				dictionary['errors']=["Error while saving the file. Try again later."]
				return render_to_response("Task1B-B.html", dictionary, RequestContext(request))
			#Delete previous uploads for the same user, system, testset and update the database with information for the saved file
			entry2=user_results_1bb.objects.filter(system=systems).filter(test_id=active[0]).delete()	
			entry2=user_results_1bb(system=systems, user=user, test_id=active[0], path=pt).save()
			dictionary['errors']=["Results saved successfully!"]
			dictionary['attempts']=[item for item in user_results_1bb.objects.all() if item.user == User.objects.get(username='******') and item.test_id in [x for x in Detail1b.objects.filter(phase="B").filter(test_id__gte=4)]]
			return render_to_response("Task1B-B.html", dictionary, RequestContext(request))
	return render_to_response("Task1B-B.html", dictionary, RequestContext(request))
Example #9
0
def task1ba(request):
	active=Detail1b.objects.filter(phase="A").filter(finished__gte=datetime.now()).filter(started__lte=datetime.now())
	if active:
		active=active[0]
	else:
		active=None	
	attempts=[item for item in user_results_1b.objects.all() if item.user == User.objects.get(username=request.user) and item.test_id in [x for x in Detail1b.objects.filter(phase="A").filter(test_id__gte=4)]]
	tests=Detail1b.objects.filter(phase="A").filter(test_id__gte=4).filter(started__lte=datetime.now())
	user=request.user
	errors=[]
	if request.method=='POST': #If the user submits data...
		form = DocumentForm(request.POST, request.FILES)
		form.fields['system_name'].queryset=system.objects.filter(user=request.user)
		if form.is_valid():
			c=form.cleaned_data
			systems=system.objects.get(system=c['system_name'])
			try: 
				user_data_raw=request.FILES['docfile'].read()
				user_data=json.loads(user_data_raw)
			except:
				errors.append("Error while decoding the JSON. Valid JSON?")
				a=upload_information_for_1b(user=user, system=systems, test_id=active, timestamp=datetime.now(), comment=errors[0]).save()
				return render_to_response("Task1B-A.html", {"tests":tests, "attempts":attempts, "current_test":active, "errors":errors, "form":form}, RequestContext(request))
			#Having the valid user JSON make whatever checks you want	
			test_ids=[]
			actives=golden_question_1b.objects.filter(testset=active)
			for question in actives:
				test_ids.append(question.question_id)
			#Check that the user submits data for every question
			if not len(user_data['questions'])==active.number_of_questions:
				errors.append("Error! The number of the questions in the JSON is different from the number of questions in the active test set.") 
				a=upload_information_for_1b(user=user, system=systems, test_id=active, timestamp=datetime.now(), comment=errors[0]).save()
				return render_to_response("Task1B-A.html", {"tests":tests, "current_test":active,"attempts":attempts, "errors":errors, "form":form}, RequestContext(request))	
			for question in user_data['questions']:
				#Check that the PMIDs submitted belong to active PMIDs
				try: 
					if not question['id'] in test_ids:
						errors.append("Error! Question id: %s. The question id doesn't belong to the active test set." %question['id']) 
  						a=upload_information_for_1b(user=user, system=systems, test_id=active, timestamp=datetime.now(), comment=errors[0]).save()
						return render_to_response("Task1B-A.html", {"tests":tests, "current_test":active,"attempts":attempts, "errors":errors, "form":form}, RequestContext(request))
				except:
					errors.append("Error! Question id not found in one of the questions. Please make sure you submit the correct JSON format.")
					a=upload_information_for_1b(user=user, system=systems, test_id=active, timestamp=datetime.now(), comment=errors[0]).save()
					return render_to_response("Task1B-A.html", {"tests":tests, "current_test":active, "errors":errors,"attempts":attempts, "form":form}, RequestContext(request))		
				flag=0
				#Each list of concepts, articles, snippets, triples has less than 100 elements
				try: 
					if len(question['documents'])>100:
						errors.append("Error! Question id: %s. The documents were more than 100." %question['id'])
						a=upload_information_for_1b(user=user, system=systems, test_id=active, timestamp=datetime.now(), comment=errors[0]).save()
						return render_to_response("Task1B-A.html", {"tests":tests, "current_test":active,"attempts":attempts, "errors":errors, "form":form}, RequestContext(request))
				except: 	 
					flag+=1
				try: 
					if len(question['snippets'])>100:
						errors.append("Error! Question id: %s. The snippets were more than 100." %question['id'])
						a=upload_information_for_1b(user=user, system=systems, test_id=active, timestamp=datetime.now(), comment=errors[0]).save()
						return render_to_response("Task1B-A.html", {"tests":tests, "current_test":active, "errors":errors,"attempts":attempts, "form":form}, RequestContext(request))
				except: 	 
					flag+=1
				try: 
					if len(question['concepts'])>100:
						errors.append("Error! Question id: %s. The concepts were more than 100." %question['id'])
						a=upload_information_for_1b(user=user, system=systems, test_id=active, timestamp=datetime.now(), comment=errors[0]).save()
						return render_to_response("Task1B-A.html", {"tests":tests, "current_test":active, "errors":errors,"attempts":attempts, "form":form}, RequestContext(request))
				except: 	 
					flag+=1
				try: 
					if len(question['triples'])>1000:
						errors.append("Error! Question id: %s. The triples were more than 1000." %question['id'])
						a=upload_information_for_1b(user=user, system=systems, test_id=active, timestamp=datetime.now(), comment=errors[0]).save()
						return render_to_response("Task1B-A.html", {"tests":tests, "current_test":active, "errors":errors,"attempts":attempts, "form":form}, RequestContext(request))
				except: 	 
					flag+=1		
				if flag==4:
					errors.append("Error! Question id: %s. No annotations for this question found.. Please try again." %question['id'])
					a=upload_information_for_1b(user=user, system=systems, test_id=active, timestamp=datetime.now(), comment=errors[0]).save()
					return render_to_response("Task1B-A.html", {"tests":tests, "current_test":active, "errors":errors,"attempts":attempts, "form":form}, RequestContext(request))
			#Save the file
			pt="/home/bioasq/public_html/webexample/uploads/users/task2b/phaseA/testset%d/%s.json" %(active.test_id, systems.system)
			try:
				f=open(pt, 'w')
				myfile=File(f)
				print>>myfile, user_data_raw
				myfile.close()
			except:
				errors.append("Error while saving the file. Try again later.")
				a=upload_information_for_1b(user=user, system=systems, test_id=active, timestamp=datetime.now(), comment=errors[0]).save()
				return render_to_response("Task1B-A.html", {"tests":tests, "current_test":active, "errors":errors,"attempts":attempts, "form":form}, RequestContext(request))
			#Delete previous uploads for the same user, system, testset and update the database with information for the saved file
			entry2=user_results_1b.objects.filter(system=systems).filter(test_id=active).delete()	
			entry2=user_results_1b(system=systems, user=user,  test_id=active, path=pt, datatime=datetime.now()).save()
			##TEMP: Show evaluation measures:
			errors.append("Results saved successfully!")
			a=upload_information_for_1b(user=user, system=systems, test_id=active, timestamp=datetime.now(), comment=errors[0]).save()
			return render_to_response("Task1B-A.html", {"tests":tests, "current_test":active, "errors":errors,"attempts":attempts, "form":form}, RequestContext(request))
	form=DocumentForm()
	form.fields['system_name'].queryset=system.objects.filter(user=user)
	return render_to_response("Task1B-A.html", {"tests":tests, "current_test":active, "errors":errors,"attempts":attempts, "form":form}, RequestContext(request))