Skip to content

Commit

Permalink
Add optional columns to status widget (Fixes LP#1634874)
Browse files Browse the repository at this point in the history
  • Loading branch information
John-Magne Bredal committed Nov 1, 2016
1 parent 7d03c82 commit 4fabbfd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
22 changes: 22 additions & 0 deletions python/nav/web/navlets/status2.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from nav.django.settings import DATETIME_FORMAT
from nav.models.profiles import AccountNavlet, Account
from nav.models.manage import Netbox
from nav.web.status2.forms import StatusWidgetForm
from nav.web.api.v1.views import AlertHistoryViewSet
from . import Navlet, NAVLET_MODE_EDIT, NAVLET_MODE_VIEW
Expand Down Expand Up @@ -55,6 +56,8 @@ def get_context_data(self, **kwargs):
elif self.mode == NAVLET_MODE_VIEW:
results = self.do_query(status_filter)
self.add_formatted_time(results)
self.add_netbox(results)
context['extra_columns'] = self.find_extra_columns(status_filter)
context['results'] = sorted(
results, key=itemgetter('start_time'), reverse=True)
context['last_updated'] = datetime.now()
Expand All @@ -75,6 +78,25 @@ def add_formatted_time(self, results):
for result in results:
result['formatted_time'] = self.format_time(result['start_time'])

@staticmethod
def add_netbox(results):
"""Adds the netbox object to the result objects"""
for result in results:
try:
netbox = Netbox.objects.select_related(
'room', 'room__location').get(pk=int(result['netbox']))
except Netbox.DoesNotExist:
pass
else:
result['netbox_object'] = netbox

@staticmethod
def find_extra_columns(status_filter):
"""Finds the chosen extra columns and returns them in a list"""
column_choices = StatusWidgetForm().fields.get('extra_columns').choices
chosen_columns = QueryDict(status_filter).getlist('extra_columns')
return [(k, v) for k, v in column_choices if k in chosen_columns]

@staticmethod
def format_time(timestamp):
"""Format the time based on time back in time"""
Expand Down
5 changes: 4 additions & 1 deletion python/nav/web/status2/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ class StatusWidgetForm(StatusPanelForm):
This form is used in the status widget and is more suitable for a smaller
screen size.
"""
extra_columns = forms.MultipleChoiceField(
required=False,
choices=(('room.location', 'Location'), ('room', 'Room')))

def __init__(self, *args, **kwargs):
super(StatusWidgetForm, self).__init__(*args, **kwargs)
Expand Down Expand Up @@ -199,7 +202,7 @@ def __init__(self, *args, **kwargs):
Row(
Column('stateless', 'stateless_threshold',
css_class=column_class),
Column('acknowledged', 'on_maintenance',
Column('acknowledged', 'on_maintenance', 'extra_columns',
css_class=column_class)
),
Submit('submit', 'Save')
Expand Down
9 changes: 9 additions & 0 deletions templates/navlets/status2_view.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% extends 'navlets/base.html' %}
{% load info %}

{% block navlet-content %}

Expand All @@ -14,6 +15,9 @@
<th>Subject</th>
<th>Type</th>
<th>Start time</th>
{% for key, value in extra_columns %}
<th>{{ value }}</th>
{% endfor %}
</tr>
</thead>

Expand All @@ -34,6 +38,11 @@
{{ result.formatted_time }}
{% if not result.end_time %}<i class="fa fa-warning" title="Stateless" style="color: #FF8F00"></i>{% endif %}
</td>
{% for key, value in extra_columns %}
<td>
{{ result.netbox_object|get_attr:key }}
</td>
{% endfor %}
</tr>
{% endfor %}

Expand Down

0 comments on commit 4fabbfd

Please sign in to comment.