Skip to content

Commit

Permalink
Change how settings are organized internalls. Remove django-appconf. …
Browse files Browse the repository at this point in the history
…Change Google Maps API key setting name.
  • Loading branch information
philippbosch committed Jun 28, 2016
1 parent feaaa78 commit cf9862e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Installation

- Set your Google API key in you settings file::

GEOPOSITION_MAP_API_KEY = 'YOUR_API_KEY'
GEOPOSITION_GOOGLE_MAPS_API_KEY = 'YOUR_API_KEY'

API keys may be obtained here: https://developers.google.com/maps/documentation/javascript/get-api-key

Expand Down
37 changes: 29 additions & 8 deletions geoposition/conf.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
# -*- coding: utf-8 -*-
from django.conf import settings
from appconf import AppConf
from django.conf import settings as django_settings
from django.core.exceptions import ImproperlyConfigured


class GeopositionConf(AppConf):
MAP_WIDGET_HEIGHT = 480
MAP_OPTIONS = {}
MARKER_OPTIONS = {}
class AppSettings(object):
defaults = {
'MAP_WIDGET_HEIGHT': 480,
'MAP_OPTIONS': {},
'MARKER_OPTIONS': {},
'GOOGLE_MAPS_API_KEY': None,
}
prefix = 'GEOPOSITION'
required_settings = ['GOOGLE_MAPS_API_KEY']

class Meta:
prefix = 'geoposition'
def __init__(self, django_settings):
self.django_settings = django_settings

for setting in self.required_settings:
prefixed_name = '%s_%s' % (self.prefix, setting)
if not hasattr(self.django_settings, prefixed_name):
raise ImproperlyConfigured("The '%s' setting is required." % prefixed_name)

def __getattr__(self, name):
prefixed_name = '%s_%s' % (self.prefix, name)
if hasattr(django_settings, prefixed_name):
return getattr(django_settings, prefixed_name)
if name in self.defaults:
return self.defaults[name]
raise AttributeError("'AppSettings' object does not have a '%s' attribute" % name)


settings = AppSettings(django_settings)
2 changes: 1 addition & 1 deletion geoposition/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import unicode_literals
from .conf import settings


try:
from south.modelsinspector import add_introspection_rules
Expand Down
1 change: 1 addition & 0 deletions geoposition/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@


GEOPOSITION_MAP_WIDGET_HEIGHT = 500
GEOPOSITION_GOOGLE_MAPS_API_KEY = 'DUMMY'
8 changes: 4 additions & 4 deletions geoposition/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ def format_output(self, rendered_widgets):
'label': _("longitude"),
},
'config': {
'map_widget_height': settings.GEOPOSITION_MAP_WIDGET_HEIGHT,
'map_options': json.dumps(settings.GEOPOSITION_MAP_OPTIONS),
'marker_options': json.dumps(settings.GEOPOSITION_MARKER_OPTIONS),
'map_widget_height': settings.MAP_WIDGET_HEIGHT or 500,
'map_options': json.dumps(settings.MAP_OPTIONS),
'marker_options': json.dumps(settings.MARKER_OPTIONS),
}
})

class Media:
js = (
'//maps.google.com/maps/api/js?key=%s&sensor=false' % settings.GEOPOSITION_MAP_API_KEY,
'//maps.google.com/maps/api/js?key=%s' % settings.GOOGLE_MAPS_API_KEY,
'geoposition/geoposition.js',
)
css = {
Expand Down
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,5 @@
'Operating System :: OS Independent',
'Programming Language :: Python',
'Framework :: Django',
],
install_requires=['django-appconf >= 0.4'],
]
)

0 comments on commit cf9862e

Please sign in to comment.