(self, sketch_id)
return self.to_json(
            annotations, status_code=HTTP_STATUS_CODE_CREATED)
Handles POST request to the resource.

        Args:
            sketch_id: Integer primary key for a sketch database model

        Returns:
            An annotation in JSON (instance of flask.wrappers.Response)
Handles POST request to the resource.
Handles POST request to the resource .
def post(self, sketch_id):
        """Handles POST request to the resource.

        Args:
            sketch_id: Integer primary key for a sketch database model

        Returns:
            An annotation in JSON (instance of flask.wrappers.Response)
        """
        form = forms.EventAnnotationForm.build(request)
        if not form.validate_on_submit():
            abort(
                HTTP_STATUS_CODE_BAD_REQUEST, 'Unable to validate form data.')

        annotations = []
        sketch = self._get_sketch(sketch_id)

        current_search_node = None
        _search_node_id = request.json.get('current_search_node_id', None)
        if _search_node_id:
            current_search_node = self._get_current_search_node(_search_node_id,
                                                                sketch)

        indices = [
            t.searchindex.index_name for t in sketch.timelines
            if t.get_status.status.lower() == 'ready']
        annotation_type = form.annotation_type.data
        events = form.events.raw_data

        for _event in events:
            searchindex_id = _event['_index']
            searchindex = SearchIndex.query.filter_by(
                index_name=searchindex_id).first()
            event_id = _event['_id']
            event_type = _event['_type']

            if searchindex_id not in indices:
                abort(
                    HTTP_STATUS_CODE_BAD_REQUEST,
                    'Search index ID ({0!s}) does not belong to the list '
                    'of indices'.format(searchindex_id))

            # Get or create an event in the SQL database to have something
            # to attach the annotation to.
            event = Event.get_or_create(
                sketch=sketch,
                searchindex=searchindex,
                document_id=event_id)

            if current_search_node:
                current_search_node.events.append(event)

            # Add the annotation to the event object.
            if 'comment' in annotation_type:
                annotation = Event.Comment(
                    comment=form.annotation.data, user=current_user)
                event.comments.append(annotation)
                self.datastore.set_label(
                    searchindex_id,
                    event_id,
                    event_type,
                    sketch.id,
                    current_user.id,
                    '__ts_comment',
                    toggle=False
                )
                if current_search_node:
                    current_search_node.add_label('__ts_comment')

            elif 'label' in annotation_type:
                annotation = Event.Label.get_or_create(
                    label=form.annotation.data, user=current_user)
                if annotation not in event.labels:
                    event.labels.append(annotation)

                toggle = False
                if '__ts_star' in form.annotation.data:
                    toggle = True
                if '__ts_hidden' in form.annotation.data:
                    toggle = True
                if form.remove.data:
                    toggle = True

                self.datastore.set_label(
                    searchindex_id,
                    event_id,
                    event_type,
                    sketch.id,
                    current_user.id,
                    form.annotation.data,
                    toggle=toggle
                )

                if current_search_node:
                    search_node_label = '__ts_label'
                    if '__ts_star' in form.annotation.data:
                        search_node_label = '__ts_star'
                    current_search_node.add_label(search_node_label)
            else:
                abort(
                    HTTP_STATUS_CODE_BAD_REQUEST,
                    'Annotation type needs to be either label or comment, '
                    'not {0!s}'.format(annotation_type))

            annotations.append(annotation)
            # Save the event to the database
            db_session.add(event)
            db_session.commit()

        return self.to_json(
            annotations, status_code=HTTP_STATUS_CODE_CREATED)
def post ( self , sketch_id ) : form = forms . EventAnnotationForm . build ( request ) if not form . validate_on_submit ( ) : abort ( HTTP_STATUS_CODE_BAD_REQUEST , 'Unable to validate form data.' ) annotations = [ ] sketch = self . _get_sketch ( sketch_id ) current_search_node = None _search_node_id = request . json . get ( 'current_search_node_id' , None ) if _search_node_id : current_search_node = self . _get_current_search_node ( _search_node_id , sketch ) indices = [ t . searchindex . index_name for t in sketch . timelines if t . get_status . status . lower ( ) == 'ready' ] annotation_type = form . annotation_type . data events = form . events . raw_data for _event in events : searchindex_id = _event [ '_index' ] searchindex = SearchIndex . query . filter_by ( index_name = searchindex_id ) . first ( ) event_id = _event [ '_id' ] event_type = _event [ '_type' ] if searchindex_id not in indices : abort ( HTTP_STATUS_CODE_BAD_REQUEST , 'Search index ID ({0!s}) does not belong to the list ' 'of indices' . format ( searchindex_id ) ) # Get or create an event in the SQL database to have something # to attach the annotation to. event = Event . get_or_create ( sketch = sketch , searchindex = searchindex , document_id = event_id ) if current_search_node : current_search_node . events . append ( event ) # Add the annotation to the event object. if 'comment' in annotation_type : annotation = Event . Comment ( comment = form . annotation . data , user = current_user ) event . comments . append ( annotation ) self . datastore . set_label ( searchindex_id , event_id , event_type , sketch . id , current_user . id , '__ts_comment' , toggle = False ) if current_search_node : current_search_node . add_label ( '__ts_comment' ) elif 'label' in annotation_type : annotation = Event . Label . get_or_create ( label = form . annotation . data , user = current_user ) if annotation not in event . labels : event . labels . append ( annotation ) toggle = False if '__ts_star' in form . annotation . data : toggle = True if '__ts_hidden' in form . annotation . data : toggle = True if form . remove . data : toggle = True self . datastore . set_label ( searchindex_id , event_id , event_type , sketch . id , current_user . id , form . annotation . data , toggle = toggle ) if current_search_node : search_node_label = '__ts_label' if '__ts_star' in form . annotation . data : search_node_label = '__ts_star' current_search_node . add_label ( search_node_label ) else : abort ( HTTP_STATUS_CODE_BAD_REQUEST , 'Annotation type needs to be either label or comment, ' 'not {0!s}' . format ( annotation_type ) ) annotations . append ( annotation ) # Save the event to the database db_session . add ( event ) db_session . commit ( ) return self . to_json ( annotations , status_code = HTTP_STATUS_CODE_CREATED )