Modifying datacenterasset view #3419

Hi all,
what is the preferred way to modify the datacenterasset view, e.g. at
http://example.org/data_center/datacenterasset/
I’d like to have less/other fields visible.
Thanks!

Hi
If you want to have less or other fields visible.
Only modify the field list in source code.(ralph/src/ralph/data_center/admin.py)

@register(DataCenterAsset)
class DataCenterAssetAdmin(
・・・
・・・
・・・
fieldsets = (


If you don’t want to loose updates, the recommended way is to hide it through permissions.
It will disapper from GUI from the user perspective.

Thanks guys, I found the relevant parts of the code! I fiddled with the rights system, but I quickly produced something non-usable the last time. An ability to not see parts of ralph that you don’t want to use would be nice. (e.g. IMEIs…) This could be done exploiting the existing rights system, I think.

For reference for other people, here’s what I have done:

@register(DataCenterAsset)
class DataCenterAssetAdmin(
    SCMStatusCheckInChangeListMixin,
    ScanStatusInChangeListMixin,
    ActiveDeploymentMessageMixin,
    MulitiAddAdminMixin,
    TransitionAdminMixin,
    BulkEditChangeListMixin,
    AttachmentsMixin,
    AssetInvoiceReportMixin,
    CustomFieldValueAdminMixin,
    RalphAdmin,
):
    """Data Center Asset admin class."""

    add_form_template = 'data_center/datacenterasset/add_form.html'
    actions = ['bulk_edit_action']
    change_views = [
        DataCenterAssetComponents,
        DataCenterAssetNetworkView,
        DataCenterAssetSecurityInfo,
        DataCenterAssetSCMInfo,
        DataCenterAssetRelationsView,
        DataCenterAssetLicence,
        DataCenterAssetSupport,
        DataCenterAssetOperation,
    ]
    form = DataCenterAssetForm
    if settings.ENABLE_DNSAAS_INTEGRATION:
        change_views += [DNSView]
    show_transition_history = True
    resource_class = resources.DataCenterAssetResource
    list_display = [
        'hostname',
        'status',
        'barcode',
        'model',
        'sn',
#        'invoice_date',
#        'invoice_no',
        'show_location',
        'service_env',
        'configuration_path',
#        'scan_status',
#        'scm_status_check'
    ]
    multiadd_summary_fields = list_display + ['rack']
    one_of_mulitvalue_required = ['sn', 'barcode']
    bulk_edit_list = [
        'hostname', 'status', 'barcode', 'model', 'sn', 'invoice_date',
        'invoice_no', 'rack', 'orientation', 'position', 'slot_no', 'price',
        'provider', 'service_env', 'configuration_path', 'tags', 'start_usage'
    ]
    bulk_edit_no_fillable = ['barcode', 'sn']
    search_fields = [
        'barcode', 'sn', 'hostname',# 'invoice_no', 'order_no',
        'ethernet_set__ipaddress__address', 'ethernet_set__ipaddress__hostname'
    ]
    list_filter_prefix = ['hostname']
    list_filter_postfix = [
#        'invoice_no', 'invoice_date', 'status',
 'barcode', 'sn',
        'order_no', 'model__name',
        ('model__category', RelatedAutocompleteFieldListFilter),
#        'depreciation_end_date', 'force_depreciation', 'remarks',
#        'budget_info', 'rack', 'rack__server_room',
        'rack__server_room__data_center', 'position', 'property_of',
#        LiquidatedStatusFilter, TagsListFilter,
#        'fibrechannelcard_set__wwn'
    ]
    list_filter = generate_list_filter_with_common_fields(
        list_filter_prefix,
        list_filter_postfix
    )
    date_hierarchy = 'created'
    list_select_related = [
        'model',
        'model__manufacturer',
        'model__category',
        'rack',
        'rack__server_room',
        'rack__server_room__data_center',
        'service_env',
        'service_env__service',
        'service_env__environment',
        'configuration_path',
    ]
    raw_id_fields = [
        'model', 'rack', 'service_env', 'parent', 'budget_info',
        'configuration_path',
    ]
    raw_id_override_parent = {'parent': DataCenterAsset}
    _invoice_report_name = 'invoice-data-center-asset'
    readonly_fields = ['get_created_date', 'go_to_visualization']

    fieldsets = (
        (_('Basic info'), {
            'fields': (
                'hostname', 'model', 'status', 'barcode', 'sn', 'niw',
                'required_support', 'remarks', 'tags', 'property_of',
                'firmware_version', 'bios_version',
            )
        }),
        (_('Location Info'), {
            'fields': (
                'rack', 'position', 'orientation', 'slot_no', 'parent',
                'management_ip', 'management_hostname', 'go_to_visualization'
            )
        }),
        (_('Usage info'), {
            'fields': (
                'service_env', 'configuration_path', 'production_year',
                'production_use_date',
            )
        }),
        (_('Financial & Order Info'), {
            'fields': (
                'order_no',# 'invoice_date', 'invoice_no', 'task_url', 'price',
#                'depreciation_rate', 'depreciation_end_date',
#                'force_depreciation', 'source', 'provider', 'delivery_date',
#                'budget_info', 'start_usage', 'get_created_date',
            )
        }),
    )

    def get_multiadd_fields(self, obj=None):
        multiadd_fields = [
            {'field': 'sn', 'allow_duplicates': False},
            {'field': 'barcode', 'allow_duplicates': False},
        ]
        return getattr(
            settings, 'MULTIADD_DATA_CENTER_ASSET_FIELDS', None
        ) or multiadd_fields

    def go_to_visualization(self, obj):
        if not obj.rack:
            return '—'
        url = '{}#/sr/{}/rack/{}'.format(
            reverse('dc_view'),
            obj.rack.server_room_id,
            obj.rack.id,
        )
        label = ' / '.join(obj.get_location())
        return generate_html_link(url, label=label, params={})
    go_to_visualization.short_description = _('Visualization')
    go_to_visualization.allow_tags = True

    def show_location(self, obj):
        return obj.location
    show_location.short_description = _('Location')
    show_location.allow_tags = True

    # NOTE(romcheg): Django Admin can only order custom fields by one field.
    #                The rest of the ordering is configured in
    #                DataCenterAssetChangeList.get_ordering()
    show_location.admin_order_field = 'slot_no'

    def get_created_date(self, obj):
        """
        Return created date for asset (since created is blacklisted by
        permissions, it cannot be displayed directly, because only superuser
        will see it)
        """
        return obj.created or '-'
    get_created_date.short_description = _('Created at')

    def get_changelist(self, request, **kwargs):
        return DataCenterAssetChangeList