mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Closes #1283: Added a time zone field to the site model
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from collections import OrderedDict
|
||||
import pytz
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
@@ -97,6 +98,23 @@ class ContentTypeFieldSerializer(Field):
|
||||
raise ValidationError("Invalid content type")
|
||||
|
||||
|
||||
class TimeZoneField(Field):
|
||||
"""
|
||||
Represent a pytz time zone.
|
||||
"""
|
||||
|
||||
def to_representation(self, obj):
|
||||
return obj.zone if obj else None
|
||||
|
||||
def to_internal_value(self, data):
|
||||
if not data:
|
||||
return ""
|
||||
try:
|
||||
return pytz.timezone(str(data))
|
||||
except pytz.exceptions.UnknownTimeZoneError:
|
||||
raise ValidationError('Invalid time zone "{}"'.format(data))
|
||||
|
||||
|
||||
#
|
||||
# Viewsets
|
||||
#
|
||||
|
@@ -1,5 +1,8 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import datetime
|
||||
import pytz
|
||||
|
||||
from django import template
|
||||
from django.utils.safestring import mark_safe
|
||||
from markdown import markdown
|
||||
@@ -117,6 +120,14 @@ def example_choices(field, arg=3):
|
||||
return ', '.join(examples) or 'None'
|
||||
|
||||
|
||||
@register.filter()
|
||||
def tzoffset(value):
|
||||
"""
|
||||
Returns the hour offset of a given time zone using the current time.
|
||||
"""
|
||||
return datetime.datetime.now(value).strftime('%z')
|
||||
|
||||
|
||||
#
|
||||
# Tags
|
||||
#
|
||||
|
Reference in New Issue
Block a user