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())
def test_match_item(self): match = RawMatch.match_item(item_id=self.item.id, editor_name=self.editor.name, entities=[self.entity_1, self.entity_2]) self.assertEqual(match.item.id, self.item.id) self.assertEqual(match.editor, self.editor)