Skip to content

Commit

Permalink
[annotations] Improves UX on annotation validation, start_dttm, end_d…
Browse files Browse the repository at this point in the history
…ttm (#7326)
  • Loading branch information
dpgaspar authored and mistercrunch committed Apr 30, 2019
1 parent 06c4610 commit f504568
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion superset/models/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Annotation(Model, AuditMixinNullable):
id = Column(Integer, primary_key=True)
start_dttm = Column(DateTime)
end_dttm = Column(DateTime)
layer_id = Column(Integer, ForeignKey('annotation_layer.id'))
layer_id = Column(Integer, ForeignKey('annotation_layer.id'), nullable=False)
short_descr = Column(String(500))
long_descr = Column(Text)
layer = relationship(
Expand Down
32 changes: 25 additions & 7 deletions superset/views/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,30 @@
from flask_appbuilder.models.sqla.interface import SQLAInterface
from flask_babel import gettext as __
from flask_babel import lazy_gettext as _
from wtforms.validators import StopValidation

from superset import appbuilder
from superset.models.annotations import Annotation, AnnotationLayer
from .base import DeleteMixin, SupersetModelView


class StartEndDttmValidator(object):
"""
Validates dttm fields.
"""
def __call__(self, form, field):
if not form['start_dttm'].data and not form['end_dttm'].data:
raise StopValidation(
_('annotation start time or end time is required.'),
)
elif (form['end_dttm'].data and
form['start_dttm'].data and
form['end_dttm'].data < form['start_dttm'].data):
raise StopValidation(
_('Annotation end time must be no earlier than start time.'),
)


class AnnotationModelView(SupersetModelView, DeleteMixin): # noqa
datamodel = SQLAInterface(Annotation)

Expand Down Expand Up @@ -53,17 +71,17 @@ class AnnotationModelView(SupersetModelView, DeleteMixin): # noqa
annotation needs to add more context.',
}

validators_columns = {
'start_dttm': [
StartEndDttmValidator(),
],
}

def pre_add(self, obj):
if not obj.layer:
raise Exception('Annotation layer is required.')
if not obj.start_dttm and not obj.end_dttm:
raise Exception('Annotation start time or end time is required.')
elif not obj.start_dttm:
if not obj.start_dttm:
obj.start_dttm = obj.end_dttm
elif not obj.end_dttm:
obj.end_dttm = obj.start_dttm
elif obj.end_dttm < obj.start_dttm:
raise Exception('Annotation end time must be no earlier than start time.')

def pre_update(self, obj):
self.pre_add(obj)
Expand Down

0 comments on commit f504568

Please sign in to comment.