def read_tracker_by_id(tracker_id: int, max_revision_id=None) -> Optional[dict]: """ Reads a tracker (active or inactive) that matches the specified tracker ID :param tracker_id: ID of the tracker :param max_revision_id: Maximum revision ID for the search ("None" for latest revision) :return: Tracker information object Returned dictionary contains items: - id - project_id - short_name - full_name - description - active - revision_id """ connection = DatabaseInterface.create_connection() if max_revision_id is None: max_revision_id = DatabaseInterface.tables().revision.read_current_revision_id( connection) # Read a tracker that matches the specified tracker ID tracker = None if max_revision_id is not None: tracker = TrackerManagementInterface.__read_tracker_by_id(connection, tracker_id, max_revision_id) return tracker
def activate_tracker(requested_by_user: int, tracker_id: int) -> bool: """ Activates an inactive tracker :param requested_by_user: ID of the user that requested modification of the user :param tracker_id: ID of the tracker that should be activated :return: Success or failure """ connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Start a new revision revision_id = None if success: revision_id = DatabaseInterface.tables().revision.insert_row( connection, datetime.datetime.utcnow(), requested_by_user) if revision_id is None: success = False # Read tracker tracker = None if success: tracker = TrackerManagementInterface.__read_tracker_by_id(connection, tracker_id, revision_id) if tracker is None: success = False elif tracker["active"]: # Error, tracker is already active success = False # Activate tracker if success: success = DatabaseInterface.tables().tracker_information.insert_row( connection, tracker_id, tracker["short_name"], tracker["full_name"], tracker["description"], True, revision_id) if success: connection.commit_transaction() else: connection.rollback_transaction() except: connection.rollback_transaction() raise return success
def read_all_tracker_ids(project_id: int, tracker_selection=TrackerSelection.Active, max_revision_id=None) -> List[int]: """ Reads all tracker IDs from the database :param project_id: ID of the project :param tracker_selection: Search for active, inactive or all tracker :param max_revision_id: Maximum revision ID for the search ("None" for latest revision) :return: List of tracker IDs """ connection = DatabaseInterface.create_connection() if max_revision_id is None: max_revision_id = DatabaseInterface.tables( ).revision.read_current_revision_id(connection) # Reads all tracker IDs from the database trackers = None if max_revision_id is not None: trackers = DatabaseInterface.tables( ).tracker_information.read_all_tracker_ids(connection, project_id, tracker_selection, max_revision_id) return trackers
def read_project_by_id(project_id: int, max_revision_id=None) -> Optional[dict]: """ Reads a project (active or inactive) that matches the specified project ID :param project_id: ID of the project :param max_revision_id: Maximum revision ID for the search ("None" for latest revision) :return: Project information object Returned dictionary contains items: - id - short_name - full_name - description - active - revision_id """ connection = DatabaseInterface.create_connection() if max_revision_id is None: max_revision_id = DatabaseInterface.tables( ).revision.read_current_revision_id(connection) # Read a project that matches the specified project ID project = None if max_revision_id is not None: project = ProjectManagementInterface.__read_project_by_id( connection, project_id, max_revision_id) return project
def __read_user_by_display_name(token: str, display_name: str) -> dict: """ Reads current user's information :param token: Session token which contains the current user's information :param display_name: Display name :return: User information object Returned dictionary contains items: - id - user_name - display_name - email - active """ user = None success = False error_code = None error_message = None connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Extract session user if success: session_user = RestrictedResource._read_session_user( connection, token) if session_user is None: success = False error_code = 400 error_message = "Invalid session token" # Read requested user if success: user = UserManagementInterface.read_user_by_display_name( connection, display_name) if user is None: success = False error_code = 400 error_message = "Invalid user name" connection.rollback_transaction() except: connection.rollback_transaction() abort(500, message="Internal error, please try again") # Return user if success: return jsonify(user) else: if (error_code is not None) and (error_message is not None): abort(error_code, message=error_message) else: abort(500, message="Internal error")
def read_all_project_ids(project_selection=ProjectSelection.Active, max_revision_id=None) -> List[int]: """ Reads all project IDs from the database :param project_selection: Search for active, inactive or all project :param max_revision_id: Maximum revision ID for the search ("None" for latest revision) :return: List of project IDs """ connection = DatabaseInterface.create_connection() if max_revision_id is None: max_revision_id = DatabaseInterface.tables().revision.read_current_revision_id( connection) # Reads all project IDs from the database projects = None if max_revision_id is not None: projects = DatabaseInterface.tables().project_information.read_all_project_ids( connection, project_selection, max_revision_id) return projects
def deactivate_project(requested_by_user: int, project_id: int) -> bool: """ Deactivates an active project :param requested_by_user: ID of the user that requested modification of the user :param project_id: ID of the project that should be deactivated :return: Success or failure """ connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Start a new revision revision_id = None if success: revision_id = DatabaseInterface.tables().revision.insert_row( connection, datetime.datetime.utcnow(), requested_by_user) if revision_id is None: success = False # Read project project = None if success: project = ProjectManagementInterface.__read_project_by_id(connection, project_id, revision_id) if project is None: success = False elif not project["active"]: # Error, project is already inactive success = False # Deactivate project if success: success = DatabaseInterface.tables().project_information.insert_row( connection, project_id, project["short_name"], project["full_name"], project["description"], False, revision_id) if success: connection.commit_transaction() else: connection.rollback_transaction() except: connection.rollback_transaction() raise return success
def read_all_tracker_field_ids(tracker_id: int, tracker_field_selection=TrackerFieldSelection.Active, max_revision_id=None) -> List[int]: """ Reads all tracker field IDs from the database :param tracker_id: ID of the tracker :param tracker_field_selection: Search for active, inactive or all tracker :param max_revision_id: Maximum revision ID for the search ("None" for latest revision) :return: List of tracker field IDs """ connection = DatabaseInterface.create_connection() if max_revision_id is None: max_revision_id = DatabaseInterface.tables().revision.read_current_revision_id( connection) # Reads all tracker field IDs from the database tracker_fields = None if max_revision_id is not None: tracker_fields = \ DatabaseInterface.tables().tracker_field_information.read_all_tracker_field_ids( connection, tracker_id, tracker_field_selection, max_revision_id) return tracker_fields
def read_all_project_ids(project_selection=ProjectSelection.Active, max_revision_id=None) -> List[int]: """ Reads all project IDs from the database :param project_selection: Search for active, inactive or all project :param max_revision_id: Maximum revision ID for the search ("None" for latest revision) :return: List of project IDs """ connection = DatabaseInterface.create_connection() if max_revision_id is None: max_revision_id = DatabaseInterface.tables( ).revision.read_current_revision_id(connection) # Reads all project IDs from the database projects = None if max_revision_id is not None: projects = DatabaseInterface.tables( ).project_information.read_all_project_ids(connection, project_selection, max_revision_id) return projects
def read_project_by_id(project_id: int, max_revision_id=None) -> Optional[dict]: """ Reads a project (active or inactive) that matches the specified project ID :param project_id: ID of the project :param max_revision_id: Maximum revision ID for the search ("None" for latest revision) :return: Project information object Returned dictionary contains items: - id - short_name - full_name - description - active - revision_id """ connection = DatabaseInterface.create_connection() if max_revision_id is None: max_revision_id = DatabaseInterface.tables().revision.read_current_revision_id( connection) # Read a project that matches the specified project ID project = None if max_revision_id is not None: project = ProjectManagementInterface.__read_project_by_id(connection, project_id, max_revision_id) return project
def read_tracker_by_id(tracker_id: int, max_revision_id=None) -> Optional[dict]: """ Reads a tracker (active or inactive) that matches the specified tracker ID :param tracker_id: ID of the tracker :param max_revision_id: Maximum revision ID for the search ("None" for latest revision) :return: Tracker information object Returned dictionary contains items: - id - project_id - short_name - full_name - description - active - revision_id """ connection = DatabaseInterface.create_connection() if max_revision_id is None: max_revision_id = DatabaseInterface.tables( ).revision.read_current_revision_id(connection) # Read a tracker that matches the specified tracker ID tracker = None if max_revision_id is not None: tracker = TrackerManagementInterface.__read_tracker_by_id( connection, tracker_id, max_revision_id) return tracker
def __read_user_by_display_name(token: str, display_name: str) -> dict: """ Reads current user's information :param token: Session token which contains the current user's information :param display_name: Display name :return: User information object Returned dictionary contains items: - id - user_name - display_name - email - active """ user = None success = False error_code = None error_message = None connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Extract session user if success: session_user = RestrictedResource._read_session_user(connection, token) if session_user is None: success = False error_code = 400 error_message = "Invalid session token" # Read requested user if success: user = UserManagementInterface.read_user_by_display_name(connection, display_name) if user is None: success = False error_code = 400 error_message = "Invalid user name" connection.rollback_transaction() except: connection.rollback_transaction() abort(500, message="Internal error, please try again") # Return user if success: return jsonify(user) else: if (error_code is not None) and (error_message is not None): abort(error_code, message=error_message) else: abort(500, message="Internal error")
def deactivate_tracker_field(requested_by_user: int, tracker_field_id: int) -> bool: """ Deactivates an active tracker field :param requested_by_user: ID of the user that requested modification of the user :param tracker_field_id: ID of the tracker field that should be deactivated :return: Success or failure """ connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Start a new revision revision_id = None if success: revision_id = DatabaseInterface.tables().revision.insert_row( connection, datetime.datetime.utcnow(), requested_by_user) if revision_id is None: success = False # Read tracker field tracker_field = None if success: tracker_field = TrackerFieldManagementInterface.__read_tracker_field_by_id( connection, tracker_field_id, revision_id) if tracker_field is None: success = False elif not tracker_field["active"]: # Error, tracker field is already inactive success = False # Deactivate tracker field if success: success = DatabaseInterface.tables( ).tracker_field_information.insert_row( connection, tracker_field_id, tracker_field["name"], tracker_field["display_name"], tracker_field["description"], tracker_field["field_type"], tracker_field["required"], False, revision_id) if success: connection.commit_transaction() else: connection.rollback_transaction() except: connection.rollback_transaction() raise return success
def update_user_information(user_to_modify: int, user_name: str, display_name: str, email: str, active: bool) -> bool: """ Updates user's information :param user_to_modify: ID of the user that should be modified :param user_name: User's new user name :param display_name: User's new display name :param email: User's new email address :param active: User's new state (active or inactive) :return: Success or failure """ connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Check if there is already an existing user with the same user name if success: user = UserManagementInterface.__read_user_by_user_name(connection, user_name) if user is not None: if user["id"] != user_to_modify: success = False # Check if there is already an existing user with the same display name if success: user = UserManagementInterface.__read_user_by_display_name(connection, display_name) if user is not None: if user["id"] != user_to_modify: success = False # Update user's information if success: success = DatabaseInterface.tables().user.update_row(connection, user_to_modify, user_name, display_name, email, active) if success: connection.commit_transaction() else: connection.rollback_transaction() except: connection.rollback_transaction() raise return success
def read_all_user_ids(user_selection=UserSelection.Active) -> List[int]: """ Reads all user IDs from the database :param user_selection: Search for active, inactive or all users :return: List of user IDs """ connection = DatabaseInterface.create_connection() return DatabaseInterface.tables().user.read_all_ids(connection, user_selection)
def deactivate_project(requested_by_user: int, project_id: int) -> bool: """ Deactivates an active project :param requested_by_user: ID of the user that requested modification of the user :param project_id: ID of the project that should be deactivated :return: Success or failure """ connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Start a new revision revision_id = None if success: revision_id = DatabaseInterface.tables().revision.insert_row( connection, datetime.datetime.utcnow(), requested_by_user) if revision_id is None: success = False # Read project project = None if success: project = ProjectManagementInterface.__read_project_by_id( connection, project_id, revision_id) if project is None: success = False elif not project["active"]: # Error, project is already inactive success = False # Deactivate project if success: success = DatabaseInterface.tables( ).project_information.insert_row(connection, project_id, project["short_name"], project["full_name"], project["description"], False, revision_id) if success: connection.commit_transaction() else: connection.rollback_transaction() except: connection.rollback_transaction() raise return success
def create_tracker(requested_by_user: int, project_id: int, short_name: str, full_name: str, description: str) -> Optional[int]: """ Creates a new tracker :param requested_by_user: ID of the user that requested creation of the new tracker :param project_id: ID of the project :param short_name: Tracker's short name :param full_name: Tracker's full name :param description: Tracker's description :return: Tracker ID of the new tracker """ tracker_id = None connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Start a new revision revision_id = None if success: revision_id = DatabaseInterface.tables().revision.insert_row( connection, datetime.datetime.utcnow(), requested_by_user) if revision_id is None: success = False # Create the tracker if success: tracker_id = TrackerManagementInterface.__create_tracker(connection, project_id, short_name, full_name, description, revision_id) if tracker_id is None: success = False if success: connection.commit_transaction() else: connection.rollback_transaction() except: connection.rollback_transaction() raise return tracker_id
def create_tracker_fields(requested_by_user: int, tracker_id: int, fields: List[dict]) -> bool: """ Creates a new tracker :param requested_by_user: ID of the user that requested creation of the new tracker field :param tracker_id: ID of the tracker :param fields: Tracker fields :return: Success or failure Fields parameter needs to contain a list of dictionaries with the following items: - name: Tracker field's name - display_name: Tracker field's display name - description: Tracker field's description - field_type: Tracker field's type - required: Necessity of the tracker field (required or not) """ connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Start a new revision revision_id = None if success: revision_id = DatabaseInterface.tables().revision.insert_row( connection, datetime.datetime.utcnow(), requested_by_user) if revision_id is None: success = False # Create the tracker if success: for field in fields: tracker_field_id = TrackerFieldManagementInterface.__create_tracker_field( connection, tracker_id, field["name"], field["display_name"], field["description"], field["field_type"], field["required"], revision_id) if tracker_field_id is None: success = False break if success: connection.commit_transaction() else: connection.rollback_transaction() except: connection.rollback_transaction() raise return success
def activate_tracker(requested_by_user: int, tracker_id: int) -> bool: """ Activates an inactive tracker :param requested_by_user: ID of the user that requested modification of the user :param tracker_id: ID of the tracker that should be activated :return: Success or failure """ connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Start a new revision revision_id = None if success: revision_id = DatabaseInterface.tables().revision.insert_row( connection, datetime.datetime.utcnow(), requested_by_user) if revision_id is None: success = False # Read tracker tracker = None if success: tracker = TrackerManagementInterface.__read_tracker_by_id( connection, tracker_id, revision_id) if tracker is None: success = False elif tracker["active"]: # Error, tracker is already active success = False # Activate tracker if success: success = DatabaseInterface.tables( ).tracker_information.insert_row(connection, tracker_id, tracker["short_name"], tracker["full_name"], tracker["description"], True, revision_id) if success: connection.commit_transaction() else: connection.rollback_transaction() except: connection.rollback_transaction() raise return success
def read_all_user_ids(user_selection=UserSelection.Active) -> List[int]: """ Reads all user IDs from the database :param user_selection: Search for active, inactive or all users :return: List of user IDs """ connection = DatabaseInterface.create_connection() return DatabaseInterface.tables().user.read_all_ids( connection, user_selection)
def update_user_information(user_to_modify: int, user_name: str, display_name: str, email: str, active: bool) -> bool: """ Updates user's information :param user_to_modify: ID of the user that should be modified :param user_name: User's new user name :param display_name: User's new display name :param email: User's new email address :param active: User's new state (active or inactive) :return: Success or failure """ connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Check if there is already an existing user with the same user name if success: user = UserManagementInterface.__read_user_by_user_name( connection, user_name) if user is not None: if user["id"] != user_to_modify: success = False # Check if there is already an existing user with the same display name if success: user = UserManagementInterface.__read_user_by_display_name( connection, display_name) if user is not None: if user["id"] != user_to_modify: success = False # Update user's information if success: success = DatabaseInterface.tables().user.update_row( connection, user_to_modify, user_name, display_name, email, active) if success: connection.commit_transaction() else: connection.rollback_transaction() except: connection.rollback_transaction() raise return success
def post(self): """ Logs out the user """ # Extract session token from the request token = self._read_session_token() # Log out success = False error_code = None error_message = None connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Get the session token session_token = None if success: session_token = UserManagementInterface.read_session_token(connection, token) if session_token is None: success = False error_code = 400 error_message = "Invalid session token" # Delete session token if success: success = UserManagementInterface.delete_session_token(connection, token) if not success: error_code = 500 error_message = "Failed to log out, please try again" if success: connection.commit_transaction() else: connection.rollback_transaction() except: connection.rollback_transaction() abort(500, message="Internal error, please try again") # Return response if success: return None else: if (error_code is not None) and (error_message is not None): abort(error_code, message=error_message) else: abort(500, message="Internal error")
def create_tracker_field(requested_by_user: int, tracker_id: int, name: str, display_name: str, description: str, field_type: str, required: bool) -> Optional[int]: """ Creates a new tracker :param requested_by_user: ID of the user that requested creation of the new tracker field :param tracker_id: ID of the tracker :param name: Tracker field's name :param display_name: Tracker field's display name :param description: Tracker field's description :param field_type: Tracker field's type :param required: Necessity of the tracker field (required or not) :return: Tracker field ID of the new tracker field """ tracker_field_id = None connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Start a new revision revision_id = None if success: revision_id = DatabaseInterface.tables().revision.insert_row( connection, datetime.datetime.utcnow(), requested_by_user) if revision_id is None: success = False # Create the tracker if success: tracker_field_id = TrackerFieldManagementInterface.__create_tracker_field( connection, tracker_id, name, display_name, description, field_type, required, revision_id) if tracker_field_id is None: success = False if success: connection.commit_transaction() else: connection.rollback_transaction() except: connection.rollback_transaction() raise return tracker_field_id
def read_tracker_fields_by_display_name(display_name: str, max_revision_id=None ) -> List[dict]: """ Reads all active and inactive tracker fields that match the specified display name :param display_name: Tracker field's display name :param max_revision_id: Maximum revision ID for the search ("None" for latest revision) :return: Tracker field information of all tracker fields that match the search attribute Each dictionary in the returned list contains items: - id - tracker_id - name - display_name - description - field_type - required - active - revision_id """ connection = DatabaseInterface.create_connection() if max_revision_id is None: max_revision_id = DatabaseInterface.tables( ).revision.read_current_revision_id(connection) # Read tracker fields that match the specified display name tracker_fields = list() if max_revision_id is not None: tracker_field_information_list = \ DatabaseInterface.tables().tracker_field_information.read_information( connection, "display_name", display_name, TrackerFieldSelection.All, max_revision_id) for tracker_field_information in tracker_field_information_list: tracker_fields.append( TrackerFieldManagementInterface. __parse_tracker_field_information( tracker_field_information)) return tracker_fields
def create_tracker(requested_by_user: int, project_id: int, short_name: str, full_name: str, description: str) -> Optional[int]: """ Creates a new tracker :param requested_by_user: ID of the user that requested creation of the new tracker :param project_id: ID of the project :param short_name: Tracker's short name :param full_name: Tracker's full name :param description: Tracker's description :return: Tracker ID of the new tracker """ tracker_id = None connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Start a new revision revision_id = None if success: revision_id = DatabaseInterface.tables().revision.insert_row( connection, datetime.datetime.utcnow(), requested_by_user) if revision_id is None: success = False # Create the tracker if success: tracker_id = TrackerManagementInterface.__create_tracker( connection, project_id, short_name, full_name, description, revision_id) if tracker_id is None: success = False if success: connection.commit_transaction() else: connection.rollback_transaction() except: connection.rollback_transaction() raise return tracker_id
def read_tracker_fields_by_display_name(display_name: str, max_revision_id=None) -> List[dict]: """ Reads all active and inactive tracker fields that match the specified display name :param display_name: Tracker field's display name :param max_revision_id: Maximum revision ID for the search ("None" for latest revision) :return: Tracker field information of all tracker fields that match the search attribute Each dictionary in the returned list contains items: - id - tracker_id - name - display_name - description - field_type - required - active - revision_id """ connection = DatabaseInterface.create_connection() if max_revision_id is None: max_revision_id = DatabaseInterface.tables().revision.read_current_revision_id( connection) # Read tracker fields that match the specified display name tracker_fields = list() if max_revision_id is not None: tracker_field_information_list = \ DatabaseInterface.tables().tracker_field_information.read_information( connection, "display_name", display_name, TrackerFieldSelection.All, max_revision_id) for tracker_field_information in tracker_field_information_list: tracker_fields.append( TrackerFieldManagementInterface.__parse_tracker_field_information( tracker_field_information)) return tracker_fields
def read_trackers_by_full_name(full_name: str, max_revision_id=None) -> List[dict]: """ Reads all active and inactive trackers that match the specified full name :param full_name: Tracker's full name :param max_revision_id: Maximum revision ID for the search ("None" for latest revision) :return: Tracker information of all trackers that match the search attribute Each dictionary in the returned list contains items: - id - project_id - short_name - full_name - description - active - revision_id """ connection = DatabaseInterface.create_connection() if max_revision_id is None: max_revision_id = DatabaseInterface.tables( ).revision.read_current_revision_id(connection) # Read trackers that match the specified full name trackers = list() if max_revision_id is not None: tracker_information_list = \ DatabaseInterface.tables().tracker_information.read_information( connection, "full_name", full_name, TrackerSelection.All, max_revision_id) for tracker_information in tracker_information_list: trackers.append( TrackerManagementInterface.__parse_tracker_information( tracker_information)) return trackers
def read_trackers_by_full_name(full_name: str, max_revision_id=None) -> List[dict]: """ Reads all active and inactive trackers that match the specified full name :param full_name: Tracker's full name :param max_revision_id: Maximum revision ID for the search ("None" for latest revision) :return: Tracker information of all trackers that match the search attribute Each dictionary in the returned list contains items: - id - project_id - short_name - full_name - description - active - revision_id """ connection = DatabaseInterface.create_connection() if max_revision_id is None: max_revision_id = DatabaseInterface.tables().revision.read_current_revision_id( connection) # Read trackers that match the specified full name trackers = list() if max_revision_id is not None: tracker_information_list = \ DatabaseInterface.tables().tracker_information.read_information( connection, "full_name", full_name, TrackerSelection.All, max_revision_id) for tracker_information in tracker_information_list: trackers.append(TrackerManagementInterface.__parse_tracker_information( tracker_information)) return trackers
def read_projects_by_short_name(short_name: str, max_revision_id=None) -> List[dict]: """ Reads all active and inactive projects that match the specified short name :param short_name: Project's short name :param max_revision_id: Maximum revision ID for the search ("None" for latest revision) :return: Project information of all projects that match the search attribute Each dictionary in the returned list contains items: - id - short_name - full_name - description - active - revision_id """ connection = DatabaseInterface.create_connection() if max_revision_id is None: max_revision_id = DatabaseInterface.tables( ).revision.read_current_revision_id(connection) # Read projects that match the specified short name projects = list() if max_revision_id is not None: project_information_list = \ DatabaseInterface.tables().project_information.read_information( connection, "short_name", short_name, ProjectSelection.All, max_revision_id) for project_information in project_information_list: projects.append( ProjectManagementInterface.__parse_project_information( project_information)) return projects
def deactivate_user(user_id: int) -> bool: """ Deactivates an active user :param user_id: ID of the user that should be deactivated :return: Success or failure """ connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Read user user = None if success: user = UserManagementInterface.read_user_by_id(connection, user_id) if user is None: success = False elif not user["active"]: # Error, user is already inactive success = False # Deactivate user if success: success = DatabaseInterface.tables().user.update_row(connection, user_id, user["user_name"], user["display_name"], user["email"], False) if success: connection.commit_transaction() else: connection.rollback_transaction() except: connection.rollback_transaction() raise return success
def create_user(user_name: str, display_name: str, email: str, authentication_type: str, authentication_parameters: dict) -> Optional[int]: """ Creates a new user :param user_name: User's user name :param display_name: User's display name :param email: Email address of the user :param authentication_type: User's authentication type :param authentication_parameters: User's authentication parameters :return: User ID of the new user """ user_id = None connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Create the user if success: user_id = UserManagementInterface.__create_user(connection, user_name, display_name, email, authentication_type, authentication_parameters) if user_id is None: success = False if success: connection.commit_transaction() else: connection.rollback_transaction() except: connection.rollback_transaction() raise return user_id
def read_projects_by_short_name(short_name: str, max_revision_id=None) -> List[dict]: """ Reads all active and inactive projects that match the specified short name :param short_name: Project's short name :param max_revision_id: Maximum revision ID for the search ("None" for latest revision) :return: Project information of all projects that match the search attribute Each dictionary in the returned list contains items: - id - short_name - full_name - description - active - revision_id """ connection = DatabaseInterface.create_connection() if max_revision_id is None: max_revision_id = DatabaseInterface.tables().revision.read_current_revision_id( connection) # Read projects that match the specified short name projects = list() if max_revision_id is not None: project_information_list = \ DatabaseInterface.tables().project_information.read_information( connection, "short_name", short_name, ProjectSelection.All, max_revision_id) for project_information in project_information_list: projects.append(ProjectManagementInterface.__parse_project_information( project_information)) return projects
def deactivate_user(user_id: int) -> bool: """ Deactivates an active user :param user_id: ID of the user that should be deactivated :return: Success or failure """ connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Read user user = None if success: user = UserManagementInterface.read_user_by_id( connection, user_id) if user is None: success = False elif not user["active"]: # Error, user is already inactive success = False # Deactivate user if success: success = DatabaseInterface.tables().user.update_row( connection, user_id, user["user_name"], user["display_name"], user["email"], False) if success: connection.commit_transaction() else: connection.rollback_transaction() except: connection.rollback_transaction() raise return success
def create_user(user_name: str, display_name: str, email: str, authentication_type: str, authentication_parameters: dict) -> Optional[int]: """ Creates a new user :param user_name: User's user name :param display_name: User's display name :param email: Email address of the user :param authentication_type: User's authentication type :param authentication_parameters: User's authentication parameters :return: User ID of the new user """ user_id = None connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Create the user if success: user_id = UserManagementInterface.__create_user( connection, user_name, display_name, email, authentication_type, authentication_parameters) if user_id is None: success = False if success: connection.commit_transaction() else: connection.rollback_transaction() except: connection.rollback_transaction() raise return user_id
def read_tracker_field_by_display_name(display_name: str, max_revision_id=None) -> Optional[dict]: """ Reads an active tracker field that matches the specified display name :param display_name: Tracker field's display name :param max_revision_id: Maximum revision ID for the search ("None" for latest revision) :return: Tracker field information object Returned dictionary contains items: - id - tracker_id - name - display_name - description - field_type - required - active - revision_id """ connection = DatabaseInterface.create_connection() if max_revision_id is None: max_revision_id = DatabaseInterface.tables().revision.read_current_revision_id( connection) # Read a tracker field that matches the specified display name tracker_field = None if max_revision_id is not None: tracker_field = TrackerFieldManagementInterface.__read_tracker_field_by_display_name( connection, display_name, max_revision_id) return tracker_field
def read_tracker_field_by_display_name(display_name: str, max_revision_id=None ) -> Optional[dict]: """ Reads an active tracker field that matches the specified display name :param display_name: Tracker field's display name :param max_revision_id: Maximum revision ID for the search ("None" for latest revision) :return: Tracker field information object Returned dictionary contains items: - id - tracker_id - name - display_name - description - field_type - required - active - revision_id """ connection = DatabaseInterface.create_connection() if max_revision_id is None: max_revision_id = DatabaseInterface.tables( ).revision.read_current_revision_id(connection) # Read a tracker field that matches the specified display name tracker_field = None if max_revision_id is not None: tracker_field = TrackerFieldManagementInterface.__read_tracker_field_by_display_name( connection, display_name, max_revision_id) return tracker_field
def read_user_authentication(user_id: int) -> Optional[dict]: """ Reads user's authentication :param user_id: ID of the user :return: User authentication object Returned dictionary contains items: - authentication_type - authentication_parameters """ connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Read the user's authentication user_authentication = None if success: user_authentication = UserManagementInterface.__read_user_authentication( connection, user_id) if user_authentication is None: success = False if success: connection.commit_transaction() else: connection.rollback_transaction() except: connection.rollback_transaction() raise return user_authentication
def read_user_authentication(user_id: int) -> Optional[dict]: """ Reads user's authentication :param user_id: ID of the user :return: User authentication object Returned dictionary contains items: - authentication_type - authentication_parameters """ connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Read the user's authentication user_authentication = None if success: user_authentication = UserManagementInterface.__read_user_authentication(connection, user_id) if user_authentication is None: success = False if success: connection.commit_transaction() else: connection.rollback_transaction() except: connection.rollback_transaction() raise return user_authentication
def update_project_information(requested_by_user: int, project_to_modify: int, short_name: str, full_name: str, description: str, active: bool) -> bool: """ Updates project's information :param requested_by_user: ID of the user that requested modification of the user :param project_to_modify: ID of the project that should be modified :param short_name: Project's new short name :param full_name: Project's new full name :param description: Project's new description :param active: Project's new state (active or inactive) :return: Success or failure """ connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Start a new revision revision_id = None if success: revision_id = DatabaseInterface.tables().revision.insert_row( connection, datetime.datetime.utcnow(), requested_by_user) if revision_id is None: success = False # Check if there is already an existing project with the same short name if success: project = ProjectManagementInterface.__read_project_by_short_name( connection, short_name, revision_id) if project is not None: if project["id"] != project_to_modify: success = False # Check if there is already an existing project with the same full name if success: project = ProjectManagementInterface.__read_project_by_full_name( connection, full_name, revision_id) if project is not None: if project["id"] != project_to_modify: success = False # Update project's information in the new revision if success: row_id = DatabaseInterface.tables( ).project_information.insert_row(connection, project_to_modify, short_name, full_name, description, active, revision_id) if row_id is None: success = False if success: connection.commit_transaction() else: connection.rollback_transaction() except: connection.rollback_transaction() raise return success
def deactivate_tracker_field(requested_by_user: int, tracker_field_id: int) -> bool: """ Deactivates an active tracker field :param requested_by_user: ID of the user that requested modification of the user :param tracker_field_id: ID of the tracker field that should be deactivated :return: Success or failure """ connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Start a new revision revision_id = None if success: revision_id = DatabaseInterface.tables().revision.insert_row( connection, datetime.datetime.utcnow(), requested_by_user) if revision_id is None: success = False # Read tracker field tracker_field = None if success: tracker_field = TrackerFieldManagementInterface.__read_tracker_field_by_id( connection, tracker_field_id, revision_id) if tracker_field is None: success = False elif not tracker_field["active"]: # Error, tracker field is already inactive success = False # Deactivate tracker field if success: success = DatabaseInterface.tables().tracker_field_information.insert_row( connection, tracker_field_id, tracker_field["name"], tracker_field["display_name"], tracker_field["description"], tracker_field["field_type"], tracker_field["required"], False, revision_id) if success: connection.commit_transaction() else: connection.rollback_transaction() except: connection.rollback_transaction() raise return success
def update_tracker_field_information(requested_by_user: int, tracker_field_to_modify: int, name: str, display_name: str, description: str, field_type: str, required: bool, active: bool) -> bool: """ Updates tracker's information :param requested_by_user: ID of the user that requested modification of the user :param tracker_field_to_modify: ID of the tracker field that should be modified :param name: Tracker field's new name :param display_name: Tracker field's new display name :param description: Tracker field's new description :param field_type: Tracker field's new type :param required: Necessity of the tracker field (required or not) :param active: Tracker field's new state (active or inactive) :return: Success or failure """ connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Start a new revision revision_id = None if success: revision_id = DatabaseInterface.tables().revision.insert_row( connection, datetime.datetime.utcnow(), requested_by_user) if revision_id is None: success = False # Check if there is already an existing tracker field with the same name if success: tracker = TrackerFieldManagementInterface.__read_tracker_field_by_name(connection, name, revision_id) if tracker is not None: if tracker["id"] != tracker_field_to_modify: success = False # Check if there is already an existing tracker field with the same display name if success: tracker = TrackerFieldManagementInterface.__read_tracker_field_by_display_name( connection, display_name, revision_id) if tracker is not None: if tracker["id"] != tracker_field_to_modify: success = False # Update tracker field's information in the new revision if success: row_id = DatabaseInterface.tables().tracker_field_information.insert_row( connection, tracker_field_to_modify, name, display_name, description, field_type, required, active, revision_id) if row_id is None: success = False if success: connection.commit_transaction() else: connection.rollback_transaction() except: connection.rollback_transaction() raise return success
def post(self): """ Logs in a user with the specified parameters :return: Session token """ # Extract arguments from the request request_data = request.get_json() if ("user_name" not in request_data) or ("authentication_parameters" not in request_data): abort(400, message="Missing parameters") # Log in token = None success = False error_code = None error_message = None connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Authenticate the user user_id = None if success: user_id = UserManagementInterface.authenticate_user( connection, request_data["user_name"], request_data["authentication_parameters"]) if user_id is None: success = False error_code = 400 error_message = "Invalid user name or authentication parameters" # Create session token if success: token = UserManagementInterface.create_session_token(connection, user_id) if token is None: success = False error_code = 500 error_message = "Failed to generate a session token, please try again" if success: connection.commit_transaction() else: connection.rollback_transaction() except Exception as e: connection.rollback_transaction() abort(500, message="Internal error, please try again") # Return response if success: return jsonify({'session_token': token}) else: if (error_code is not None) and (error_message is not None): abort(error_code, message=error_message) else: abort(500, message="Internal error")
def update_project_information(requested_by_user: int, project_to_modify: int, short_name: str, full_name: str, description: str, active: bool) -> bool: """ Updates project's information :param requested_by_user: ID of the user that requested modification of the user :param project_to_modify: ID of the project that should be modified :param short_name: Project's new short name :param full_name: Project's new full name :param description: Project's new description :param active: Project's new state (active or inactive) :return: Success or failure """ connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Start a new revision revision_id = None if success: revision_id = DatabaseInterface.tables().revision.insert_row( connection, datetime.datetime.utcnow(), requested_by_user) if revision_id is None: success = False # Check if there is already an existing project with the same short name if success: project = ProjectManagementInterface.__read_project_by_short_name(connection, short_name, revision_id) if project is not None: if project["id"] != project_to_modify: success = False # Check if there is already an existing project with the same full name if success: project = ProjectManagementInterface.__read_project_by_full_name(connection, full_name, revision_id) if project is not None: if project["id"] != project_to_modify: success = False # Update project's information in the new revision if success: row_id = DatabaseInterface.tables().project_information.insert_row( connection, project_to_modify, short_name, full_name, description, active, revision_id) if row_id is None: success = False if success: connection.commit_transaction() else: connection.rollback_transaction() except: connection.rollback_transaction() raise return success
def update_user_authentication(user_to_modify: int, authentication_type: str, authentication_parameters: dict) -> bool: """ Updates user's authentication :param user_to_modify: ID of the user that should be modified :param authentication_type: User's new authentication type :param authentication_parameters: User's new authentication parameters :return: Success or failure """ connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Update user's authentication user_authentication = None if success: # Read users current authentication information user_authentication = \ DatabaseInterface.tables().user_authentication.read_authentication( connection, user_to_modify) if user_authentication is None: # Error, no authentication was found for that user success = False # Modify authentication type if needed if success and (authentication_type != user_authentication["authentication_type"]): success = DatabaseInterface.tables().user_authentication.update_row( connection, user_to_modify, authentication_type) # Modify authentication parameters reference_authentication_parameters = None if success: DatabaseInterface.tables().user_authentication_parameter.delete_rows(connection, user_to_modify) reference_authentication_parameters = \ AuthenticationInterface.generate_reference_authentication_parameters( authentication_type, authentication_parameters) if reference_authentication_parameters is None: success = False if success: success = DatabaseInterface.tables().user_authentication_parameter.insert_rows( connection, user_to_modify, reference_authentication_parameters) if success: connection.commit_transaction() else: connection.rollback_transaction() except: connection.rollback_transaction() raise return success
def update_user_authentication(user_to_modify: int, authentication_type: str, authentication_parameters: dict) -> bool: """ Updates user's authentication :param user_to_modify: ID of the user that should be modified :param authentication_type: User's new authentication type :param authentication_parameters: User's new authentication parameters :return: Success or failure """ connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Update user's authentication user_authentication = None if success: # Read users current authentication information user_authentication = \ DatabaseInterface.tables().user_authentication.read_authentication( connection, user_to_modify) if user_authentication is None: # Error, no authentication was found for that user success = False # Modify authentication type if needed if success and (authentication_type != user_authentication["authentication_type"]): success = DatabaseInterface.tables( ).user_authentication.update_row(connection, user_to_modify, authentication_type) # Modify authentication parameters reference_authentication_parameters = None if success: DatabaseInterface.tables( ).user_authentication_parameter.delete_rows( connection, user_to_modify) reference_authentication_parameters = \ AuthenticationInterface.generate_reference_authentication_parameters( authentication_type, authentication_parameters) if reference_authentication_parameters is None: success = False if success: success = DatabaseInterface.tables( ).user_authentication_parameter.insert_rows( connection, user_to_modify, reference_authentication_parameters) if success: connection.commit_transaction() else: connection.rollback_transaction() except: connection.rollback_transaction() raise return success
def update_tracker_field_information(requested_by_user: int, tracker_field_to_modify: int, name: str, display_name: str, description: str, field_type: str, required: bool, active: bool) -> bool: """ Updates tracker's information :param requested_by_user: ID of the user that requested modification of the user :param tracker_field_to_modify: ID of the tracker field that should be modified :param name: Tracker field's new name :param display_name: Tracker field's new display name :param description: Tracker field's new description :param field_type: Tracker field's new type :param required: Necessity of the tracker field (required or not) :param active: Tracker field's new state (active or inactive) :return: Success or failure """ connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Start a new revision revision_id = None if success: revision_id = DatabaseInterface.tables().revision.insert_row( connection, datetime.datetime.utcnow(), requested_by_user) if revision_id is None: success = False # Check if there is already an existing tracker field with the same name if success: tracker = TrackerFieldManagementInterface.__read_tracker_field_by_name( connection, name, revision_id) if tracker is not None: if tracker["id"] != tracker_field_to_modify: success = False # Check if there is already an existing tracker field with the same display name if success: tracker = TrackerFieldManagementInterface.__read_tracker_field_by_display_name( connection, display_name, revision_id) if tracker is not None: if tracker["id"] != tracker_field_to_modify: success = False # Update tracker field's information in the new revision if success: row_id = DatabaseInterface.tables( ).tracker_field_information.insert_row( connection, tracker_field_to_modify, name, display_name, description, field_type, required, active, revision_id) if row_id is None: success = False if success: connection.commit_transaction() else: connection.rollback_transaction() except: connection.rollback_transaction() raise return success