Skip to content

Commit

Permalink
[fix] Fixed WifiSession.stop_time formatting in django admin #525
Browse files Browse the repository at this point in the history
Fixes #525
  • Loading branch information
pandafy committed Aug 29, 2023
1 parent af744e4 commit 91350f7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
3 changes: 2 additions & 1 deletion openwisp_monitoring/device/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.forms import ModelForm
from django.templatetags.static import static
from django.urls import resolve, reverse
from django.utils.formats import localize
from django.utils.html import format_html
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -385,7 +386,7 @@ def wps(self, obj):
def get_stop_time(self, obj):
if obj.stop_time is None:
return mark_safe('<strong style="color:green;">online</strong>')
return obj.stop_time
return localize(obj.stop_time)

get_stop_time.short_description = _('stop time')

Expand Down
31 changes: 30 additions & 1 deletion openwisp_monitoring/device/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.core.cache import cache
from django.test import TestCase
from django.urls import reverse
from django.utils.timezone import now, timedelta
from django.utils.timezone import datetime, now, timedelta
from freezegun import freeze_time
from swapper import get_model_name, load_model

Expand Down Expand Up @@ -1030,3 +1030,32 @@ def test_wifi_client_he_vht_ht_unknown(self):
),
html=True,
)

def test_wifi_session_stop_time_formatting(self):
start_time = datetime.strptime('2023-8-24 17:08:00', '%Y-%m-%d %H:%M:%S')
stop_time = datetime.strptime('2023-8-24 19:46:00', '%Y-%m-%d %H:%M:%S')
session = self._create_wifi_session(stop_time=stop_time)
# WifiSession.start_time has "auto_now" set to True.
# To overwrite the current date, we set the start_time explicitly
WifiSession.objects.update(start_time=start_time)
url = reverse(
f'admin:{self.wifi_session_app_label}_{self.wifi_session_model_name}_change',
args=(session.id,),
)
response = self.client.get(url)
self.assertContains(
response,
(
'<label>Start time:</label>\n\n'
'<div class="readonly">Aug. 24, 2023, 5:08 p.m.</div>'
),
html=True,
)
self.assertContains(
response,
(
'<label>Stop time:</label>\n\n'
'<div class="readonly">Aug. 24, 2023, 5:46 p.m.</div>'
),
html=True,
)

0 comments on commit 91350f7

Please sign in to comment.