Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add address settings #163

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
29 changes: 29 additions & 0 deletions address/fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
##
# A field for addresses in other models.
##
from django.db import models
from . import models as address_models
from . import forms as address_forms


class AddressField(models.ForeignKey):
description = 'An address'

def __init__(self, *args, **kwargs):
kwargs['to'] = 'address.Address'
# The address should be set to null when deleted if the relationship could be null
default_on_delete = models.SET_NULL if kwargs.get('null', False) else models.CASCADE
kwargs['on_delete'] = kwargs.get('on_delete', default_on_delete)
super(AddressField, self).__init__(*args, **kwargs)

def contribute_to_class(self, cls, name, virtual_only=False):
from address.compat import compat_contribute_to_class

compat_contribute_to_class(self, cls, name, virtual_only)

setattr(cls, self.name, address_models.AddressDescriptor(self))

def formfield(self, **kwargs):
defaults = dict(form_class=address_forms.AddressField)
defaults.update(kwargs)
return super(AddressField, self).formfield(**defaults)
28 changes: 1 addition & 27 deletions address/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
basestring = (str, bytes)
unicode = str

__all__ = ['Country', 'State', 'Locality', 'Address', 'AddressField']
__all__ = ['Country', 'State', 'Locality', 'Address',]


class InconsistentDictError(Exception):
Expand Down Expand Up @@ -295,30 +295,4 @@ class AddressDescriptor(ForwardManyToOneDescriptor):
def __set__(self, inst, value):
super(AddressDescriptor, self).__set__(inst, to_python(value))

##
# A field for addresses in other models.
##


class AddressField(models.ForeignKey):
description = 'An address'

def __init__(self, *args, **kwargs):
kwargs['to'] = 'address.Address'
# The address should be set to null when deleted if the relationship could be null
default_on_delete = models.SET_NULL if kwargs.get('null', False) else models.CASCADE
kwargs['on_delete'] = kwargs.get('on_delete', default_on_delete)
super(AddressField, self).__init__(*args, **kwargs)

def contribute_to_class(self, cls, name, virtual_only=False):
from address.compat import compat_contribute_to_class

compat_contribute_to_class(self, cls, name, virtual_only)

setattr(cls, self.name, AddressDescriptor(self))

def formfield(self, **kwargs):
from .forms import AddressField as AddressFormField
defaults = dict(form_class=AddressFormField)
defaults.update(kwargs)
return super(AddressField, self).formfield(**defaults)
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
3 changes: 2 additions & 1 deletion address/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from django.db import IntegrityError
from django.core.exceptions import ValidationError

from address.models import Country, State, Locality, Address, AddressField
from address.models import Country, State, Locality, Address
from address.fields import AddressField
from address.models import to_python

# Python 3 fixes.
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')),
]
2 changes: 1 addition & 1 deletion example_site/person/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.contrib import admin
from address.models import AddressField
from address.fields import AddressField
from address.forms import AddressWidget
from .models import Person

Expand Down
3 changes: 2 additions & 1 deletion example_site/person/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by Django 2.0.3 on 2018-03-29 22:34

import address.models
import address.fields
from django.db import migrations, models
import django.db.models.deletion

Expand All @@ -18,7 +19,7 @@ class Migration(migrations.Migration):
name='Person',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('address', address.models.AddressField(on_delete=django.db.models.deletion.CASCADE, to='address.Address')),
('address', address.fields.AddressField(on_delete=django.db.models.deletion.CASCADE, to='address.Address')),
],
),
]
3 changes: 2 additions & 1 deletion example_site/person/migrations/0003_auto_20200628_1920.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by Django 3.0.6 on 2020-06-28 19:20

import address.models
import address.fields
from django.db import migrations, models
import django.db.models.deletion

Expand All @@ -21,6 +22,6 @@ class Migration(migrations.Migration):
migrations.AlterField(
model_name='person',
name='address',
field=address.models.AddressField(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='address.Address'),
field=address.fields.AddressField(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='address.Address'),
),
]
2 changes: 1 addition & 1 deletion example_site/person/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.db import models
from address.models import AddressField
from address.fields import AddressField


class Person(models.Model):
Expand Down
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