Exemple #1
0
def update_calls(session, issue_list):

    # Get a list of the intervention group's node ids
    nids = [int(i) for (i,) in session.query(RParticipant.nid).filter_by(ptype=1)]
    
    # We are only concerned with completed calls that belong to our sample group
    for r_call in session.query(RCall).filter(RCall.pnid.in_(nids)).filter(RCall.completed != None):

        # Find the Participant object for this call
        pid = session.query(RParticipant).filter_by(nid=r_call.pnid).first().pid
        participant = Participant.objects.get(pid=pid)

        # Check if this Call object is already in our database
        try:
            call = Call.objects.get(participant=participant, number=r_call.number)
        except Call.DoesNotExist:
            call = Call(participant=participant, number=r_call.number)

        # Update fields
        call.completed_date = datetime.datetime.strptime(r_call.completed, '%Y-%m-%dT%H:%M:%S').date()
        
        if r_call.goal_met:
            call.goal_met = True

        try:
            call.veg_servings = massage_number('veg_servings', r_call.veg_servings)
        except ValueError as ve:
            issue_list.append({
                'participant': participant.pid,
                'call_num': int(r_call.number),
                'field': 'Call: Vegetable Servings',
                'reason': 'Value: %s' % r_call.veg_servings
            })

            call.veg_servings = 0
        except RequiredValueError as rve:
            issue_list.append({
                'participant': participant.pid,
                'call_num': int(r_call.number),
                'field': 'Call: Vegetable Servings',
                'reason': rve.msg
            })

            call.veg_servings = 0

        try:
            call.fruit_servings = massage_number('fruit_servings', r_call.fruit_servings)
        except ValueError as ve:
            issue_list.append({
                'participant': participant.pid,
                'call_num': int(r_call.number),
                'field': 'Call: Fruit Servings',
                'reason': 'Value: %s' % r_call.fruit_servings
            })

            call.fruit_servings = 0
        except RequiredValueError as rve:
            issue_list.append({
                'participant': participant.pid,
                'call_num': int(r_call.number),
                'field': 'Call: Fruit Servings',
                'reason': rve.msg
            })

            call.fruit_servings = 0

        try:
            call.fiber_grams = massage_number('fiber_grams', r_call.fiber_grams)
        except ValueError as ve:
            issue_list.append({
                'participant': participant.pid,
                'call_num': int(r_call.number),
                'field': 'Call: Fiber Grams',
                'reason': 'Value: %s' % r_call.fiber_grams
            })

            call.fiber_grams = 0
        except RequiredValueError as rve:
            issue_list.append({
                'participant': participant.pid,
                'call_num': int(r_call.number),
                'field': 'Call: Fiber Grams',
                'reason': rve.msg
            })

            call.fiber_grams = 0

        try:
            call.fat_grams = massage_number('fat_grams', r_call.fat_grams)
        except ValueError as ve:
            issue_list.append({
                'participant': participant.pid,
                'call_num': int(r_call.number),
                'field': 'Call: Fat Grams',
                'reason': 'Value: %s' % r_call.fat_grams
            })

            call.fat_grams = 0
        except RequiredValueError as rve:
            issue_list.append({
                'participant': participant.pid,
                'call_num': int(r_call.number),
                'field': 'Call: Fat Grams',
                'reason': rve.msg
            })

            call.fat_grams = 0

        try:
            call.steps = massage_number('steps', r_call.steps)
        except ValueError as ve:
            issue_list.append({
                'participant': participant.pid,
                'call_num': int(r_call.number),
                'field': 'Call: Steps',
                'reason': 'Value: %s' % r_call.steps
            })

            call.steps = 0
        except RequiredValueError as rve:
            issue_list.append({
                'participant': participant.pid,
                'call_num': int(r_call.number),
                'field': 'Call: Steps',
                'reason': rve.msg
            })

            call.steps = 0

        call.save()