Skip to content

Commit

Permalink
Fix date time localization bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ldeluigi committed Feb 22, 2023
1 parent 28bf15a commit 3dde095
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
9 changes: 7 additions & 2 deletions backend/spellbook/admin/job_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
class JobAdmin(admin.ModelAdmin):
readonly_fields = ['created_local', 'expected_termination_local', 'termination_local']
fields = ['id', 'name', 'status', 'created_local', 'expected_termination_local', 'termination_local', 'message', 'started_by']
list_display = ['id', 'name', 'status', 'created', 'expected_termination', 'termination', 'variants_count']
list_display = ['id', 'name', 'status', 'created_local', 'expected_termination_local', 'termination_local', 'variants_count']

def variants_count(self, obj):
return obj.variants.count()

def datetime_to_html(self, datetime):
return format_html('<span class="local-datetime" title="{}">{}</span>', datetime.isoformat(), localize(datetime))
if datetime is None:
return None
return format_html('<span class="local-datetime" data-iso="{}">{}</span>', datetime.isoformat(), localize(datetime))

@admin.display(description='Created')
def created_local(self, obj):
Expand All @@ -36,3 +38,6 @@ def has_change_permission(self, request, obj=None):

def has_delete_permission(self, request, obj=None):
return False

class Media:
js = ['admin/js/jquery.init.js', 'admin/js/iso_to_local_time.js']
8 changes: 5 additions & 3 deletions backend/spellbook/static/admin/js/iso_to_local_time.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
django.jQuery(function() {
django.jQuery('.local-datetime').each(function() {
django.jQuery('span.local-datetime').each(function() {
var $this = django.jQuery(this);
var iso_date = $this.attr('title');
var iso_date = $this.attr('data-iso');
if (iso_date) {
var local_date = new Date(iso_date);
$this.text(local_date.toLocaleString());
if (local_date instanceof Date && !isNaN(local_date.valueOf())) {
$this.text(local_date.toLocaleString());
}
}
});
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
{% extends "admin/change_form.html" %}
{% load static %}

{% block extrahead %}
{{ block.super }}
<script src="{% static 'admin/js/iso_to_local_time.js' %}" type="text/javascript"></script>
{% endblock %}

{% block after-content %}
<table class="after-content">
Expand Down

0 comments on commit 3dde095

Please sign in to comment.