Skip to content

Commit

Permalink
fix: separate participants data and answers data in registration-rece…
Browse files Browse the repository at this point in the history
…ipt xlsx download
  • Loading branch information
AmooHashem committed Dec 13, 2024
1 parent 5e8cc37 commit 035440f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
5 changes: 2 additions & 3 deletions apps/report/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from django.urls import path
from .views import get_registration_receipts, get_program_merchandises_purchases, get_answer_sheets
from .views import get_participants, get_program_merchandises_purchases, get_answer_sheets

urlpatterns = [
path('registration-receipts/', get_registration_receipts,
name='registration-receipts-info'),
path('participants/', get_participants, name='participants-info'),
path('answer-sheets/', get_answer_sheets, name='answer-sheets-excel-file'),
path('program-merchandises-purchases/',
get_program_merchandises_purchases, name='program_merchandises_purchases'),
Expand Down
13 changes: 5 additions & 8 deletions apps/report/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from apps.report.utils import extract_content_from_html, gregorian_to_jalali


def _get_registration_receipts_excel_file(form_id):
def _get_participants_excel_file(form_id):
# Fetching data using ORM
receipts = RegistrationReceipt.objects.filter(form_id=form_id).select_related(
'user',
Expand All @@ -31,15 +31,13 @@ def _get_registration_receipts_excel_file(form_id):
"Province",
"Receipt Status",
"Is Participating",
"Answers",
]

# Collect data
data = []
for receipt in receipts:
user = receipt.user
school = user.school_studentship.school if user.school_studentship else None
answers = ", ".join(str(answer) for answer in receipt.answers.all())

data.append({
"ID": receipt.id,
Expand All @@ -52,7 +50,6 @@ def _get_registration_receipts_excel_file(form_id):
"Province": school.province if school else "",
"Receipt Status": receipt.status,
"Is Participating": "Yes" if receipt.is_participating else "No",
"Answers": answers,
})

# Create DataFrame
Expand All @@ -69,7 +66,7 @@ def _get_registration_receipts_excel_file(form_id):

# Create an in-memory file
in_memory_file = SimpleUploadedFile(
f"form_{form_id}_receipts.xlsx", buffer.read(), content_type="application/vnd.ms-excel"
f"form_{form_id}_participants.xlsx", buffer.read(), content_type="application/vnd.ms-excel"
)

# Serialize and save the file
Expand Down Expand Up @@ -252,17 +249,17 @@ def _get_answer_sheets_excel_file_by_form_id(form_id):


@api_view(["get"])
def get_registration_receipts(request):
def get_participants(request):
registration_form_id = request.GET.get('registration_form_id')
file_content = _get_registration_receipts_excel_file(
file_content = _get_participants_excel_file(
form_id=registration_form_id)
return Response(file_content)


@api_view(["get"])
def get_program_merchandises_purchases(request):
program_id = request.GET.get('program_id')
# todo: EHSAN: this function should not get form_id. It should get program_id as input
# todo: this function should not get form_id. It should get program_id as input
file_content = _get_program_merchandises_purchases_file(form_id=program_id)
return Response(file_content)

Expand Down

0 comments on commit 035440f

Please sign in to comment.