• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

jamesmfriedman/django-easytz: Timezone localization without any thinking or doin ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

jamesmfriedman/django-easytz

开源软件地址(OpenSource Url):

https://github.com/jamesmfriedman/django-easytz

开源编程语言(OpenSource Language):

Python 100.0%

开源软件介绍(OpenSource Introduction):

Django EasyTZ

Timezone localization without any thinking or doing whatsoever.

The purpose of this django app is to piggy back off of Django's already great timezone handling and add some automagic to the equation. This package is perfect for you if:

  • you don't care about accurate timezone names for your users (many timezones have the same UTC offset but have different names, I.E. America/Eastern and America/New_York)
  • you trust the time and timezone setting of the client's machine
  • you are using Django 1.4 or above

Setup

  1. Use your favorite python package installer. $ pip install django-easytz
  2. Make sure USE_TZ is set to True in your settings file. This is Django's default since they introdcued timezone support in 1.4
  3. add easytz.middleware.TimezonesMiddleware to your MIDDLEWARE_CLASSES
  4. add easytz to your INSTALLED_APPS
  5. include {{ STATIC_URL }}easytz/js/easytz.js with your scripts
  6. Run ./manage.py syncdb to add the model for storing users timezones

How It Works

On the client side, jstz (http://pellepim.bitbucket.org/jstz/) is used to set a cookie with the users timezone. A middleware picks up the cookie and activates the timezone using Django's timezone handling. If a user is authenticated, it will also set the timezone in the session under a key called timezone as well as save it to the database.

Anytime you want to access a users timezone, you can do so by calling my_user.tz. This will get_or_create a timezone entry for the user. If there is no entry present, it defaults to what is set in settings.TIME_ZONE or 'GMT'. From there, its up to you to use Django's built in handling. Django automatically converts aware datetimes that are used in templates. If you're in Django, just do the following

from django.utils import timezone

def foo(request):
	timezone.localtime(timezone.now(), request.user.tz)
	...

Model

There is a simple storage model in easytz.models.TimezoneStore with the following fields:

  • user: OneToOne to the user
  • timezone: the current timezone string for that user

If you want to cut down on redundant queries, just make sure to select_related timezone if you are accessing the .tz property on users.

user = User.objects.select_related('timezone').get(user_id = 1)
users = User.objects.select_related('timezone').all()



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap