def project_detail_view( request, *args, **kwargs ):
	# putting together the required context.
	if request.method == 'GET':
	# very basic implementation.
		ctx = { }
		ctx['project'] = get_object_or_404( Project, pk = kwargs['project'] )
		ctx['task_list'] = Task.objects.filter( parent = ctx['project'] )
		ctx['update_list'] = Update.objects.filter( parent = ctx['project'] )
		ctx['documents'] = Document.objects.filter( project = ctx['project'] )
		if Update.objects.filter( parent = ctx['project'] ).count():
			ctx['no_updates'] = 0
		else:
			ctx['no_updates'] = 1

		if Task.objects.filter( parent = ctx['project'] ).count():
			ctx['no_tasks'] = 0
		else:
			ctx['no_tasks'] = 1

		if not PermissionHandler.edit_project( request.user, project = kwargs['project'] ):
			ctx['allow_project_edit'] = 0
		else:
			ctx['allow_project_edit'] = 1

		if not PermissionHandler.create_update( request.user, project = kwargs['project'] ):
			ctx['allow_update'] = 0
		else:
			ctx['allow_update'] = 1

		if not PermissionHandler.create_task( request.user, project = kwargs['project'] ):
			ctx['allow_task'] = 0
		else:
			ctx['allow_task'] = 1

		if not PermissionHandler.add_user_to_project( request.user, project = kwargs['project'] ):
			ctx['allow_add_user'] = 0
		else:
			ctx['allow_add_user'] = 1

		if not PermissionHandler.add_mentor_to_project( request.user, project = kwargs['project'] ):
			ctx['allow_add_mentor'] = 0
		else:
			ctx['allow_add_mentor'] = 1


		return render_to_response( 'project/project_details.html', ctx, context_instance = RequestContext(request) )

	elif request.method == 'POST':
		return HttpResponse('Method inapplicable')
	def form_valid( self, form ):
		if( not PermissionHandler.create_update( self.request.user, **self.kwargs ) ):
			raise PermissionDenied( 'You do NOT have permission to make an update :P' ) # <--- change this to a more tactful message :)

		form.instance.user = self.request.user
		form.instance.parent = Project.objects.filter( pk = self.kwargs['project'] )[0]
		super( CreateUpdate, self ).form_valid( form )
		return HttpResponseRedirect(reverse('project:project_detail',args=[self.kwargs['project']]))
def global_context(request):
    # provide context data for the leftbar.
    ctx = {'club_list': Club.objects.all(), 'all_users':User.objects.all()}
    if request.user.is_authenticated() and PermissionHandler.create_club( request.user ):
        ctx['allow_club_create'] = 1
    else:
        ctx['allow_club_create'] = 0
    
    return ctx
def upload_document( request, *args, **kwargs ):
	if not PermissionHandler.edit_project( request.user, project = kwargs['project'] ):
		return PermissionDenied('You are not allowed to upload to this project!')

	d = Document( doc = request.FILES['doc'], user = request.user, desc = request.POST['desc'] )
	d.save()
	project = get_object_or_404( Project, pk = kwargs['project'] )
	project.documents.add(d)
	project.save()
	return redirect( reverse('project:project_detail', args=[kwargs['project']] ) )
	def form_valid( self, form ):
		if( not PermissionHandler.create_comment(self.request.user, **self.kwargs ) ): # check for permission using the permissions module.
			raise PermissionDenied( 'You do NOT have permission to make a comment :P' ) 

		form.instance.user = self.request.user 
		if( self.kwargs['target_model'] == 'Update'):
			Update.objects.filter( pk = self.kwargs['update'] )[0].comments.add( form.instance )
		elif( self.kwargs['target_model'] == 'Task'):
			Task.objects.filter( pk = self.kwargs['task'] )[0].comments.add( form.instance )

		return super( CreateProject, self ).form_valid( form )
def add_task( request, **kwargs ):

	project = get_object_or_404( Project, pk = kwargs['project'] )

	# controller auth checking.
	if( (not PermissionHandler.create_update( request.user, **kwargs )) ): # check for permission using the permissions module.
		dajax = Dajax()	# controller feedback
		dajax.alert('You do NOT have this permission')
		return dajax.json()


	



	# error handling for form data
	# controller input data checking
	try:
		for x in ['content','title','deadline']:
			if x not in kwargs.keys() or kwargs[x] == '':
				if x+'_feedback_field' in kwargs.keys():
					raise ValueError(x+' missing..',kwargs[x+'_feedback_field']) # controller feedback
				else:
					raise ValueError(x+' missing..')
	except ValueError as e:
		if (len(e.args) == 2):	# intercept special value errors
			dajax = Dajax()
			# clear feedback fields. controller feedback initialization
			for x in ['content','title','deadline']:
				dajax.script('document.getElementById("'+kwargs[x+'_feedback_field']+'").innerHTML = ""')
			# write scripts to be executed on the client side to inform the client of the value error in a proper manner.
			dajax.script('document.getElementById("'+e.args[1]+'").innerHTML = "'+e.args[0]+'"')
			return dajax.json()

	# controller operation
	task = Task()
	task.content = kwargs['content']
	task.title = kwargs['title']
	task.deadline = kwargs['deadline']
	task.parent = project

	if 'assignees' in kwargs.keys():
		for i in kwargs['assignees']:
			user = get_object_or_404( User, pk=i )
			task.assigned.add(user)


	task.user = request.user
	task.save()

	# controller response on success.
	dajax = Dajax()
	dajax.script('location.reload(true)')
	return dajax.json()
def delete_document( request, **kwargs ):

	doc = get_object_or_404( Document, pk = kwargs['doc'] )

	if( (not PermissionHandler.delete_document(request.user, **kwargs )) ): # check for permission using the permissions module.
		raise PermissionDenied( 'You do NOT have permission to create a club :P' )

	doc.delete()

	dajax = Dajax()
	dajax.script('location.reload(true)')
	return dajax.json()
def create_blank_club( request, **kwargs):
	if( not PermissionHandler.create_club(request.user, **kwargs ) ): # check for permission using the permissions module.
		raise PermissionDenied( 'You do NOT have permission to create a club :P' )

	club = Club()
	club.description = 'Detailed Writeup'
	club.name = 'Sample Name'
	club.save()

	dajax = Dajax()
	#dajax.script('window.location=\'http://localhost:8000/club/'+format(club.pk)+'/\'')
	dajax.script('window.location=\''+reverse('project:club_detail',args=[club.pk])+'\'')
	return dajax.json()
def club_detail_view( request, **kwargs ):

	ctx = { }

	ctx['club'] = get_object_or_404( Club, pk = kwargs['club'] )
	ctx['project_list'] = Project.objects.filter( club = ctx['club'] )

	if not PermissionHandler.edit_club( request.user, club = kwargs['club'] ):
		ctx['allow_club_edit'] = 0
	else:
		ctx['allow_club_edit'] = 1

	if not PermissionHandler.create_project( request.user, club = kwargs['club'] ):
		ctx['allow_project_create'] = 0
	else:
		ctx['allow_project_create'] = 1

	if not PermissionHandler.add_core_to_club( request.user, club = kwargs['club'] ):
		ctx['allow_add_core'] = 0
	else:
		ctx['allow_add_core'] = 1
		
	return render_to_response( 'project/club_details.html', ctx, context_instance = RequestContext(request) )
def delete_task( request, **kwargs ):

	task = get_object_or_404( Task, pk = kwargs['task'] )

	if( (not PermissionHandler.delete_task(request.user, **kwargs )) ): # check for permission using the permissions module.
		dajax = Dajax()
		dajax.alert('You do NOT have this permission')
		return dajax.json()

	task.delete()

	dajax = Dajax()
	dajax.script('location.reload(true)')
	return dajax.json()
def club_update( request, *args, **kwargs ):

	if not PermissionHandler.edit_club( request.user, club = kwargs['club'] ):
		return PermissionDenied('You are not allowed to edit!')

	club = get_object_or_404( Club, pk = kwargs['club'] )

	try:
		club.description = request.POST['description']
		club.name = request.POST['name']
		club.image = request.FILES['image']
		club.save()
	except ValueError as e:	# implement a form clean mechanism here.
		raise PermissionDenied('Error')

	return redirect( reverse('project:club_detail', args=[kwargs['club']] ) )
def comment_on_task( request, **kwargs ):

	if( (not PermissionHandler.create_comment(request.user, **kwargs )) ): # check for permission using the permissions module.
		raise PermissionDenied( 'You do NOT have permission to make comments :P' )

	comment = Comment( user = request.user, content = kwargs['content'] )
	comment.save()

	task = get_object_or_404( Task, pk = kwargs['task'] )
	task.comments.add(comment)
	task.save()


	dajax = Dajax()
	dajax.script('location.reload(true)')
	return dajax.json()
def create_blank_project( request, **kwargs):
  if( not PermissionHandler.create_project(request.user, **kwargs ) ): # check for permission using the permissions module.
    raise PermissionDenied( 'You do NOT have permission to create a project :P' )

  project = Project()
  project.desc = 'Detailed Writeup'
  project.brief = 'Brief Writeup'
  project.status = 'Sample Status'
  project.name = 'Sample Name'
  project.budget = 0
  project.year = str(datetime.datetime.today().year)
  project.club = get_object_or_404( Club, pk = kwargs['club'])
  project.save()

  dajax = Dajax()
  dajax.script('window.location=\''+reverse('project:project_detail', args=[project.pk])+'\'')
  return dajax.json()
def project_update( request, *args, **kwargs ):

	if not PermissionHandler.edit_project( request.user, project = kwargs['project'] ):
		return PermissionDenied('You are not allowed to edit!')

	project = get_object_or_404( Project, pk = kwargs['project'] )

	try:
		project.desc = request.POST['detailed_writeup']
		project.brief = request.POST['brief_writeup']
		project.status = request.POST['status']
		project.name = request.POST['title']
		if('bgImg' in request.FILES):
			project.image = request.FILES['bgImg']
		project.save()
	except ValueError as e:	# implement a form clean mechanism here.
		raise PermissionDenied('Error')

	return redirect( reverse('project:project_detail', args=[kwargs['project']]) )
def add_update( request, **kwargs ):


	import pdb;pdb.set_trace()
	project = get_object_or_404( Project, pk = kwargs['project'] )

	if( (not PermissionHandler.create_update( request.user, **kwargs )) ): # check for permission using the permissions module.
		dajax = Dajax()
		dajax.alert('You do NOT have this permission')
		return dajax.json() 

	# error handling for form data
	# controller input data checking

	try:
		if 'content' not  in kwargs.keys() or kwargs['content'] == '':
			raise ValueError('Content missing..',kwargs['content_feedback_field']) # controller feedback

		if 'title' not in kwargs.keys() or kwargs['title'] == '':
			raise ValueError('Title missing..',kwargs['title_feedback_field']) # controller feedback
	except ValueError as e:
		if (len(e.args) == 2):	# intercept special value errors
			dajax = Dajax()
			# prepare the feedback handles.
			for x in ['content','title']:
				dajax.script('document.getElementById("'+kwargs[x+'_feedback_field']+'").innerHTML = ""')

			# write scripts to be executed on the client side to inform the client of the value error in a proper manner.
			dajax.script('document.getElementById("'+e.args[1]+'").innerHTML = "'+e.args[0]+'"')
			return dajax.json()

	# controller operation
	update = Update()
	update.parent = project
	update.content = kwargs['content']
	update.title = kwargs['title']
	update.user = request.user
	update.save()


	dajax = Dajax()
	dajax.script('location.reload(true)')
	return dajax.json()
def remove_core_from_club( request, **kwargs ):

	core = get_object_or_404( User, pk = kwargs['core'] )
	project = get_object_or_404( Project, pk = kwargs['project'] )

	if( (not PermissionHandler.remove_core_from_club( request.user, **kwargs )) ): # check for permission using the permissions module.
		dajax = Dajax()
		dajax.alert('You do NOT have this permission')
		return dajax.json()


	if core in club.cores.all():
		club.cores.remove( core )
	else:
		raise ValueError('User does not belong to this club')


	dajax = Dajax()
	dajax.script('location.reload(true)')
	return dajax.json()
def remove_mentor_from_project( request, **kwargs ):

	mentor = get_object_or_404( User, pk = kwargs['mentor'] )
	project = get_object_or_404( Project, pk = kwargs['project'] )

	if( (not PermissionHandler.remove_mentor_from_project( request.user, **kwargs )) ): # check for permission using the permissions module.
		dajax = Dajax()
		dajax.alert('You do NOT have this permission')
		return dajax.json()


	if mentor in project.mentors.all():
		project.mentors.remove( mentor )
	else:
		raise ValueError('User does not belong to this project')


	dajax = Dajax()
	dajax.script('location.reload(true)')
	return dajax.json()
def delete_comment( request, **kwargs ):

	comment = get_object_or_404( Comment, pk = kwargs['comment'] )

	if( (not PermissionHandler.delete_comment(request.user, **kwargs )) ): # check for permission using the permissions module.
		dajax = Dajax()
		dajax.alert('You do NOT have this permission')
		return dajax.json()

	for update in Update.objects.filter( comments__in = [comment] ):
		update.commments.remove(comment)

	for task in Task.objects.filter( comments__in = [comment] ):
		task.commments.remove(comment)

	comment.delete()

	dajax = Dajax()
	dajax.script('location.reload(true)')
	return dajax.json()
	def form_valid( self, form ):
		if( not PermissionHandler.create_club(self.request.user, **self.kwargs ) ): # check for permission using the permissions module.
			raise PermissionDenied( 'You do NOT have permission to make a club :P' ) 
		return super( CreateProject, self ).form_valid( form )
	def form_valid( self, form ):
		if( not PermissionHandler.create_project(self.request.user, **self.kwargs ) ): # check for permission using the permissions module.
			raise PermissionDenied( 'You do NOT have permission to make a task :P' ) 
		form.instance.club = Club.objects.filter( pk = self.kwargs['club'] )[0]
		return super( CreateProject, self ).form_valid( form )