Exemplo n.º 1
0
  def wrap(request, orgStr, *args, **kwargs):
#    logging.info("In org_required wrap, orgStr '%s' isNone %s user.orgStr %s"
#                 % (orgStr, orgStr is None, request.user.organization))
    if not request.user.organization:
      return HttpResponseRedirect('/org-required')
    if not request.user.organization == orgStr:
      return HttpResponseRedirect('/org-must-match')
    if not models.organization_exists(orgStr):
      raise Http404
    return func(request, orgStr, *args, **kwargs)
Exemplo n.º 2
0
def patient_new_with_visit(request, orgStr):
  """Add a new patient and a new visit to the new patient in one screen.

  Note: You can add patients and visits without being logged in, but you will
  only be able to see the last one.
  """
  #logging.info("request.LANGUAGE_CODE: %s" % request.LANGUAGE_CODE)
  #logging.info("request: %s" % request)
  patient_exists = False

  if not models.organization_exists(orgStr):
    raise Http404

  # NOTE(dan): patient doesn't change, so it need not be a param
  def render_this(patient_exists, confirmationForm, patientForm, visitForm):
    return respond(request, 'patient_new_with_visit.html',
                   {'patient_exists': patient_exists,
                    'confirmationform': confirmationForm,
                    'patientform': patientForm,
                    'visitform': visitForm,
                    'orgStr' : orgStr})

  if request.method != 'POST':
    confirmationForm = ConfirmationForm()
    default_country = getattr(request.user, 'default_country', '')
    if default_country:
      patientForm = PatientForm(initial={'organization':orgStr, 'country': default_country})
    else:
      patientForm = PatientForm(initial={'organization':orgStr})
        
    visitForm = VisitForm(initial={'organization':orgStr})
  else:
    confirmationForm = ConfirmationForm(request.POST)
    patientForm = PatientForm(request.POST)
    visitForm = VisitForm(request.POST)

    if patientForm.is_valid() and visitForm.is_valid():
      # First check if patient exists based on name and date of birth
      name = patientForm.cleaned_data['name']
      birth_date = patientForm.cleaned_data['birth_date']
      patient = models.Patient.get_patient_by_name_birthdate(name, birth_date)
      if patient:
        patient_exists = True
        request.session['patient'] = patient
      
      # If patient is new
      if not patient_exists:
        # Need to store both
        if not _store_new_patient_and_new_visit(request, orgStr, patientForm, visitForm):
          return render_this(patient_exists, confirmationForm, patientForm, visitForm)
      # We already went through this once and there is another patient with same name and birthdate
      else:         
        if confirmationForm.is_valid():  
          confirmation_option = confirmationForm.cleaned_data['confirmation_option']
          if confirmation_option == 'addpatientvisit':
            # Need to store both
            if not _store_new_patient_and_new_visit(request, orgStr, patientForm, visitForm):
              return render_this(patient_exists, confirmationForm, patientForm, visitForm)
          else:              
            # Patient is still the same 
            patient = request.session['patient']                       
            # Just add the visit
            newVisit = models.Visit(parent = patient)
            visitForm = VisitForm(request.POST, instance = newVisit)
            visit = _store_new_visit(patient, visitForm, request.user)        
            if not visit:
              return render_this(patient_exists, confirmationForm, patientForm, visitForm)      
        else:
          return render_this(patient_exists, confirmationForm, patientForm, visitForm)        
            
      patient = request.session['patient']
      # Redirect to viewing
      return HttpResponseRedirect(patient.get_view_url())
                
  return render_this(patient_exists, confirmationForm, patientForm, visitForm)