Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[annotations] Improves annotation validation, start_dttm, end_dttm #7326

Merged
merged 1 commit into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: capitalize the a

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The final user message is: "Not a valid datetime value 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