Open
Conversation
sunilshetye
requested changes
Jul 7, 2025
Contributor
sunilshetye
left a comment
There was a problem hiding this comment.
added comments. initialise reader and loop only once. build cache if required. do not use bulk query.
Comment on lines
+701
to
+702
| decoded_file = csv_file.read().decode('utf-8') | ||
| reader = csv.DictReader(io.StringIO(decoded_file)) |
Contributor
There was a problem hiding this comment.
Suggested change
| decoded_file = csv_file.read().decode('utf-8') | |
| reader = csv.DictReader(io.StringIO(decoded_file)) | |
| decoded_file = io.TextIOWrapper(csv_file, encoding='utf-8') | |
| reader = csv.DictReader(decoded_file) |
This will still read the entire file in memory. Use something like the one suggested.
Comment on lines
+703
to
+706
| row_count = sum(1 for _ in reader) | ||
| if row_count > MAX_ROWS: | ||
| messages.error(request, f"CSV has too many rows ({row_count}). Limit is {MAX_ROWS}.") | ||
| return redirect('events:new_ac') |
Contributor
There was a problem hiding this comment.
Remove the row count check here as this is still going to read the entire file.
| messages.error(request, f"CSV columns do not match the expected format. Expected columns: {', '.join(EXPECTED_COLUMNS)}") | ||
| return redirect('events:new_ac') | ||
|
|
||
| reader = csv.DictReader(io.StringIO(decoded_file)) |
Contributor
There was a problem hiding this comment.
Don't initialise reader again.
Comment on lines
+730
to
+742
| for idx, row in enumerate(reader, start=2): # Start from 2nd row | ||
| _states.add(row.get('state').strip()) | ||
| _universities.add(row.get('university').strip()) | ||
| _institution_types.add(row.get('institution_type').strip()) | ||
| _districts.add(row.get('district').strip()) | ||
| _cities.add(row.get('city').strip()) | ||
|
|
||
| # Bulk query | ||
| states = { s.name.lower(): s for s in State.objects.filter(name__in=_states)} | ||
| universities = { (u.name.lower(), u.state.name.lower()): u for u in University.objects.filter(name__in=_universities).select_related('state')} | ||
| districts = { (d.name.lower(), d.state.name.lower()): d for d in District.objects.filter(name__in=_districts).select_related('state')} | ||
| cities = { (c.name.lower(), c.state.name.lower()): c for c in City.objects.filter(name__in=_cities).select_related('state')} | ||
| institution_types = { i.name.lower(): i for i in InstituteType.objects.filter(name__in=_institution_types)} |
Contributor
There was a problem hiding this comment.
this bulk query is also incorrect. you have to loop only once. you may build the cache in that loop.
| return 1 if val == 'yes' else 0 | ||
| else: | ||
| return 0 | ||
| reader = csv.DictReader(io.StringIO(decoded_file)) |
Contributor
There was a problem hiding this comment.
don't initialise the reader again
| else: | ||
| return 0 | ||
| reader = csv.DictReader(io.StringIO(decoded_file)) | ||
| for idx, row in enumerate(reader, start=2): # Start from 2nd row |
Contributor
There was a problem hiding this comment.
don't specify the start parameter.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.