Created
August 17, 2017 09:52
-
-
Save leonardocintra/ca88b74dc3b531b864cd758505a468dc to your computer and use it in GitHub Desktop.
CreateView para Laudo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Atencao, esse codigo não esta completo! Ainda precisa de uma refatorada boa. | |
# Estou usando aqui apenas para mostrar um exemplo | |
class LaudoCreate(LoginRequiredMixin, FormView): | |
model = Laudo | |
template_name = 'laudo/laudo_form.html' | |
form_class = LaudoForm | |
def get_initial(self): | |
initial = super(LaudoCreate, self).get_initial() | |
initial['controle'] = self.kwargs['pk_controle'] | |
initial['paciente'] = self.kwargs['pk'] | |
initial['exame'] = self.kwargs['pk_exame'] | |
return initial | |
def form_valid(self, form, **kwargs): | |
context = self.get_context_data(**kwargs) | |
item_exame = context['item_exame'] | |
item_item_exame = context['item_item_exame'] | |
exame = context['exame'] | |
paciente = context['paciente'] | |
exames_marcados = self.request.POST.getlist("item_exames") | |
gravou_lista_multiplas_opcoes = False | |
gravou_lista_unica_opcao = False | |
ultimo_id_lista_unica_opcao = -1 | |
gravar_no_banco = False | |
with transaction.atomic(): | |
# If temporario para teste | |
if gravar_no_banco: | |
# Salva o laudo | |
obj = form.save() | |
# Salva o tipo de exame do laudo no controle_ficha_exame | |
ControleFichaExames.objects.create( | |
controle_id=self.kwargs['pk_controle'], | |
exame_id=self.kwargs['pk_exame'], | |
user=self.request.user | |
) | |
else: | |
obj = form.save(commit=False) | |
for item in item_exame: | |
# Captura os dados do exame de listas tipo multiplas opções | |
if item.tipo_campo == "LIM": | |
# Faz a leitura dos exames feitos (marcados no template) | |
for exame_feito in exames_marcados: | |
for item_exame in item_item_exame: | |
if int(exame_feito) == int(item_exame.id) and item_exame.item_exame_id == item.id and gravou_lista_multiplas_opcoes == False: | |
if gravar_no_banco: | |
LaudoExame.objects.create ( | |
laudo_id=obj.id, | |
item_exame_id=item.id, | |
campo_lista_varias_opcao=exames_marcados | |
) | |
gravou_lista_multiplas_opcoes = True | |
elif item.tipo_campo == "LIU": | |
for item_exame in item_item_exame: | |
x = '{}{}'.format('item_exame_', item.nome.replace(" ", "_")) | |
valueX = self.request.POST.get(x) | |
if valueX: | |
if int(ultimo_id_lista_unica_opcao) == int(valueX): | |
gravou_lista_unica_opcao = True | |
else: | |
gravou_lista_unica_opcao = False | |
ultimo_id_lista_unica_opcao = valueX | |
if gravar_no_banco and gravou_lista_unica_opcao == False: | |
LaudoExame.objects.create( | |
laudo_id=obj.id, | |
item_exame_id=item.id, | |
campo_lista_unica_opcao=valueX | |
) | |
elif item.tipo_campo == "DEC": | |
# Captura o valor do formulario conforme for decimal e o item | |
x = '{}{}'.format("campo_decimal_", item.id) | |
valueX = self.request.POST.get(x) | |
# Grava o valor no laudoExame se for difernete de None | |
if valueX: | |
if gravar_no_banco: | |
LaudoExame.objects.create( | |
laudo_id=obj.id, | |
item_exame_id=item.id, | |
campo_decimal=Decimal(valueX) | |
) | |
else: | |
print("Tipo nao identificado") | |
# end for | |
# Monta dados para enviar a mensagem de LOG pro Telegram | |
url_paciente = '{}/paciente/{}'.format(self.request.get_host(), paciente.id) | |
mensagem = "POST Laudo: {} \n Ficha: {} \n Exame: {} \n Paciente: {}".format( | |
obj.id, | |
self.kwargs['pk_controle'], | |
exame.nome, | |
'[#{} {}]({})'.format(paciente.id, paciente.nome, url_paciente) | |
) | |
telegram.sendMensagemTelegram(self.request.user, mensagem) | |
return HttpResponseRedirect(self.get_success_url()) | |
def get_context_data(self, **kwargs): | |
context = super(LaudoCreate, self).get_context_data(**kwargs) | |
context['paciente'] = Paciente.objects.get(pk=self.kwargs['pk']) | |
exame = Exame.objects.get(pk=self.kwargs['pk_exame']) | |
item_exame = ItemExame.objects.filter(exame_id=exame.id).order_by('ordem_exibicao') | |
context['exame'] = exame | |
context['item_exame'] = item_exame | |
context['item_item_exame'] = ItemsItemExame.objects.filter(item_exame_id__in=item_exame) | |
context['controle'] = self.kwargs['pk_controle'] | |
return context | |
def get_success_url(self): | |
return reverse_lazy('paciente:paciente_detail', kwargs={'pk': self.kwargs['pk'] }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://we.tl/t-57XrGImfya
https://gitlab.luizalabs.com/luizalabs/integra-venda-manual.git