示例#1
0
def get_dumps_info() -> (dict, int):
    """
    Endpoint to get dumps info.

    :return: Dictionary with info or
        an empty dictionary with 404 HTTP return code if access was denied.
    """
    if not check_admin():
        return {}, 404

    info = {
        'message': "OK"
    }

    for filename in backup_filenames:
        info[filename] = {}
        filepath = "{}{}".format(Config.c.constants.backup_path, filename)
        if os.path.isfile(filepath):
            info[filename]['created'] = datetime.fromtimestamp(os.path.getmtime(filepath))
            info[filename]['size'] = os.path.getsize(filepath)
            logger.debug("Backup file {}: {}".format(filename, info[filename]))
        else:
            logger.warning("No backup file in path: {}".format(filepath))

    return info, 200
示例#2
0
def view_all_presentations():
    """
    Route to show all presentations.

    :return: Page with all presentations,
        or an empty dictionary  with 404 HTTP code if access was denied.
    """
    if not check_admin():
        return {}, 404
    return render_template('show_all_presentations.html'), 200
示例#3
0
def view_admin():
    """
    Route to show admin page with links to 'show_all_trainings', 'show_all_presentations', 'get_logs'.

    :return: Admin page, or
        an empty dictionary with 404 HTTP return code if access was denied.
    """
    if not check_admin():
        return {}, 404
    return render_template('admin.html')
示例#4
0
def view_all_trainings():
    """
    Route to show all trainings.

    :return: Page with all trainings,
        or an empty dictionary if access was denied.
    """
    username = request.args.get('username', '')
    full_name = request.args.get('full_name', '')
    authorized = check_auth() is not None
    if not (check_admin() or (authorized and session.get('session_id') == username)):
        return {}, 404
    return render_template('show_all_trainings.html', username=username, full_name=full_name, is_admin="true" if check_admin() else 'false'), 200
示例#5
0
def view_dumps():
    """
    Route to show dump page.

    :return: Dumps page, or
        an empty dictionary with 404 HTTP return code if access was denied.
    """
    if not check_admin():
        return {}, 404

    dumps, _ = get_dumps_info()
    del dumps['message']
    return render_template('dumps.html', dumps=dumps)
示例#6
0
def get_all_presentations() -> (dict, int):
    """
    Endpoint to get information about all presentations.

    :return: Dictionary with information about all presentations and 'OK' message, or
        an empty dictionary with 404 HTTP code if access was denied.
    """
    if not check_admin():
        return {}, 404
    presentation_files = PresentationFilesDBManager().get_presentation_files()
    presentation_files_json = {'presentations': {}}
    for current_presentation_file in presentation_files:
        file_id = current_presentation_file.file_id
        presentation_files_json['presentations'][str(
            file_id)] = get_presentation_information(current_presentation_file)
    presentation_files_json['message'] = 'OK'
    return presentation_files_json, 200
示例#7
0
def download_dump(backup_name: str) -> (dict, int):
    """
    Endpoint to download dump.

    :return: Dump file or
        an empty dictionary with 404 HTTP return code if access was denied.
    """
    if not check_admin():
        return {}, 404
 
    backup_name = secure_filename(backup_name)

    filepath = "{}{}".format(Config.c.constants.backup_path, backup_name)
    if backup_name not in backup_filenames or not os.path.isfile(filepath):
        return {'message': "No such backup file: {}".format(backup_name)}, 404

    return send_file(filepath, as_attachment=True, attachment_filename=backup_name)
示例#8
0
def get_count_page() -> (dict, int):
    username = request.args.get('username', None)
    full_name = request.args.get('full_name', None)

    countItems = request.args.get('count')
    if not countItems:
        countItems = 10
    else:
        countItems = int(countItems)

    authorized = check_auth() is not None
    if not (check_admin() or
            (authorized and session.get('session_id') == username)):
        return {}, 404

    count = TrainingsDBManager().get_count_page(
        remove_blank_and_none({
            'username': username,
            'full_name': full_name
        }), countItems)
    result = {"count": count}
    return result, 200
示例#9
0
def create_dump(backup_name) -> (dict, int):
    """
    Endpoint to create dumps.

    :return: Dictionary with info about created backups (from get_dumps_info) or
        an empty dictionary with 404 HTTP return code if access was denied.
    """
    if not check_admin():
        return {}, 404
    
    backup_name = secure_filename(backup_name)
    
    if backup_name not in backup_filenames:
        return {'message': "No such backup filename: {}".format(backup_name)}, 404
    
    code = create_db_dump(backup_name)

    if code != 0:
        logger.error("Non-zero code for dump command for '{}'. Code {}".format(backup_name, code))
        return {'message': "Non-zero code for dump command: {}".format(code)}, 404

    return get_dumps_info()
示例#10
0
def get_all_trainings() -> (dict, int):
    """
    Endpoint to get information about all trainings. Can be optionally filtered by username or full name.
    :return: Dictionary with information about all trainings and 'OK' message, or
        an empty dictionary with 404 HTTP code if access was denied.
    """
    username = request.args.get('username', None)
    full_name = request.args.get('full_name', None)

    numberPage = request.args.get('page')
    if not numberPage:
        numberPage = 0
    else:
        numberPage = int(numberPage)

    countItems = request.args.get('count')
    if not countItems:
        countItems = 10
    else:
        countItems = int(countItems)

    print(numberPage, countItems)
    authorized = check_auth() is not None
    if not (check_admin() or
            (authorized and session.get('session_id') == username)):
        return {}, 404
    trainings = TrainingsDBManager().get_trainings_filtered(
        remove_blank_and_none({
            'username': username,
            'full_name': full_name
        }), numberPage, countItems)
    trainings_json = {'trainings': {}}
    for i, current_training in enumerate(trainings):
        trainings_json['trainings'][str(
            current_training.pk)] = get_training_information(current_training)
    trainings_json['message'] = 'OK'
    return trainings_json, 200
示例#11
0
def get_logs() -> (dict, int):
    """
    Endpoint to get logs.

    :return: Dictionary with logs, or
        an empty dictionary with 404 HTTP return code if access was denied.
    """
    if not check_admin():
        return {}, 404

    try:
        limit = request.args.get('limit', default=None, type=int)
    except Exception as e:
        logger.info('Limit value {} is invalid.\n{}'.format(request.args.get('limit'), e))
        limit = None

    try:
        offset = request.args.get('offset', default=None, type=int)
    except Exception as e:
        logger.info('Offset value {} is invalid.\n{}'.format(request.args.get('offset', default=None), e))
        offset = None

    raw_filters = request.args.get('filter', default=None)
    if raw_filters is not None:
        try:
            filters = literal_eval(raw_filters)
            if not isinstance(filters, dict):
                filters = None
        except Exception as e:
            logger.info('Filter value {} is invalid.\n{}'.format(raw_filters, e))
            filters = None
    else:
        filters = raw_filters

    raw_ordering = request.args.get('ordering', default=None)
    if raw_ordering is not None:
        try:
            ordering = literal_eval(raw_ordering)
            if not isinstance(ordering, list) or not all(map(lambda x: x[1] in [-1, 1], ordering)):
                logger.info('Ordering value {} is invalid.'.format(raw_ordering))
                ordering = None
        except Exception as e:
            logger.info('Ordering value {} is invalid.\n{}'.format(request.args.get('ordering', default=None), e))
            ordering = None
    else:
        ordering = raw_ordering

    try:
        logs = LogsDBManager().get_logs_filtered(filters=filters, limit=limit, offset=offset, ordering=ordering)
    except Exception as e:
        message = 'Incorrect get_logs_filtered execution, {}: {}.'.format(e.__class__, e)
        logger.warning(message)
        return {'message': message}, 404

    logs_json = {'logs': {}}
    for current_log in logs:
        _id = current_log.pk
        current_log_json = {
            'timestamp': datetime.fromtimestamp(current_log.timestamp.time, tz=datetime.now().astimezone().tzinfo),
            'serviceName': current_log.serviceName,
            'levelname': current_log.levelname,
            'levelno': current_log.levelno,
            'message': current_log.message,
            'pathname': current_log.pathname,
            'filename': current_log.filename,
            'funcName': current_log.funcName,
            'lineno': current_log.lineno,
        }
        logs_json['logs'][str(_id)] = current_log_json
    logs_json['message'] = 'OK'
    return logs_json, 200