Skip to content

Commit e2cf860

Browse files
authored
Record whether a download came from an instructor or a student (#1776)
1 parent dbae141 commit e2cf860

5 files changed

Lines changed: 65 additions & 2 deletions

File tree

salesforce/management/commands/update_resource_downloads.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def handle(self, *args, **options):
3232
'Book__c': nrd.book.salesforce_abbreviation,
3333
'Book_Format__c': nrd.book_format,
3434
'Source__c': nrd.source,
35+
'Role__c': nrd.role,
3536
'Accounts_UUID__c': str(nrd.account_uuid)}
3637
new_data.append(data_dict_item)
3738

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Generated by Django 6.0.6 on 2026-08-20 10:40
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("salesforce", "0117_resourcedownload_source"),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name="resourcedownload",
15+
name="role",
16+
field=models.CharField(
17+
blank=True, help_text="Whether the reader was an instructor or a student", max_length=20, null=True
18+
),
19+
),
20+
]

salesforce/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,8 @@ class ResourceDownload(models.Model):
351351
# here to show up in the data.
352352
source = models.CharField(max_length=255, null=True, blank=True,
353353
help_text="Path of the page the reader downloaded from")
354+
role = models.CharField(max_length=20, null=True, blank=True,
355+
help_text="Whether the reader was an instructor or a student")
354356

355357
class Meta:
356358
indexes = [

salesforce/serializers.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def create(self, validated_data):
142142
# An empty string and NULL both mean "not provided", and every one of
143143
# these is part of the key - so letting the two differ would file one
144144
# reader's download twice.
145-
for field in ('book_format', 'resource_name', 'source', 'contact_id'):
145+
for field in ('book_format', 'resource_name', 'source', 'contact_id', 'role'):
146146
if validated_data.get(field) == '':
147147
validated_data[field] = None
148148

@@ -158,19 +158,24 @@ def create(self, validated_data):
158158
return ResourceDownload.objects.create(**validated_data)
159159

160160
contact_id = validated_data.get('contact_id')
161+
role = validated_data.get('role')
161162

162163
existing.last_access = validated_data.get('last_access', existing.last_access)
163164
# Signed-in students have no Salesforce contact, so an incoming
164165
# download without one must not erase a contact we already know.
165166
if contact_id:
166167
existing.contact_id = contact_id
168+
# A student who gets verified keeps the same rows; report the role they
169+
# hold now rather than the one they held the first time.
170+
if role:
171+
existing.role = role
167172
existing.save()
168173

169174
return existing
170175

171176
class Meta:
172177
model = ResourceDownload
173-
fields = ('id', 'book', 'book_format', 'account_uuid', 'contact_id', 'last_access', 'resource_name', 'source', 'created')
178+
fields = ('id', 'book', 'book_format', 'account_uuid', 'contact_id', 'last_access', 'resource_name', 'source', 'role', 'created')
174179
read_only_fields = ('id', 'created', 'last_access')
175180

176181

salesforce/tests.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,16 @@ def test_sends_a_student_download_that_has_no_contact(self, salesforce):
275275
self.assertEqual(1, len(sent))
276276
self.assertIsNone(sent[0]['Contact__c'])
277277

278+
@patch('salesforce.management.commands.update_resource_downloads.Salesforce')
279+
def test_sends_the_role_of_the_person_who_downloaded(self, salesforce):
280+
sf = salesforce.return_value.__enter__.return_value
281+
self.make_download('310bb96b-0df8-4d10-a759-c7d366c1f527', role='instructor')
282+
283+
call_command('update_resource_downloads')
284+
285+
sent = sf.bulk.Resource__c.insert.call_args[0][0]
286+
self.assertEqual('instructor', sent[0]['Role__c'])
287+
278288
@patch('salesforce.management.commands.update_resource_downloads.Salesforce')
279289
def test_needs_no_salesforce_query_to_build_the_batch(self, salesforce):
280290
sf = salesforce.return_value.__enter__.return_value
@@ -698,3 +708,28 @@ def test_a_download_without_a_contact_keeps_the_one_already_known(self):
698708
self.post_download(book=book.pk, resource_name="Test Bank")
699709

700710
self.assertEqual("0032f00003zYVdSAAZ", ResourceDownload.objects.get().contact_id)
711+
712+
def test_role_is_stored_as_sent(self):
713+
book = self.make_book()
714+
715+
response = self.post_download(book=book.pk, resource_name="Test Bank", role="instructor")
716+
717+
self.assertEqual("instructor", response.data['role'])
718+
self.assertEqual("instructor", ResourceDownload.objects.get().role)
719+
720+
def test_a_verified_student_keeps_one_row_and_reports_the_role_they_hold_now(self):
721+
book = self.make_book()
722+
723+
self.post_download(book=book.pk, resource_name="Test Bank", role="student")
724+
self.post_download(book=book.pk, resource_name="Test Bank", role="instructor")
725+
726+
self.assertEqual(1, ResourceDownload.objects.count())
727+
self.assertEqual("instructor", ResourceDownload.objects.get().role)
728+
729+
def test_a_download_without_a_role_keeps_the_one_already_known(self):
730+
book = self.make_book()
731+
732+
self.post_download(book=book.pk, resource_name="Test Bank", role="instructor")
733+
self.post_download(book=book.pk, resource_name="Test Bank", role="")
734+
735+
self.assertEqual("instructor", ResourceDownload.objects.get().role)

0 commit comments

Comments
 (0)