def add_tabs_for_type(entity: Type) -> dict[str, Tab]: tabs = { 'subs': Tab('subs', entity=entity), 'entities': Tab('entities', entity=entity)} for sub_id in entity.subs: sub = g.types[sub_id] tabs['subs'].table.rows.append([ link(sub), sub.count, sub.description]) if entity.category == 'value': tabs['entities'].table.header = \ [_('name'), _('value'), _('class'), _('info')] for item in entity.get_linked_entities( ['P2', 'P89'], inverse=True, types=True): if item.class_.name in ['location', 'reference_system']: continue # pragma: no cover if item.class_.name == 'object_location': item = item.get_linked_entity_safe('P53', inverse=True) data = [link(item)] if entity.category == 'value': data.append(format_number(item.types[entity])) data.append(item.class_.label) data.append(item.description) tabs['entities'].table.rows.append(data) if not tabs['entities'].table.rows: # If no entities available get links with this type_id tabs['entities'].table.header = [_('domain'), _('range')] for row in Link.get_links_by_type(entity): tabs['entities'].table.rows.append([ link(Entity.get_by_id(row['domain_id'])), link(Entity.get_by_id(row['range_id']))]) return tabs
def add_tabs_for_reference_system(entity: ReferenceSystem) -> dict[str, Tab]: tabs = {} for name in entity.classes: tabs[name] = Tab( name, entity=entity, table=Table([_('entity'), 'id', _('precision')])) for link_ in entity.get_links('P67'): name = link_.description if entity.resolver_url: name = \ f'<a href="{entity.resolver_url}{name}"' \ f' target="_blank" rel="noopener noreferrer">{name}</a>' tabs[link_.range.class_.name].table.rows.append([ link(link_.range), name, link_.type.name]) for name in entity.classes: tabs[name].buttons = [] if not tabs[name].table.rows and is_authorized('manager'): tabs[name].buttons = [button( _('remove'), url_for( 'reference_system_remove_class', system_id=entity.id, class_name=name))] return tabs
def profile_index() -> str: tabs = {'profile': Tab( 'profile', content=display_info(get_form_settings(ProfileForm(), True)), buttons=[manual('tools/profile')])} if is_authorized('contributor'): tabs['modules'] = Tab( 'modules', content=display_info(get_form_settings(ModulesForm(), True)), buttons=[manual('tools/profile')]) tabs['display'] = Tab( 'display', content=display_info(get_form_settings(DisplayForm(), True)), buttons=[manual('tools/profile')]) if not app.config['DEMO_MODE']: tabs['profile'].buttons += [ button(_('edit'), url_for('profile_settings', category='profile')), button(_('change password'), url_for('profile_password'))] tabs['modules'].buttons.append( button(_('edit'), url_for('profile_settings', category='modules'))) tabs['display'].buttons.append( button(_('edit'), url_for('profile_settings', category='display'))) return render_template( 'tabs.html', tabs=tabs, title=_('profile'), crumbs=[_('profile')])
def admin_check_dates() -> str: tabs = { 'dates': Tab('invalid_dates', table=Table( ['name', 'class', 'type', 'created', 'updated', 'description'])), 'link_dates': Tab('invalid_link_dates', table=Table(['link', 'domain', 'range'])), 'involvement_dates': Tab('invalid_involvement_dates', table=Table( ['actor', 'event', 'class', 'involvement', 'description'])) } for entity in Entity.get_invalid_dates(): tabs['dates'].table.rows.append([ link(entity), entity.class_.label, link(entity.standard_type), format_date(entity.created), format_date(entity.modified), entity.description ]) for link_ in Link.get_invalid_link_dates(): name = '' if link_.property.code == 'OA7': # pragma: no cover name = 'relation' elif link_.property.code == 'P107': # pragma: no cover name = 'member' elif link_.property.code in ['P11', 'P14', 'P22', 'P23']: name = 'involvement' tabs['link_dates'].table.rows.append([ link( _(name), url_for('link_update', id_=link_.id, origin_id=link_.domain.id)), link(link_.domain), link(link_.range) ]) for link_ in Link.invalid_involvement_dates(): event = link_.domain actor = link_.range data = [ link(actor), link(event), event.class_.label, link_.type.name if link_.type else '', link_.description, link(_('edit'), url_for('link_update', id_=link_.id, origin_id=actor.id)) ] tabs['involvement_dates'].table.rows.append(data) for tab in tabs.values(): tab.buttons = [manual('admin/data_integrity_checks')] if not tab.table.rows: # pragma: no cover tab.content = _('Congratulations, everything looks fine!') return render_template( 'tabs.html', tabs=tabs, title=_('admin'), crumbs=[[_('admin'), f"{url_for('admin_index')}#tab-data"], _('check dates')])
def add_tabs_for_event(entity: Entity) -> dict[str, Tab]: tabs = {} for name in ['subs', 'source', 'actor']: tabs[name] = Tab(name, entity=entity) for sub_event in entity.get_linked_entities( 'P9', inverse=True, types=True): tabs['subs'].table.rows.append(get_base_table_data(sub_event)) tabs['actor'].table.header.insert(5, _('activity')) for link_ in entity.get_links(['P11', 'P14', 'P22', 'P23']): first = link_.first if not link_.first and entity.first: first = f'<span class="inactive">{entity.first}</span>' last = link_.last if not link_.last and entity.last: last = f'<span class="inactive">{entity.last}</span>' tabs['actor'].table.rows.append([ link(link_.range), link_.range.class_.label, link_.type.name if link_.type else '', first, last, g.properties[link_.property.code].name_inverse, link_.description, edit_link( url_for('link_update', id_=link_.id, origin_id=entity.id)), remove_link(link_.range.name, link_, entity, 'actor')]) entity.linked_places = [ location.get_linked_entity_safe('P53', True) for location in entity.get_linked_entities(['P7', 'P26', 'P27'])] return tabs
def note_view(id_: int) -> str: note = User.get_note_by_id(id_) if not note['public'] and note['user_id'] != current_user.id: abort(403) # pragma: no cover entity = Entity.get_by_id(note['entity_id']) buttons: list[str] = [manual('tools/notes')] if note['user_id'] == current_user.id: buttons += [ button(_('edit'), url_for('note_update', id_=note['id'])), button(_('delete'), url_for('note_delete', id_=note['id'])) ] elif is_authorized('manager'): # pragma: no cover buttons += [ button(_('set private'), url_for('note_set_private', id_=note['id'])) ] tabs = { 'info': Tab('info', buttons=buttons, content=f"<h1>{uc_first(_('note'))}</h1>{note['text']}") } return render_template('tabs.html', tabs=tabs, entity=entity, crumbs=[[ _(entity.class_.view), url_for('index', view=entity.class_.view) ], link(entity), _('note')])
def type_delete_recursive(id_: int) -> Union[str, Response]: class DeleteRecursiveTypesForm(FlaskForm): confirm_delete = BooleanField( _("I'm sure to delete this type, it's subs and links"), default=False, validators=[InputRequired()]) save = SubmitField(_('delete types and remove all links')) type_ = g.types[id_] root = g.types[type_.root[0]] if type_.root else None if type_.category in ('standard', 'system', 'place') and not root: abort(403) form = DeleteRecursiveTypesForm() if form.validate_on_submit() and form.confirm_delete.data: for sub_id in Type.get_all_sub_ids(type_): g.types[sub_id].delete() type_.delete() flash(_('types deleted'), 'info') logger.log_user(id_, 'Recursive type delete') return redirect( url_for('view', id_=root.id) if root else url_for('type_index')) tabs = { 'info': Tab('info', content=_( 'Warning: this type has subs and/or links to entities ' '(see tabs). Please check if you want to delete these subs ' 'and links too.'), form=form), 'subs': Tab('subs', entity=type_), 'entities': Tab('entities', entity=type_) } for sub_id in Type.get_all_sub_ids(type_): sub = g.types[sub_id] tabs['subs'].table.rows.append([link(sub), sub.count, sub.description]) for item in get_entities_linked_to_type_recursive(type_.id, []): data = [link(item), item.class_.label, item.description] tabs['entities'].table.rows.append(data) crumbs = [[_('types'), url_for('type_index')]] if root: crumbs += [g.types[type_id] for type_id in type_.root] crumbs += [type_, _('delete')] return render_template('tabs.html', tabs=tabs, crumbs=crumbs)
def add_tabs_for_place(entity: Entity) -> dict[str, Tab]: tabs = {'source': Tab('source', entity=entity)} if entity.class_.name == 'place': tabs['event'] = Tab('event', entity=entity) tabs['reference'] = Tab('reference', entity=entity) if entity.class_.name == 'place': tabs['actor'] = Tab('actor', entity=entity) tabs['feature'] = Tab('feature', entity=entity) elif entity.class_.name == 'feature': tabs['stratigraphic_unit'] = Tab( 'stratigraphic_unit', entity=entity) elif entity.class_.name == 'stratigraphic_unit': tabs['artifact'] = Tab('artifact', entity=entity) tabs['human_remains'] = Tab('human_remains', entity=entity) entity.location = entity.get_linked_entity_safe('P53', types=True) events = [] # Collect events to display actors event_ids = [] # Keep track of event ids to prevent event doubles for event in entity.location.get_linked_entities( ['P7', 'P26', 'P27'], inverse=True): events.append(event) tabs['event'].table.rows.append(get_base_table_data(event)) event_ids.append(event.id) for event in entity.get_linked_entities('P24', inverse=True): if event.id not in event_ids: # Don't add again if already in table tabs['event'].table.rows.append(get_base_table_data(event)) events.append(event) if entity.class_.name == 'place': for link_ in entity.location.get_links( ['P74', 'OA8', 'OA9'], inverse=True): actor = Entity.get_by_id(link_.domain.id) tabs['actor'].table.rows.append([ link(actor), g.properties[link_.property.code].name, actor.class_.name, actor.first, actor.last, actor.description]) actor_ids = [] for event in events: for actor in event.get_linked_entities( ['P11', 'P14', 'P22', 'P23']): if actor.id in actor_ids: continue # pragma: no cover actor_ids.append(actor.id) tabs['actor'].table.rows.append([ link(actor), f"{_('participated at an event')}", event.class_.name, '', '', '']) return tabs
def add_note_tab(entity: Entity) -> Tab: tab = Tab('note', entity=entity) for note in current_user.get_notes_by_entity_id(entity.id): data = [ format_date(note['created']), uc_first(_('public')) if note['public'] else uc_first(_('private')), link(User.get_by_id(note['user_id'])), note['text'], f'<a href="{url_for("note_view", id_=note["id"])}">' f'{uc_first(_("view"))}</a>'] tab.table.rows.append(data) return tab
def import_project_view(id_: int) -> str: project = Import.get_project_by_id(id_) tabs = { 'info': Tab('info', content=render_template('import/project_view.html', project=project)), 'entities': Tab('entities', table=Table(['name', 'class', 'description', 'origin ID', 'date'])) } for entity in Entity.get_by_project_id(id_): tabs['entities'].table.rows.append([ link(entity), entity.class_.label, entity.description, entity.origin_id, format_date(entity.created) ]) return render_template( 'tabs.html', tabs=tabs, title=_('import'), crumbs=[[_('admin'), f"{url_for('admin_index')}#tab-data"], [_('import'), url_for('import_index')], project.name])
def add_tabs_for_reference(entity: Entity) -> dict[str, Tab]: tabs = {} for name in [ 'source', 'event', 'actor', 'place', 'feature', 'stratigraphic_unit', 'human_remains', 'artifact', 'file']: tabs[name] = Tab(name, entity=entity) for link_ in entity.get_links('P67'): range_ = link_.range data = get_base_table_data(range_) data.append(link_.description) data.append(edit_link( url_for('link_update', id_=link_.id, origin_id=entity.id))) data.append( remove_link(range_.name, link_, entity, range_.class_.name)) tabs[range_.class_.view].table.rows.append(data) return tabs
def add_tabs_for_source(entity: Entity) -> dict[str, Tab]: tabs = {} for name in [ 'actor', 'artifact', 'feature', 'event', 'human_remains', 'place', 'stratigraphic_unit', 'text' ]: tabs[name] = Tab(name, entity=entity) for text in entity.get_linked_entities('P73', types=True): tabs['text'].table.rows.append([ link(text), next(iter(text.types)).name if text.types else '', text.description ]) for link_ in entity.get_links('P67'): range_ = link_.range data = get_base_table_data(range_) data.append(remove_link(range_.name, link_, entity, range_.class_.name)) tabs[range_.class_.view].table.rows.append(data) return tabs
def note_view(id_: int) -> str: note = User.get_note_by_id(id_) if not note['public'] and note['user_id'] != current_user.id: abort(403) # pragma: no cover entity = Entity.get_by_id(note['entity_id']) buttons: List[str] = [] if note['user_id'] == current_user.id: buttons = [ button(_('edit'), url_for('note_update', id_=note['id'])), button(_('delete'), url_for('note_delete', id_=note['id']))] elif is_authorized('manager'): # pragma: no cover buttons = [button(_('set private'), url_for('note_set_private', id_=note['id']))] return render_template( 'user/note.html', entity=entity, note=note, tabs={'info': Tab('info')}, buttons=buttons, crumbs=[ [_(entity.class_.view), url_for('index', view=entity.class_.view)], link(entity), _('note')])
def admin_index(action: Optional[str] = None, id_: Optional[int] = None) -> Union[str, Response]: if is_authorized('manager'): if id_ and action == 'delete_user': user = User.get_by_id(id_) if not user \ or user.id == current_user.id \ or (user.group == 'admin' and not is_authorized('admin')): abort(403) User.delete(id_) flash(_('user deleted'), 'info') elif action == 'remove_logo': Settings.set_logo() return redirect(f"{url_for('admin_index')}#tab-file") tables = { 'user': Table([ 'username', 'name', 'group', 'email', 'newsletter', 'created', 'last login', 'entities' ], defs=[{ 'className': 'dt-body-right', 'targets': 7 }]), 'content': Table(['name'] + list(app.config['LANGUAGES'])) } for user in User.get_all(): count = User.get_created_entities_count(user.id) email = user.email \ if is_authorized('manager') or user.settings['show_email'] else '' tables['user'].rows.append([ link(user), user.real_name, user.group, email, _('yes') if user.settings['newsletter'] else '', format_date(user.created), format_date(user.login_last_success), format_number(count) if count else '' ]) for item, languages in get_content().items(): content = [uc_first(_(item))] for language in app.config['LANGUAGES']: content.append(sanitize(languages[language], 'text')) content.append(link(_('edit'), url_for('admin_content', item=item))) tables['content'].rows.append(content) form = None if is_authorized('admin'): form = TestMailForm() if form.validate_on_submit( ) and g.settings['mail']: # pragma: no cover subject = _('Test mail from %(site_name)s', site_name=g.settings['site_name']) body = _('This test mail was sent by %(username)s', username=current_user.username) body += f" {_('at')} '{request.headers['Host']}" if send_mail(subject, body, form.receiver.data): flash( _('A test mail was sent to %(email)s.', email=form.receiver.data), 'info') else: form.receiver.data = current_user.email tabs = { 'files': Tab(_('files'), buttons=[ manual('entity/file'), button(_('edit'), url_for('admin_settings', category='files')) if is_authorized('manager') else '', button(_('list'), url_for('index', view='file')), button(_('file'), url_for('insert', class_='file')) ], content=render_template('admin/file.html', info=get_form_settings(FilesForm()), disk_space_info=get_disk_space_info())), 'user': Tab(_('user'), table=tables['user'], buttons=[ manual('admin/user'), button(_('activity'), url_for('user_activity')), button(_('newsletter'), url_for('admin_newsletter')) if is_authorized('manager') and g.settings['mail'] else '', button(_('user'), url_for('user_insert')) if is_authorized('manager') else '' ]) } if is_authorized('admin'): tabs['general'] = Tab( 'general', content=display_info(get_form_settings(GeneralForm())), buttons=[ manual('admin/general'), button(_('edit'), url_for('admin_settings', category='general')), button(_('system log'), url_for('admin_log')) ]) tabs['email'] = Tab( 'email', content=display_info(get_form_settings(MailForm())), buttons=[ manual('admin/mail'), button(_('edit'), url_for('admin_settings', category='mail')) ]) if g.settings['mail']: tabs['email'].content += display_form(form) if is_authorized('manager'): tabs['modules'] = Tab(_('modules'), content=f""" <h1>{_('Defaults for new user')}</h1> {display_info(get_form_settings(ModulesForm()))}""", buttons=[ manual('admin/modules'), button( _('edit'), url_for('admin_settings', category='modules')) ]) tabs['map'] = Tab('map', content=display_info(get_form_settings(MapForm())), buttons=[ manual('admin/map'), button(_('edit'), url_for('admin_settings', category='map')) ]) tabs['content'] = Tab('content', content=tables['content'].display(), buttons=[manual('admin/content')]) if is_authorized('contributor'): tabs['data'] = Tab('data', content=render_template( 'admin/data.html', imports=Import.get_all_projects(), info=get_form_settings(ApiForm()))) return render_template('tabs.html', tabs=tabs, title=_('admin'), crumbs=[_('admin')])
def admin_orphans() -> str: header = [ 'name', 'class', 'type', 'system type', 'created', 'updated', 'description' ] tabs = { 'orphans': Tab('orphans', table=Table(header)), 'unlinked': Tab('unlinked', table=Table(header)), 'types': Tab('type', table=Table( ['name', 'root'], [[link(type_), link(g.types[type_.root[0]])] for type_ in Type.get_type_orphans()])), 'missing_files': Tab('missing_files', table=Table(header)), 'orphaned_files': Tab('orphaned_files', table=Table(['name', 'size', 'date', 'ext'])), 'circular': Tab('circular_dependencies', table=Table(['entity'], [[link(e)] for e in Entity.get_entities_linked_to_itself()])) } for entity in filter(lambda x: not isinstance(x, ReferenceSystem), Entity.get_orphans()): tabs['unlinked' if entity.class_. view else 'orphans'].table.rows.append([ link(entity), link(entity.class_), link(entity.standard_type), entity.class_.label, format_date(entity.created), format_date(entity.modified), entity.description ]) # Orphaned file entities with no corresponding file entity_file_ids = [] for entity in Entity.get_by_class('file', types=True): entity_file_ids.append(entity.id) if not get_file_path(entity): tabs['missing_files'].table.rows.append([ link(entity), link(entity.class_), link(entity.standard_type), entity.class_.label, format_date(entity.created), format_date(entity.modified), entity.description ]) # Orphaned files with no corresponding entity for file in app.config['UPLOAD_DIR'].iterdir(): if file.name != '.gitignore' \ and os.path.isfile(file) \ and int(file.stem) not in entity_file_ids: tabs['orphaned_files'].table.rows.append([ file.stem, convert_size(file.stat().st_size), format_date( datetime.datetime.utcfromtimestamp(file.stat().st_ctime)), file.suffix, link(_('download'), url_for('download_file', filename=file.name)), delete_link(file.name, url_for('admin_file_delete', filename=file.name)) ]) for tab in tabs.values(): tab.buttons = [manual('admin/data_integrity_checks')] if not tab.table.rows: tab.content = _('Congratulations, everything looks fine!') if tabs['orphaned_files'].table.rows and is_authorized('admin'): text = uc_first(_('delete all files without corresponding entities?')) tabs['orphaned_files'].buttons.append( button(_('delete all files'), url_for('admin_file_delete', filename='all'), onclick=f"return confirm('{text}')")) return render_template( 'tabs.html', tabs=tabs, title=_('admin'), crumbs=[[_('admin'), f"{url_for('admin_index')}#tab-data"], _('orphans')])
def overview() -> str: tabs = { 'info': Tab('info'), 'bookmarks': Tab('bookmarks', table=Table(['name', 'class', 'begin', 'end'])), 'notes': Tab('notes', table=Table( ['date', _('visibility'), 'entity', 'class', _('note')])) } tables = { 'overview': Table(paging=False, defs=[{ 'className': 'dt-body-right', 'targets': 1 }]), 'latest': Table(paging=False, order=[[0, 'desc']]) } if current_user.is_authenticated and hasattr(current_user, 'bookmarks'): for entity_id in current_user.bookmarks: entity = Entity.get_by_id(entity_id) tabs['bookmarks'].table.rows.append([ link(entity), entity.class_.label, entity.first, entity.last, bookmark_toggle(entity.id, True) ]) for note in User.get_notes_by_user_id(current_user.id): entity = Entity.get_by_id(note['entity_id']) tabs['notes'].table.rows.append([ format_date(note['created']), uc_first(_('public') if note['public'] else _('private')), link(entity), entity.class_.label, note['text'], f'<a href="{url_for("note_view", id_=note["id"])}">' f'{uc_first(_("view"))}</a>' ]) for name, count in Entity.get_overview_counts().items(): if count: url = url_for('index', view=g.class_view_mapping[name]) if name == 'administrative_unit': url = f"{url_for('node_index')}#menu-tab-places" elif name == 'type': url = url_for('node_index') elif name == 'find': url = url_for('index', view='artifact') elif name in [ 'feature', 'human_remains', 'stratigraphic_unit', 'source_translation' ]: url = '' tables['overview'].rows.append([ link(g.classes[name].label, url) if url else g.classes[name].label, format_number(count) ]) for entity in Entity.get_latest(8): tables['latest'].rows.append([ format_date(entity.created), link(entity), entity.class_.label, entity.first, entity.last, link(logger.get_log_for_advanced_view(entity.id)['creator']) ]) tabs['info'].content = render_template('index/index.html', intro=get_translation('intro'), tables=tables) return render_template('tabs.html', tabs=tabs, crumbs=['overview'])
def view(id_: int) -> Union[str, Response]: if id_ in g.types: # Types have their own view entity = g.types[id_] if not entity.root: return redirect( f"{url_for('type_index')}" f"#menu-tab-{entity.category}_collapse-{id_}") elif id_ in g.reference_systems: entity = g.reference_systems[id_] else: entity = Entity.get_by_id(id_, types=True, aliases=True) if not entity.class_.view: flash(_("This entity can't be viewed directly."), 'error') abort(400) event_links = None # Needed for actor and info data tabs = {'info': Tab('info')} if isinstance(entity, Type): tabs |= add_tabs_for_type(entity) elif isinstance(entity, ReferenceSystem): tabs |= add_tabs_for_reference_system(entity) elif entity.class_.view == 'actor': event_links = entity.get_links( ['P11', 'P14', 'P22', 'P23', 'P25'], True) tabs |= add_tabs_for_actor(entity, event_links) elif entity.class_.view == 'artifact': tabs['source'] = Tab('source', entity=entity) elif entity.class_.view == 'event': tabs |= add_tabs_for_event(entity) elif entity.class_.view == 'file': tabs |= add_tabs_for_file(entity) elif entity.class_.view == 'place': tabs |= add_tabs_for_place(entity) elif entity.class_.view == 'reference': tabs |= add_tabs_for_reference(entity) elif entity.class_.view == 'source': tabs |= add_tabs_for_source(entity) overlays = None # Needed for place if entity.class_.view in [ 'actor', 'artifact', 'event', 'place', 'source', 'type']: if not isinstance(entity, Type): tabs['reference'] = Tab('reference', entity=entity) if entity.class_.view == 'artifact': tabs['event'] = Tab('event', entity=entity) for link_ in entity.get_links(['P25', 'P108'], True): data = get_base_table_data(link_.domain) tabs['event'].table.rows.append(data) tabs['file'] = Tab('file', entity=entity) entity.image_id = entity.get_profile_image_id() if entity.class_.view == 'place' \ and is_authorized('editor') \ and current_user.settings['module_map_overlay']: tabs['file'].table.header.append(uc_first(_('overlay'))) for link_ in entity.get_links('P67', inverse=True): domain = link_.domain data = get_base_table_data(domain) if domain.class_.view == 'file': # pragma: no cover extension = data[3] data.append( get_profile_image_table_link( domain, entity, extension, entity.image_id)) if not entity.image_id \ and extension in app.config['DISPLAY_FILE_EXTENSIONS']: entity.image_id = domain.id if entity.class_.view == 'place' \ and is_authorized('editor') \ and current_user.settings['module_map_overlay']: overlays = Overlay.get_by_object(entity) if extension in app.config['DISPLAY_FILE_EXTENSIONS']: if domain.id in overlays: data.append(edit_link( url_for( 'overlay_update', id_=overlays[domain.id].id))) else: data.append(link(_('link'), url_for( 'overlay_insert', image_id=domain.id, place_id=entity.id, link_id=link_.id))) else: # pragma: no cover data.append('') if domain.class_.view not in ['source', 'file']: data.append(link_.description) data.append(edit_link( url_for('link_update', id_=link_.id, origin_id=entity.id))) if domain.class_.view == 'reference_system': entity.reference_systems.append(link_) continue data.append( remove_link(domain.name, link_, entity, domain.class_.view)) tabs[domain.class_.view].table.rows.append(data) if 'file' in tabs \ and current_user.settings['table_show_icons'] \ and g.settings['image_processing']: tabs['file'].table.header.insert(1, uc_first(_('icon'))) for row in tabs['file'].table.rows: row.insert( 1, file_preview( int(row[0].replace('<a href="/entity/', '').split('"')[0])) ) place_structure = None gis_data = None if entity.class_.view in ['artifact', 'place']: place_structure = get_structure(entity) if place_structure: for item in place_structure['subunits']: tabs[item.class_.name].table.rows.append( get_base_table_data(item)) gis_data = Gis.get_all([entity], place_structure) if gis_data['gisPointSelected'] == '[]' \ and gis_data['gisPolygonSelected'] == '[]' \ and gis_data['gisLineSelected'] == '[]' \ and (not place_structure or not place_structure['super_id']): gis_data = {} entity.info_data = get_entity_data(entity, event_links=event_links) if not gis_data: # Has to be after get_entity_data() gis_data = Gis.get_all(entity.linked_places) \ if entity.linked_places else None problematic_type_id = entity.check_for_too_many_links_for_single_type() tabs['note'] = add_note_tab(entity) tabs['info'].content = render_template( 'entity/view.html', buttons=add_buttons(entity, bool(problematic_type_id)), entity=entity, gis_data=gis_data, structure=place_structure, overlays=overlays, title=entity.name, problematic_type_id=problematic_type_id) return render_template( 'tabs.html', tabs=tabs, gis_data=gis_data, crumbs=add_crumbs(entity, place_structure), entity=entity)
def entity_view(id_: int) -> Union[str, Response]: if id_ in g.nodes: # Nodes have their own view entity = g.nodes[id_] if not entity.root: if entity.class_.name == 'administrative_unit': tab_hash = '#menu-tab-places_collapse-' elif entity.standard: tab_hash = '#menu-tab-standard_collapse-' elif entity.value_type: tab_hash = '#menu-tab-value_collapse-' else: tab_hash = '#menu-tab-custom_collapse-' return redirect(f"{url_for('node_index')}{tab_hash}{id_}") elif id_ in g.reference_systems: entity = g.reference_systems[id_] else: entity = Entity.get_by_id(id_, nodes=True, aliases=True) if not entity.class_.view: flash(_("This entity can't be viewed directly."), 'error') abort(400) event_links = None # Needed for actor overlays = None # Needed for place tabs = {'info': Tab('info')} if isinstance(entity, Node): tabs['subs'] = Tab('subs', entity=entity) tabs['entities'] = Tab('entities', entity=entity) root = g.nodes[entity.root[-1]] if entity.root else None if root and root.value_type: # pragma: no cover tabs['entities'].table.header = [ _('name'), _('value'), _('class'), _('info') ] for item in entity.get_linked_entities(['P2', 'P89'], inverse=True, nodes=True): if item.class_.name in ['location', 'reference_system']: continue # pragma: no cover if item.class_.name == 'object_location': # pragma: no cover item = item.get_linked_entity_safe('P53', inverse=True) data = [link(item)] if root and root.value_type: # pragma: no cover data.append(format_number(item.nodes[entity])) data.append(item.class_.label) data.append(item.description) tabs['entities'].table.rows.append(data) for sub_id in entity.subs: sub = g.nodes[sub_id] tabs['subs'].table.rows.append( [link(sub), sub.count, sub.description]) if not tabs['entities'].table.rows: # If no entities available get links with this type_id tabs['entities'].table.header = [_('domain'), _('range')] for row in Link.get_entities_by_node(entity): tabs['entities'].table.rows.append([ link(Entity.get_by_id(row['domain_id'])), link(Entity.get_by_id(row['range_id'])) ]) elif isinstance(entity, ReferenceSystem): for form_id, form in entity.get_forms().items(): tabs[form['name']] = Tab(form['name'], entity=entity) tabs[form['name']].table = \ Table([_('entity'), 'id', _('precision')]) for link_ in entity.get_links('P67'): name = link_.description if entity.resolver_url: name = \ f'<a href="{entity.resolver_url + name}"' \ f' target="_blank" rel="noopener noreferrer">{name}</a>' tab_name = link_.range.class_.name tabs[tab_name].table.rows.append( [link(link_.range), name, link_.type.name]) for form_id, form in entity.get_forms().items(): tabs[form['name']].buttons = [] if not tabs[form['name']].table.rows and is_authorized('manager'): tabs[form['name']].buttons = [ button( _('remove'), url_for('reference_system_remove_form', system_id=entity.id, form_id=form_id)) ] elif entity.class_.view == 'actor': for name in [ 'source', 'event', 'relation', 'member_of', 'member', 'artifact' ]: tabs[name] = Tab(name, entity=entity) event_links = entity.get_links(['P11', 'P14', 'P22', 'P23', 'P25'], True) for link_ in event_links: event = link_.domain places = event.get_linked_entities(['P7', 'P26', 'P27']) link_.object_ = None # Needed for first/last appearance for place in places: object_ = place.get_linked_entity_safe('P53', True) entity.linked_places.append(object_) link_.object_ = object_ first = link_.first if not link_.first and event.first: first = f'<span class="inactive">{event.first}</span>' last = link_.last if not link_.last and event.last: last = f'<span class="inactive">{event.last}</span>' data = [ link(event), event.class_.label, _('moved') if link_.property.code == 'P25' else link( link_.type), first, last, link_.description ] if link_.property.code == 'P25': data += [''] else: add_edit_link( data, url_for('involvement_update', id_=link_.id, origin_id=entity.id)) add_remove_link(data, link_.domain.name, link_, entity, 'event') tabs['event'].table.rows.append(data) for link_ in entity.get_links('OA7') + entity.get_links('OA7', True): type_ = '' if entity.id == link_.domain.id: related = link_.range if link_.type: type_ = link(link_.type.get_name_directed(), url_for('entity_view', id_=link_.type.id)) else: related = link_.domain if link_.type: type_ = link(link_.type.get_name_directed(True), url_for('entity_view', id_=link_.type.id)) data = [ type_, link(related), link_.first, link_.last, link_.description ] add_edit_link( data, url_for('relation_update', id_=link_.id, origin_id=entity.id)) add_remove_link(data, related.name, link_, entity, 'relation') tabs['relation'].table.rows.append(data) for link_ in entity.get_links('P107', True): data = [ link(link_.domain), link(link_.type), link_.first, link_.last, link_.description ] add_edit_link( data, url_for('member_update', id_=link_.id, origin_id=entity.id)) add_remove_link(data, link_.domain.name, link_, entity, 'member-of') tabs['member_of'].table.rows.append(data) if entity.class_.name != 'group': del tabs['member'] else: for link_ in entity.get_links('P107'): data = [ link(link_.range), link(link_.type), link_.first, link_.last, link_.description ] add_edit_link( data, url_for('member_update', id_=link_.id, origin_id=entity.id)) add_remove_link(data, link_.range.name, link_, entity, 'member') tabs['member'].table.rows.append(data) for link_ in entity.get_links('P52', True): data = [ link(link_.domain), link_.domain.class_.label, link(link_.domain.standard_type), link_.domain.first, link_.domain.last, link_.domain.description ] tabs['artifact'].table.rows.append(data) elif entity.class_.view == 'artifact': tabs['source'] = Tab('source', entity=entity) elif entity.class_.view == 'event': for name in ['subs', 'source', 'actor']: tabs[name] = Tab(name, entity=entity) for sub_event in entity.get_linked_entities('P117', inverse=True, nodes=True): tabs['subs'].table.rows.append(get_base_table_data(sub_event)) tabs['actor'].table.header.insert(5, _('activity')) # Activity column for link_ in entity.get_links(['P11', 'P14', 'P22', 'P23']): first = link_.first if not link_.first and entity.first: first = f'<span class="inactive">{entity.first}</span>' last = link_.last if not link_.last and entity.last: last = f'<span class="inactive">{entity.last}</span>' data = [ link(link_.range), link_.range.class_.label, link_.type.name if link_.type else '', first, last, g.properties[link_.property.code].name_inverse, link_.description ] add_edit_link( data, url_for('involvement_update', id_=link_.id, origin_id=entity.id)) add_remove_link(data, link_.range.name, link_, entity, 'actor') tabs['actor'].table.rows.append(data) entity.linked_places = [ location.get_linked_entity_safe('P53', True) for location in entity.get_linked_entities(['P7', 'P26', 'P27']) ] elif entity.class_.view == 'file': for name in [ 'source', 'event', 'actor', 'place', 'feature', 'stratigraphic_unit', 'artifact', 'human_remains', 'reference', 'type' ]: tabs[name] = Tab(name, entity=entity) entity.image_id = entity.id if get_file_path(entity.id) else None for link_ in entity.get_links('P67'): range_ = link_.range data = get_base_table_data(range_) add_remove_link(data, range_.name, link_, entity, range_.class_.name) tabs[range_.class_.view].table.rows.append(data) for link_ in entity.get_links('P67', True): data = get_base_table_data(link_.domain) data.append(link_.description) add_edit_link( data, url_for('reference_link_update', link_id=link_.id, origin_id=entity.id)) add_remove_link(data, link_.domain.name, link_, entity, 'reference') tabs['reference'].table.rows.append(data) elif entity.class_.view == 'place': tabs['source'] = Tab('source', entity=entity) if entity.class_.name == 'place': tabs['event'] = Tab('event', entity=entity) tabs['reference'] = Tab('reference', entity=entity) if entity.class_.name == 'place': tabs['actor'] = Tab('actor', entity=entity) tabs['feature'] = Tab('feature', entity=entity) elif entity.class_.name == 'feature': tabs['stratigraphic_unit'] = Tab('stratigraphic_unit', entity=entity) elif entity.class_.name == 'stratigraphic_unit': tabs['find'] = Tab('find', entity=entity) tabs['human_remains'] = Tab('human_remains', entity=entity) entity.location = entity.get_linked_entity_safe('P53', nodes=True) event_ids = [] # Keep track of inserted events to prevent doubles for event in entity.location.get_linked_entities(['P7', 'P26', 'P27'], inverse=True): tabs['event'].table.rows.append(get_base_table_data(event)) event_ids.append(event.id) for event in entity.get_linked_entities('P24', inverse=True): if event.id not in event_ids: # Don't add again if already in table tabs['event'].table.rows.append(get_base_table_data(event)) if 'actor' in tabs: for link_ in entity.location.get_links(['P74', 'OA8', 'OA9'], inverse=True): actor = Entity.get_by_id(link_.domain.id) tabs['actor'].table.rows.append([ link(actor), g.properties[link_.property.code].name, actor.class_.name, actor.first, actor.last, actor.description ]) elif entity.class_.view == 'reference': for name in [ 'source', 'event', 'actor', 'place', 'feature', 'stratigraphic_unit', 'human_remains', 'artifact', 'file' ]: tabs[name] = Tab(name, entity=entity) for link_ in entity.get_links('P67'): range_ = link_.range data = get_base_table_data(range_) data.append(link_.description) add_edit_link( data, url_for('reference_link_update', link_id=link_.id, origin_id=entity.id)) add_remove_link(data, range_.name, link_, entity, range_.class_.name) tabs[range_.class_.view].table.rows.append(data) elif entity.class_.view == 'source': for name in [ 'actor', 'artifact', 'feature', 'event', 'human_remains', 'place', 'stratigraphic_unit', 'text' ]: tabs[name] = Tab(name, entity=entity) for text in entity.get_linked_entities('P73', nodes=True): tabs['text'].table.rows.append([ link(text), next(iter(text.nodes)).name if text.nodes else '', text.description ]) for link_ in entity.get_links('P67'): range_ = link_.range data = get_base_table_data(range_) add_remove_link(data, range_.name, link_, entity, range_.class_.name) tabs[range_.class_.view].table.rows.append(data) if entity.class_.view in [ 'actor', 'artifact', 'event', 'place', 'source', 'type' ]: if entity.class_.view != 'reference' and not isinstance(entity, Node): tabs['reference'] = Tab('reference', entity=entity) if entity.class_.view == 'artifact': tabs['event'] = Tab('event', entity=entity) for link_ in entity.get_links('P25', True): data = get_base_table_data(link_.domain) tabs['event'].table.rows.append(data) tabs['file'] = Tab('file', entity=entity) entity.image_id = entity.get_profile_image_id() if entity.class_.view == 'place' and is_authorized('editor') and \ current_user.settings['module_map_overlay']: tabs['file'].table.header.append(uc_first(_('overlay'))) for link_ in entity.get_links('P67', inverse=True): domain = link_.domain data = get_base_table_data(domain) if domain.class_.view == 'file': # pragma: no cover extension = data[3] data.append( get_profile_image_table_link(domain, entity, extension, entity.image_id)) if not entity.image_id \ and extension in app.config['DISPLAY_FILE_EXTENSIONS']: entity.image_id = domain.id if entity.class_.view == 'place' \ and is_authorized('editor') \ and current_user.settings['module_map_overlay']: overlays = Overlay.get_by_object(entity) if extension in app.config['DISPLAY_FILE_EXTENSIONS']: if domain.id in overlays: add_edit_link( data, url_for('overlay_update', id_=overlays[domain.id].id)) else: data.append( link( _('link'), url_for('overlay_insert', image_id=domain.id, place_id=entity.id, link_id=link_.id))) else: # pragma: no cover data.append('') if domain.class_.view not in ['source', 'file']: data.append(link_.description) add_edit_link( data, url_for('reference_link_update', link_id=link_.id, origin_id=entity.id)) if domain.class_.view == 'reference_system': entity.reference_systems.append(link_) continue add_remove_link(data, domain.name, link_, entity, domain.class_.view) tabs[domain.class_.view].table.rows.append(data) structure = None # Needed for place gis_data = None # Needed for place if entity.class_.view in ['artifact', 'place']: structure = get_structure(entity) if structure: for item in structure['subunits']: tabs[item.class_.name].table.rows.append( get_base_table_data(item)) gis_data = Gis.get_all([entity], structure) if gis_data['gisPointSelected'] == '[]' \ and gis_data['gisPolygonSelected'] == '[]' \ and gis_data['gisLineSelected'] == '[]' \ and (not structure or not structure['super_id']): gis_data = {} if not gis_data: gis_data = Gis.get_all(entity.linked_places) \ if entity.linked_places else None entity.info_data = get_entity_data(entity, event_links=event_links) tabs['note'] = Tab('note', entity=entity) for note in current_user.get_notes_by_entity_id(entity.id): data = [ format_date(note['created']), uc_first(_('public')) if note['public'] else uc_first(_('private')), link(User.get_by_id(note['user_id'])), note['text'], f'<a href="{url_for("note_view", id_=note["id"])}">' f'{uc_first(_("view"))}</a>' ] tabs['note'].table.rows.append(data) if 'file' in tabs and current_user.settings['table_show_icons'] and \ session['settings']['image_processing']: tabs['file'].table.header.insert(1, uc_first(_('icon'))) for row in tabs['file'].table.rows: row.insert( 1, file_preview( int(row[0].replace('<a href="/entity/', '').split('"')[0]))) tabs['info'].content = render_template( 'entity/view.html', buttons=add_buttons(entity), entity=entity, gis_data=gis_data, structure=structure, # Needed for place views overlays=overlays, # Needed for place views title=entity.name) return render_template('tabs.html', tabs=tabs, gis_data=gis_data, crumbs=add_crumbs(entity, structure), entity=entity)
def add_tabs_for_actor( entity: Entity, event_links: list[Link]) -> dict[str, Tab]: tabs = {} for name in [ 'source', 'event', 'relation', 'member_of', 'member', 'artifact']: tabs[name] = Tab(name, entity=entity) for link_ in event_links: event = link_.domain link_.object_ = None # Needed for first/last appearance for place in event.get_linked_entities(['P7', 'P26', 'P27']): object_ = place.get_linked_entity_safe('P53', True) entity.linked_places.append(object_) link_.object_ = object_ first = link_.first if not link_.first and event.first: first = f'<span class="inactive">{event.first}</span>' last = link_.last if not link_.last and event.last: last = f'<span class="inactive">{event.last}</span>' data = [ link(event), event.class_.label, _('moved') if link_.property.code == 'P25' else link(link_.type), first, last, link_.description] if link_.property.code == 'P25': data.append('') else: data.append(edit_link( url_for('link_update', id_=link_.id, origin_id=entity.id))) data.append(remove_link(link_.domain.name, link_, entity, 'event')) tabs['event'].table.rows.append(data) for link_ in entity.get_links('OA7') + entity.get_links('OA7', True): type_ = '' if entity.id == link_.domain.id: related = link_.range if link_.type: type_ = link( link_.type.get_name_directed(), url_for('view', id_=link_.type.id)) else: related = link_.domain if link_.type: type_ = link( link_.type.get_name_directed(True), url_for('view', id_=link_.type.id)) tabs['relation'].table.rows.append([ type_, link(related), link_.first, link_.last, link_.description, edit_link( url_for('link_update', id_=link_.id, origin_id=entity.id)), remove_link(related.name, link_, entity, 'relation')]) for link_ in entity.get_links('P107', True): data = [ link(link_.domain), link(link_.type), link_.first, link_.last, link_.description, edit_link( url_for('member_update', id_=link_.id, origin_id=entity.id)), remove_link(link_.domain.name, link_, entity, 'member-of')] tabs['member_of'].table.rows.append(data) if entity.class_.name != 'group': del tabs['member'] else: for link_ in entity.get_links('P107'): tabs['member'].table.rows.append([ link(link_.range), link(link_.type), link_.first, link_.last, link_.description, edit_link( url_for('member_update', id_=link_.id, origin_id=entity.id) ), remove_link(link_.range.name, link_, entity, 'member')]) for link_ in entity.get_links('P52', True): data = [ link(link_.domain), link_.domain.class_.label, link(link_.domain.standard_type), link_.domain.first, link_.domain.last, link_.domain.description] tabs['artifact'].table.rows.append(data) return tabs