Skip to content

Commit 3ddc9a7

Browse files
Display gcm device_id as hex value in Django admin
The value appears as hex in both /admin/push_notifications/gcmdevice/ and /admin/push_notifications/gcmdevice/pk/
1 parent 7e5b806 commit 3ddc9a7

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

push_notifications/admin.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from django.contrib.auth import get_user_model
33
from django.utils.translation import ugettext_lazy as _
44
from .models import APNSDevice, GCMDevice, get_expired_tokens
5-
5+
from django.db import connection
66

77
User = get_user_model()
88

@@ -57,5 +57,18 @@ def prune_devices(self, request, queryset):
5757
d.save()
5858

5959

60+
class GCMDeviceAdmin(DeviceAdmin):
61+
"""
62+
Inherits from DeviceAdmin to handle displaying gcm device as a hex value
63+
"""
64+
def device_id_hex(self, obj):
65+
if connection.vendor in ("mysql", "sqlite"):
66+
return hex(obj.device_id).rstrip("L")
67+
else:
68+
return obj.device_id
69+
device_id_hex.short_description = "Device ID"
70+
71+
list_display = ("__unicode__", "device_id_hex", "user", "active", "date_created")
72+
6073
admin.site.register(APNSDevice, DeviceAdmin)
61-
admin.site.register(GCMDevice, DeviceAdmin)
74+
admin.site.register(GCMDevice, GCMDeviceAdmin)

push_notifications/fields.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
__all__ = ["HexadecimalField", "HexIntegerField"]
1111

1212

13-
hex_re = re.compile(r"^[0-9A-f]+$")
13+
hex_re = re.compile(r"^(([0-9A-f])|(0x[0-9A-f]))+$")
1414
postgres_engines = [
1515
"django.db.backends.postgresql_psycopg2",
1616
"django.contrib.gis.db.backends.postgis",
@@ -25,6 +25,13 @@ def __init__(self, *args, **kwargs):
2525
self.default_validators = [RegexValidator(hex_re, _("Enter a valid hexadecimal number"), "invalid")]
2626
super(HexadecimalField, self).__init__(*args, **kwargs)
2727

28+
def prepare_value(self, value):
29+
# converts bigint from db to hex before it is displayed in admin
30+
if value and not isinstance(value, six.string_types) \
31+
and connection.vendor in ("mysql", "sqlite"):
32+
value = hex(value).rstrip("L")
33+
return super(forms.CharField, self).prepare_value(value)
34+
2835

2936
class HexIntegerField(six.with_metaclass(models.SubfieldBase, models.BigIntegerField)):
3037
"""

0 commit comments

Comments
 (0)