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 post(self): """ Add new registration to the database. Return:Request Status """ # Parse the request body and stores in a dictionary. data = self.registration_parser.parse_args() # Check if a registration with same email for an event has been created before. registration_details = RegistrationMethods.validate_registration( data['email'], data['event_name']) if registration_details is not None: return REGISTRATION_EXISTS_ERROR.to_json(), 400 elif registration_details is None: # Creating registration number. data['registration_number'] = uuid4().hex registration_obj = RegistrationMethods(**data) if registration_obj.save_to_db(): return { "registration_number": data['registration_number'] }, 201 else: return UNKNOWN_ERROR.to_json(), 501 else: return UNKNOWN_ERROR.to_json(), 501
def post(self): """ Logs in the adminstrator. Returns: JSON message describing the status of request """ # Parses the request body and stores it in a dictionary. data = admin_parser.parse_args() # check if the admin with the provides username exists or not. admin_details = AdminMethods.fetch_by_username(data['username']) if admin_details is None: return REGISTER_ADMIN_ERROR.to_json(), 400 if admin_details is False: return UNKNOWN_ERROR.to_json(), 501 # Verify if admin exists and the pasword is correct. if admin_details and sha256_crypt.verify(data["password"], admin_details["password"]): # Create an identity for the user that will be stored in the jwt token identity = { "uuid": admin_details['uuid'], "username": data['username'] } # JWT access and refresh token creation using the above created identity. access_token = create_access_token(identity=identity, fresh=True) refresh_token = create_refresh_token(identity) return { "access_token": access_token, "refresh_token": refresh_token, "admin_name": data['username'], "expires_in": int(parser.get('API', 'EXPIRE_TIME')), "registered_user": True }, 200 return INVALID_CREDENTIALS_ERROR.to_json(), 400
def post(self): """ Changes password for the adminstrator. Parameters: Username,New password Returns: JSON message describing the status of request """ # Parse the request body and stores in a dictionary. data = self.admin_changepwd_parser.parse_args() admin_details = AdminMethods.fetch_by_username(data['username']) # Get the username from identity of the jwt token. jwt_username = get_jwt_identity()['username'] if admin_details is None: return REGISTER_ADMIN_ERROR.to_json(), 400 # Check if the username in the jwt token and the username in the request # body is the same. if admin_details['username'] != jwt_username: return INSUFFICIENT_PRIVELEGES_ERROR.to_json(), 403 # Verify if admin exists and the old password is correct. if admin_details and sha256_crypt.verify(data["old_password"], admin_details["password"]): admin_obj = AdminMethods(data['username'], data['new_password']) if admin_obj.change_pwd(): return PASSWORD_CHANGED.to_json(), 201 else: return CHANGE_PASSWORD_ERROR.to_json(), 501 else: return INCORRECT_OLD_PASSWORD_ERROR.to_json(), 400 return UNKNOWN_ERROR.to_json(), 501
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 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 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 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
def post(self): """ Description: Registers a new adminstrator with the provided credentials. Returns: JSON message describing the status of request """ # Parses the request body and stores in a dictionary. data = admin_parser.parse_args() # Check if the admin of the same username exists or not. admin_data = AdminMethods.fetch_by_username(data['username']) if admin_data: return ADMIN_EXISTS_ERROR.to_json(), 400 if admin_data is None: data['uuid'] = str(uuid4()) #create a uuid for admin. admin_obj = AdminMethods(**data) if admin_obj.save_to_db(): return ADMIN_CREATED.to_json(), 201 return REGISTRATION_ERROR.to_json(), 501 return UNKNOWN_ERROR.to_json(), 501
def get(self): """ Returns names and uuid of all admins. Parameters: Return:List of admins """ # Get the claims from the jwt token. claims = get_jwt_claims() if claims['is_super_admin']: # Fetch all the admins in a list. all_admins = AdminMethods.get_all_admins() if all_admins: return all_admins, 200 try: if not len(all_admins): return NO_ADMIN_ERROR.to_json(), 400 except TypeError: return UNKNOWN_ERROR.to_json(), 501 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