Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions chalicelib/api/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def get_timeframe(timeframe_id: str):
def delete_timeframe(timeframe_id: str):
return event_service.delete_timeframe(timeframe_id)


@events_api.route("/timeframes/{timeframe_id}/events", methods=["POST"], cors=True)
@auth(events_api, roles=["admin"])
def create_event(timeframe_id: str):
Expand Down Expand Up @@ -65,3 +66,33 @@ def update_event(event_id: str):
@auth(events_api, roles=["admin"])
def delete_event(event_id: str):
return event_service.delete(event_id)


@events_api.route("/events/rush", methods=["GET"], cors=True)
@auth(events_api, roles=["admin"])
def get_rush_events():
return event_service.get_rush_categories_and_events()

@events_api.route("/events/rush/{event_id}", methods=["GET"], cors=True)
def get_rush_event(event_id):
return event_service.get_rush_event(event_id)


@events_api.route("/events/rush/category", methods=["POST"], cors=True)
@auth(events_api, roles=["admin"])
def create_rush_category():
data = events_api.current_request.json_body
return event_service.create_rush_category(data)


@events_api.route("/events/rush", methods=["POST"], cors=True)
@auth(events_api, roles=["admin"])
def create_rush_event():
data = events_api.current_request.json_body
return event_service.create_rush_event(data)


@events_api.route("/events/rush/checkin/{event_id}", methods=["POST"], cors=True)
def checkin_rush(event_id):
data = events_api.current_request.json_body
return event_service.checkin_rush(event_id, data)
1 change: 0 additions & 1 deletion chalicelib/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ def wrapper(*args, **kwargs):
Name="/Zap/AUTH_SECRET", WithDecryption=True
)["Parameter"]["Value"]
decoded = jwt.decode(token, auth_secret, algorithms=["HS256"])
print(roles)
# TODO: if decoded role is not part of given, reject auth

return func(*args, **kwargs)
Expand Down
8 changes: 6 additions & 2 deletions chalicelib/modules/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,23 @@ def connect(self):
self.vault = self.client.vault

@add_env_suffix
def insert_document(self, collection: str, data: dict) -> None:
def insert_document(self, collection: str, data: dict) -> ObjectId:
"""
Inserts a document into the specified collection.

Args:
collection (str): The name of the collection to insert the document into.
data (dict): The document to insert into the collection.

Returns:
ObjectId: The ID of the inserted document.

Raises:
Exception: If an error occurs while inserting the document.
"""
try:
self.mongo_client.vault[collection].insert_one(data)
result = self.mongo_client.vault[collection].insert_one(data)
return result.inserted_id
except Exception as e:
print(e)

Expand Down
78 changes: 76 additions & 2 deletions chalicelib/services/EventService.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from chalicelib.modules.mongo import mongo_module
from chalice import NotFoundError, BadRequestError
from chalice import NotFoundError, BadRequestError, UnauthorizedError
import json
from bson import ObjectId
import datetime
Expand Down Expand Up @@ -160,7 +160,7 @@ def checkin(self, event_id: str, user: dict) -> dict:
sheet_name=event["sheetTab"],
cols=["A", "B"],
name_to_match=user_name,
use_similarity=True
use_similarity=True,
)

if row_num == -1:
Expand Down Expand Up @@ -217,5 +217,79 @@ def get_timeframe_sheets(self, timeframe_id: str):
sheets = gs.get_sheets(timeframe["spreadsheetId"], include_properties=False)
return [sheet["title"] for sheet in sheets]

def get_rush_categories_and_events(self):
rush_categories = mongo_module.get_all_data_from_collection(
f"{self.collection_prefix}rush"
)

return json.dumps(rush_categories, cls=self.BSONEncoder)

def create_rush_category(self, data: dict):
data["dateCreated"] = datetime.datetime.now()
data["events"] = []
return mongo_module.insert_document(f"{self.collection_prefix}rush", data)

def create_rush_event(self, data: dict):
data["dateCreated"] = datetime.datetime.now()
data_copy = data.copy()
data_copy.pop("categoryId", None)

# Add event to its own collection
data["attendees"] = []
data["numAttendees"] = 0
event_id = mongo_module.insert_document(
f"{self.collection_prefix}rush-event", data
)

data_copy["eventId"] = str(event_id)

# Add event to rush category
mongo_module.update_document(
f"{self.collection_prefix}rush",
data["categoryId"],
{"$push": {"events": data_copy}},
)

return

def get_rush_event(self, event_id: str, hide_attendees: bool = True):
event = mongo_module.get_document_by_id(
f"{self.collection_prefix}rush-event", event_id
)

if hide_attendees:
event.pop("attendees", None)
event.pop("numAttendees", None)

event.pop("code")

return json.dumps(event, cls=self.BSONEncoder)

def checkin_rush(self, event_id: str, user_data: dict):
event = mongo_module.get_document_by_id(
f"{self.collection_prefix}rush-event", event_id
)

code = user_data["code"]
user_data.pop("code")

if code != event["code"]:
raise UnauthorizedError("Invalid code.")

if any(d["email"] == user_data["email"] for d in event["attendees"]):
raise BadRequestError("User has already checked in.")

user_data["checkinTime"] = datetime.datetime.now()
event["attendees"].append(user_data)
event["numAttendees"] += 1

mongo_module.update_document(
f"{self.collection_prefix}rush-event",
event_id,
{"$set": event},
)

return


event_service = EventService()