def cmp_observation(x, y): """ for a given COBservation, do the following. 1: If it's an addendum/reconciliation trumps all 2: If it's direct, and other is not direct, the direct wins 3: If both direct, do by earliest date 4: If neither direct, do by earliest encounter_date regardless of method. < -1 = 0 > 1 Assumes that x and y have the same observation date (cell in the DOT json array) Encounter date is the date in which the date cell is observed. """ assert x.observed_date.date() == y.observed_date.date() x_is_reconciliation = getattr(x, 'is_reconciliation', None) y_is_reconciliation = getattr(y, 'is_reconciliation', None) # Reconciliation handling # reconciliations always win. if x_is_reconciliation and y_is_reconciliation: # sort by earlier date, so flip x,y return cmp(y.submitted_date, x.submitted_date) elif x_is_reconciliation and not y_is_reconciliation: # result: x > y return 1 elif not x_is_reconciliation and y_is_reconciliation: # result: x < y return -1 elif x.method == DOT_OBSERVATION_DIRECT and y.method == DOT_OBSERVATION_DIRECT: # Direct observations win next # sort by earlier date, so flip x,y return cmp(y.encounter_date, x.encounter_date) elif x.method == DOT_OBSERVATION_DIRECT and y.method != DOT_OBSERVATION_DIRECT: # result: x > y return 1 elif x.method != DOT_OBSERVATION_DIRECT and y.method == DOT_OBSERVATION_DIRECT: # result: x < y return -1 elif (x.adherence, x.method) == DOT_UNCHECKED_CELL and (y.adherence, y.method) != DOT_UNCHECKED_CELL: # unchecked should always lose return -1 elif (x.adherence, x.method) != DOT_UNCHECKED_CELL and (y.adherence, y.method) == DOT_UNCHECKED_CELL: return 1 else: return cmp(y.encounter_date, x.encounter_date)
def action_cmp(first_action, second_action): # compare server dates if the forms aren't submitted by the same user if first_action.user_id != second_action.user_id: return cmp(first_action.server_date, second_action.server_date) if first_action.xform_id and first_action.xform_id == second_action.xform_id: # short circuit if they are from the same form return cmp(type_index(first_action.action_type), type_index(second_action.action_type)) if not (first_action.server_date and second_action.server_date and first_action.date and second_action.date): raise MissingServerDate() if abs(first_action.server_date - second_action.server_date) > const.SIGNIFICANT_TIME: return cmp(first_action.server_date, second_action.server_date) return cmp(sort_key(first_action), sort_key(second_action))
def _compare_submissions(x, y): # these are backwards because we want most recent to come first return cmp(y.received_on, x.received_on)