Commit 9b36e298 authored by David Haynes's avatar David Haynes 🙆

Merge branch 'admin-actions' into '2.2-dev'

Admin actions

See merge request !44
parents ded1cb8c 6db18a40
Pipeline #2747 failed with stage
in 51 seconds
......@@ -9,10 +9,15 @@ https://docs.djangoproject.com/en/1.11/ref/contrib/admin/
"""
# Django Imports
from django.contrib import admin
from django.contrib import messages
from django.contrib.gis.admin import OSMGeoAdmin
from django.core.exceptions import ObjectDoesNotExist
from django.http import HttpResponseRedirect
from django.shortcuts import render
# App Imports
from .models import Facility, Schedule, OpenTime, Category, Location, Alert
@admin.register(Facility)
class FacilityAdmin(admin.ModelAdmin):
"""
......@@ -20,6 +25,64 @@ class FacilityAdmin(admin.ModelAdmin):
Allows admins to create new facilities through the admin interface.
"""
def drop_special_schedules(self, request, queryset):
num = queryset.count()
for facility in queryset:
facility.special_schedules.clear()
self.message_user(request,
"Successfully cleared all special schedules for %d facilities." % num)
drop_special_schedules.short_description = 'Clear all special schedules for selected facilities'
def assign_bulk_schedules(self, request, queryset):
num = queryset.count()
# all admin actions-related requests are post requests, so we're looking for
# the one that has the associated value with our confirmation input button
if 'bulk_schedule' in 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()
self.message_user(request,
"Set %s as the main schedule for %d facilities." % (name, num))
except ObjectDoesNotExist:
self.message_user(request,
"Unable to set a new main schedule for %d facilities." % num,
level=messages.ERROR)
return HttpResponseRedirect(request.get_full_path())
return render(request,
'bulk_schedules.html',
context = {'facilities': queryset,
'schedules': Schedule.objects.all()})
assign_bulk_schedules.short_description = 'Set a main schedule for selected facilities'
def assign_bulk_special_schedules(self, request, queryset):
num = queryset.count()
if 'bulk_special_schedule' in request.POST:
try:
new_special_schedule = Schedule.objects.get(pk=request.POST['special_schedule'])
name = new_special_schedule.name
for facility in queryset:
facility.special_schedules.add(new_special_schedule)
facility.save()
self.message_user(request,
"Added %s as a special schedule to %d facilities." % (name, num))
except ObjectDoesNotExist:
self.message_user(request,
"Unable to add additional special schedule to %d facilities." % num,
level=messages.ERROR)
return HttpResponseRedirect(request.get_full_path())
return render(request,
'bulk_special_schedules.html',
context = {'facilities': queryset,
'schedules': Schedule.objects.all()})
assign_bulk_special_schedules.short_description = 'Add a special schedule to selected facilities'
# a list of all actions to be added
actions = [drop_special_schedules,
assign_bulk_schedules, assign_bulk_special_schedules ]
# Allow filtering by the following fields
list_filter = ['facility_category', 'facility_location']
# Modify the rendered layout of the "create a new facility" page
......
{% extends "admin/base_site.html" %}
{% load static %}
{% block extrahead %}
<link href="{% static "admin/css/vendor/select2/select2.min.css" %}" type="text/css" media="screen" rel="stylesheet" />
<link href="{% static "admin/css/autocomplete.css" %}" type="text/css" media="screen" rel="stylesheet" />
<script type="text/javascript" src="{% static "admin/js/vendor/jquery/jquery.min.js" %}"></script>
<script type="text/javascript" src="{% static "admin/js/vendor/select2/select2.full.min.js" %}"></script>
<!-- Django changes jQuery to django.jQuery in case the version bundled with Django is not the version
you want to use in your app
It's super important that it is loaded after the jQuery library, and
any jQuery-reliant libraries are loaded-->
<script type="text/javascript" src="{% static "admin/js/jquery.init.js" %}"></script>
<script type="text/javascript" src="{% static "admin/js/cancel.js" %}"></script>
<script>
/**
Django's admin inferface uses select2 for its new autocompletion selects
Unfortunately, use of that specific widget and the associated support attributes
are limited to admin.ModelAdmin change views
You need instead to use it manually
See
https://docs.djangoproject.com/en/2.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin.autocomplete_fields
https://docs.djangoproject.com/en/2.0/ref/contrib/admin/#reversing-admin-urls
https://select2.org/getting-started/basic-usage
for more information
**/
(function($) {
$(document).ready(function() {
$('.select2-basic-single').select2();
});
// note how this is wrapped in a function to use django.jQuery for references to $
})(django.jQuery);
</script>
{{ block.super }}
{% endblock %}
{% block extrastyle %}
{{ block.super }}
<link href="{% static "admin/css/forms.css" %}" type="text/css" rel="stylesheet" />
<style type="text/css">
a.button.cancel-link {
display: inline-block;
vertical-align: middle;
height: 15px;
line-height: 15px;
background: #ddd;
border-radius: 4px;
padding: 10px 15px;
color: #333;
margin: 0 0 0 10px;
}
a.button.cancel-link:active,
a.button.cancel-link:focus,
a.button.cancel-link:hover {
background: #ccc;
}
</style>
{% endblock %}
{% extends "base_bulk_schedules_intermediate.html" %}
{% block title %}
Set main schedule for multiple facilities
{{ block.super }}
{% endblock %}
{% block breadcrumbs %}
<div class="breadcrumbs">
<a href="{% url 'admin:index' %}">Home</a>
&rsaquo; <a href="{% url 'admin:app_list' 'api' %}">Api</a>
&rsaquo; <a href="{% url 'admin:api_facility_changelist' %}">Facilities</a>
&rsaquo; Set main schedule for multiple facilities
</div>
{% endblock %}
{% block content %}
<form action="" method="post">
{% csrf_token %}
<label for="id_label_single">
Schedule
<select name="schedule" class="select2-basic-single" id="id_label_single">
{% for schedule in schedules %}
<option value={{ schedule.pk }} >{{ schedule }}</option>
{% endfor %}
</select>
</label>
<p>Set this as the main schedule for all of these facilities?</p>
<ul>
{% for facility in facilities %}
<li>{{ facility.facility_name }}</li>
<input type="hidden" name="_selected_action" value="{{ facility.pk }}" />
{% endfor %}
</ul>
<input type="hidden" name="action" value="assign_bulk_schedules" />
<input type="submit" name="bulk_schedule" value="Yes, I'm sure" />
<a href="#" class="button cancel-link">No, take me back</a>
</form>
{% endblock %}
{% extends "base_bulk_schedules_intermediate.html" %}
{% block title %}
Add special schedule to multiple facilities
{{ block.super }}
{% endblock %}
{% block breadcrumbs %}
<div class="breadcrumbs">
<a href="{% url 'admin:index' %}">Home</a>
&rsaquo; <a href="{% url 'admin:app_list' 'api' %}">Api</a>
&rsaquo; <a href="{% url 'admin:api_facility_changelist' %}">Facilities</a>
&rsaquo; Add special schedule to multiple facilities
</div>
{% endblock %}
{% block content %}
<form action="" method="post">
{% csrf_token %}
<label for="id_label_single">
Special Schedule
<select name="special_schedule" class="select2-basic-single" id="id_label_single">
{% for schedule in schedules %}
<option value={{ schedule.pk }} >{{ schedule }}</option>
{% endfor %}
</select>
</label>
<p>Add this special schedule to all of these facilities?</p>
<ul>
{% for facility in facilities %}
<li>{{ facility.facility_name }}</li>
<input type="hidden" name="_selected_action" value="{{ facility.pk }}" />
{% endfor %}
</ul>
<input type="hidden" name="action" value="assign_bulk_special_schedules" />
<input type="submit" name="bulk_special_schedule" value="Yes, I'm sure" />
<a href="#" class="button cancel-link">No, take me back</a>
</form>
{% endblock %}
......@@ -152,7 +152,9 @@ TEMPLATES = [
'BACKEND': 'django.template.backends.django.DjangoTemplates',
# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-dirs
'DIRS': [
normpath(join(SITE_ROOT, 'templates'))
normpath(join(SITE_ROOT, 'templates')),
# may specify to avoid requiring paths
normpath(join(SITE_ROOT, 'api/templates')),
],
'OPTIONS': {
# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-context-processors
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment