Skip to content

Commit ad837a3

Browse files
wip
1 parent 24504cf commit ad837a3

File tree

14 files changed

+231
-4
lines changed

14 files changed

+231
-4
lines changed

examples/simple/books/admin.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Author,
66
Book,
77
City,
8+
Continent,
89
Country,
910
Order,
1011
OrderLine,
@@ -17,6 +18,7 @@
1718
'AuthorAdmin',
1819
'BookAdmin',
1920
'CityAdmin',
21+
'ContinentAdmin',
2022
'CountryAdmin',
2123
'OrderAdmin',
2224
'OrderLineAdmin',
@@ -105,3 +107,12 @@ class CountryAdmin(admin.ModelAdmin):
105107
list_display = ('name', 'latitude', 'longitude',)
106108
list_editable = ('latitude', 'longitude',)
107109
search_fields = ('name',)
110+
111+
112+
@admin.register(Continent)
113+
class ContinentAdmin(admin.ModelAdmin):
114+
"""Continent admin."""
115+
116+
list_display = ('name', 'latitude', 'longitude',)
117+
list_editable = ('latitude', 'longitude',)
118+
search_fields = ('name',)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.10.8 on 2018-06-22 21:17
3+
from __future__ import unicode_literals
4+
5+
from django.db import migrations, models
6+
import django.db.models.deletion
7+
8+
9+
class Migration(migrations.Migration):
10+
11+
dependencies = [
12+
('books', '0014_auto_20171003_1434'),
13+
]
14+
15+
operations = [
16+
migrations.CreateModel(
17+
name='Continent',
18+
fields=[
19+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
20+
('name', models.CharField(max_length=255)),
21+
('info', models.TextField(blank=True, null=True)),
22+
('latitude', models.DecimalField(blank=True, decimal_places=15, default=0, max_digits=19, null=True)),
23+
('longitude', models.DecimalField(blank=True, decimal_places=15, default=0, max_digits=19, null=True)),
24+
],
25+
options={
26+
'ordering': ['id'],
27+
},
28+
),
29+
migrations.AddField(
30+
model_name='country',
31+
name='continent',
32+
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='books.Continent'),
33+
preserve_default=False,
34+
),
35+
]

examples/simple/books/models/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from .country import Country
66
from .order import Order
77
from .order_line import OrderLine
8+
from .continent import Continent
89
from .publisher import Publisher
910
from .tag import Tag
1011

@@ -13,6 +14,7 @@
1314
'Author',
1415
'Book',
1516
'City',
17+
'Continent',
1618
'Country',
1719
'Order',
1820
'OrderLine',

examples/simple/books/models/address.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,38 @@ def country_indexing(self):
8181
})
8282

8383
return wrapper
84+
85+
@property
86+
def continent_indexing(self):
87+
"""Continent data (nested) for indexing.
88+
89+
Example:
90+
91+
>>> mapping = {
92+
>>> 'continent': {
93+
>>> 'name': 'Asia',
94+
>>> 'country': {
95+
>>> 'name': 'Netherlands',
96+
>>> 'province': {
97+
>>> 'name': 'North Holland',
98+
>>> 'city': {
99+
>>> 'name': 'Amsterdam',
100+
>>> }
101+
>>> }
102+
>>> }
103+
>>> }
104+
>>> }
105+
106+
:return:
107+
"""
108+
wrapper = dict_to_obj({
109+
'name': self.city.country.continent.name,
110+
'country': {
111+
'name': self.city.country.name,
112+
'city': {
113+
'name': self.city.name,
114+
}
115+
}
116+
})
117+
118+
return wrapper
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from __future__ import unicode_literals
2+
3+
from django.db import models
4+
5+
from six import python_2_unicode_compatible
6+
7+
__all__ = ('Continent',)
8+
9+
10+
@python_2_unicode_compatible
11+
class Continent(models.Model):
12+
"""Continent."""
13+
14+
name = models.CharField(max_length=255)
15+
info = models.TextField(null=True, blank=True)
16+
latitude = models.DecimalField(
17+
null=True,
18+
blank=True,
19+
decimal_places=15,
20+
max_digits=19,
21+
default=0
22+
)
23+
longitude = models.DecimalField(
24+
null=True,
25+
blank=True,
26+
decimal_places=15,
27+
max_digits=19,
28+
default=0
29+
)
30+
31+
class Meta(object):
32+
"""Meta options."""
33+
34+
ordering = ["id"]
35+
36+
def __str__(self):
37+
return self.name
38+
39+
@property
40+
def location_field_indexing(self):
41+
"""Location for indexing.
42+
43+
Used in Elasticsearch indexing/tests of `geo_distance` native filter.
44+
"""
45+
return {
46+
'lat': self.latitude,
47+
'lon': self.longitude,
48+
}

examples/simple/books/models/country.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Country(models.Model):
1313

1414
name = models.CharField(max_length=255)
1515
info = models.TextField(null=True, blank=True)
16+
continent = models.ForeignKey('books.Continent', on_delete=models.CASCADE)
1617
latitude = models.DecimalField(
1718
null=True,
1819
blank=True,

examples/simple/factories/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from .books_address import AddressFactory
33
from .books_author import *
44
from .books_book import *
5+
from .books_continent import *
56
from .books_city import CityFactory
67
from .books_country import CountryFactory
78
from .books_order import OrderFactory
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from factory import DjangoModelFactory
2+
from factory.fuzzy import FuzzyChoice
3+
4+
from books.models import Continent
5+
6+
from .factory_faker import Faker
7+
8+
__all__ = ('ContinentFactory',)
9+
10+
11+
class BaseContinentFactory(DjangoModelFactory):
12+
"""Base continent factory."""
13+
14+
name = FuzzyChoice([
15+
'Asia',
16+
'Africa',
17+
'North America',
18+
'South America',
19+
'Antarctica',
20+
'Europe',
21+
'Australia'
22+
])
23+
info = Faker('text')
24+
latitude = Faker('latitude')
25+
longitude = Faker('longitude')
26+
27+
class Meta(object):
28+
"""Meta class."""
29+
30+
model = Continent
31+
abstract = True
32+
django_get_or_create = ('name',)
33+
34+
35+
class ContinentFactory(BaseContinentFactory):
36+
"""Continent factory."""

examples/simple/factories/books_country.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from factory import DjangoModelFactory
1+
from factory import DjangoModelFactory, SubFactory
22

33
from books.models import Country
44

@@ -12,6 +12,7 @@ class BaseCountryFactory(DjangoModelFactory):
1212

1313
name = Faker('country')
1414
info = Faker('text')
15+
continent = SubFactory('factories.books_continent.ContinentFactory')
1516
latitude = Faker('latitude')
1617
longitude = Faker('longitude')
1718

examples/simple/search_indexes/documents/address.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class AddressDocument(DocType):
100100
'suggest': fields.CompletionField(),
101101
}
102102
),
103-
'city': fields.NestedField(
103+
'city': fields.ObjectField(
104104
properties={
105105
'name': StringField(
106106
analyzer=html_strip,
@@ -113,6 +113,39 @@ class AddressDocument(DocType):
113113
}
114114
)
115115

116+
# Continent object
117+
continent = fields.NestedField(
118+
attr='continent_indexing',
119+
properties={
120+
'name': StringField(
121+
analyzer=html_strip,
122+
fields={
123+
'raw': KeywordField(),
124+
}
125+
),
126+
'country': fields.ObjectField(
127+
properties={
128+
'name': StringField(
129+
analyzer=html_strip,
130+
fields={
131+
'raw': KeywordField(),
132+
}
133+
),
134+
'city': fields.ObjectField(
135+
properties={
136+
'name': StringField(
137+
analyzer=html_strip,
138+
fields={
139+
'raw': KeywordField(),
140+
}
141+
)
142+
}
143+
)
144+
}
145+
)
146+
}
147+
)
148+
116149
location = fields.GeoPointField(attr='location_field_indexing')
117150

118151
class Meta(object):

0 commit comments

Comments
 (0)