Skip to content

Commit

Permalink
Merge pull request #778 from kids-first/template_events
Browse files Browse the repository at this point in the history
✨ Add template download events
  • Loading branch information
gsantia authored Oct 21, 2021
2 parents 22322e8 + 4039ac1 commit b6498ab
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion creator/data_templates/packaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,4 @@ def template_package(
filepath_or_buffer=filepath_or_buffer,
)

return path_or_buf
return path_or_buf, template_versions
14 changes: 13 additions & 1 deletion creator/data_templates/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from creator.data_templates.packaging import template_package
from creator.data_templates.models import TemplateVersion
from creator.events.models import Event
from creator.studies.models import Study

EXCEL_FORMAT = "excel"
Expand Down Expand Up @@ -49,7 +50,7 @@ def download_templates(request, study_kf_id=None):
# Check if studies and template versions exist
stream = BytesIO()
try:
stream = template_package(
stream, template_versions = template_package(
study_kf_id=study_kf_id,
filepath_or_buffer=stream,
template_version_ids=tv_ids,
Expand All @@ -76,4 +77,15 @@ def download_templates(request, study_kf_id=None):
response["Content-Type"] = mime_type
stream.close()

# Fire a template download event for each template
username = getattr(user, "display_name", "Anonymous user")
tv_ids = ",".join(tv.pk for tv in template_versions)
message = f"{username} downloaded template_versions: [{tv_ids}]"
event = Event(
user=user,
organization=template_versions[0].data_template.organization,
description=message,
event_type="TV_DOW",
)
event.save()
return response
19 changes: 19 additions & 0 deletions creator/events/migrations/0021_add_template_download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 2.2.24 on 2021-10-19 15:35

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('events', '0020_add_template_events'),
]

operations = [
migrations.AlterField(
model_name='event',
name='event_type',
field=models.CharField(choices=[('TV_CRE', 'Template Version Created'), ('TV_UPD', 'Template Version Updated'), ('TV_DEL', 'Template Version Deleted'), ('TV_DOW', 'Template Version Downloaded'), ('DT_CRE', 'Data Template Created'), ('DT_UPD', 'Data Template Updated'), ('DT_DEL', 'Data Template Deleted'), ('VR_INI', 'Validation Run Initializing'), ('VR_STA', 'Validation Run Started'), ('VR_CLG', 'Validation Run Canceling'), ('VR_CAN', 'Validation Run Canceled'), ('VR_COM', 'Validation Run Completed'), ('VR_FAI', 'Validation Run Failed'), ('IR_INI', 'Ingest Run Initializing'), ('IR_STA', 'Ingest Run Started'), ('IR_CAN', 'Ingest Run Canceled'), ('IR_CLG', 'Ingest Run Canceling'), ('IR_COM', 'Ingest Run Completed'), ('IR_FAI', 'Ingest Run Failed'), ('SF_CRE', 'Study File Created'), ('SF_UPD', 'Study File Updated'), ('SF_DEL', 'Study File Deleted'), ('FV_CRE', 'File Version Created'), ('FV_UPD', 'File Version Updated'), ('SD_CRE', 'Study Created'), ('SD_UPD', 'Study Updated'), ('PR_CRE', 'Project Created'), ('PR_UPD', 'Project Updated'), ('PR_DEL', 'Project Deleted'), ('PR_LIN', 'Project Linked'), ('PR_UNL', 'Project Unlinked'), ('PR_STR', 'Project Creation Start'), ('PR_ERR', 'Project Creation Error'), ('PR_SUC', 'Project Creation Success'), ('BK_STR', 'Bucket Creation Start'), ('BK_ERR', 'Bucket Creation Error'), ('BK_SUC', 'Bucket Creation Success'), ('BK_LIN', 'Bucket Linked'), ('BK_UNL', 'Bucket Unlinked'), ('IM_STR', 'File Import Start'), ('IM_ERR', 'File Import Error'), ('IM_SUC', 'File Import Success'), ('CB_ADD', 'Collaborator Added'), ('CB_UPD', 'Collaborator Updated'), ('CB_REM', 'Collaborator Removed'), ('MB_ADD', 'Organization Member Added'), ('MB_REM', 'Organization Member Removed'), ('IN_UPD', 'Ingestion Status Updated'), ('PH_UPD', 'Phenotype Status Updated'), ('ST_UPD', 'Sequencing Status Updated'), ('OR_UPD', 'Study Organization Updated'), ('RT_CRE', 'Referral Token Created'), ('RT_CLA', 'Referral Token Claimed'), ('SL_STR', 'Slack Channel Creation Start'), ('SL_ERR', 'Slack Channel Creation Error'), ('SL_SUC', 'Slack Channel Creation Success'), ('DR_STA', 'Data Review Started'), ('DR_WAI', 'Data Review Waiting for Updates'), ('DR_UPD', 'Data Review Updated'), ('DR_APP', 'Data Review Approved'), ('DR_CLO', 'Data Review Closed'), ('DR_REO', 'Data Review Re-opened'), ('OTH', 'Other')], default='OTH', max_length=6),
),
]
1 change: 1 addition & 0 deletions creator/events/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Meta:
("TV_CRE", "Template Version Created"),
("TV_UPD", "Template Version Updated"),
("TV_DEL", "Template Version Deleted"),
("TV_DOW", "Template Version Downloaded"),
("DT_CRE", "Data Template Created"),
("DT_UPD", "Data Template Updated"),
("DT_DEL", "Data Template Deleted"),
Expand Down
14 changes: 14 additions & 0 deletions tests/data_templates/test_download_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pandas

from creator.studies.factories import StudyFactory, Study
from creator.events.models import Event
from creator.data_templates.factories import (
DataTemplateFactory,
TemplateVersionFactory,
Expand Down Expand Up @@ -91,6 +92,12 @@ def test_download_templates_study(
)
assert resp.get("Content-Length")
assert pandas.read_excel(BytesIO(resp.content), sheet_name=None)

# Check appropriate event fired
template_events = Event.objects.filter(event_type="TV_DOW").all()
assert len(template_events) == 1
for tv in study.template_versions.all():
assert tv.pk in template_events[0].description
else:
assert resp.status_code == 401

Expand Down Expand Up @@ -125,6 +132,12 @@ def test_download_templates_no_study(
)
assert resp.get("Content-Length")
assert pandas.read_excel(BytesIO(resp.content), sheet_name=None)

# Check appropriate events fired
template_events = Event.objects.filter(event_type="TV_DOW").all()
assert len(template_events) == 1
for tv in template_versions_mult_studies:
assert tv.pk in template_events[0].description
else:
assert resp.status_code == 401

Expand Down Expand Up @@ -153,6 +166,7 @@ def test_download_templates_query_params(
"""
mock_study_pkg = mocker.patch(
"creator.data_templates.views.template_package",
return_value=(BytesIO(), template_versions),
)
client = clients.get("Administrators")

Expand Down

0 comments on commit b6498ab

Please sign in to comment.