Skip to content

Commit

Permalink
feat: create API endpt to reuse importfile for meters
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Lara committed Jun 17, 2021
1 parent dc963c2 commit 7ea4037
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
29 changes: 29 additions & 0 deletions seed/tests/test_import_file_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,35 @@ def test_get_check_for_meters_tab_returns_false_when_not_xlsx(self):
body = json.loads(response.content)
self.assertEqual(body.get('data'), 'false')

def test_post_reuse_inventory_file_for_meters_creates_new_import_file_based_on_the_same_file_and_returns_the_new_id(self):
# create import file record with Meter Entries tab
import_record = ImportRecord.objects.create(owner=self.user, last_modified_by=self.user, super_organization=self.org)
filename = "example-pm-monthly-meter-usage.xlsx"
filepath = os.path.dirname(os.path.abspath(__file__)) + "/data/" + filename

import_file = ImportFile.objects.create(
import_record=import_record,
uploaded_filename=filename,
mapping_done=True,
source_type="Assessed Raw",
file=SimpleUploadedFile(name=filename, content=open(filepath, 'rb').read()),
)

# hit endpoint with record ID
url = reverse_lazy('api:v3:import_files-reuse-inventory-file-for-meters') + '?organization_id=' + str(self.org.id)
response = self.client.post(
url,
data=json.dumps({"import_file_id": import_file.id}),
content_type='application/json'
)

# check that the new and old file reference the same 'file'
newest_import_file = ImportFile.objects.order_by('-id').first()
body = json.loads(response.content)

self.assertEqual(body.get('import_file_id'), newest_import_file.id)
self.assertEqual(import_file.file, newest_import_file.file)


class TestDataImportViewWithCRLF(DataMappingBaseTestCase):
"""Tests for dealing with SEED related tasks for mapping data."""
Expand Down
42 changes: 42 additions & 0 deletions seed/views/v3/import_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,48 @@ def check_meters_tab_exists(self, request, pk=None):
'status': 'success',
'data': 'false'
})

@swagger_auto_schema(
manual_parameters=[
AutoSchemaHelper.query_org_id_field(),
],
request_body=AutoSchemaHelper.schema_factory({
'import_file_id': 'integer'
})
)
@api_endpoint_class
@ajax_request_class
@has_perm_class('requires_member')
@action(detail=False, methods=['POST'])
def reuse_inventory_file_for_meters(self, request):
org_id = self.get_organization(request)
try:
import_file_id = request.data.get('import_file_id', 0)
original_file = ImportFile.objects.get(
id=import_file_id,
import_record__super_organization_id=org_id,
mapping_done=True,
source_type="Assessed Raw"
)
except ImportFile.DoesNotExist:
resp = {
'status': 'error',
'message': 'Could not find previously imported inventory file with pk=' + str(import_file_id)
}
return JsonResponse(resp, status=status.HTTP_400_BAD_REQUEST)

new_file = ImportFile.objects.create(
import_record=original_file.import_record,
file=original_file.file,
uploaded_filename=original_file.uploaded_filename,
source_type="PM Meter Usage",
)

return JsonResponse({
'status': 'success',
'import_file_id': new_file.id
})

@swagger_auto_schema_org_query_param
@api_endpoint_class
@ajax_request_class
Expand Down

0 comments on commit 7ea4037

Please sign in to comment.