@@ -8,4 +8,127 @@ meta: Python code examples for Django settings files.
88
99
1010# django.conf settings Examples
11+ The [ Django] ( /django.html )
12+ [ settings] ( https://docs.djangoproject.com/en/dev/topics/settings/ )
13+ file contains all of the configuration for a web application.
14+
15+
16+ ## Example 1 from django-easy-timezones
17+ [ django-easy-timezones] ( https://github.com/Miserlou/django-easy-timezones )
18+ ([ project website] ( https://www.gun.io/blog/django-easy-timezones ) )
19+ is a [ Django] ( /django.html )
20+ [ middleware] ( https://docs.djangoproject.com/en/2.2/topics/http/middleware/ )
21+ [ code library] ( https://pypi.org/project/django-easy-timezones/ )
22+ to simplify handling time data in your applications using
23+ users' geolocation data.
24+
25+ [ ** django-easy-timezones/easy_timezones/middleware.py** ] ( https://github.com/Miserlou/django-easy-timezones/blob/master/easy_timezones/middleware.py )
26+
27+ ``` python
28+ import django
29+ ~~ from django.conf import settings
30+ from django.contrib.auth import get_user_model
31+ from django.core.exceptions import ImproperlyConfigured
32+ from django.utils import timezone
33+ import pytz
34+ import pygeoip
35+ import os
36+
37+ from .signals import detected_timezone
38+ from .utils import get_ip_address_from_request, is_valid_ip, is_local_ip
39+
40+
41+ db_loaded = False
42+ db = None
43+ db_v6 = None
44+
45+ def load_db_settings ():
46+ ~~ GEOIP_DATABASE = getattr (settings, ' GEOIP_DATABASE' , ' GeoLiteCity.dat' )
47+
48+ if not GEOIP_DATABASE :
49+ raise ImproperlyConfigured(" GEOIP_DATABASE setting has not been " + \
50+ " properly defined." )
51+
52+ if not os.path.exists(GEOIP_DATABASE ):
53+ raise ImproperlyConfigured(" GEOIP_DATABASE setting is defined, " + \
54+ " but file does not exist." )
55+
56+ ~~ GEOIPV6_DATABASE = getattr (settings, ' GEOIPV6_DATABASE' ,
57+ ~~ ' GeoLiteCityv6.dat' )
58+
59+ if not GEOIPV6_DATABASE :
60+ raise ImproperlyConfigured(" GEOIPV6_DATABASE setting has not " + \
61+ " been properly defined." )
62+
63+ if not os.path.exists(GEOIPV6_DATABASE ):
64+ raise ImproperlyConfigured(" GEOIPV6_DATABASE setting is " + \
65+ " defined, but file does not exist." )
66+
67+ return (GEOIP_DATABASE , GEOIPV6_DATABASE )
68+
69+ load_db_settings()
70+
71+ def load_db ():
72+
73+ GEOIP_DATABASE , GEOIPV6_DATABASE = load_db_settings()
74+
75+ global db
76+ db = pygeoip.GeoIP(GEOIP_DATABASE , pygeoip.MEMORY_CACHE )
77+
78+ global db_v6
79+ db_v6 = pygeoip.GeoIP(GEOIPV6_DATABASE , pygeoip.MEMORY_CACHE )
80+
81+ global db_loaded
82+ db_loaded = True
83+
84+
85+ if django.VERSION >= (1 , 10 ):
86+ from django.utils.deprecation import MiddlewareMixin
87+ middleware_base_class = MiddlewareMixin
88+ else :
89+ middleware_base_class = object
90+
91+
92+ class EasyTimezoneMiddleware (middleware_base_class ):
93+ def process_request (self , request ):
94+ """
95+ If we can get a valid IP from the request,
96+ look up that address in the database to get the appropriate
97+ timezone and activate it. Else, use the default.
98+ """
99+
100+ if not request:
101+ return
102+
103+ if not db_loaded:
104+ load_db()
105+
106+ tz = request.session.get(' django_timezone' )
107+
108+ if not tz:
109+ # use the default timezone (settings.TIME_ZONE) for localhost
110+ tz = timezone.get_default_timezone()
111+
112+ client_ip = get_ip_address_from_request(request)
113+ ip_addrs = client_ip.split(' ,' )
114+ for ip in ip_addrs:
115+ if is_valid_ip(ip) and not is_local_ip(ip):
116+ if ' :' in ip:
117+ tz = db_v6.time_zone_by_addr(ip)
118+ break
119+ else :
120+ tz = db.time_zone_by_addr(ip)
121+ break
122+
123+ if tz:
124+ timezone.activate(tz)
125+ request.session[' django_timezone' ] = str (tz)
126+ ~~ if getattr (settings, ' AUTH_USER_MODEL' ,
127+ ~~ None ) and getattr (request, ' user' , None ):
128+ detected_timezone.send(sender = get_user_model(),
129+ instance = request.user,
130+ timezone = tz)
131+ else :
132+ timezone.deactivate()
133+ ```
11134
0 commit comments