def new_benefit(request, patient_id): dentist = Dentist.objects.get(user=request.user) patient = get_object_or_404(Patient, id=patient_id) if request.method == 'POST': date = request.POST.get('date', None) if date: month, year = date.split(' - ') if patient.social_work and patient.social_work.initial == 'APROSS': date_exists = Apross.objects.filter( patient=patient, month=month, year=int(year) ).exists() benefit_form = AprossForm(request.POST) else: date_exists = Benefit.objects.filter( patient=patient, month=month, year=int(year) ).exists() benefit_form = BenefitForm(request.POST) if not date_exists: if benefit_form.is_valid(): new_benefit = benefit_form.save(commit=False) new_benefit.patient = patient new_benefit.month = month new_benefit.year = int(year) new_benefit.real_date = Date(int(year), int(new_benefit.get_month_display()), 1) new_benefit.save() return JsonResponse({'status': 'OK'}) else: return JsonResponse({'status': 'ERROR', 'errors': benefit_form.errors}) else: benefit_form.errors['date'] = [u'Ingrese una fecha valida.'] return JsonResponse({'status': 'ERROR', 'errors': benefit_form.errors}) else: date_error = {'date': [u'Ingrese una fecha valida.']} if patient.social_work and patient.social_work.initial == 'APROSS': benefit_form = AprossForm(request.POST) else: benefit_form = BenefitForm(request.POST) if benefit_form.is_valid(): return JsonResponse({'status': 'ERROR', 'errors': date_error}) else: benefit_form.errors['date'] = [u'Ingrese una fecha valida.'] return JsonResponse({'status': 'ERROR', 'errors': benefit_form.errors})
def edit_benefit(request, patient_id): if request.method == 'POST': patient = get_object_or_404(Patient, id=patient_id) bf_id = request.POST.get('bf_id', None) get = request.POST.get('get') if get == '1': if patient.social_work and patient.social_work.initial == 'APROSS': benefit = Apross.objects.get(id=bf_id) bf_edit_form = AprossForm(instance=benefit) template = 'register/monthly_detail/apross/_edit.html' else: #cambiar benefit = Benefit.objects.get(id=bf_id) bf_edit_form = BenefitForm(instance=benefit) template = 'register/monthly_detail/benefit/_edit.html' return render_to_response( template, { 'bf': benefit, 'bf_edit_form': bf_edit_form, 'patient': patient }, RequestContext(request) ) else: date = request.POST.get('date', None) if date: month, year = date.split(' - ') if patient.social_work and patient.social_work.initial == 'APROSS': benefit = Apross.objects.get(id=bf_id) benefit_form = AprossForm(request.POST, instance=benefit) date_exists = Apross.objects.filter( patient=patient, month=month, year=int(year) ).exclude(id=benefit.id).exists() else: benefit = Benefit.objects.get(id=bf_id) benefit_form = BenefitForm(request.POST, instance=benefit) date_exists = Benefit.objects.filter( patient=patient, month=month, year=int(year) ).exclude(id=benefit.id).exists() if not date_exists: if benefit_form.is_valid(): benefit = benefit_form.save(commit=False) benefit.patient = patient benefit.month = month benefit.year = int(year) benefit.real_date = Date(int(year), int(benefit.get_month_display()), 1) benefit.save() return JsonResponse({'status': 'OK'}) else: return JsonResponse({'status': 'ERROR', 'errors': benefit_form.errors}) else: benefit_form.errors['date'] = [u'Ingrese una fecha valida.'] return JsonResponse({'status': 'ERROR', 'errors': benefit_form.errors}) else: date_error = {'date': [u'Ingrese una fecha valida.']} if patient.social_work and patient.social_work.initial == 'APROSS': benefit_form = AprossForm(request.POST) else: benefit_form = BenefitForm(request.POST) if benefit_form.is_valid(): return JsonResponse({'status': 'ERROR', 'errors': date_error}) else: benefit_form.errors['date'] = [u'Ingrese una fecha valida.'] return JsonResponse({'status': 'ERROR', 'errors': benefit_form.errors})