from allocation.models import *


def allocator(univ):
	a=Applicant.objects.filter(institute__name=univ).order_by('rank')
	c=Choice.objects.filter(institute__name=univ)
	my_dict=dict([(x,0) for x in c])
	for x in a:
		choices=x.ordered_choices()
		temp=0
		i=0
		while temp==0 and i<len(choices):
			if(choices[i].capacity>my_dict[choices[i]]):
				my_dict[choices[i]]=my_dict[choices[i]]+1
				temp=1
				x.alloted_choice.clear()
				clalloc=Allocationcl(choice=choices[i],applicant=x)
				clalloc.save()
				# x.alloted_choice.clear()
				# x.alloted_choice.create(choices[i])
		# if(temp!=1)
			# x.alloted_choice.clear()
			# x.alloted_choice.create(Null_choice)
		# 	cll=Allocationcl(choice=Null_choice,applicant=x)
			i=i+1
	return a