Beispiel #1
0
def dispenseRx(request, p_id):
#TO DO
    if request.method == 'GET':
        rx = get_object_or_404(Prescription, rx_id=p_id)
        form = PrescriptionForm(instance=rx)
        fields = list(form)
        fields.pop(0)
        fields.pop(0)
        fields.pop(0)

        form2 = dict()
        form2['Patient'] = rx.patient.__unicode__()
        form2['Prescriber'] = rx.prescriber.__unicode__()
        form2['Pharmacy'] = rx.pharmacy.__unicode__()        

        RxEntryForm= inlineformset_factory(Prescription, RxEntry, can_delete=True, extra=0)
        rxforms = RxEntryForm(instance=rx)
        date_created = rx.created_date
        date_modified = rx.last_modified

        data = {'date_created': date_created, 'date_modified': date_modified, 'prescription': rx,
                'form': form2,'fields': fields, 'rxform': rxforms, 'rx_id': rx.rx_id}

        return render_to_response('erx/dispense_prescription.html', data,
            context_instance=RequestContext(request))
    else:
        if request.method == 'POST':
            rx = get_object_or_404(Prescription, rx_id=p_id)
            urx = rx.dispense()
            update = urx.__dict__

            data = dict({'submitted_date': update['submitted_date'], 'status': update['status'], 'prescriber': update['prescriber_id'],
                    'pharmacy': update['pharmacy_id'], 'patient': update['patient_id']})

            form = PrescriptionForm(data, instance=urx)

            if form.is_valid():
                form.save()
                return pharmacyHome(request, prescription=p_id,
                    message='[%s] Prescription %s successfully dispensed.' % (strftime("%Y-%m-%d %H:%M:%S"), rx))

            else:
                return render_to_response('erx/done.html', {'message': 'Failed', 'errors': form.errors},
            context_instance=RequestContext(request))
Beispiel #2
0
def createPrescription(request):

    if request.method == "POST":
        form = PrescriptionForm(request.POST)

        if form.is_valid():
            instance=form.save()
            rxentry = RxEntryForm(request.POST, instance=instance)
            
            if rxentry.is_valid():
                rxentry.save()
                return render_to_response('erx/done.html', {'message': "Prescription created."}, context_instance=RequestContext(request))

            else:
                return render_to_response('erx/done.html', {'message': rxentry.errors}, context_instance=RequestContext(request))

        else:
            return render_to_response('erx/done.html', {'message': form.errors}, context_instance=RequestContext(request))

    else:
        if request.method == "GET":
            return render_to_response('erx/new_prescription.html',
                {'form': PrescriptionForm, 'rxform': RxEntryForm}, context_instance=RequestContext(request))
Beispiel #3
0
def handlePrescription(request, rx_id):

    ItemFormSet = get_ordereditem_formset(AutoRxEntryForm, extra=1, can_delete=True)

    if request.method == 'GET':
        rx = get_object_or_404(Prescription, rx_id=rx_id)

        form = PrescriptionForm(instance=rx)
        formset = ItemFormSet(instance=rx)
        date_created = rx.created_date
        date_modified = rx.last_modified
        return render_to_response('erx/cur_prescription.html', {'date_created': date_created, 'prescription': rx,
                                                                'date_modified': date_modified,
                                                                'form': form, 'rxform': formset},
            context_instance=RequestContext(request))

    if request.method == 'POST':
        rx = get_object_or_404(Prescription, rx_id=rx_id)
        pin = rx.prescriber.pin_code

        if not request.POST['pin'] or not request.POST['pin'] == pin:
            form = PrescriptionForm(request.POST, instance=rx)
            formset = ItemFormSet(request.POST, instance=rx)
            date_created = rx.created_date
            date_modified = rx.last_modified
            message = 'Invalid PIN CODE provided.'
            return render_to_response('erx/cur_prescription.html', {'date_created': date_created, 'message': message,
                                                                    'date_modified': date_modified,
                                                                    'form': form, 'rxform': formset},
                context_instance=RequestContext(request))

        if 'update' in request.POST:            
            form = PrescriptionForm(request.POST, instance=rx)

            if form.is_valid():
                instance=form.save()
                rxentry = ItemFormSet(request.POST, instance=instance)

                if rxentry.is_valid():
                    
                    drug_list = []
                    for form in rxentry:
                        if form.cleaned_data.get('drug_name') is not None:
                            drug_list.append(form.cleaned_data.get('drug_name'))

                    flag = checkInteractions(drug_list)
                    if flag == False:
                        rxentry.save()
                        return prescriberHome(request, prescription=rx_id,
                            message='[%s] Prescription %s successfully updated.' %(strftime("%Y-%m-%d %H:%M:%S"), rx))

                    else:
                        Prescription.objects.filter(rx_id=instance.rx_id).delete()
                        form = PrescriptionForm(request.POST, instance=rx)
                        formset = ItemFormSet(request.POST, instance=rx)
                        date_created = rx.created_date
                        date_modified = rx.last_modified
                        if flag == True:
                            message = 'Prescription not saved. Drug interactions have been detected.'  
                        else:
                            message = flag
                        return render_to_response('erx/cur_prescription.html', {'date_created': date_created, 'message': message,
                                                                                'date_modified': date_modified,
                                                                                'form': form, 'rxform': formset},
                            context_instance=RequestContext(request))

                else:
                    form = PrescriptionForm(request.POST, instance=rx)
                    formset = ItemFormSet(request.POST, instance=rx)
                    date_created = rx.created_date
                    date_modified = rx.last_modified
                    message = '%s' % (rxentry.errors)
                    return render_to_response('erx/cur_prescription.html', {'date_created': date_created, 'message': message,
                                                                            'date_modified': date_modified,
                                                                            'form': form, 'rxform': formset},
                        context_instance=RequestContext(request))
            else:
                errors = form.errors
                form = PrescriptionForm(request.POST, instance=rx)
                formset = ItemFormSet(request.POST, instance=rx)
                date_created = rx.created_date
                date_modified = rx.last_modified
                message = '%s' % (errors)
                return render_to_response('erx/cur_prescription.html', {'date_created': date_created, 'message': message,
                                                                        'date_modified': date_modified,
                                                                        'form': form, 'rxform': formset},
                    context_instance=RequestContext(request))

        else:
            if 'delete' in request.POST:
                try:
                    RxEntry.objects.filter(prescription=rx).delete()
                    Prescription.objects.filter(rx_id=rx_id).delete()
                    return prescriberHome(request, prescription=rx_id,
                        message='[%s] Prescription %s deleted successfully.' % (strftime("%Y-%m-%d %H:%M:%S"), rx))
                except Exception as e:
                    return prescriberHome(request, prescription=rx_id,
                        message='[%s] Prescription %s deletion failed.' % (strftime("%Y-%m-%d %H:%M:%S"), rx))
Beispiel #4
0
def createPrescriptionForPrescriber(request, p_id):

    ItemFormSet = get_ordereditem_formset(AutoRxEntryForm, extra=2, can_delete=True)

    if request.method == "POST":
        if not request.POST['pin'] or not request.POST['pin'] == Prescriber.objects.get(prescriber_id=p_id).pin_code:
            prescriber = Prescriber.objects.get(prescriber_id = p_id)
            form = PrescriptionForm2(data=request.POST, prescriber=prescriber)
            formset = ItemFormSet(request.POST)
            message = "Invalid PIN CODE provided."
            return render_to_response('erx/new_prescription.html',
                {'message': message, 'form': form, 'rxform': formset}, context_instance=RequestContext(request))

        form = PrescriptionForm(request.POST)

        if form.is_valid():
            instance=form.save()
            rxentry = ItemFormSet(request.POST, instance=instance)
            
            if rxentry.is_valid():
                drug_list = []
                for form in rxentry:
                    if form.cleaned_data.get('drug_name') is not None:
                        drug_list.append(form.cleaned_data.get('drug_name'))

                flag = checkInteractions(drug_list)

                if flag == False:
                    rxentry.save()
                    message="[%s] Prescription %s successfully created." % (strftime("%Y-%m-%d %H:%M:%S"), instance)
                    return prescriberHome(request, prescriber=p_id, message=message)

                else:
                    if flag == True:
                        message = 'Prescription not saved. Drug interactions have been detected.'
                    else:
                        message = flag
                    Prescription.objects.filter(rx_id=instance.rx_id).delete()
                    prescriber = Prescriber.objects.get(prescriber_id = p_id)
                    form = PrescriptionForm2(data=request.POST, prescriber=prescriber)
                    rxforms = ItemFormSet(request.POST)
                    return render_to_response('erx/new_prescription.html', {'message': message,'form': form, 'rxform': rxforms},
                        context_instance=RequestContext(request))


            else:
                prescriber = Prescriber.objects.get(prescriber_id = p_id)
                form = PrescriptionForm2(data=request.POST, prescriber=prescriber)
                rxforms = ItemFormSet(request.POST)
                message = '%s' % (rxentry.errors)
                return render_to_response('erx/new_prescription.html', {'message': message,'form': form, 'rxform': rxforms},
                    context_instance=RequestContext(request))
        else:
            errors = form.errors
            prescriber = Prescriber.objects.get(prescriber_id = p_id)
            form = PrescriptionForm2(data=request.POST, prescriber=prescriber)
            rxforms = ItemFormSet(request.POST)
            message = '%s' % (errors)
            return render_to_response('erx/new_prescription.html', {'message': message, 'form': form, 'rxform': rxforms},
                context_instance=RequestContext(request))
    else:
        if request.method == "GET":
            prescriber = Prescriber.objects.get(prescriber_id = p_id)
            prescription = Prescription(prescriber=prescriber)
            form = PrescriptionForm2(instance=prescription, prescriber=prescriber)
            formset = ItemFormSet(instance=prescription)
            return render_to_response('erx/new_prescription.html', {'form': form, 'rxform': formset},
                context_instance=RequestContext(request))