From 0f95c0117292a2b746279de1865c8e53bbe168a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Wed, 21 Sep 2016 17:39:48 +0200 Subject: [PATCH] Added editing of shots --- attract/shot_manager.py | 25 +++++++++++++++++++++++++ attract/shots.py | 12 ++++++++++++ tests/test_shots.py | 26 ++++++++++++++++++++++++++ tests/test_tasks.py | 1 - 4 files changed, 63 insertions(+), 1 deletion(-) diff --git a/attract/shot_manager.py b/attract/shot_manager.py index 10c8e64..820597a 100644 --- a/attract/shot_manager.py +++ b/attract/shot_manager.py @@ -80,3 +80,28 @@ class ShotManager(object): shot_id_to_tasks[task.parent][task_type].add(task) return shot_id_to_tasks + + def edit_shot(self, shot_id, **fields): + """Edits a shot. + + :type shot_id: str + :type fields: dict + :rtype: pillarsdk.Node + """ + + api = pillar_api() + shot = pillarsdk.Node.find(shot_id, api=api) + + shot.name = fields.pop('name') + shot.description = fields.pop('description') + shot.properties.status = fields.pop('status') + shot.properties.notes = fields.pop('notes', '').strip() or None + + self._log.info('Saving shot %s', shot.to_dict()) + + if fields: + self._log.warning('edit_shot(%r, ...) called with unknown fields %r; ignoring them.', + shot_id, fields) + + shot.update(api=api) + return shot diff --git a/attract/shots.py b/attract/shots.py index fb8694b..b87d33d 100644 --- a/attract/shots.py +++ b/attract/shots.py @@ -72,6 +72,18 @@ def view_shot(project, attract_props, shot_id): attract_props=attract_props) +@perproject_blueprint.route('/', methods=['POST']) +@attract_project_view() +def save(project, shot_id): + log.info('Saving shot %s', shot_id) + log.debug('Form data: %s', request.form) + + shot_dict = request.form.to_dict() + shot = current_attract.shot_manager.edit_shot(shot_id, **shot_dict) + + return flask.jsonify({'shot_id': shot_id, 'etag': shot._etag, 'time': shot._updated }) + + # TODO: remove GET method once Pablo has made a proper button to call this URL with a POST. @perproject_blueprint.route('/create', methods=['POST', 'GET']) @attract_project_view() diff --git a/tests/test_shots.py b/tests/test_shots.py index c3dfdd1..2da85d8 100644 --- a/tests/test_shots.py +++ b/tests/test_shots.py @@ -1,6 +1,7 @@ # -*- encoding: utf-8 -*- import responses +from bson import ObjectId import pillarsdk import pillar.tests @@ -82,3 +83,28 @@ class ShotManagerTest(AbstractAttractTest): u'effects': {task4['_id'], task5['_id']}, None: {task6['_id']}, }, shot_id_to_task[shot2_id]) + + @responses.activate + def test_edit_shot(self): + shot = self.create_shot() + + with self.app.test_request_context(): + # Log in as project admin user + pillar.auth.login_user(ctd.EXAMPLE_PROJECT_OWNER_ID) + + self.mock_blenderid_validate_happy() + + self.smngr.edit_shot(shot_id=shot['_id'], + name=u'ผัดไทย', + description=u'Shoot the Pad Thai', + status='todo') + + # Test directly with MongoDB + with self.app.test_request_context(): + nodes_coll = self.app.data.driver.db['nodes'] + found = nodes_coll.find_one(ObjectId(shot['_id'])) + self.assertEqual(u'ผัดไทย', found['name']) + self.assertEqual(u'todo', found['properties']['status']) + self.assertEqual(u'Shoot the Pad Thai', found['description']) + self.assertNotIn(u'notes', found['properties']) + diff --git a/tests/test_tasks.py b/tests/test_tasks.py index ec3ba11..ba1b66b 100644 --- a/tests/test_tasks.py +++ b/tests/test_tasks.py @@ -62,7 +62,6 @@ class TaskWorkflowTest(AbstractAttractTest): name=u'nööw name', description=u'€ ≠ ¥', status='todo') - self.assertIsNotNone(task) # Test directly with MongoDB with self.app.test_request_context():