admin.py 4.61 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
14
from django.core.exceptions import ObjectDoesNotExist
from django.http import HttpResponseRedirect
15
from django.shortcuts import render
16
# App Imports
David Haynes's avatar
David Haynes committed
17
from .models import Facility, Schedule, OpenTime, Category, Location, Alert
18

19

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

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

33
    def assign_bulk_schedules(self, request, queryset):
34
35
36
37
38
39
40
41
42
        num = queryset.count()
        if 'bulk_schedule' in request.POST:
            print('request', request.POST)
            try:
                new_schedule = Schedule.objects.get(pk=request.POST['schedule'])
                name = new_schedule.name
                for facility in queryset:
                   facility.main_schedule = new_schedule
                   facility.save()
43
                self.message_user(request, "Set %s as the main schedule for %d facilities." % (name, num))
44
45
46
            except ObjectDoesNotExist:
                self.message_user(request, "Unable to set schedule for %d facilities." % num)
            return HttpResponseRedirect(request.get_full_path())
47
48
49
50
        return render(request,
                      'bulk_schedules_intermediate.html',
                      context = {'facilities': queryset,
                                 'schedules': Schedule.objects.all()})
51
    assign_bulk_schedules.short_description = 'Assign a main schedule for multiple facilities'
52
53
54

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

David Haynes's avatar
David Haynes committed
56
    # Allow filtering by the following fields
David Haynes's avatar
David Haynes committed
57
    list_filter = ['facility_category', 'facility_location']
David Haynes's avatar
David Haynes committed
58
59
    # Modify the rendered layout of the "create a new facility" page
    # We are basically reordering things to look nicer to the user here
60
61
    fieldsets = (
        (None, {
David Haynes's avatar
David Haynes committed
62
63
64
65
66
            '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'),
67
        }),
68
    )
69

70
71
72
73
74
75
76
77
78
79
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
80
81
82
83
84
85
86
87
88
89
    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')
            ),
        }),
    )
90

David Haynes's avatar
David Haynes committed
91
@admin.register(Schedule)
92
class ScheduleAdmin(admin.ModelAdmin):
93
    """
David Haynes's avatar
David Haynes committed
94
95
96
97
98
    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.
99
    """
David Haynes's avatar
David Haynes committed
100
    # Allow filtering by the following fields
101
    list_display = ['name', 'modified']
David Haynes's avatar
David Haynes committed
102
    # Append the OpenTimeInline table to the end of our admin panel
103
    inlines = [OpenTimeInline, ]
David Haynes's avatar
David Haynes committed
104
    # Modify the rendered layout of the "create a new facility" page
105
106
    fieldsets = (
        (None, {
107
108
109
            'fields': ('name',
                       # Pair valid_start and valid_end together on the same
                       # line
David Haynes's avatar
David Haynes committed
110
                       ('valid_start', 'valid_end'),
111
                       'twenty_four_hours',
112
113
                       'schedule_for_removal',
                       'promote_to_main')
David Haynes's avatar
David Haynes committed
114
        }),
115
    )
116

David Haynes's avatar
David Haynes committed
117
# https://docs.djangoproject.com/en/1.11/ref/contrib/gis/admin/#osmgeoadmin
118
119
120
OSMGeoAdmin.default_lon = -8605757.16502
OSMGeoAdmin.default_lat = 4697457.00333
OSMGeoAdmin.default_zoom = 15
David Haynes's avatar
David Haynes committed
121
admin.site.register(Location, OSMGeoAdmin)
122
123
# Use the default ModelAdmin interface for these
admin.site.register(Category)
David Haynes's avatar
David Haynes committed
124
admin.site.register(Alert)