Ejemplo n.º 1
0
def create_assignment(request):
	#Checks user rights
	if not request.user.groups.filter(name='Admin') and not request.user.groups.filter(name = 'Teacher'):
		return HttpResponse("You do not have permission to access this page.")
	#Checks authentication
	elif request.user.is_authenticated():
		if request.method == 'POST':
			myclass_teacher = request.user.username
			myclass = MyClass.objects.get(id=request.POST['pass_class'])
			max_score = request.POST['max_score']
			real_score = 0
		
			try:
				due_date = datetime.datetime.strptime(request.POST['due_date'], "%B %d, %Y")			
			except:
				try:
					due_date = datetime.datetime.strptime(request.POST['due_date'], "%m-%d-%Y")			
				except:
					try: 
						due_date = datetime.datetime.strptime(request.POST['due_date'], "%m/%d/%Y")			
					except:
						return HttpResponse("Please enter the date in MM-DD-YYYY format.")

			create_date = datetime.date.today()
			
			#Checks if Assignment already exists
			if Assignment.objects.filter(assignment_name = request.POST['asg_name']).filter(to_class = myclass):
				return HttpResponse("Already exists.")
				
			#Tries to create assignment
			try:
				newAssignment = Assignment(is_owner = True, due_date = due_date, assign_date = create_date, assignment_name = request.POST['asg_name'], to_student = user_info.objects.get(username=request.user.username), to_class = myclass, max_score = max_score, real_score = real_score)
				newAssignment.save()
			except:
				message = "Error creating assignment!" 
				return render (request, "create_assignment.html", {"info": user_info.objects.all(), "roster": ClassRoster.objects.all(), "message": message})
				
			#Adds assignment for each member of the class roster
			for each_roster in ClassRoster.objects.all():
				if each_roster.in_class == myclass:
					for each_student in each_roster.student.all():
						if not each_student.username == request.user.username:
							newAssignment = Assignment(due_date = due_date, assign_date = create_date,assignment_name = request.POST['asg_name'], to_student = each_student, to_class = myclass, max_score = max_score, real_score = real_score)
							newAssignment.save()
			message = "Assignment added!" 
			return render (request, "create_assignment.html", {"info": user_info.objects.all(), "roster": ClassRoster.objects.all(), "message": message})
		else:
			return render (request, "create_assignment.html", {"info": user_info.objects.all(), "roster": ClassRoster.objects.all()})
	else:
		return redirect('register')
Ejemplo n.º 2
0
def approve_student_for_class(request, string):
	if request.user.is_authenticated():
		class_num = string
		try:
			myclass = MyClass.objects.get(class_number = class_num, teacher = request.user.username) 
		except:
			return HttpResponse("This page does not exist or you do not have access to this page. Please check your url and try again.")
		if request.method == 'POST':
			myaction = request.POST['action']
			myname = request.POST['user']
			edit_user = User.objects.get(username=myname)
			moreinfo = user_info.objects.get(username=myname)
			auth_user = AuthUser.objects.get(student_name = moreinfo, class_name = myclass)
			if myaction == 'remove_student':
				auth_user.authorized = False
				auth_user.delete()
				
				moreinfo.class_list.remove(myclass)
				moreinfo.save()
				
				roster = ClassRoster.objects.get(in_class = myclass)
				roster.student.remove(moreinfo)
				roster.save()
				
				#deletes assignments when student is removed
				for each_assignment in Assignment.objects.all():
					if each_assignment.to_student.username == edit_user.username and each_assignment.to_class == myclass:
						each_assignment.delete()

				date = datetime.date.today()
				title = "Removed from Class " + myclass.class_number
				message = "Your have been removed from " + myclass.class_number + " by the instructor."
				new_inbox = MyInbox(sent_by = "ADMIN", received_by = moreinfo, date = date, title = title, message = message)
				new_inbox.save()

				return render (request, "approve_student_for_class.html", {"info": user_info.objects.all(), "auth": AuthUser.objects.all(), "instance_class": myclass})
			elif myaction == 'add_student':
				mygroup = Group.objects.get(name='Student')
				edit_user.groups.add(mygroup)	
				edit_user.save()
				moreinfo.authenticate = True
				auth_user.authorized = True
				moreinfo.save()
				auth_user.save()
				
				date = datetime.date.today()
				title = "Account Approved."
				message = "Your have been added to " + myclass.class_number + "."
				new_inbox = MyInbox(sent_by = "ADMIN", received_by = moreinfo, date = date, title = title, message = message)
				new_inbox.save()

				#adds assignments when student is added to class
				for each_assignment in Assignment.objects.all():
					if each_assignment.to_student.username == myclass.teacher and each_assignment.to_class == myclass:
						new_assignment_instance = Assignment(assign_date = each_assignment.assign_date, due_date = each_assignment.due_date, to_student = moreinfo, to_class = myclass, assignment_name = each_assignment.assignment_name, max_score = each_assignment.max_score, real_score = 0, retry_limit = each_assignment.retry_limit)
						new_assignment_instance.save()		
				return render (request, "approve_student_for_class.html", {"info": user_info.objects.all(), "auth": AuthUser.objects.all(), "instance_class": myclass})
			else:
				return HttpResponse("Hi")
		else:
				return render (request, "approve_student_for_class.html", {"info": user_info.objects.all(), "auth": AuthUser.objects.all(), "instance_class": myclass})
	else:
		return redirect('register')