def admin_orphans() -> str: header = ['name', 'class', 'type', 'system type', 'created', 'updated', 'description'] tables = {'orphans': Table(header), 'unlinked': Table(header), 'missing_files': Table(header), 'circular': Table(['entity']), 'nodes': Table(['name', 'root']), 'orphaned_files': Table(['name', 'size', 'date', 'ext'])} tables['circular'].rows = [[link(entity)] for entity in EntityMapper.get_circular()] for entity in EntityMapper.get_orphans(): name = 'unlinked' if entity.class_.code in app.config['CODE_CLASS'].keys() else 'orphans' tables[name].rows.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'].rows.append([link(node), link(g.nodes[node.root[-1]])]) # Get orphaned file entities (no corresponding file) file_ids = [] for entity in EntityMapper.get_by_system_type('file', nodes=True): file_ids.append(str(entity.id)) if not get_file_path(entity): tables['missing_files'].rows.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) for file in os.scandir(app.config['UPLOAD_FOLDER_PATH']): name = file.name if name != '.gitignore' and splitext(file.name)[0] not in file_ids: confirm = ' onclick="return confirm(\'' + _('Delete %(name)s?', name=name) + '\')"' tables['orphaned_files'].rows.append([ name, convert_size(file.stat().st_size), format_date(datetime.datetime.utcfromtimestamp(file.stat().st_ctime)), splitext(name)[1], '<a href="' + url_for('download_file', filename=name) + '">' + uc_first( _('download')) + '</a>', '<a href="' + url_for('admin_file_delete', filename=name) + '" ' + confirm + '>' + uc_first(_('delete')) + '</a>']) return render_template('admin/orphans.html', tables=tables)
def delete_orphans(parameter): from openatlas.models.node import NodeMapper class_codes = tuple(app.config['CODE_CLASS'].keys()) if parameter == 'orphans': class_codes = class_codes + ('E55', ) sql_where = EntityMapper.sql_orphan + " AND e.class_code NOT IN %(class_codes)s" elif parameter == 'unlinked': sql_where = EntityMapper.sql_orphan + " AND e.class_code IN %(class_codes)s" elif parameter == 'types': count = 0 for node in NodeMapper.get_orphans(): EntityMapper.delete(node) count += 1 return count else: return 0 sql = "DELETE FROM model.entity WHERE id IN (" + sql_where + ");" g.cursor.execute(sql, {'class_codes': class_codes}) return g.cursor.rowcount
def delete_orphans(parameter) -> int: from openatlas.models.node import NodeMapper class_codes = tuple(app.config['CODE_CLASS'].keys()) if parameter == 'orphans': class_codes = class_codes + ('E55',) sql_where = EntityMapper.sql_orphan + " AND e.class_code NOT IN %(class_codes)s" elif parameter == 'unlinked': sql_where = EntityMapper.sql_orphan + " AND e.class_code IN %(class_codes)s" elif parameter == 'types': count = 0 for node in NodeMapper.get_orphans(): EntityMapper.delete(node) count += 1 return count else: return 0 sql = 'DELETE FROM model.entity WHERE id IN (' + sql_where + ');' g.cursor.execute(sql, {'class_codes': class_codes}) debug_model['div sql'] += 1 return g.cursor.rowcount
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)