Skip to content

Commit

Permalink
Fix html not rendering in some cases
Browse files Browse the repository at this point in the history
  • Loading branch information
hipek8 committed Jul 3, 2024
1 parent 3cf16d1 commit 2867b63
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 42 deletions.
3 changes: 2 additions & 1 deletion src/ralph/accounts/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from django.forms.models import model_to_dict
from django.urls import reverse
from django.utils.functional import cached_property
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
from reversion import revisions as reversion

Expand Down Expand Up @@ -365,6 +366,7 @@ def ldap_mapping(self, obj):
return self._ldap_groups.get(obj.name, '-')
ldap_mapping.short_description = _('LDAP mapping')

@mark_safe
def users_list(self, obj):
users = []
for u in obj.user_set.order_by('username'):
Expand All @@ -374,7 +376,6 @@ def users_list(self, obj):
))
return '<br>'.join(users)
users_list.short_description = _('Users list')
users_list.allow_tags = True

def formfield_for_manytomany(self, db_field, request=None, **kwargs):
if db_field.name == 'permissions':
Expand Down
3 changes: 2 additions & 1 deletion src/ralph/admin/templatetags/admin_change_list.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib.admin.views.main import PAGE_VAR
from django.template import Library
from django.utils.html import format_html
from django.utils.safestring import mark_safe

register = Library()

Expand All @@ -16,7 +17,7 @@ def admin_paginator_number(cl, i):
Wraps every entry in <li> tag comparing to regular Django pagination.
"""
if i == DOT:
return '<li>{}</li>'.format(DOTS)
return mark_safe('<li>{}</li>'.format(DOTS))
elif i == cl.page_num:
return format_html(
'<li class="current"><a href="#">{}</a></li> ', i + 1)
Expand Down
5 changes: 3 additions & 2 deletions src/ralph/assets/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
from django.db.models import Count
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _

from ralph.admin.decorators import register
Expand Down Expand Up @@ -90,6 +91,7 @@ class ConfigurationModuleAdmin(CustomFieldValueAdminMixin, RalphMPTTAdmin):
)
show_custom_fields_values_summary = False

@mark_safe
def show_children_modules(self, module):
if not module or not module.pk:
return '&ndash;'
Expand All @@ -98,9 +100,9 @@ def show_children_modules(self, module):
['name'],
url_field='name'
).render()
show_children_modules.allow_tags = True
show_children_modules.short_description = _('Children modules')

@mark_safe
def show_children_classes(self, module):
if not module or not module.pk:
return '&ndash;'
Expand All @@ -109,7 +111,6 @@ def show_children_classes(self, module):
['class_name'],
url_field='class_name'
).render()
show_children_classes.allow_tags = True
show_children_classes.short_description = _('Children classes')

def get_readonly_fields(self, request, obj=None):
Expand Down
5 changes: 3 additions & 2 deletions src/ralph/dashboards/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.urls import reverse
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext as _

from ralph.admin.decorators import register
Expand Down Expand Up @@ -88,6 +89,7 @@ class GraphAdmin(RalphAdmin):
}),
)

@mark_safe
def get_readonly_fields(self, *args, **kwargs):
readonly_fields = super().get_readonly_fields(*args, **kwargs)
allow_push_graphs_data_to_statsd = (
Expand All @@ -101,17 +103,16 @@ def get_readonly_fields(self, *args, **kwargs):
def get_preview(self, obj):
return obj.render(name='preview')

get_preview.allow_tags = True
get_preview.short_description = _('Graph')


@register(Dashboard)
class DashboardAdmin(RalphAdmin):
list_display = ['name', 'description', 'active', 'get_link']

@mark_safe
def get_link(self, obj):
return _('<a href="{}" target="_blank">Dashboard</a>'.format(reverse(
'dashboard_view', args=(obj.pk,)
)))
get_link.short_description = _('Link')
get_link.allow_tags = True
6 changes: 3 additions & 3 deletions src/ralph/data_center/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ def get_fieldsets(self, request, obj=None):
),)
return fieldsets

@mark_safe
def get_masters_summary(self, obj):
masters = obj.masters
if not masters:
Expand All @@ -256,7 +257,6 @@ def get_masters_summary(self, obj):
getattr(masters[0], '_summary_fields', []),
transpose=True,
).render()
get_masters_summary.allow_tags = True
get_masters_summary.short_description = _('Master info')


Expand Down Expand Up @@ -524,6 +524,7 @@ def get_multiadd_fields(self, obj=None):
settings, 'MULTIADD_DATA_CENTER_ASSET_FIELDS', None
) or multiadd_fields

@mark_safe
def go_to_visualization(self, obj):
if not obj.rack:
return '&mdash;'
Expand All @@ -535,12 +536,11 @@ def go_to_visualization(self, obj):
label = '&nbsp;/&nbsp;'.join(obj.get_location())
return generate_html_link(url, label=label, params={})
go_to_visualization.short_description = _('Visualization')
go_to_visualization.allow_tags = True

@mark_safe
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
Expand Down
2 changes: 1 addition & 1 deletion src/ralph/dc_view/serializers/models_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class Meta(RackBaseSerializer.Meta):


class SRSerializer(AdminLinkMixin, serializers.ModelSerializer):
rack_set = RackSerializer(many=True)
rack_set = RackSerializer(many=True, source='racks')
admin_link = serializers.SerializerMethodField('admin_link')

class Meta:
Expand Down
3 changes: 2 additions & 1 deletion src/ralph/dhcp/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.db.models import Prefetch
from django.template.defaultfilters import date, timesince_filter
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _

from ralph.admin.decorators import register
Expand Down Expand Up @@ -67,6 +68,7 @@ def servers_formatted(self, obj):
])
servers_formatted.short_description = 'DNS Servers'

@mark_safe
def networks(self, obj):
networks = obj.networks.all()
if networks:
Expand All @@ -79,4 +81,3 @@ def networks(self, obj):
result = '&ndash;'
return result
networks.short_description = _('in networks')
networks.allow_tags = True
15 changes: 8 additions & 7 deletions src/ralph/networks/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.db.models import Count, F, Prefetch
from django.urls import reverse
from django.utils.html import escape
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _

from ralph.admin.decorators import register
Expand Down Expand Up @@ -191,6 +192,7 @@ def ipaddresses_count(self, obj):
ipaddresses_count.short_description = _('IPAddress count')
ipaddresses_count.admin_order_field = 'ipaddress_count'

@mark_safe
def show_parent_networks(self, network):
if not network or not network.pk:
return '&ndash;'
Expand All @@ -202,16 +204,16 @@ def show_parent_networks(self, network):
))
return ' <br /> '.join(nodes_link)
show_parent_networks.short_description = _('Parent networks')
show_parent_networks.allow_tags = True

@mark_safe
def show_first_free_ip(self, network):
if not network or not network.pk:
return '&ndash;'
free_ip = network.get_first_free_ip()
return str(free_ip) if free_ip else '&ndash;'
show_first_free_ip.short_description = _('First free IP')
show_first_free_ip.allow_tags = True

@mark_safe
def show_subnetworks(self, network):
if not network or not network.pk:
return '&ndash;'
Expand All @@ -221,9 +223,9 @@ def show_subnetworks(self, network):
['name', 'address'],
url_field='name'
).render()
show_subnetworks.allow_tags = True
show_subnetworks.short_description = _('Subnetworks')

@mark_safe
def show_addresses(self, network):
if not network or not network.pk:
return '&ndash;'
Expand All @@ -242,7 +244,6 @@ def show_addresses(self, network):
['address', 'linked_object'],
url_field='address'
).render()
show_addresses.allow_tags = True
show_addresses.short_description = _('Addresses')

def get_queryset(self, request):
Expand Down Expand Up @@ -300,6 +301,7 @@ class IPAddressAdmin(ParentChangeMixin, RalphAdmin):
add_message = _('IP added to <a href="{}" _target="blank">{}</a>')
change_message = _('IP reassigned from network <a href="{}" target="_blank">{}</a> to <a href="{}" target="_blank">{}</a>') # noqa

@mark_safe
def get_network_path(self, obj):
if not obj.network:
return None
Expand All @@ -311,7 +313,6 @@ def get_network_path(self, obj):
))
return ' > '.join(nodes_link)
get_network_path.short_description = _('Network')
get_network_path.allow_tags = True

def get_queryset(self, request):
# use Prefetch like select-related to get base_objects with custom
Expand All @@ -321,15 +322,15 @@ def get_queryset(self, request):
queryset=BaseObject.polymorphic_objects.all())
)

@mark_safe
def ip_address(self, obj):
return '<a href="{}">{}</a>'.format(
obj.get_absolute_url(), escape(obj.address)
)
ip_address.short_description = _('IP address')
ip_address.admin_order_field = 'number'
ip_address.allow_tags = True

@mark_safe
def base_object_link(self, obj):
return ip_address_base_object_link(obj)
base_object_link.short_description = _('Linked object')
base_object_link.allow_tags = True
3 changes: 2 additions & 1 deletion src/ralph/networks/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _

from ralph.admin.m2m import RalphTabularM2MInline
Expand Down Expand Up @@ -44,6 +45,7 @@ class NetworkView(RalphDetailViewAdmin):
]
fields = ('available_networks', )

@mark_safe
def available_networks(self, instance):
networks = instance._get_available_networks(
as_query=True
Expand All @@ -58,7 +60,6 @@ def available_networks(self, instance):
result = '&ndash;'
return result
available_networks.short_description = _('Available networks')
available_networks.allow_tags = True
fieldsets = (
(_(''), {
'fields': (
Expand Down
8 changes: 5 additions & 3 deletions src/ralph/operations/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from django.db.models import DateTimeField, Prefetch
from django.utils import timezone
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _

from ralph.admin.decorators import register
Expand Down Expand Up @@ -87,14 +88,15 @@ def _get_related_objects(self, obj, field):
for name, count in objects.most_common()
])


@mark_safe
def get_services(self, obj):
return self._get_related_objects(obj, 'service_env')
get_services.allow_tags = True
get_services.short_description = _('services')

@mark_safe
def get_configuration_path(self, obj):
return self._get_related_objects(obj=obj, field='configuration_path')
get_configuration_path.allow_tags = True
get_configuration_path.short_description = _('configuration path')

def get_queryset(self, request):
Expand Down Expand Up @@ -148,12 +150,12 @@ class OperationAdmin(
}),
)

@mark_safe
def get_ticket_url(self, obj):
return '<a href="{ticket_url}" target="_blank">{ticket_id}</a>'.format(
ticket_url=obj.ticket_url,
ticket_id=obj.ticket_id
)
get_ticket_url.allow_tags = True
get_ticket_url.short_description = _('ticket ID')
get_ticket_url.admin_order_field = 'ticket_id'

Expand Down
3 changes: 2 additions & 1 deletion src/ralph/operations/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django import forms
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _

from ralph.admin.m2m import RalphTabularM2MInline
Expand Down Expand Up @@ -37,12 +38,12 @@ def get_readonly_fields(self, request, obj=None):
def has_add_permission(self, request):
return False

@mark_safe
def get_ticket_url(self, obj):
return '<a href="{ticket_url}" target="_blank">{ticket_id}</a>'.format(
ticket_url=obj.ticket_url,
ticket_id=obj.ticket_id
)
get_ticket_url.allow_tags = True
get_ticket_url.short_description = _('ticket ID')


Expand Down
5 changes: 3 additions & 2 deletions src/ralph/supports/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.db.models import Count
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _

from ralph.admin.decorators import register
Expand Down Expand Up @@ -176,6 +177,7 @@ def has_delete_permission(self, request, obj=None):

# list display fields (django admin does not support __ convention there,
# so they have to be fetched "manually")
@mark_safe
def _get_support_type(self, obj):
return generate_html_link(
obj.support.get_absolute_url(),
Expand All @@ -184,7 +186,6 @@ def _get_support_type(self, obj):
)
_get_support_type.short_description = _('support type')
_get_support_type.admin_order_field = 'support__support_type'
_get_support_type.allow_tags = True

def _get_support_contract_id(self, obj):
return obj.support.contract_id
Expand All @@ -211,6 +212,7 @@ def _get_support_date_to(self, obj):
_get_support_date_to.short_description = _('support date to')
_get_support_date_to.admin_order_field = 'support__date_to'

@mark_safe
def _get_asset_hostname(self, obj):
return generate_html_link(
obj.baseobject.get_absolute_url(),
Expand All @@ -219,7 +221,6 @@ def _get_asset_hostname(self, obj):
)
_get_asset_hostname.short_description = _('asset hostname')
_get_asset_hostname.admin_order_field = 'baseobject__asset__hostname'
_get_asset_hostname.allow_tags = True

def _get_asset_service_env(self, obj):
return obj.baseobject.service_env
Expand Down
Loading

0 comments on commit 2867b63

Please sign in to comment.