Skip to content
Snippets Groups Projects
Commit 005bd722 authored by Tanner Prestegard's avatar Tanner Prestegard Committed by gracedb-dev1
Browse files

updating notification creation interface

parent a8b561f0
No related branches found
No related tags found
No related merge requests found
......@@ -31,8 +31,9 @@ def triggerFormFactory(postdata=None, user=None):
contacts = forms.ModelMultipleChoiceField(
queryset=Contact.objects.filter(user=user),
required=False,
help_text="If blank, go back and create a Contact first.")
required=True,
help_text="If this box is empty, go back and create a contact first.",
error_messages={'required': 'You must specify at least one contact.'})
# XXX should probably override is_valid and check for
# truth of (atypes or labels)
......@@ -41,12 +42,21 @@ def triggerFormFactory(postdata=None, user=None):
def clean(self):
cleaned_data = super(TF, self).clean()
label_query = self.cleaned_data['label_query']
if (self.cleaned_data['label_query'] and
self.cleaned_data['labels']):
raise forms.ValidationError('Cannot specify labels and label query, choose one or the other.')
if not (self.cleaned_data['labels'] or
self.cleaned_data['pipelines']):
raise forms.ValidationError('Choose labels and/or pipelines for this notification.')
if len(label_query) > 0:
# now try parsing it
try:
parseLabelQuery(label_query)
except ParseException:
raise forms.ValidationError("Invalid label query.")
raise forms.ValidationError({'label_query': 'Invalid label query.'})
return cleaned_data
if postdata is not None:
......
......@@ -6,6 +6,8 @@ from gracedb.models import Label, Pipeline
from django.core.exceptions import ValidationError
from django.contrib.auth.models import User
import phonenumbers
import logging
log = logging.getLogger(__name__)
def validate_phone(value):
try:
......@@ -81,14 +83,14 @@ class Contact(models.Model):
self.call_phone, self.text_phone))
class Trigger(models.Model):
# TP 6 Jul 2017: TYPES and triggerType don't seem to be used anywhere...
TYPES = ( ("create", "create"), ("change","change"), ("label","label") )
user = models.ForeignKey(User, null=False)
#new_user = models.ForeignKey(DjangoUser, null=True)
triggerType = models.CharField(max_length=20, choices=TYPES, blank=True)
user = models.ForeignKey(User, null=False)
labels = models.ManyToManyField(Label, blank=True)
#atypes = models.ManyToManyField(AnalysisType, blank=True, verbose_name="Analysis Types")
pipelines = models.ManyToManyField(Pipeline, blank=True)
contacts = models.ManyToManyField(Contact, blank=True)
contacts = models.ManyToManyField(Contact, blank=False)
farThresh = models.FloatField(blank=True, null=True)
label_query = models.CharField(max_length=100, blank=True)
......
......@@ -19,7 +19,7 @@ import logging
log = logging.getLogger(__name__)
from .models import Trigger, Contact
from .forms import ContactForm, triggerFormFactory
from .forms import ContactForm, triggerFormFactory, TriggerForm
from gracedb.permission_utils import internal_user_required, lvem_user_required
from gracedb.query import labelQuery
from gracedb.models import Label
......@@ -86,15 +86,9 @@ def create(request):
farThresh = form.cleaned_data['farThresh']
label_query = form.cleaned_data['label_query']
if len(label_query) > 0 and labels.count() > 0:
msg = "Cannot both select labels and define label query. Choose one or the other."
return HttpResponseBadRequest(msg)
# If we've got a label query defined for this trigger, then we want
# each label mentioned in the query to be listed in the events labels.
# It would be smarter to make sure the label isn't being negated, but
# we can just leave that for later.
if len(label_query) > 0:
toks = labelQuery(label_query, names=True)
......@@ -121,15 +115,12 @@ def create(request):
request.session['flash_msg'] = "Created: %s" % t.userlessDisplay()
return HttpResponseRedirect(reverse(index))
# Data was bad
try:
if not contacts:
message += "You must specify at least one contact. "
if not (labels or pipelines):
message += "You need to indicate label(s) and/or pipeline(s)."
except NameError:
# form is not valid, so labels, contacts and pipelines were not set.
# hopefully, there are error messages in the form.
pass
else:
# Get non-field errors and display them in the message box.
# Remove them from the form so they don't display in the table too.
while form.errors['__all__']:
message += form.errors['__all__'].pop().message
else:
form = triggerFormFactory(user=request.user)
if message:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment