Skip to content

Commit

Permalink
Merge pull request City-of-Helsinki#89 from suutari-ai/admin-improvem…
Browse files Browse the repository at this point in the history
…ents

Improve model admins
  • Loading branch information
suutari-ai authored Sep 24, 2019
2 parents 23e1b3f + 85b7229 commit 281f754
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 11 deletions.
51 changes: 41 additions & 10 deletions parkings/admin.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,53 @@
from django.contrib import admin
from django.contrib.gis.admin import OSMGeoAdmin

from .admin_utils import ReadOnlyAdmin
from .admin_utils import ReadOnlyAdmin, WithAreaField
from .models import (
Operator, Parking, ParkingArea, ParkingCheck, ParkingTerminal, PaymentZone,
Permit, PermitArea, PermitLookupItem, Region)
Permit, PermitArea, PermitLookupItem, PermitSeries, Region)


@admin.register(Operator)
class OperatorAdmin(admin.ModelAdmin):
pass
list_display = ['name', 'user']


@admin.register(PaymentZone)
class PaymentZoneAdmin(OSMGeoAdmin):
class PaymentZoneAdmin(WithAreaField, OSMGeoAdmin):
list_display = ['id', 'number', 'name', 'area']
ordering = ('number',)


@admin.register(Parking)
class ParkingAdmin(OSMGeoAdmin):
date_hierarchy = 'time_start'
list_display = [
'id', 'operator', 'zone', 'parking_area', 'terminal_number',
'time_start', 'time_end', 'registration_number',
'created_at', 'modified_at']
list_filter = ['operator', 'zone']
ordering = ('-created_at',)
ordering = ('-time_start',)


@admin.register(Region)
class RegionAdmin(OSMGeoAdmin):
class RegionAdmin(WithAreaField, OSMGeoAdmin):
list_display = ['id', 'name', 'capacity_estimate', 'area']
ordering = ('name',)


@admin.register(ParkingArea)
class ParkingAreaAdmin(OSMGeoAdmin):
class ParkingAreaAdmin(WithAreaField, OSMGeoAdmin):
area_scale = 1
list_display = ['id', 'origin_id', 'capacity_estimate', 'area']
ordering = ('origin_id',)


@admin.register(ParkingCheck)
class ParkingCheckAdmin(ReadOnlyAdmin, OSMGeoAdmin):
list_display = [
'id', 'time', 'registration_number', 'location',
'allowed', 'result', 'performer', 'created_at']

modifiable = False

def get_readonly_fields(self, request, obj=None):
Expand All @@ -61,14 +70,36 @@ class ParkingTerminalAdmin(OSMGeoAdmin):

@admin.register(Permit)
class PermitAdmin(admin.ModelAdmin):
pass
date_hierarchy = 'created_at'
list_display = ['id', 'series', 'external_id', 'created_at', 'modified_at']
list_filter = ['series__active']
ordering = ('-series', '-id')


@admin.register(PermitArea)
class PermitAreaAdmin(OSMGeoAdmin):
class PermitAreaAdmin(WithAreaField, OSMGeoAdmin):
list_display = ['id', 'identifier', 'name', 'area']
ordering = ('identifier',)


@admin.register(PermitLookupItem)
class PermitLookupItemAdmin(ReadOnlyAdmin):
pass
list_display = [
'id', 'series', 'permit',
'registration_number', 'area_identifier',
'start_time', 'end_time']
list_filter = ['permit__series__active']
ordering = ('-permit__series', 'permit')

def series(self, instance):
series = instance.permit.series
return '{id}{active}'.format(
id=series.id, active='*' if series.active else '')


@admin.register(PermitSeries)
class PermitSeriesAdmin(admin.ModelAdmin):
date_hierarchy = 'created_at'
list_display = ['id', 'active', 'created_at', 'modified_at']
list_filter = ['active']
ordering = ('-created_at', '-id')
10 changes: 10 additions & 0 deletions parkings/admin_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ def has_change_permission(self, request, obj=None):

def has_delete_permission(self, request, obj=None):
return False


class WithAreaField:
area_scale = 1000000

def area(self, instance):
assert self.area_scale in [1000000, 1]
unit = 'km\u00b2' if self.area_scale == 1000000 else 'm\u00b2'
return '{area:.1f} {unit}'.format(
area=instance.geom.area / self.area_scale, unit=unit)
2 changes: 2 additions & 0 deletions parkings/migrations/0019_permits.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class Migration(migrations.Migration):
],
options={
'ordering': ('created_at', 'id'),
'verbose_name': 'permit series',
'verbose_name_plural': 'permit series',
},
),
migrations.AddField(
Expand Down
8 changes: 7 additions & 1 deletion parkings/models/permit.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class PermitSeries(TimestampedModelMixin, models.Model):

class Meta:
ordering = ('created_at', 'id')
verbose_name = _("permit series")
verbose_name_plural = _("permit series")

def __str__(self):
return str(self.id)
Expand Down Expand Up @@ -103,7 +105,11 @@ class Meta:
ordering = ('series', 'id')

def __str__(self):
return ('Permit {}'.format(self.id))
return 'Permit {id} ({series}{active} / {external_id})'.format(
id=self.id,
series=self.series,
active='*' if self.series.active else '',
external_id=self.external_id)

def save(self, using=None, *args, **kwargs):
self.full_clean()
Expand Down

0 comments on commit 281f754

Please sign in to comment.