def _transaction_cmp(first_transaction, second_transaction): # if the forms aren't submitted by the same user, just default to server dates if first_transaction.user_id != second_transaction.user_id: return cc_cmp(first_transaction.server_date, second_transaction.server_date) if first_transaction.form_id and first_transaction.form_id == second_transaction.form_id: # short circuit if they are from the same form return cc_cmp( _type_sort(first_transaction.type), _type_sort(second_transaction.type) ) if not (first_transaction.server_date and second_transaction.server_date): raise MissingServerDate() # checks if the dates received are within a particular range if abs(first_transaction.server_date - second_transaction.server_date) > fudge_factor: return cc_cmp(first_transaction.server_date, second_transaction.server_date) def _sortkey(transaction): def form_index(form_id): try: return xform_ids.index(form_id) except ValueError: return sys.maxsize # if the user is the same you should compare with the special logic below return ( transaction.client_date, form_index(transaction.form_id), _type_sort(transaction.type), ) return cc_cmp(_sortkey(first_transaction), _sortkey(second_transaction))
def _sortkey(action): if not action.server_date or not action.date: raise MissingServerDate() form_cmp = lambda form_id: (form_ids.index(form_id) if form_id in form_ids else sys.maxint, form_id) return ( # this is sneaky - it's designed to use just the date for the # server time in case the phone submits two forms quickly out of order action.server_date.date(), action.date, form_cmp(action.xform_id), _type_sort(action.action_type), )
def _sortkey(action): if not action.server_date or not action.date: raise MissingServerDate() form_cmp = lambda form_id: (form_ids.index( form_id) if form_id in form_ids else sys.maxint, form_id) # if the user is the same you should compare with the special logic below # if the user is not the same you should compare just using received_on return ( # this is sneaky - it's designed to use just the date for the # server time in case the phone submits two forms quickly out of order action.server_date.date(), action.date, form_cmp(action.xform_id), _type_sort(action.action_type), )
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))