version 0.0.4: add app locations with models City, Regions, Address, Countries

This commit is contained in:
Dmitriy Kuzmenko 2019-08-08 17:06:07 +03:00
parent a253ce0442
commit c5df0faa16
11 changed files with 225 additions and 0 deletions

View File

47
apps/location/admin.py Normal file
View File

@ -0,0 +1,47 @@
from django.contrib.gis import admin
from location import models
from django.contrib.gis.geos import Point
from django.utils.translation import gettext_lazy as _
@admin.register(models.Country)
class CountryAdmin(admin.ModelAdmin):
"""Country admin."""
@admin.register(models.Region)
class RegionAdmin(admin.ModelAdmin):
"""Region admin."""
@admin.register(models.City)
class CityAdmin(admin.ModelAdmin):
"""City admin."""
@admin.register(models.Address)
class AddressAdmin(admin.OSMGeoAdmin):
"""Address admin."""
list_display = ('__str__', 'geo_lon', 'geo_lat')
readonly_fields = ['geo_lon', 'geo_lat',]
fieldsets = (
(_('Main'), {
'fields': ['street_name_1', 'street_name_2',]
}),
(_('Location'), {
'fields': ['coordinates', 'geo_lon', 'geo_lat', ]
}),
(_('Address detail'), {
'fields': ['city', 'postal_code', ]
}),
)
def geo_lon(self, item):
# Point(longitude, latitude)
if isinstance(item.coordinates, Point):
return item.coordinates.x
def geo_lat(self, item):
if isinstance(item.coordinates, Point):
return item.coordinates.y

7
apps/location/apps.py Normal file
View File

@ -0,0 +1,7 @@
from django.apps import AppConfig
from django.utils.translation import gettext_lazy as _
class LocationConfig(AppConfig):
name = 'location'
verbose_name = _('location')

View File

@ -0,0 +1,73 @@
# Generated by Django 2.2.4 on 2019-08-08 13:55
import django.contrib.gis.db.models.fields
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Country',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=250, verbose_name='name')),
],
options={
'verbose_name': 'country',
'verbose_name_plural': 'countries',
},
),
migrations.CreateModel(
name='Region',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=250, verbose_name='name')),
('code', models.CharField(max_length=250, verbose_name='code')),
('country', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='location.Country', verbose_name='country')),
('parent_region', models.ForeignKey(blank=True, default=None, null=True, on_delete=django.db.models.deletion.CASCADE, to='location.Region', verbose_name='parent region')),
],
options={
'verbose_name': 'region',
'verbose_name_plural': 'regions',
},
),
migrations.CreateModel(
name='City',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=250, verbose_name='name')),
('code', models.CharField(max_length=250, verbose_name='code')),
('postal_code', models.CharField(default='', help_text='Ex.: 350018', max_length=10, verbose_name='postal code')),
('is_island', models.BooleanField(default=False, verbose_name='is island')),
('country', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='location.Country', verbose_name='country')),
('region', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='location.Region', verbose_name='parent region')),
],
options={
'verbose_name': 'city',
'verbose_name_plural': 'cities',
},
),
migrations.CreateModel(
name='Address',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('street_name_1', models.CharField(blank=True, default='', max_length=500, verbose_name='street name 1')),
('street_name_2', models.CharField(blank=True, default='', max_length=500, verbose_name='street name 2')),
('number', models.IntegerField(verbose_name='number')),
('postal_code', models.CharField(blank=True, default='', help_text='Ex.: 350018', max_length=10, verbose_name='postal code')),
('coordinates', django.contrib.gis.db.models.fields.PointField(blank=True, default=None, null=True, srid=4326, verbose_name='Coordinates')),
('city', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='location.City', verbose_name='city')),
],
options={
'verbose_name': 'Address',
'verbose_name_plural': 'Address',
},
),
]

View File

80
apps/location/models.py Normal file
View File

@ -0,0 +1,80 @@
from django.contrib.gis.db import models
from django.utils.translation import gettext_lazy as _
class Country(models.Model):
"""Country model."""
name = models.CharField(_('name'), max_length=250)
class Meta:
verbose_name_plural = _('countries')
verbose_name = _('country')
def __str__(self):
return self.name
class Region(models.Model):
"""Region model."""
name = models.CharField(_('name'), max_length=250)
code = models.CharField(_('code'), max_length=250)
parent_region = models.ForeignKey(
'self', verbose_name=_('parent region'), null=True,
blank=True, default=None, on_delete=models.CASCADE)
country = models.ForeignKey(
Country, verbose_name=_('country'), on_delete=models.CASCADE)
class Meta:
verbose_name_plural = _('regions')
verbose_name = _('region')
def __str__(self):
return self.name
class City(models.Model):
"""Region model."""
name = models.CharField(_('name'), max_length=250)
code = models.CharField(_('code'), max_length=250)
region = models.ForeignKey(
Region, verbose_name=_('parent region'), on_delete=models.CASCADE)
country = models.ForeignKey(
Country, verbose_name=_('country'), on_delete=models.CASCADE)
postal_code = models.CharField(
_('postal code'), max_length=10, default='', help_text=_('Ex.: 350018'))
is_island = models.BooleanField(_('is island'), default=False)
class Meta:
verbose_name_plural = _('cities')
verbose_name = _('city')
def __str__(self):
return self.name
class Address(models.Model):
"""Address model."""
city = models.ForeignKey(City, verbose_name=_('city'), on_delete=models.CASCADE)
street_name_1 = models.CharField(
_('street name 1'), max_length=500, blank=True, default='')
street_name_2 = models.CharField(
_('street name 2'), max_length=500, blank=True, default='')
number = models.IntegerField(_('number'))
postal_code = models.CharField(
_('postal code'), max_length=10, blank=True,
default='', help_text=_('Ex.: 350018'))
coordinates = models.PointField(
_('Coordinates'), blank=True, null=True, default=None)
class Meta:
verbose_name_plural = _('Address')
verbose_name = _('Address')
def __str__(self):
return f'{self.id}: {self.get_street_name()[:50]}'
def get_street_name(self):
return self.street_name_1 or self.street_name_2

View File

@ -0,0 +1,2 @@
from rest_framework import serializers
from location import models

3
apps/location/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

9
apps/location/urls.py Normal file
View File

@ -0,0 +1,9 @@
"""Location app urlconf."""
from django.urls import path
from location import views
app_name = 'location'
urlpatterns = [
]

3
apps/location/views.py Normal file
View File

@ -0,0 +1,3 @@
from rest_framework import generics, permissions
from location import models, serializers

View File

@ -52,6 +52,7 @@ CONTRIB_APPS = [
PROJECT_APPS = [
'account.apps.AccountConfig',
'location.apps.LocationConfig',
]