def add_story(self, title): """Add a story to the Sketch. Args: title: The name of the view. Raises: ValueError: If both query_string an query_dsl are missing. Returns: An instance of a Story object. """ story = SQLStory.query.filter_by(title=title, sketch=self.sql_sketch, user=None).first() if story: return Story(story) story = SQLStory.get_or_create(title=title, content='[]', sketch=self.sql_sketch, user=None) db_session.add(story) db_session.commit() return Story(story)
def post(self, sketch_id): """Handles POST request to the resource. Args: sketch_id: Integer primary key for a sketch database model Returns: A view in JSON (instance of flask.wrappers.Response) """ form = forms.StoryForm.build(request) if not form.validate_on_submit(): abort(HTTP_STATUS_CODE_BAD_REQUEST, 'Unable to validate form data.') sketch = Sketch.query.get_with_acl(sketch_id) if not sketch: abort(HTTP_STATUS_CODE_NOT_FOUND, 'No sketch found with this ID.') if not sketch.has_permission(current_user, 'write'): abort(HTTP_STATUS_CODE_FORBIDDEN, 'User does not have write access controls on sketch.') title = '' if form.title.data: title = form.title.data story = Story(title=title, content='[]', sketch=sketch, user=current_user) db_session.add(story) db_session.commit() return self.to_json(story, status_code=HTTP_STATUS_CODE_CREATED)
def _create_story(self, sketch, user): """Create a story in the database. Args: sketch: A sketch (instance of timesketch.models.sketch.Sketch) user: A user (instance of timesketch.models.user.User) Returns: A story (instance of timesketch.models.story.Story) """ story = Story.get_or_create( title='Test', content='Test', sketch=sketch, user=user) self._commit_to_database(story) return story