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,})
def activity(request,template='board/activity.html'): if request.method == 'POST': add_form = ActivityDateForm(request.POST,extra=None) if add_form.is_valid(): if request.POST.get('add'): #if (add_form.cleaned_data['to_date']-add_form.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('activity')) #else : return HttpResponseRedirect(reverse('view_activity',kwargs={'from_d':add_form.cleaned_data['from_date'].strftime('%m%d%Y'), 'to_d':add_form.cleaned_data['to_date'].strftime('%m%d%Y')})) else: messages.add_message(request,messages.WARNING,"<br />".join(add_form.non_field_errors())) else : add_form = ActivityDateForm(extra=None) return render(request,template,{'conf':conf,'add_form':add_form,'title':'activity'})
def add_activity(request,from_d,to_d,template='board/add_activity.html'): try: f_d=datetime.strptime(from_d,'%m%d%Y') t_d=datetime.strptime(to_d,'%m%d%Y') except ValueError: return HttpResponse(status=400) if (t_d-f_d).days<0: return HttpResponse(status=400) try: auser=User.objects.get(id=request.user.id) except ObjectDoesNotExist: raise Http404 data={} post_kwargs={'user':request.user} data['beneficiaries']={} for b in auser.beneficiaries.all().order_by('id'): #weeks=[int(i) for i in b.activityWeek.all().values_list('week',flat=True)] dic={'id':b.id,'lname':b.lname,'fname':b.fname,'ty':b.ty,} data['beneficiaries'][b.id]=dic data['groups']=[] for a in auser.beneficiaryGroup.order_by('id'): data['groups'].append({'id':a.id,'name':a.name}); data=json.dumps(data) bffset=formset_factory(BeneficiaryForm,max_num=50,formset=RequiredFormSet) if request.method== 'POST': aform=ActivityForm(request.POST,**post_kwargs) bform=bffset(request.POST) dform=ActivityDateForm(request.POST,extra={ 'from_date':(f_d.month,f_d.year,f_d.day), 'to_date':(t_d.month,t_d.year,t_d.day),}) dform.fields['to_date'].widget=forms.HiddenInput() if dform.is_valid(): if request.POST.get('save'): #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('add_activity',kwargs={'from_d':from_d,'to_d':to_d})) if bform.is_valid(): if aform.is_valid(): frd=dform.cleaned_data['from_date'] tod=frd+relativedelta(months=1) a=aform.save(fd=frd,td=tod) for form in bform.forms: b=form.save(act=a,user=request.user) messages.add_message(request,messages.SUCCESS,'The data have been successfully added.') return HttpResponseRedirect(reverse('activity')) else: messages.add_message(request,messages.WARNING,"<br />".join(bform.non_form_errors())) else: aform=ActivityForm(**post_kwargs) bform=bffset() dform=ActivityDateForm(extra={ 'from_date':(f_d.month,f_d.year,f_d.day), 'to_date':(t_d.month,t_d.year,t_d.day),}) dform.fields['to_date'].widget=forms.HiddenInput() return render(request,template,{'conf':conf,'dform':dform,'aform':aform,'bform':bform,'title':'add activity' ,'from_d':from_d,'to_d':to_d,'data':data,})
def edit_activity(request,act_id,template='board/add_activity.html'): try: auser=User.objects.get(id=request.user.id) if auser.is_superuser: act=Activity.objects.get(id=act_id) else: act=auser.activities.get(id=act_id) except ObjectDoesNotExist: raise Http404 editing_user=act.user aw=act.description.all() bens=Beneficiary.objects.filter(activityWeek__in=aw).distinct() f_d=act.fdate t_d=act.tdate if bens.count()==0: e=1 else: e=0 bffset=modelformset_factory(Beneficiary,extra=e,form=BeneficiaryForm,formset=RequiredModelFormSet,max_num=50) bffset.form = staticmethod(curry(BeneficiaryForm,extra_args=RequiredModelFormSet.extra_args)) try: auser=User.objects.get(id=editing_user.id) except ObjectDoesNotExist: raise Http404 data={} post_kwargs={'user':editing_user} data['beneficiaries']={} for b in auser.beneficiaries.all().order_by('id'): weeks=[int(i) for i in b.activityWeek.all().values_list('week',flat=True)] dic={'id':b.id,'lname':b.lname,'fname':b.fname,'ty':b.ty,'weeks':weeks} data['beneficiaries'][b.id]=dic data['groups']=[] for a in auser.beneficiaryGroup.order_by('id'): data['groups'].append({'id':a.id,'name':a.name}) data=json.dumps(data) if request.method== 'POST': aform=ActivityForm(request.POST,instance=act,**post_kwargs) bform=bffset(request.POST,queryset=bens,extra_args=act) dform=ActivityDateForm(request.POST,extra={ 'from_date':(f_d.month,f_d.year,f_d.day), 'to_date':(t_d.month,t_d.year,t_d.day),}) dform.fields['to_date'].widget=forms.HiddenInput() if dform.is_valid(): if request.POST.get('save'): #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_activity',kwargs={'act_id':act_id})) if bform.is_valid(): if aform.is_valid(): frd=dform.cleaned_data['from_date'] tod=frd+relativedelta(months=1) a=aform.save(fd=frd,td=tod) #a.beneficiaries.clear() for aw in a.description.all(): aw.beneficiaries.clear() for form in bform.forms: b=form.save(act=a,user=editing_user) messages.add_message(request,messages.SUCCESS,'The data have been successfully edited.') return HttpResponseRedirect(reverse('edit_activity',kwargs={'act_id':act_id})) else : messages.add_message(request,messages.WARNING,"<br />".join(bform.non_form_errors())) else: aform=ActivityForm(instance=act,**post_kwargs) bform=bffset(queryset=bens,extra_args=act) dform=ActivityDateForm(extra={ 'from_date':(f_d.month,f_d.year,f_d.day), 'to_date':(t_d.month,t_d.year,t_d.day),}) dform.fields['to_date'].widget=forms.HiddenInput() return render(request,template,{'conf':conf,'dform':dform,'aform':aform,'bform':bform,'title':'edit activity' ,'act_id':act_id,'data':data,})