Пример #1
0
def beneficiary(request,template='board/beneficiary.html'):
	try:
		auser=User.objects.get(id=request.user.id)
	except ObjectDoesNotExist:
		raise Http404

	if auser.is_superuser:
		bens=Beneficiary.objects.all().order_by('user','id')
	else:
		bens=auser.beneficiaries.all().order_by('user','id')
	
	post_kwargs={'user':request.user}
	capsule=[]
	if request.method=='POST':
		if 'add' in request.POST:
			bform=BeneficiaryRegisterForm(request.POST,**post_kwargs)
			if bform.is_valid():
				b=bform.save()
				b.save()
				messages.add_message(request,messages.SUCCESS,'The data have been successfully added.')
				return HttpResponseRedirect(reverse('beneficiary'))
	else:
		bform=BeneficiaryRegisterForm(**post_kwargs)
		
	for ben in bens:
		center=ben.user.username
		a=ben.lname
		b=ben.fname
		ty=dict(Beneficiary.B_TYPE)[ben.ty]
		group=ben.group.all().order_by('id')
		capsule.append({'center':center,'id':ben.id,'a':a,'b':b,'ty':ty,'group':group})

	paginator=Paginator(capsule,10)
	page=request.GET.get('page')
	try:
		contents=paginator.page(page)
	except PageNotAnInteger:
		contents=paginator.page(1)
	except EmptyPage:
		contents=paginator.page(paginator.num_pages)
	
	adjacent_pages=3
	startPage = max(contents.number - adjacent_pages, 1)
	endPage = contents.number + adjacent_pages
	if endPage >= contents.paginator.num_pages : 
		endPage = contents.paginator.num_pages
	page_numbers = [n for n in range(startPage, endPage+1) if n > 0 and n <= contents.paginator.num_pages]



	return render(request,template,{'conf':conf,'title':'beneficiary','bform':bform,'contents':contents,'page_numbers':page_numbers})
Пример #2
0
def edit_beneficiary(request,ben_id,template='board/edit_beneficiary.html'):

	try:
		auser=User.objects.get(id=request.user.id)
		if auser.is_superuser:
			bens=Beneficiary.objects.get(id=ben_id)
		else:
			bens=auser.beneficiaries.get(id=ben_id)
	except ObjectDoesNotExist:
		raise Http404

	editing_user=bens.user
	
	post_kwargs={'user':editing_user}
	if request.method== 'POST':
		if 'save' in request.POST:
			bform=BeneficiaryRegisterForm(request.POST,instance=bens,**post_kwargs)
			if bform.is_valid():
				bform.save()
				messages.add_message(request,messages.SUCCESS,'The data have been successfully edited.')
				return HttpResponseRedirect(reverse('edit_beneficiary',kwargs={'ben_id':ben_id}))
			else:
				messages.add_message(request,messages.WARNING,'The process went wrong.')
				return HttpResponseRedirect(reverse('edit_beneficiary',kwargs={'ben_id':ben_id}))


		else:
			dform=ActivityDateForm(request.POST,extra=None,)
			bform=BeneficiaryRegisterForm(instance=bens,**post_kwargs)
		
		if dform.is_valid():
			if request.POST.get('search'):
				#if (dform.cleaned_data['to_date']-dform.cleaned_data['from_date']).days<0:
				#	messages.add_message(request,messages.WARNING,'From date and To date must be in chronological order.')
				#	return HttpResponseRedirect(reverse('edit_beneficiary',kwargs={'ben_id':ben_id}))
				#else :
				return HttpResponseRedirect(reverse('view_beneficiary_history',
					kwargs={'from_d':dform.cleaned_data['from_date'].strftime('%m%d%Y'),
						'to_d':dform.cleaned_data['to_date'].strftime('%m%d%Y'),
						'ben_id':bens.id}))
		else:
			messages.add_message(request,messages.WARNING,"<br />".join(dform.non_field_errors()))
				
	else:
		dform=ActivityDateForm(extra=None)
		bform=BeneficiaryRegisterForm(instance=bens,**post_kwargs)

	return render(request,template,{'conf':conf,'dform':dform,'bform':bform,'title':'edit beneficiary','ben_id':ben_id,})