コード例 #1
0
ファイル: views.py プロジェクト: tallysmartins/gestorpsi
def diagnosis_item_html(request, client_id, referral_id, diagnosis_id):
    client = get_object_or_404(
        Client,
        pk=client_id,
        person__organization=request.user.get_profile().org_active)

    # check if logged user can read it
    if not _access_ehr_check_read(request, client):
        return render_to_response(
            '403.html', {
                'object': _("Oops! You don't have access for this service!"),
            },
            context_instance=RequestContext(request))

    referral = get_object_or_404(
        Referral,
        pk=referral_id,
        service__organization=request.user.get_profile().org_active)

    diagnosis = get_object_or_None(Diagnosis, pk=diagnosis_id) or Demand()

    if diagnosis.pk and diagnosis.referral.service.organization != request.user.get_profile(
    ).org_active:
        raise Http404

    return render_to_response('ehr/ehr_diagnosis_list_item.html', {
        'i': diagnosis,
        'object': client,
        'referral': referral,
    },
                              context_instance=RequestContext(request))
コード例 #2
0
ファイル: views.py プロジェクト: tallysmartins/gestorpsi
def demand_form(request, client_id, referral_id, demand_id=0):
    if not settings.DEBUG and not request.is_ajax(): raise Http404

    client = get_object_or_404(
        Client,
        pk=client_id,
        person__organization=request.user.get_profile().org_active)
    referral = get_object_or_404(
        Referral,
        pk=referral_id,
        service__organization=request.user.get_profile().org_active)

    # check if logged user can write it
    if not _access_ehr_check_read(request, client):
        return render_to_response(
            '403.html', {
                'object': _("Oops! You don't have access for this service!"),
            },
            context_instance=RequestContext(request))

    demand = get_object_or_None(Demand, pk=demand_id) or Demand()

    if demand.pk and demand.referral.service.organization != request.user.get_profile(
    ).org_active:
        raise Http404

    have_perms_to_write = None
    # check if logged user can write on it, just to hide save button on template, is verified by post method also
    if _ehr_can_save(request, demand):
        have_perms_to_write = True
    """ need pass to template time unit forms """
    howlong_form = TimeUnitForm(instance=demand.how_long_it_happens,
                                prefix="howlong")
    frequency_form = TimeUnitForm(instance=demand.frequency,
                                  prefix="frequency")
    duration_form = TimeUnitForm(instance=demand.duration, prefix="duration")

    if request.method == 'POST':
        if not _access_ehr_check_write(request, referral):
            return render_to_response('403.html', {
                'object':
                _("Oops! You don't have access for this service!"),
            },
                                      context_instance=RequestContext(request))

        form = DemandForm(request.POST, instance=demand)
        form.fields['occurrence'].queryset = referral.occurrences()

        if not form.is_valid() or demand.pk and not _ehr_can_save(
                request, demand):
            return render_to_response(
                'ehr/ehr_demand_form.html', {
                    'object': client,
                    'referral': referral,
                    'form': form,
                    'howlong_form': howlong_form,
                    'frequency_form': frequency_form,
                    'duration_form': duration_form,
                    'have_perms_to_write': have_perms_to_write,
                },
                context_instance=RequestContext(request))
        else:
            demand = form.save(commit=False)
            demand.client_id = client.id
            demand.referral_id = referral.id
            demand.occurrence = get_object_or_None(
                ScheduleOccurrence, pk=request.POST.get(
                    'occurrence')) if request.POST.get('occurrence') else None

            demand.edit_status = _ehr_set_edit_status(request)

            if request.POST.get('howlong-unit'):
                howlong_form = TimeUnitForm(
                    request.POST,
                    instance=demand.how_long_it_happens
                    if demand.how_long_it_happens else TimeUnit(),
                    prefix="howlong")
                if not howlong_form.is_valid():
                    messages.error(
                        request,
                        _("There's an error in 'How long it happens' field."))
                    return HttpResponseRedirect(
                        '/client/%s/%s/demand/%s/' %
                        (client_id, referral_id,
                         demand.id)) if demand.id else HttpResponseRedirect(
                             '/client/%s/%s/demand/add/' %
                             (client_id, referral_id))
                demand.how_long_it_happens = howlong_form.save()
            else:
                demand.how_long_it_happens = None

            if request.POST.get('frequency-unit'):
                frequency_form = TimeUnitForm(request.POST,
                                              instance=demand.frequency if
                                              demand.frequency else TimeUnit(),
                                              prefix="frequency")
                if not frequency_form.is_valid():
                    messages.error(request,
                                   _("There's an error in 'Frequency' field."))
                    return HttpResponseRedirect(
                        '/client/%s/%s/demand/%s/' %
                        (client_id, referral_id,
                         demand.id)) if demand.id else HttpResponseRedirect(
                             '/client/%s/%s/demand/add/' %
                             (client_id, referral_id))
                demand.frequency = frequency_form.save()
            else:
                demand.frequency = None

            if request.POST.get('duration-unit'):
                duration_form = TimeUnitForm(request.POST,
                                             instance=demand.duration if
                                             demand.duration else TimeUnit(),
                                             prefix="duration")
                if not duration_form.is_valid():
                    messages.error(request,
                                   _("There's an error in 'Duration' field."))
                    return HttpResponseRedirect(
                        '/client/%s/%s/demand/%s/' %
                        (client_id, referral_id,
                         demand.id)) if demand.id else HttpResponseRedirect(
                             '/client/%s/%s/demand/add/' %
                             (client_id, referral_id))
                demand.duration = duration_form.save()
            else:
                demand.duration = None

            demand.save()
            return render_to_response('ehr/ehr_demand_form_done.html', {
                'demand': demand,
                'client': client,
                'referral': referral,
            },
                                      context_instance=RequestContext(request))

    else:
        form = DemandForm(instance=demand)
        form.fields['occurrence'].queryset = referral.occurrences()

        return render_to_response('ehr/ehr_demand_form.html', {
            'object': client,
            'referral': referral,
            'form': form,
            'howlong_form': howlong_form,
            'frequency_form': frequency_form,
            'duration_form': duration_form,
            'have_perms_to_write': have_perms_to_write,
        },
                                  context_instance=RequestContext(request))