Skip to content

Commit

Permalink
Add address settings
Browse files Browse the repository at this point in the history
Prior to this change to modify the address javascript you had to fork
and edit the `address.js` manually. This change let to define a setting
dict to customize the address.js.

e.g.

```
ADDRESS_SETTINGS = {
    'country': ['AU'],
}
```
it will be injected in the address.js to limit the address to the
australian country.

To achieve that I move the address.js from a static file to a
templateview and threat the address.js file as a django template.

The TemplateView is responsible to retrieve the settings and generate
the context for the template.

There is a requirement to make django-address works now. The url must be
added in your project:

e.g.

```
urlpatterns = [
    # ... other urls
    path('address/', include('address.urls')),
]
```
  • Loading branch information
karimone committed Jan 10, 2022
1 parent 6b4aed8 commit bf20229
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 9 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ INSTALLED_APPS = [
]
```

Then, add the url in your urls.py

```
urlpatterns = [
# other urls...
path('address/', include('address.urls')),
]
```

You can either store your Google API key in an environment variable as `GOOGLE_API_KEY` or you can
specify the key in `settings.py`. If you have an environment variable set it will override what you put in settings.py.
For more information, including enabling the Google Places API, refer to [the example site](https://github.com/furious-luke/django-address/blob/master/example_site/README.md).
Expand All @@ -38,6 +47,16 @@ You can either store your Google API key in an environment variable as `GOOGLE_A
GOOGLE_API_KEY = 'AIzaSyD--your-google-maps-key-SjQBE'
```

## Settings
Is possible to specify custom settings in your settings module.
For example, to get address only for the Australia add these to your settings

```python
ADDRESS_SETTINGS = {
"country": ["AU"]
}
```

# The Model

The rationale behind the model structure is centered on trying to make
Expand Down
Empty file added address/templates/__init__.py
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
$(function () {
$('input.address').each(function () {
let address_settings = {{ address_settings|safe }};
var self = $(this);
var cmps = $('#' + self.attr('name') + '_components');
var fmtd = $('input[name="' + self.attr('name') + '_formatted"]');
self.geocomplete({
details: cmps,
detailsAttribute: 'data-geo'
}).change(function () {
detailsAttribute: 'data-geo',
...address_settings,
}
).change(function () {
if (self.val() != fmtd.val()) {
var cmp_names = [
'country',
Expand Down
7 changes: 7 additions & 0 deletions address/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.urls import path

from .views import AddressJS

urlpatterns = [
path('address.js', AddressJS.as_view(), name="address.js"),
]
14 changes: 14 additions & 0 deletions address/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import json
from django.views.generic import TemplateView
from django.conf import settings


class AddressJS(TemplateView):
content_type = "text/javascript"
template_name = "address/address.js"

def get_context_data(self, **kwargs):
context = super().get_context_data()
address_settings = getattr(settings, "ADDRESS_SETTINGS", {})
context["address_settings"] = json.dumps(address_settings)
return context
3 changes: 2 additions & 1 deletion address/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.conf import settings
from django.utils.html import escape
from django.utils.safestring import mark_safe
from django.urls import reverse

from .models import Address

Expand Down Expand Up @@ -32,7 +33,7 @@ class Media:
js = [
'https://maps.googleapis.com/maps/api/js?libraries=places&key=%s' % settings.GOOGLE_API_KEY,
'js/jquery.geocomplete.min.js',
'address/js/address.js',
reverse('address.js'),
]

if JQUERY_URL:
Expand Down
9 changes: 6 additions & 3 deletions example_site/example_site/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from django.contrib import admin
from django.urls import path
from django.urls import path, include

from person import views as person
from person import views as person_views
from address import views as address_views
from address import urls as address_urls

urlpatterns = [
path('', person.home, name='home'),
path('', person_views.home, name='home'),
path('admin/', admin.site.urls),
path('address/', include('address.urls')),
]
3 changes: 1 addition & 2 deletions example_site/person/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
from django.shortcuts import render

from address.models import Address
from .forms import PersonForm


def home(request):
from .forms import PersonForm
success = False
addresses = Address.objects.all()
if settings.GOOGLE_API_KEY:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from setuptools import find_packages, setup

version = '0.2.5'
version = '0.2.6'

if sys.argv[-1] == 'tag':
print("Tagging the version on github:")
Expand Down

0 comments on commit bf20229

Please sign in to comment.