def post(self): data = request.get_json() if not data: abort(400, message='Please supply a preset!') errors = Preset.data_schema.validate(data) if errors: abort(400, errors=errors) preset = Preset(name=data.get('name')) preset.set_data(data) preset.save(force_insert=True) signals.emit('preset:added', {'id': preset.id}) return preset.to_dict(), 201
def test_list_presets(client): p1 = Preset(name='p1') p1.set_data({ 'voices': { 'melody': [{ 'soundfont': 'test', }], 'drone': [], 'trompette': [], } }) p1.save() Preset.create(name='p2', data='{}') rv = client.get('/api/presets') assert rjson(rv) == [ { 'id': 1, 'name': 'p1', 'number': 1, 'voices': { 'melody': [{ 'soundfont': 'test', 'bank': 0, 'program': 0, 'note': 60, 'polyphonic': False, 'muted': True, 'capo': 0, 'volume': 127, 'panning': 64, 'mode': 'midigurdy', 'finetune': 0, 'chien_threshold': 50, }], 'trompette': [], 'drone': [], } }, { 'id': 2, 'name': 'p2', 'number': 2, }, ] assert rv.status_code == 200
def save_preset(self, name=None, preset_id=None): if preset_id is not None: try: preset = Preset.get(Preset.id == int(preset_id)) except Preset.DoesNotExist: log.error('Preset {} not found!'.format(preset_id)) return if name is not None: preset.name = name else: preset = Preset() preset.name = name or 'Unnamed' preset.set_data(self.to_preset_dict()) preset.save() signals.emit('preset:changed', {'id': preset.id})
def test_update_preset(client): p = Preset(name='p1') p.set_data({ 'voices': { 'melody': [{ 'soundfont': 'test', }], 'drone': [{ 'soundfont': 'test', 'program': 1, 'note': 61, }], } }) p.save() assert Preset.select().count() == 1 data = { 'id': 1, 'name': 'p1-updated', 'voices': { 'melody': [{ 'soundfont': 'test', 'bank': 1, 'program': 0, 'note': 12, 'polyphonic': True, 'muted': True, 'capo': 12, 'volume': 20, 'panning': 0, 'mode': 'midigurdy', 'finetune': 0, 'chien_threshold': 50, }, { 'soundfont': 'test', 'bank': 1, 'program': 1, 'note': 1, 'polyphonic': False, 'muted': True, 'capo': 6, 'volume': 100, 'panning': 0, 'mode': 'midigurdy', 'finetune': 0, 'chien_threshold': 50, }], 'drone': [], 'trompette': [], }, } rv = client.put('/api/presets/1', data=json.dumps(data), content_type='application/json') expected = data expected['number'] = 1 assert rjson(rv) == expected assert rv.status_code == 200 assert Preset.select().count() == 1