예제 #1
0
파일: views.py 프로젝트: icsystems/SAPeM
def handle_form(request, formId, patientId, f=''):
	if not request.user.is_authenticated():
		return HttpResponseRedirect(settings.SITE_ROOT)
	import_str = 'from forms.models import Paciente, UnidadeSaude,Ficha, Formulario, HistoricoFicha'
	exec import_str
	if request.method == 'POST':
		form = request.POST
		if int(patientId) == 0: # new patient
			nome = form['nome']
			nome_mae = form['nome_mae']
			data_nascimento = form['data_nascimento']
			new_patient = Paciente(
				nome=nome,
				nome_mae = nome_mae,
				data_nascimento=data_nascimento
			)
			try:
				new_patient.save()
			except IntegrityError:
				msg = 'Paciente já existente no sistema'
				url = settings.SITE_ROOT
				return render_to_response('error.html',
					locals(), RequestContext(request, {}))
			p = new_patient
		else:
			p = Paciente.objects.get(id=int(patientId))
		keys = []
		for k in form:
			if k != 'edit':
				keys.append(k)
		# Protection for empty submissions
		if not len(keys):
			return HttpResponseNotFound(u'Formulário submetido vazio')
		xmlStr = createXML(keys, form)
		us = request.user.get_profile().unidadesaude_favorita
		if 'edit' in form.keys():
			newFicha = Ficha.objects.get(pk=int(form['edit']))
			oldXML = newFicha.conteudo
			hf = HistoricoFicha(
				ficha = newFicha,
				conteudo = oldXML
			)
			hf.save()
			newFicha.conteudo = xmlStr
		else:#New Entry
			f = Formulario.objects.get(id=int(formId))
			newFicha = Ficha(
				paciente   = p,
				formulario = f,
				unidadesaude = us,
				conteudo   = xmlStr
			)
		# For sharing info from a portable device to the server
		if not settings.SERVER_VERSION:
			f = Formulario.objects.get(id=int(formId))
			tmp_file = tempfile.NamedTemporaryFile(
				suffix='.info',
				prefix='ficha',
				dir=settings.COMM_DIR,
			)
			#Workaround for open a utf-8 file
			info_filename = tmp_file.name
			tmp_file.close()
			info_file = codecs.open(info_filename, 'w', encoding='utf-8')
			info_file.write(p.nome + '\n')
			info_file.write(p.data_nascimento + '\n' )
			info_file.write(p.nome_mae+ '\n')
			info_file.write(f.nome + '\n')
			info_file.write(f.tipo.nome + '\n')
			info_file.write(us.nome + '\n')
			if 'edit' in form.keys():
				info_file.write('edit')
			xml_file = codecs.open(info_file.name.replace('.info', '.xml'), 'w',encoding='utf-8')
			xml_file.write(xmlStr)
			info_file.close()
			xml_file.close()

		newFicha.save()
		return HttpResponseRedirect(settings.SITE_ROOT)
	# else METHOD == GET
	form = Formulario.objects.get(id=int(formId))
	pathname, moduleFormName = os.path.split(form.path)
	pathname ='%s/'%(pathname,)
	if not pathname in sys.path:
		sys.path.append(pathname)
	try:
		moduleForm = __import__(moduleFormName)
	except ImportError:
		msg = 'Módulo não encontrado'
		url = settings.SITE_ROOT
		return render_to_response('error.html',
			locals(), RequestContext(request, {}))
	return moduleForm.handle_request(request, f)
예제 #2
0
from forms.models import Ficha, Paciente

import unicodedata

fichas   = Ficha.objects.filter(paciente__id=593)
paciente = Paciente.objects.get(id=731)

for f in fichas:
	conteudo                = f.conteudo
	data_insercao           = f.data_insercao
	data_ultima_modificacao = f.data_ultima_modificacao

	if f.formulario.id != 6: # se nao estiver tratando do Triagem Implementacao...
		new = Ficha(paciente=paciente, formulario=f.formulario, unidadesaude=f.unidadesaude, conteudo=conteudo, data_insercao=data_insercao, data_ultima_modificacao=data_ultima_modificacao)
		new.save()
	print 'Ficha ', f.formulario.nome, ' movida com sucesso'
예제 #3
0
		form = Formulario.objects.filter(nome=patient_formname)[0]
	except Formulario.DoesNotExist:
		error_file = open(os.path.join(SYNC_LOG_PATH,'error.log'), 'a')
		error_file.write('%s: Formulario naun encontrado \n'%(fname))
		error_file.close()
		files_log.write('%s\n'%fname)
		continue
	try:
		us = UnidadeSaude.objects.filter(nome=patient_us)[0]
	except UnidadeSaude.DoesNotExist:
		error_file = open(os.path.join(SYNC_LOG_PATH,'error.log'), 'a')
		error_file.write('%s: Unidade Saude naun encontrada \n'%(fname))
		error_file.close()
		files_log.write('%s\n'%fname)
		continue
	f.close()
	xml = open(xmlname, 'r')
	newFicha = Ficha(
		paciente   = patient,
		formulario = form,
		unidadesaude = us,
		conteudo   = xml.read()
	)
	newFicha.save()
	files_log.write('%s\n'%fname)
files_log.close()
sys.exit(0)