コード例 #1
0
ファイル: signals.py プロジェクト: dimagi/carehq
def case_saved(sender, instance, created, **kwargs):
    """
    When a case is saved due to creation
    reversion will automatically save the original information.
    
    However, we will be doing an additional operation to create a CaseEvent to record the actual happening.
    
    Other case events such as making phone calls or doing other work around a case will also be saved,
    but this particular signal will create a CaseEvent of a specific type.
    """
    event_new = CaseEvent()
    event_new.case = instance
    
    if created:        
        event_create_date = instance.opened_date
        event_creator = instance.opened_by
        notes = "New case created by " + event_creator.description
        
        try:
            event_new.activity = constants.CASE_EVENT_OPEN
        except Exception, ex:
            logging.error("Error, Event category [%s] activity [%s] does not exist in the database - perhaps the configuration is not fully loaded:: %s" % (instance.category, constants.CASE_EVENT_OPEN, ex))
コード例 #2
0
ファイル: views.py プロジェクト: dimagi/carehq
def manage_case(request, case_id, template_name="casetracker/manage_case.html"):
    """
    This view handles all aspects of lifecycle depending on the URL params and the request type.
    """
    context = RequestContext(request)
    thecase = Case.objects.get(id=case_id)

    do_edit = False
    activity_slug = None
    activity = None
    for key, value in request.GET.items():
        if key == "activity":
            activity = value
    context["case"] = thecase

    # template_name = thecase.category.handler.read_template(request, context)
    # context = thecase.category.handler.read_context(thecase,request, context)
    ########################
    # Inline Form display
    if activity:
        context["show_form"] = True
        context["form_headline"] = activity

        if request.method == "POST":
            if activity == constants.CASE_EVENT_EDIT or activity == constants.CASE_EVENT_ASSIGN:
                form = CaseModelForm(data=request.POST, instance=thecase, editor_user=request.user, activity=activity)
                context["form"] = form
                if form.is_valid():
                    case = form.save(commit=False)
                    edit_comment = form.cleaned_data["comment"]
                    case.last_edit_by = request.user
                    case.last_edit_date = datetime.utcnow()
                    # next, we need to see the mode and flip the fields depending on who does what.
                    if activity == constants.CASE_EVENT_ASSIGN:
                        case.assigned_date = datetime.utcnow()
                        case.assigned_by = request.user
                        edit_comment += " (%s to %s by %s)" % (
                            activity.past_tense.title(),
                            case.assigned_to.title(),
                            request.user.title(),
                        )

                    case.save(activity=activity, save_comment=edit_comment)
                    return HttpResponseRedirect(reverse("manage-case", kwargs={"case_id": case_id}))

            elif activity == constants.CASE_EVENT_RESOLVE or activity == constants.CASE_EVENT_CLOSE:
                form = CaseResolveCloseForm(data=request.POST, case=thecase, activity=activity)
                context["form"] = form
                if form.is_valid():
                    status = form.cleaned_data["state"]
                    comment = form.cleaned_data["comment"]
                    thecase.status = status
                    thecase.last_edit_by = request.user

                    if activity == constants.CASE_EVENT_CLOSE:
                        thecase.closed_by = request.user
                    elif activity == constants.CASE_EVENT_RESOLVE:
                        thecase.resolved_by = request.user

                    thecase.save(activity=activity, save_comment=comment)

                    return HttpResponseRedirect(reverse("manage-case", kwargs={"case_id": case_id}))
            elif activity == constants.CASE_EVENT_COMMENT:
                form = CaseCommentForm(data=request.POST)
                context["form"] = form
                if form.is_valid():
                    if form.cleaned_data.has_key("comment") and form.cleaned_data["comment"] != "":
                        comment = form.cleaned_data["comment"]
                    evt = CaseEvent()
                    evt.case = thecase
                    evt.notes = comment
                    evt.activity = constants.CASE_EVENT_COMMENT
                    evt.created_by = request.user
                    evt.save()
                    return HttpResponseRedirect(reverse("manage-case", kwargs={"case_id": case_id}))
        else:
            # it's a GET
            caseform = activity.bridge.form_class()
            # This is a bit ugly at the moment as this view itself is the only place that instantiates the forms
            if caseform == CaseModelForm:
                context["form"] = caseform(instance=thecase, editor_user=request.user, activity=activity)
                context["can_comment"] = False
            elif caseform == CaseResolveCloseForm:
                context["form"] = caseform(case=thecase, activity=activity)
                context["can_comment"] = False
            elif caseform == CaseCommentForm:
                context["form"] = caseform()

            else:
                logging.error("no form definition")
            context["submit_url"] = ""
    return render_to_response(template_name, context_instance=context)
コード例 #3
0
ファイル: loader.py プロジェクト: dimagi/carehq
def load_interaction(careteam, interaction_arr):
    """
    works through the entire array to simulate the case lifecycle
    """
    startdelta = timedelta(days=random.randint(1,7)) #sometime in the past week
    
    
    main_info = interaction_arr[0:4]
    category_txt = main_info[0].strip()
    title = main_info[1].strip()
    body = main_info[2].strip()
    source = main_info[3].strip()
    
    if title.count("%s") == 1:
        title = title % careteam.patient.user.first_name
    if body.count("%s") == 1:
        body = body % careteam.patient.user.first_name
    
    newcase = Case()
    newcase.description = title
    newcase.body = body
    newcase.category = Category.objects.get(slug=category_txt)
    
    creator=None
    if source.lower() == 'provider':
        creator = careteam.primary_provider.user            
    elif source.lower() == 'caregiver':
        creator = careteam.caregivers.all()[0]
    elif source.lower() == 'patient':
        creator = careteam.patient.user
    elif source.lower() == 'home monitor':
        creator = User.objects.get(username ='******')
    
    if creator == None:
        print "no creator, wtf: " + str(interaction_arr)
    newcase.opened_by = creator    
    newcase.opened_date = datetime.utcnow() - startdelta
    newcase.last_edit_by = creator
    newcase.last_edit_date = newcase.opened_date
    newcase.status = Status.objects.all().filter(category=newcase.category).filter(state_class=constants.CASE_STATE_OPEN)[0]

    newcase.assigned_to = careteam.providers.all()[random.randint(0,careteam.providers.all().count()-1)].user
    newcase.assigned_date = newcase.opened_date 

    newcase.priority = Priority.objects.all()[random.randint(0, Priority.objects.all().count() -1)]    
    newcase.save(unsafe=True)
    careteam.add_case(newcase)
    
    
    
    subarr = interaction_arr[4:]
    while len(subarr) >= 3:
        resp = subarr[0].strip()
        src = subarr[1].lower().strip()
        evt = subarr[2].lower().strip()
        
        if resp == '' and src == '' and evt == '':
            break
        responder=None
        if src.lower() == 'provider':
            responder = careteam.primary_provider.user            
        elif src.lower() == 'caregiver':
            responder = careteam.caregivers.all()[0]
        elif src.lower() == 'patient':
            responder = careteam.patient.user
        if responder == None:
            print "wtf: " + str(subarr)
        
        evt = CaseEvent()
        evt.case = newcase
        if resp.count("%s") == 1:
            resp = resp % careteam.patient.user.first_name
        evt.notes = resp
        evt.activity = ActivityClass.objects.filter(category=newcase.category)\
            .filter(event_class=constants.CASE_EVENT_COMMENT)[0]
        evt.created_by = responder
        startdelta = startdelta - timedelta(minutes=random.randint(4,480))
        evt.created_date = datetime.utcnow() - startdelta
        evt.save(unsafe=True)
            
        
        if len(subarr[3:]) >= 3:
            subarr = subarr[3:]
        else:
            break