Skip to content

Commit

Permalink
admin classes python2.7 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbrooks committed Jan 13, 2020
1 parent b6b4ea0 commit 1d6ca8b
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions simple_locations/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from django.conf import settings
from django.contrib.gis import admin
from mptt.admin import MPTTModelAdmin
from simple_locations.models import Point, AreaType, Area

area_admin_classes = [admin.OSMGeoAdmin, MPTTModelAdmin]

# Translated models are "nice to have"
Expand All @@ -16,7 +18,14 @@
TranslationAdmin = admin.ModelAdmin
pass

from simple_locations.models import Point, AreaType, Area
try:
# optionally use django_extensions' ForeignKeyAutocompleteAdmin if
# available
from django_extensions.admin import ForeignKeyAutocompleteAdmin
except ImportError:
area_admin_classes.append(ForeignKeyAutocompleteAdmin)

This comment has been minimized.

Copy link
@RobertoMaurizzi

RobertoMaurizzi Jan 13, 2020

Contributor

Ehm... should this be above the except?

This comment has been minimized.

Copy link
@joshbrooks

joshbrooks Jan 13, 2020

Author Contributor

True!

ForeignKeyAutocompleteAdmin = None
pass


class PointAdmin(admin.ModelAdmin):
Expand All @@ -34,20 +43,26 @@ class AreaChildrenInline(admin.TabularInline):
extra = 0


class AreaAdmin(*area_admin_classes):
default_lon = getattr(settings, 'GIS_DEFAULT_LAT', -8.8742)
default_lat = getattr(settings, 'GIS_DEFAULT_LON', 125.7275)
default_zoom = 16
map_template = "simple_locations/admin/osm.html"
# debug = True - enable if copy/pasting WKTs is useful
units = 'km'
list_display = ('name', 'kind', 'location', 'code')
search_fields = ['code', 'name']
list_filter = ('kind',)
related_search_fields = {'parent': ('^name',)}
inlines = [AreaChildrenInline]
area_admin_settings = {
"default_lon": getattr(settings, 'GIS_DEFAULT_LAT', -8.8742),
"default_lat": getattr(settings, 'GIS_DEFAULT_LON', 125.7275),
"default_zoom": 16,
"map_template": "simple_locations/admin/osm.html",
# "debug": True - enable if copy/pasting WKTs is useful,
"units": 'km',
"list_display": ('name', 'kind', 'location', 'code'),
"search_fields": ['code', 'name'],
"list_filter": ('kind',),
"related_search_fields": {'parent': ('^name',)},
"inlines": [AreaChildrenInline]
}

AreaAdminClass = type(
"AreaAdminClass",
tuple(area_admin_classes),
area_admin_settings
)

admin.site.register(Point, PointAdmin)
admin.site.register(AreaType, AreaTypeAdmin)
admin.site.register(Area, AreaAdmin)
admin.site.register(Area, AreaAdminClass)

0 comments on commit 1d6ca8b

Please sign in to comment.