Skip to content

Commit

Permalink
Merge pull request #40 from Clubs-Council-IIITH/drafts-include
Browse files Browse the repository at this point in the history
included status added all events option
  • Loading branch information
bhavberi authored Jul 11, 2024
2 parents f3a6ae8 + ad87048 commit 2c61972
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 30 deletions.
19 changes: 9 additions & 10 deletions mtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ class Event_State_Status(StrEnum):
# if the event is deleted, its state is `deleted`
deleted = auto()


# @strawberry.enum
# class Event_Room_Status (Enum) :
# unapproved = auto()
# approved = auto()
# @strawberry.enum
# class Event_Budget_Status (Enum) :
# unapproved = auto()
# approved = auto()
# Event Full State Names
class Event_Full_State_Status:
incomplete = "Incomplete"
pending_cc = "Pending CC Approval"
pending_budget = "Pending Budget Approval"
pending_room = "Pending Room Approval"
approved = "Approved"
deleted = "Deleted"


@strawberry.type
Expand Down Expand Up @@ -104,7 +103,7 @@ class Event_Mode(StrEnum):
offline = auto()


# Event Full Locations
# Event Full Location Names
class Event_Full_Location:
h101 = "Himalaya 101"
h102 = "Himalaya 102"
Expand Down
3 changes: 2 additions & 1 deletion otypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ class InputEditEventDetails:

@strawberry.input
class InputDataReportDetails:
allEvents: bool | None
clubid: str | None
fields: List[str]
dateperiod: List[date] | None = None
fields: List[str]


@strawberry.type
Expand Down
75 changes: 56 additions & 19 deletions queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@

# import all models and types
from models import Event
from mtypes import Event_Full_Location, Event_Location, Event_State_Status
from mtypes import (
Event_Full_Location,
Event_Full_State_Status,
Event_Location,
Event_State_Status,
)
from otypes import (
CSVResponse,
EventType,
Expand Down Expand Up @@ -322,10 +327,12 @@ def downloadEventsData(
given details in the given date period.
"""
user = info.context.user
all_events = list()
include_status = user["role"] in ["cc", "slo"] and details.allEvents

all_events = list()
allclubs = getClubs(info.context.cookies)
searchspace = dict()

if details.clubid:
clubid = details.clubid
if details.clubid == "allclubs":
Expand All @@ -350,12 +357,19 @@ def downloadEventsData(
"$lte": datetime_end,
}

# include only approved events
searchspace["status.state"] = {
"$in": [
Event_State_Status.approved.value,
]
}
if not include_status:
# include only approved events
searchspace["status.state"] = {
"$in": [
Event_State_Status.approved.value,
]
}
else:
searchspace["status.state"] = {
"$nin": [
Event_State_Status.deleted.value,
]
}

all_events = eventsWithSorting(searchspace, date_filter=True)

Expand All @@ -372,21 +386,30 @@ def downloadEventsData(
"location": "Venue",
"budget": "Budget",
"poster": "Poster URL",
"status": "Status",
}

# Prepare CSV content
csv_output = io.StringIO()
fieldnames = [
header_mapping.get(field.lower(), field) for field in details.fields
header_mapping.get(field.lower(), field)
for field in details.fields
if field != "status"
]

if include_status:
fieldnames.append(header_mapping["status"])

csv_writer = csv.DictWriter(csv_output, fieldnames=fieldnames)
csv_writer.writeheader()

for event in all_events:
event_data = {}
for field in details.fields:
mapped_field = header_mapping.get(field, field)
if mapped_field not in fieldnames:
continue

value = event.get(field)

if field in ["datetimeperiod.0", "datetimeperiod.1"]:
Expand All @@ -396,10 +419,8 @@ def downloadEventsData(
if field == "datetimeperiod.0"
else value[1].split("T")[0]
)
if value in [None, "", []]:
event_data[mapped_field] = "No " + mapped_field
elif field == "clubid":
event_data[mapped_field] = next(
value = next(
(
club["name"]
for club in allclubs
Expand All @@ -408,19 +429,35 @@ def downloadEventsData(
"",
)
elif field == "location":
if value == []:
event_data[mapped_field] = "No location"
else:
event_data[mapped_field] = ", ".join(
value = event.get(field, [])
if len(value) >= 1:
value = ", ".join(
getattr(Event_Full_Location, loc) for loc in value
)
event_data[mapped_field] = f"{event_data[mapped_field]}"
else:
event_data[mapped_field] = value
elif field == "budget":
if isinstance(value, list):
budget_items = [
f"{item['description']} {'(Advance)' if item['advance'] else ''}: {item['amount']}"
for item in value
]
value = ", ".join(budget_items)
elif field == "status":
status_value = event.get(field, {})
value = status_value.get("state", None)

if value:
value = getattr(Event_Full_State_Status, value)

if value in [None, "", []]:
value = "No " + mapped_field

event_data[mapped_field] = value

csv_writer.writerow(event_data)

csv_content = csv_output.getvalue()
csv_output.close()

return CSVResponse(
csvFile=csv_content,
successMessage="CSV file generated successfully",
Expand Down

0 comments on commit 2c61972

Please sign in to comment.