示例#1
0
def admin_orphans_delete(parameter: str) -> str:
    count = EntityMapper.delete_orphans(parameter)
    flash(_('info orphans deleted:') + ' ' + str(count), 'info')
    return redirect(url_for('admin_orphans'))
示例#2
0
def admin_orphans(delete=None):
    if delete:
        count = EntityMapper.delete_orphans(delete)
        flash(_('info orphans deleted:') + ' ' + str(count), 'info')
        return redirect(url_for('admin_orphans'))
    header = [
        'name', 'class', 'type', 'system type', 'created', 'updated',
        'description'
    ]
    tables = {
        'orphans': {
            'id': 'orphans',
            'header': header,
            'data': []
        },
        'unlinked': {
            'id': 'unlinked',
            'header': header,
            'data': []
        },
        'nodes': {
            'id': 'nodes',
            'header': ['name', 'root'],
            'data': []
        },
        'missing_files': {
            'id': 'missing_files',
            'header': header,
            'data': []
        },
        'orphaned_files': {
            'id': 'orphaned_files',
            'data': [],
            'header': ['name', 'size', 'date', 'ext']
        }
    }
    for entity in EntityMapper.get_orphans():
        name = 'unlinked' if entity.class_.code in app.config[
            'CODE_CLASS'].keys() else 'orphans'
        tables[name]['data'].append([
            link(entity),
            link(entity.class_),
            entity.print_base_type(), entity.system_type,
            format_date(entity.created),
            format_date(entity.modified),
            truncate_string(entity.description)
        ])
    for node in NodeMapper.get_orphans():
        tables['nodes']['data'].append(
            [link(node), link(g.nodes[node.root[-1]])])

    file_ids = []
    # Get orphaned file entities (no corresponding file)
    for entity in EntityMapper.get_by_system_type('file'):
        file_ids.append(str(entity.id))
        if not get_file_path(entity):
            tables['missing_files']['data'].append([
                link(entity),
                link(entity.class_),
                entity.print_base_type(), entity.system_type,
                format_date(entity.created),
                format_date(entity.modified),
                truncate_string(entity.description)
            ])

    # Get orphaned files (no corresponding entity)
    path = app.config['UPLOAD_FOLDER_PATH']
    for file in [
            f for f in os.listdir(path)
            if os.path.isfile(os.path.join(path, f))
    ]:
        name = basename(file)
        file_path = path + '/' + name
        if name != '.gitignore' and splitext(name)[0] not in file_ids:
            tables['orphaned_files']['data'].append([
                name,
                convert_size(os.path.getsize(file_path)),
                format_date(
                    datetime.datetime.fromtimestamp(
                        os.path.getmtime(file_path))),
                splitext(name)[1],
                '<a href="' + url_for('download_file', filename=name) + '">' +
                uc_first(_('download')) + '</a>'
            ])
    return render_template('admin/orphans.html', tables=tables)