Skip to content

Commit

Permalink
Remove extra logging in command
Browse files Browse the repository at this point in the history
  • Loading branch information
aviupadhyayula committed Jan 23, 2025
1 parent 86355ce commit 41b1729
Showing 1 changed file with 9 additions and 48 deletions.
57 changes: 9 additions & 48 deletions backend/clubs/management/commands/merge_duplicate_committees.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import json
import logging
from datetime import datetime

from django.core.management.base import BaseCommand
from django.db import transaction
Expand All @@ -24,7 +22,6 @@ def add_arguments(self, parser):

def handle(self, *args, **options):
dry_run = options["dry_run"]

Check warning on line 24 in backend/clubs/management/commands/merge_duplicate_committees.py

View check run for this annotation

Codecov / codecov/patch

backend/clubs/management/commands/merge_duplicate_committees.py#L24

Added line #L24 was not covered by tests
audit_log = []

try:
with transaction.atomic():

Check warning on line 27 in backend/clubs/management/commands/merge_duplicate_committees.py

View check run for this annotation

Codecov / codecov/patch

backend/clubs/management/commands/merge_duplicate_committees.py#L26-L27

Added lines #L26 - L27 were not covered by tests
Expand All @@ -47,33 +44,14 @@ def handle(self, *args, **options):
if not primary_committee:
continue

Check warning on line 45 in backend/clubs/management/commands/merge_duplicate_committees.py

View check run for this annotation

Codecov / codecov/patch

backend/clubs/management/commands/merge_duplicate_committees.py#L43-L45

Added lines #L43 - L45 were not covered by tests

self.stdout.write(
logger.info(

Check warning on line 47 in backend/clubs/management/commands/merge_duplicate_committees.py

View check run for this annotation

Codecov / codecov/patch

backend/clubs/management/commands/merge_duplicate_committees.py#L47

Added line #L47 was not covered by tests
f"Processing duplicate committees with name '{name}' "
f"in application {application_id}"
)

# Record the merge operation
merge_record = {
"timestamp": datetime.now().isoformat(),
"primary_committee_id": primary_committee.id,
"name": name,
"application_id": application_id,
"merged_committees": [],
}

# Merge all other committees into the primary one
for committee in committees[1:]:
try:
# Record the committee being merged
committee_record = {
"committee_id": committee.id,
"questions": list(
committee.applicationquestion_set.values_list(
"id", flat=True
)
),
}

if not dry_run:

Check warning on line 55 in backend/clubs/management/commands/merge_duplicate_committees.py

View check run for this annotation

Codecov / codecov/patch

backend/clubs/management/commands/merge_duplicate_committees.py#L53-L55

Added lines #L53 - L55 were not covered by tests
# Reassign questions to primary committee
questions_moved = (

Check warning on line 57 in backend/clubs/management/commands/merge_duplicate_committees.py

View check run for this annotation

Codecov / codecov/patch

backend/clubs/management/commands/merge_duplicate_committees.py#L57

Added line #L57 was not covered by tests
Expand All @@ -83,47 +61,30 @@ def handle(self, *args, **options):
)
committee.delete()

Check warning on line 62 in backend/clubs/management/commands/merge_duplicate_committees.py

View check run for this annotation

Codecov / codecov/patch

backend/clubs/management/commands/merge_duplicate_committees.py#L62

Added line #L62 was not covered by tests

self.stdout.write(
logger.info(

Check warning on line 64 in backend/clubs/management/commands/merge_duplicate_committees.py

View check run for this annotation

Codecov / codecov/patch

backend/clubs/management/commands/merge_duplicate_committees.py#L64

Added line #L64 was not covered by tests
f"Moved {questions_moved} questions from committee "
f"{committee.id} to {primary_committee.id}"
)
else:
self.stdout.write(
logger.info(

Check warning on line 69 in backend/clubs/management/commands/merge_duplicate_committees.py

View check run for this annotation

Codecov / codecov/patch

backend/clubs/management/commands/merge_duplicate_committees.py#L69

Added line #L69 was not covered by tests
f"[DRY RUN] Would move questions from committee "
f"{committee.id} to {primary_committee.id}"
)

merge_record["merged_committees"].append(committee_record)

except Exception as e:
self.stdout.write(
self.style.ERROR(
f"Failed to merge committee {committee.id} into "
f"{primary_committee.id}: {str(e)}"
)
logger.error(

Check warning on line 75 in backend/clubs/management/commands/merge_duplicate_committees.py

View check run for this annotation

Codecov / codecov/patch

backend/clubs/management/commands/merge_duplicate_committees.py#L74-L75

Added lines #L74 - L75 were not covered by tests
f"Failed to merge committee {committee.id} into "
f"{primary_committee.id}: {str(e)}"
)
raise

Check warning on line 79 in backend/clubs/management/commands/merge_duplicate_committees.py

View check run for this annotation

Codecov / codecov/patch

backend/clubs/management/commands/merge_duplicate_committees.py#L79

Added line #L79 was not covered by tests

audit_log.append(merge_record)

if dry_run:
self.stdout.write(
self.style.SUCCESS("Dry run completed - rolling back")
)
logger.info("Dry run completed - rolling back")
raise Exception("Dry run completed - rolling back")

Check warning on line 83 in backend/clubs/management/commands/merge_duplicate_committees.py

View check run for this annotation

Codecov / codecov/patch

backend/clubs/management/commands/merge_duplicate_committees.py#L81-L83

Added lines #L81 - L83 were not covered by tests
else:
# Save audit log
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"committee_merge_audit_{timestamp}.json"
with open(filename, "w") as f:
json.dump(audit_log, f, indent=2)
self.stdout.write(
self.style.SUCCESS(
f"Merge completed. Audit log saved to {filename}"
)
)
logger.info("Successfully merged duplicate committees")

Check warning on line 85 in backend/clubs/management/commands/merge_duplicate_committees.py

View check run for this annotation

Codecov / codecov/patch

backend/clubs/management/commands/merge_duplicate_committees.py#L85

Added line #L85 was not covered by tests

except Exception as e:
if not dry_run:
self.stdout.write(self.style.ERROR(f"Failed: {str(e)}"))
logger.error(f"Failed: {str(e)}")
raise

Check warning on line 90 in backend/clubs/management/commands/merge_duplicate_committees.py

View check run for this annotation

Codecov / codecov/patch

backend/clubs/management/commands/merge_duplicate_committees.py#L87-L90

Added lines #L87 - L90 were not covered by tests

0 comments on commit 41b1729

Please sign in to comment.