Commit da1e7c0a authored by Tanner Prestegard's avatar Tanner Prestegard Committed by GraceDB

Changes to utility for adding a label to a superevent

We use get_or_create now to add labels to a superevent in order
to prevent IntegrityErrors when a user makes the same request
too rapidly and both sneak by the initial check.
parent 33065db7
......@@ -112,7 +112,7 @@ def create_superevent(submitter, t_start, t_0, t_end, preferred_event,
# Add labels
for label in labels:
l = add_label_to_superevent(s, label, submitter,
labelling, _ = add_label_to_superevent(s, label, submitter,
add_log_message=True, issue_alert=issue_alert)
# Create superevent data directory
......@@ -415,20 +415,32 @@ def remove_event_from_superevent(superevent, event, user, add_event_log=True,
def add_label_to_superevent(superevent, label, user, add_log_message=True,
issue_alert=True):
# Create Labelling object
labelling = Labelling.objects.create(label=label, creator=user,
superevent=superevent)
# Create Labelling object - we use get_or_create due to the possibility
# of IntegrityErrors. Sometimes two requests can get through the
# before the first one is actually applied so get_or_create allows
# us to safeguard against that.
labelling, created = Labelling.objects.get_or_create(label=label,
creator=user, superevent=superevent)
# If the label is actually created (i.e. this isn't a secondary request
# that squeaked through), then we optionally create a log message and
# issue an alert. If it's *not* created, then that stuff should have been
# done already on a separate request and we don't need to repeat it here.
# This is not ideal because users will get a response like they created
# the label on that request but I don't think that is a significant
# problem. This should be a rare occurrence and is basically here to
# prevent server errors when users are doing dumb things.
log_for_label_addition = None
if add_log_message:
# Record label addition in superevent logs
comment = 'Added label: {label_name}'.format(label_name=label.name)
log_for_label_addition = create_log(user, comment, superevent,
issue_alert=False, autogenerated=True)
if issue_alert:
SupereventLabelAlertIssuer(labelling, alert_type='label_added') \
.issue_alerts()
if created:
if add_log_message:
# Record label addition in superevent logs
comment = 'Added label: {label_name}'.format(label_name=label.name)
log_for_label_addition = create_log(user, comment, superevent,
issue_alert=False, autogenerated=True)
if issue_alert:
SupereventLabelAlertIssuer(labelling, alert_type='label_added') \
.issue_alerts()
return labelling, log_for_label_addition
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment