def get(self): """ Return a list of event and the registration count for each. """ # Get claims from jwt token. claims = get_jwt_claims() # Get all events if super admin sends the request if claims['is_super_admin']: event_names = EventMethods.get_events() else: # Fetch identity from jwt token. id = get_jwt_identity() event_names = EventMethods.get_events(id['username']) if event_names: # Store all event names in a list. event_names_list = [i['event_name'] for i in event_names] # Store a count of registration in each event in a dictionary event_registration_count = RegistrationMethods.get_registration_count_by_event(event_names_list) if event_registration_count: return event_registration_count, 200 return UNKNOWN_ERROR.to_json(), 501 try: # Check if the list is empty or None. if not len(event_names): return NO_EVENT_ERROR.to_json(), 400 except TypeError: return UNKNOWN_ERROR.to_json(), 501
def get(self, registration_number): """ Finds registration details corresponding to id from the database. Parameters: 1. Registration Number(String) Returns: Registration Details """ # Get claims from the jwt token. claims = get_jwt_claims() if claims['is_super_admin']: # Store all events in a list. event_names = EventMethods.get_events() else: # Fetch username from jwt token. username = get_jwt_identity()['username'] # Get a dictionary event names hosted by the admin. event_names = EventMethods.get_events(username) if event_names: # Created a list of all event names. event_name_list = [i["event_name"] for i in event_names] #Find the registration details using the registration_number # and event name list. registration_details = RegistrationMethods.find_by_registration_id( registration_number, event_name_list) if registration_details: return registration_details, 200 if registration_details is None: return NO_REGISTRATIONS_ERROR.to_json(), 400 return UNKNOWN_ERROR.to_json(), 501 return NO_REGISTRATIONS_ERROR.to_json(), 400
def get(self): """ Finds all user registrations from the database Parameters:None Returns: Registrations """ # Get the claims frpm jwt token. claims = get_jwt_claims() if claims['is_super_admin']: # Store all the registrations in a list. all_registrations = RegistrationMethods.find_all_registrations() else: #Get identity fom jwt token. username = get_jwt_identity()['username'] event_names = EventMethods.get_events(username) # Store all the events in a list. event = [i["event_name"] for i in event_names] # Store all the registrations for the events in a list. all_registrations = RegistrationMethods.find_registration_by_event( event) if all_registrations: return all_registrations, 200 try: # Check if the list is empty or None. if not (len(all_registrations)): return NO_REGISTRATIONS_ERROR.to_json(), 400 except TypeError: return UNKNOWN_ERROR.to_json(), 501
def delete(self): """ Deletes an admin and all its corresponding information. Parameters:None Returns:JSON message describing the status of request """ # Parses the request body and stores in a dictionary. data = self.delete_admin_parser.parse_args() # Check if the username provided is not that of the super admin. if data['username'] == parser.get('API', 'ADMIN_NAME'): return INSUFFICIENT_PRIVELEGES_ERROR.to_json(), 401 # Get the claims from jwt_token claims = get_jwt_claims() # Check if the username in the jwt token and the username in the request # body is the same if the request has not been made by the super admin. if not claims["is_super_admin"]: id = get_jwt_identity() jwt_username = id['username'] if jwt_username != data['username']: return INSUFFICIENT_PRIVELEGES_ERROR.to_json(), 401 # Fetch all the events the admin has created. event_details = EventMethods.get_events(data["username"]) if event_details: event_name_list = [i['event_name'] for i in event_details] event_obj = EventMethods(event_name_list, data["username"]) # Delete all the admin events and the registrations for the event. if event_obj.delete_from_db( ) and RegistrationMethods.delete_event_registrations( event_name_list): # Delete the user only if the all the corresponding details are deleted. if AdminMethods.delete_admin(data["username"]): return ADMIN_DELETED.to_json(), 200 else: return UNKNOWN_ERROR.to_json(), 501 # If the admin has no events. else: if AdminMethods.fetch_by_username(data["username"]): if AdminMethods.delete_admin(data["username"]): return ADMIN_DELETED.to_json(), 200 return UNKNOWN_ERROR.to_json(), 501 else: return NO_ADMIN_ERROR.to_json(), 400 return INSUFFICIENT_PRIVELEGES_ERROR.to_json(), 401
def delete(self): """ Deletes event from the database. Parameters:None Returns: JSON message describing the status of request """ # Parse the request body and stores in a dictionary. data = event_parser.parse_args() # Check if there is an ongoing event of the same name. event_data = EventMethods.find_by_event_name(data["event_name"]) if event_data: # Get claims from jwt token. claims = get_jwt_claims() if not claims['is_super_admin']: # Fetch username from jwt token. jwt_id = get_jwt_identity()['username'] # Check if the username in the jwt token and the username in the request # body is the same. if event_data['username'] != jwt_id: return INSUFFICIENT_PRIVELEGES_ERROR.to_json(), 403 data['username'] = jwt_id if claims['is_super_admin']: # Store the username in the parser. data['username'] = event_data['username'] event_obj = EventMethods(**data) event_names_list = [data['event_name']] # Delete the event and all the registrations for that event. if event_obj.delete_from_db() and RegistrationMethods.delete_event_registrations(event_names_list): return EVENT_DELETED.to_json(), 202 return EVENT_DELETE_ERROR.to_json(), 501 # Check if an admin exists, but has no event hosted with provided name. if event_data is None: return NO_EVENT_ERROR.to_json(), 400 return UNKNOWN_ERROR.to_json(), 501
def post(self): """ Adds new event to the database. Parameters:None Returns: JSON message describing the status of request """ # Parse the request body and stores in a dictionary. data = event_parser.parse_args() # Check if there is an ongoing event of the same name. event_data = EventMethods.find_by_event_name(data["event_name"]) if event_data: return ONGOING_EVENT_ERROR.to_json(), 400 # Create new event. if event_data is None: # Get identity fom jwt token and store it in the parser. # Fetch username from identity. data['username'] = get_jwt_identity()['username'] event_obj = EventMethods(**data) if event_obj.save_to_db(): return EVENT_ADDED.to_json(), 201 return EVENT_ADD_ERROR.to_json(), 501 return UNKNOWN_ERROR.to_json(), 501
def get(self): """ Finds all the events from the database hosted by the admin that requests. Returns: List of all events """ # Get the claims frpm jwt token. claims = get_jwt_claims() if claims['is_super_admin']: # Store list of all the events. event_names = EventMethods.get_events() else: # Get identity fom jwt token. id = get_jwt_identity() # Store all the events hosted by the admin in a list. event_names = EventMethods.get_events(id['username']) if event_names: return event_names,200 try: # Check if the list is empty or None. if not len(event_names): return NO_EVENT_ERROR.to_json(), 400 except TypeError: return UNKNOWN_ERROR.to_json(), 501
def get(self): """ Returns all the events from database Parameters:None Return: All events,None, False """ # Fetch a list of all the ongoing events. event_names = EventMethods.get_events() if event_names: return event_names,200 try: # Check if the list is empty or None. if not len(event_names): return NO_EVENT_ERROR.to_json(), 400 except TypeError: return UNKNOWN_ERROR.to_json(), 501