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 test_delete(self): match_entities = RawMatchEntity.query.all() assert len(match_entities) == 0 # Helper items editor = Editor(name='Tester') db.session.add(editor) item = Item() db.session.add(item) db.session.flush() match = RawMatch(item_id=item.id, editor_name=editor.name, timestamp=datetime.now()) db.session.add(match) entity = Entity(mbid='f27ec8db-af05-4f36-916e-3d57f91ecf5e', type='test') db.session.add(entity) db.session.flush() match_entity = RawMatchEntity(raw_match_id=match.id, entity_mbid=entity.mbid) db.session.add(match_entity) db.session.flush() match_entities = RawMatchEntity.query.all() assert len(match_entities) == 1 assert match_entities[0] == match_entity match.delete() entities = RawMatchEntity.query.all() assert len(entities) == 0
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_to_dict(self): match = RawMatch(item_id=self.item.id, editor_name=self.editor.name, timestamp=datetime.now()) db.session.add(match) self.assertDictEqual(match.to_dict(), { 'id': match.id, 'item': match.item_id, 'timestamp': match.timestamp, 'superseded': match.superseded, 'entities': [e.entity.to_dict() for e in match.entities], })
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)