def item_matches(item_id): current = RawMatch.get_by_item(item_id, superseded=False) previous = RawMatch.get_by_item(item_id, superseded=True) return jsonify({ 'currentMatch': current[0].to_dict() if current else None, 'previousMatches': [match.to_dict() for match in previous] })
def match_item(item_id): '''This endpoint is passed a set of mbids, and an item. It then checks if the set matches the current match for the item; if so, it does nothing. If not, the submitted match becomes the new match, superseding the former match. As a precaution, empty sets will be ignored unless a special extra parameter is set. The item ID should be passed in the URL; the rest in POSTed JSON. ''' is_mbid = re.compile('^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$') request_json = request.get_json() empty = request_json.get('empty', False) mbids = [mbid.lower() for mbid in request_json.get('matches')] if len(mbids) == 0 and not empty: return jsonify({'error': 'No matches provided.'}) if len([True for mbid in mbids if (is_mbid.match(mbid) is None)]) > 0: return jsonify({'error': 'MBIDs improperly formatted.'}) existing = RawMatch.get_by_item(item_id, superseded=False) if len(existing) > 1: raise Exception('Why are there %d non-superseded matches for item %d?' % (len(existing), item_id)) elif len(existing) == 1: if set(mbids) == set([entity.entity_mbid for entity in existing[0].entities]): return jsonify({'error': 'Matches not changed'}) entities = Entity.query.filter(Entity.mbid.in_(mbids)).all() if len(entities) != len(mbids): return jsonify({'error': 'Not all entities were found in the DB'}) match = RawMatch.match_item(item_id, current_user.id, entities) db.session.commit() return jsonify(match=match.to_dict())