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

updating api and associated event creation functions to allow events to be created with labels

parent e65f9815
No related branches found
No related tags found
1 merge request!3Allow events to be created with labels
......@@ -12,7 +12,7 @@ import json
from django.contrib.auth.models import User, Permission
from django.contrib.auth.models import Group as AuthGroup
from django.contrib.contenttypes.models import ContentType
from gracedb.models import Event, Group, Search, Pipeline, EventLog, Tag
from gracedb.models import Event, Group, Search, Pipeline, EventLog, Tag, Label
from gracedb.models import EMGroup, EMBBEventLog, EMSPECTRUM
#from gracedb.models import EMObservation, EMFootprint
from gracedb.models import VOEvent
......@@ -508,7 +508,7 @@ class EventList(APIView):
status = status.HTTP_400_BAD_REQUEST)
if not user_has_perm(request.user, "populate", pipeline):
return HttpResponseForbidden("You don't have permission on this pipeline.")
# The following looks a bit funny but it is actually necessary. The
# django form expects a dict containing the POST data as the first
# arg, and a dict containing the FILE data as the second. In the
......@@ -1561,6 +1561,7 @@ class GracedbRoot(APIView):
"groups" : [group.name for group in Group.objects.all()],
"pipelines" : [pipeline.name for pipeline in Pipeline.objects.all()],
"searches" : [search.name for search in Search.objects.all()],
"labels" : [label.name for label in Label.objects.all()],
"em-groups" : [g.name for g in EMGroup.objects.all()],
"wavebands" : dict(EMSPECTRUM),
"eel-statuses" : dict(EMBBEventLog.EEL_STATUS_CHOICES),
......
......@@ -48,11 +48,13 @@ class CreateEventForm(forms.Form):
#typeChoices= [("","")]+list(Event.ANALYSIS_TYPE_CHOICES)
pipelineChoices = [("","")]+[(p.name, p.name) for p in Pipeline.objects.all()]
searchChoices = [("","")]+[(s.name, s.name) for s in Search.objects.all()]
eventFile = forms.FileField()
eventFile = forms.FileField()
group = forms.ChoiceField(groupChoices)
pipeline = forms.ChoiceField(pipelineChoices)
search = forms.ChoiceField(searchChoices, required=False)
# List of labels as a comma-separated string
labels = forms.CharField(required=False)
#type = forms.ChoiceField(choices=typeChoices)
......
......@@ -35,7 +35,7 @@ from django.utils import timezone
import logging
import pytz
logger = logging.getLogger('gracedb.view_logic')
logger = logging.getLogger(__name__)
def _createEventFromForm(request, form):
saved = False
......@@ -44,6 +44,7 @@ def _createEventFromForm(request, form):
group = Group.objects.get(name=form.cleaned_data['group'])
pipeline = Pipeline.objects.get(name=form.cleaned_data['pipeline'])
search_name = form.cleaned_data['search']
label_str = form.cleaned_data['labels']
if search_name:
search = Search.objects.get(name=form.cleaned_data['search'])
else:
......@@ -120,6 +121,40 @@ def _createEventFromForm(request, form):
temp_data_loc, translator_warnings = handle_uploaded_data(event, uploadDestination,
file_contents = file_contents)
warnings += translator_warnings
# Add labels here - need event to have been saved already
for label in label_str.split(","):
# Handle case where no labels are sent (labels=[] in
# gracedb-client), here label_str.split(",") will be [""]
if not label:
break
# Try to get label from database. gracedb-client has a test for
# this, but we should have a safeguard on the server.
try:
label_obj = Label.objects.get(name=label)
except Label.DoesNotExist:
msg = "Label {0} does not exist and was not applied" \
.format(label)
warnings.append(msg)
continue
# If event already has this label, don't do anything.
# Append a warning message.
if label_obj in event.labels.all():
warnings.append("Event {0} already labeled with '{1}'" \
.format(event.graceid(), label))
else:
# Otherwise, create label
labelling = Labelling(event=event, label=label_obj,
creator=event.submitter)
labelling.save()
# Create log message about label
message = "Event created with label: {0}".format(label)
log = EventLog(event=event, issuer=event.submitter,
comment=message)
log.save()
try:
# Send an alert.
# XXX This reverse will give the web-interface URL, not the REST URL.
......
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