Skip to content

Commit 2b5bb1c

Browse files
authored
Merge pull request #47 from cfpb/collect-view
Improve Collections implementation
2 parents b63dc7f + bc93def commit 2b5bb1c

6 files changed

Lines changed: 30 additions & 13 deletions

File tree

django/parse_m2/admin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def has_delete_permission(self, request, obj=None):
6969
class M2DataFileAdmin(admin.ModelAdmin):
7070
list_display = ['id', 'event', 'file_name', 'parsing_status',
7171
'parsed_lines', 'unparseable_lines', 'timestamp',
72-
'error_message', 'parser_version']
72+
'error_message', 'collection', 'parser_version']
7373

7474
def parsed_lines(self, obj):
7575
return obj.accountactivity_set.count()

django/parse_m2/m2_parser.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@
1515
class M2FileParser():
1616
# Parser version is saved on each file record.
1717
# Increment this version for all updates to parser functionality.
18-
parser_version = "3.0"
18+
parser_version = "3.1"
1919

2020
chunk_size = 2000 # TODO: determine a good number for this
2121
any_non_whitespace = r'\S'
2222

23-
collection = None
2423
activity_date = None
2524

2625
def __init__(self, event: Metro2Event, filepath: str, collection: str = None) -> None:
@@ -34,13 +33,12 @@ def __init__(self, event: Metro2Event, filepath: str, collection: str = None) ->
3433
file_name=filepath,
3534
parsing_status="In progress",
3635
parser_version=self.parser_version,
36+
# Collection, if present, will be prepended to consumer account number
37+
# for all records in this file
38+
collection=collection,
3739
)
3840
self.file_record.save()
3941

40-
# Collection, if present, will be prepended to consumer account number
41-
# for all records in this file
42-
self.collection = collection
43-
4442
def update_file_record(self, status=None, msg=None) -> None:
4543
"""
4644
Update the file record with the given status and error message.
@@ -190,7 +188,7 @@ def parse_line(self, line: str) -> dict:
190188

191189
# parse the base segment into AccountActivity
192190
acct_activity = AccountActivity.parse_from_segment(
193-
line, self.file_record, self.activity_date, self.collection)
191+
line, self.file_record, self.activity_date)
194192
parsed["AccountActivity"] = acct_activity
195193

196194
# parse the extra segments
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 4.2.29 on 2026-03-24 20:01
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('parse_m2', '0001_initial'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='m2datafile',
15+
name='collection',
16+
field=models.CharField(blank=True, max_length=50, null=True),
17+
),
18+
]

django/parse_m2/models.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class Meta:
6464
error_message = models.CharField(max_length=2000, blank=True)
6565
parser_version = models.CharField(max_length=200, blank=True)
6666
activity_date = models.DateField(null=True)
67+
collection = models.CharField(max_length=50, blank=True, null=True)
6768

6869
def __str__(self) -> str:
6970
return self.file_name
@@ -155,7 +156,7 @@ def __str__(self) -> str:
155156

156157

157158
@classmethod
158-
def parse_from_segment(cls, base_seg: str, m2_data_file: M2DataFile, activity_date: date, collection: str = None):
159+
def parse_from_segment(cls, base_seg: str, m2_data_file: M2DataFile, activity_date: date):
159160
# Construct the php1 field from php
160161
php = get_field_value(fields.base_fields, "php", base_seg)
161162
php1 = php[0] if php else ""
@@ -167,8 +168,8 @@ def parse_from_segment(cls, base_seg: str, m2_data_file: M2DataFile, activity_da
167168

168169
# If 'collection' is provided, prepend to account number
169170
account_num = get_field_value(fields.base_fields, "cons_acct_num", base_seg)
170-
if collection:
171-
account_num = f"{collection}{account_num}"
171+
if m2_data_file.collection:
172+
account_num = f"{m2_data_file.collection}.{account_num}"
172173

173174
return cls(
174175
data_file = m2_data_file,

django/parse_m2/tests/test_initiate_parsing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def test_open_zipfiles(self):
5454
self.assertEqual(AccountActivity.objects.count(), 1997)
5555

5656
def test_prepend_collection_onto_account_num(self):
57-
parse_files_from_local_filesystem(self.event, collection="HEALTH.")
57+
parse_files_from_local_filesystem(self.event, collection="HEALTH")
5858

5959
# one M2DataFile object for each file
6060
self.assertEqual(M2DataFile.objects.count(), 3)

django/parse_m2/tests/test_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ def test_parser_saves_header_as_unparseable(self):
265265
# Tests for prepending a collection name to the account number
266266
def test_collection_prefix_on_account_number(self):
267267
event = Metro2Event.objects.create(name='exam_with_collections')
268-
parser = M2FileParser(event=event, filepath="file.txt", collection="HEALTH.")
268+
parser = M2FileParser(event=event, filepath="file.txt", collection="HEALTH")
269269

270270
file_size = os.path.getsize(self.tiny_file)
271271
with open(self.tiny_file, mode='r') as filestream:

0 commit comments

Comments
 (0)