admin.py 3.83 KB
Newer Older
David Haynes's avatar
David Haynes committed
1
2
#!/usr/bin/env python
# -*- coding: utf-8 -*-
3
"""
4
5
api/admin.py

6
7
8
9
Django admin interface configuration.

https://docs.djangoproject.com/en/1.11/ref/contrib/admin/
"""
10
# Django Imports
11
from django.contrib import admin
David Haynes's avatar
David Haynes committed
12
from django.contrib.gis.admin import OSMGeoAdmin
13
from django.shortcuts import render
14
# App Imports
David Haynes's avatar
David Haynes committed
15
from .models import Facility, Schedule, OpenTime, Category, Location, Alert
16

David Haynes's avatar
David Haynes committed
17
@admin.register(Facility)
Ben Waters's avatar
Ben Waters committed
18
class FacilityAdmin(admin.ModelAdmin):
19
    """
David Haynes's avatar
David Haynes committed
20
21
22
    Custom Admin panel for the Facility model.

    Allows admins to create new facilities through the admin interface.
23
    """
24
25
    def drop_special_schedules(self, request, queryset):
        num = queryset.count()
26
27
        for facility in queryset:
            facility.special_schedules.clear()
28
        self.message_user(request, "Successfully removed special schedules from %d facilities." % num)
29

30
31
32
33
34
35
36
37
38
    def assign_bulk_schedules(self, request, queryset):
        return render(request,
                      'bulk_schedules_intermediate.html',
                      context = {'facilities': queryset,
                                 'schedules': Schedule.objects.all()})
    assign_bulk_schedules.short_description = 'Assign a schedule to multiple facilities'

    # a list of all actions to be added
    actions = [drop_special_schedules, assign_bulk_schedules, ]
39

David Haynes's avatar
David Haynes committed
40
    # Allow filtering by the following fields
David Haynes's avatar
David Haynes committed
41
    list_filter = ['facility_category', 'facility_location']
David Haynes's avatar
David Haynes committed
42
43
    # Modify the rendered layout of the "create a new facility" page
    # We are basically reordering things to look nicer to the user here
44
45
    fieldsets = (
        (None, {
David Haynes's avatar
David Haynes committed
46
47
48
49
50
            'fields': ('facility_name', 'logo', 'facility_category',
                       'facility_location', 'main_schedule', 'special_schedules',
                       ('facility_product_tags', 'facility_labels',
                        'facility_classifier'),
                       'tapingo_url', 'phone_number', 'note', 'owners'),
51
        }),
52
    )
53

54
55
56
57
58
59
60
61
62
63
class OpenTimeInline(admin.TabularInline):
    """
    A table of time periods that represent an "open time" for a Facility.

    https://docs.djangoproject.com/en/1.11/ref/contrib/admin/#django.contrib.admin.TabularInline
    """
    # Columns correspond to each attribute in the OpenTime table
    model = OpenTime
    # 7 days of the week, so only have 7 rows
    max_num = 7
64
65
66
67
68
69
70
71
72
73
    extra = 7
    # We are basically reordering things to look nicer to the user here
    fieldsets = (
        (None, {
            'fields': (
                ('start_day', 'start_time'),
                ('end_day', 'end_time')
            ),
        }),
    )
74

David Haynes's avatar
David Haynes committed
75
@admin.register(Schedule)
76
class ScheduleAdmin(admin.ModelAdmin):
77
    """
David Haynes's avatar
David Haynes committed
78
79
80
81
82
    Custom Admin panel for the Schedule model.

    Allows admins to create new schedules through the admin interface.
    Additionally, we append the OpenTimeInline table to allow for open times to
    be defined for the schedule we are creating.
83
    """
David Haynes's avatar
David Haynes committed
84
    # Allow filtering by the following fields
85
    list_display = ['name', 'modified']
David Haynes's avatar
David Haynes committed
86
    # Append the OpenTimeInline table to the end of our admin panel
87
    inlines = [OpenTimeInline, ]
David Haynes's avatar
David Haynes committed
88
    # Modify the rendered layout of the "create a new facility" page
89
90
    fieldsets = (
        (None, {
91
92
93
            'fields': ('name',
                       # Pair valid_start and valid_end together on the same
                       # line
David Haynes's avatar
David Haynes committed
94
                       ('valid_start', 'valid_end'),
95
                       'twenty_four_hours',
96
97
                       'schedule_for_removal',
                       'promote_to_main')
David Haynes's avatar
David Haynes committed
98
        }),
99
    )
100

David Haynes's avatar
David Haynes committed
101
# https://docs.djangoproject.com/en/1.11/ref/contrib/gis/admin/#osmgeoadmin
102
103
104
OSMGeoAdmin.default_lon = -8605757.16502
OSMGeoAdmin.default_lat = 4697457.00333
OSMGeoAdmin.default_zoom = 15
David Haynes's avatar
David Haynes committed
105
admin.site.register(Location, OSMGeoAdmin)
106
107
# Use the default ModelAdmin interface for these
admin.site.register(Category)
David Haynes's avatar
David Haynes committed
108
admin.site.register(Alert)