Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1 | from django.contrib import admin |
| 2 | |
Scott Baker | c749d62 | 2015-12-29 23:47:06 -0800 | [diff] [blame] | 3 | from services.hpc.models import * |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 4 | from django import forms |
| 5 | from django.utils.safestring import mark_safe |
| 6 | from django.contrib.auth.admin import UserAdmin |
| 7 | from django.contrib.admin.widgets import FilteredSelectMultiple |
| 8 | from django.contrib.auth.forms import ReadOnlyPasswordHashField |
| 9 | from django.contrib.auth.signals import user_logged_in |
| 10 | from django.utils import timezone |
| 11 | from django.contrib.contenttypes import generic |
| 12 | from suit.widgets import LinkedSelect |
Scott Baker | 9da5304 | 2015-05-15 09:08:19 -0700 | [diff] [blame] | 13 | from core.admin import ServiceAppAdmin,SliceInline,ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, SliderWidget, ServicePrivilegeInline |
Scott Baker | a622f0b | 2015-05-15 15:00:48 -0700 | [diff] [blame] | 14 | from core.middleware import get_request |
Scott Baker | 26a5494 | 2015-04-02 23:17:59 -0700 | [diff] [blame] | 15 | |
Scott Baker | 0e0d968 | 2015-04-02 21:56:00 -0700 | [diff] [blame] | 16 | from functools import update_wrapper |
Scott Baker | 26a5494 | 2015-04-02 23:17:59 -0700 | [diff] [blame] | 17 | from django.contrib.admin.views.main import ChangeList |
Scott Baker | 4609e48 | 2015-05-21 10:25:58 -0700 | [diff] [blame] | 18 | from django.core.urlresolvers import reverse, NoReverseMatch |
Scott Baker | 26a5494 | 2015-04-02 23:17:59 -0700 | [diff] [blame] | 19 | from django.contrib.admin.utils import quote |
| 20 | |
Scott Baker | 4609e48 | 2015-05-21 10:25:58 -0700 | [diff] [blame] | 21 | from filteredadmin import FilteredChangeList, FilteredAdmin, FilteredInline |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 22 | |
Scott Baker | 1facc3e | 2015-05-11 16:38:16 -0700 | [diff] [blame] | 23 | class HpcServiceForm(forms.ModelForm): |
| 24 | scale = forms.IntegerField(widget = SliderWidget, required=False) |
| 25 | |
| 26 | def __init__(self, *args, **kwargs): |
| 27 | super(HpcServiceForm, self).__init__(*args, **kwargs) |
Scott Baker | 67e1ccb | 2016-01-14 09:33:13 -0800 | [diff] [blame] | 28 | if ("instance" in kwargs) and (hasattr(kwargs["instance"], "scale")): |
| 29 | self.fields['scale'].initial = kwargs["instance"].scale |
Scott Baker | 1facc3e | 2015-05-11 16:38:16 -0700 | [diff] [blame] | 30 | |
| 31 | def save(self, *args, **kwargs): |
| 32 | if self.cleaned_data['scale']: |
| 33 | self.instance.scale = self.cleaned_data['scale'] |
| 34 | |
| 35 | return super(HpcServiceForm, self).save(*args, **kwargs) |
| 36 | |
Scott Baker | 0e0d968 | 2015-04-02 21:56:00 -0700 | [diff] [blame] | 37 | class HpcServiceAdmin(ReadOnlyAwareAdmin): |
Scott Baker | 446eaca | 2014-05-23 13:16:57 -0700 | [diff] [blame] | 38 | model = HpcService |
| 39 | verbose_name = "HPC Service" |
| 40 | verbose_name_plural = "HPC Service" |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 41 | list_display = ("backend_status_icon", "name","enabled") |
| 42 | list_display_links = ('backend_status_icon', 'name', ) |
Scott Baker | 84652a2 | 2015-05-18 16:55:31 -0700 | [diff] [blame] | 43 | fieldsets = [(None, {'fields': ['backend_status_text', 'name','scale','enabled','versionNumber', 'description', "cmi_hostname", "hpc_port80", "watcher_hpc_network", "watcher_dnsredir_network", "watcher_dnsdemux_network"], 'classes':['suit-tab suit-tab-general']})] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 44 | readonly_fields = ('backend_status_text', ) |
Scott Baker | 9da5304 | 2015-05-15 09:08:19 -0700 | [diff] [blame] | 45 | inlines = [SliceInline,ServiceAttrAsTabInline,ServicePrivilegeInline] |
Scott Baker | 1facc3e | 2015-05-11 16:38:16 -0700 | [diff] [blame] | 46 | form = HpcServiceForm |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 47 | |
Scott Baker | 0e0d968 | 2015-04-02 21:56:00 -0700 | [diff] [blame] | 48 | extracontext_registered_admins = True |
| 49 | |
Scott Baker | 446eaca | 2014-05-23 13:16:57 -0700 | [diff] [blame] | 50 | user_readonly_fields = ["name", "enabled", "versionNumber", "description"] |
Scott Baker | 446eaca | 2014-05-23 13:16:57 -0700 | [diff] [blame] | 51 | |
| 52 | suit_form_tabs =(('general', 'HPC Service Details'), |
Scott Baker | d4050d9 | 2015-02-26 17:05:02 -0800 | [diff] [blame] | 53 | ('administration', 'Administration'), |
Scott Baker | 15c56a1 | 2015-03-20 15:59:56 -0700 | [diff] [blame] | 54 | ('tools', 'Tools'), |
Scott Baker | 446eaca | 2014-05-23 13:16:57 -0700 | [diff] [blame] | 55 | ('slices','Slices'), |
| 56 | ('serviceattrs','Additional Attributes'), |
Scott Baker | 9da5304 | 2015-05-15 09:08:19 -0700 | [diff] [blame] | 57 | ('serviceprivileges','Privileges'), |
Scott Baker | 446eaca | 2014-05-23 13:16:57 -0700 | [diff] [blame] | 58 | ) |
| 59 | |
Scott Baker | 15c56a1 | 2015-03-20 15:59:56 -0700 | [diff] [blame] | 60 | suit_form_includes = (('hpcadmin.html', 'top', 'administration'), |
| 61 | ('hpctools.html', 'top', 'tools') ) |
Scott Baker | d4050d9 | 2015-02-26 17:05:02 -0800 | [diff] [blame] | 62 | |
Scott Baker | 0288aae | 2015-04-03 17:45:56 -0700 | [diff] [blame] | 63 | def url_for_model_changelist(self, request, model): |
| 64 | if not request.resolver_match.args: |
| 65 | return reverse('admin:%s_%s_changelist' % (model._meta.app_label, model._meta.model_name), current_app=model._meta.app_label) |
| 66 | else: |
| 67 | obj_id = request.resolver_match.args[0] |
| 68 | changelist_url = reverse('admin:%s_%s_filteredchangelist' % (model._meta.app_label, model._meta.model_name), args=(obj_id,), current_app=model._meta.app_label) |
| 69 | return changelist_url |
| 70 | |
Scott Baker | 26a5494 | 2015-04-02 23:17:59 -0700 | [diff] [blame] | 71 | class HPCAdmin(FilteredAdmin): |
Scott Baker | fbe38ee | 2015-02-27 12:12:14 -0800 | [diff] [blame] | 72 | # Change the application breadcrumb to point to an HPC Service if one is |
| 73 | # defined |
| 74 | |
Scott Baker | fbe38ee | 2015-02-27 12:12:14 -0800 | [diff] [blame] | 75 | custom_app_breadcrumb_name = "Hpc" |
| 76 | @property |
| 77 | def custom_app_breadcrumb_url(self): |
| 78 | services = HpcService.objects.all() |
| 79 | if len(services)==1: |
| 80 | return "/admin/hpc/hpcservice/%s/" % services[0].id |
| 81 | else: |
| 82 | return "/admin/hpc/hpcservice/" |
| 83 | |
Scott Baker | 4609e48 | 2015-05-21 10:25:58 -0700 | [diff] [blame] | 84 | class CDNPrefixInline(FilteredInline): |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 85 | model = CDNPrefix |
| 86 | extra = 0 |
| 87 | suit_classes = 'suit-tab suit-tab-prefixes' |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 88 | fields = ('backend_status_icon', 'cdn_prefix_id', 'prefix', 'defaultOriginServer', 'enabled') |
| 89 | readonly_fields = ('backend_status_icon', 'cdn_prefix_id',) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 90 | |
Scott Baker | 4609e48 | 2015-05-21 10:25:58 -0700 | [diff] [blame] | 91 | class OriginServerInline(FilteredInline): |
Scott Baker | 2a3ee07 | 2015-05-19 16:44:09 -0700 | [diff] [blame] | 92 | model = OriginServer |
| 93 | extra = 0 |
| 94 | suit_classes = 'suit-tab suit-tab-origins' |
| 95 | fields = ('backend_status_icon', 'origin_server_id', 'url') |
| 96 | readonly_fields = ('backend_status_icon', 'origin_server_id') |
| 97 | |
Scott Baker | 4609e48 | 2015-05-21 10:25:58 -0700 | [diff] [blame] | 98 | class ContentProviderInline(FilteredInline): |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 99 | model = ContentProvider |
| 100 | extra = 0 |
| 101 | suit_classes = 'suit-tab suit-tab-cps' |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 102 | fields = ('backend_status_icon', 'content_provider_id', 'name', 'enabled') |
| 103 | readonly_fields = ('backend_status_icon', 'content_provider_id',) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 104 | |
Scott Baker | fbe38ee | 2015-02-27 12:12:14 -0800 | [diff] [blame] | 105 | class OriginServerAdmin(HPCAdmin): |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 106 | list_display = ('backend_status_icon', 'url','protocol','redirects','contentProvider','authenticated','enabled' ) |
| 107 | list_display_links = ('backend_status_icon', 'url', ) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 108 | |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 109 | fields = ('backend_status_text', 'url','protocol','redirects','contentProvider','authenticated','enabled','origin_server_id','description' ) |
| 110 | readonly_fields = ('backend_status_text', 'origin_server_id',) |
Scott Baker | 2a21ba1 | 2014-06-03 12:25:40 -0700 | [diff] [blame] | 111 | user_readonly_fields = ('url','protocol','redirects','contentProvider','authenticated','enabled','origin_server_id','description') |
Scott Baker | 446eaca | 2014-05-23 13:16:57 -0700 | [diff] [blame] | 112 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 113 | class ContentProviderForm(forms.ModelForm): |
Scott Baker | 841e305 | 2015-05-19 16:57:02 -0700 | [diff] [blame] | 114 | users = forms.ModelMultipleChoiceField( |
| 115 | queryset=User.objects.all(), |
| 116 | required=False, |
| 117 | help_text="Select which users can manage this ContentProvider", |
| 118 | widget=FilteredSelectMultiple( |
| 119 | verbose_name=('Users'), is_stacked=False |
| 120 | ) |
| 121 | ) |
| 122 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 123 | class Meta: |
Scott Baker | 841e305 | 2015-05-19 16:57:02 -0700 | [diff] [blame] | 124 | model = ContentProvider |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 125 | widgets = { |
| 126 | 'serviceProvider' : LinkedSelect |
| 127 | } |
| 128 | |
Scott Baker | 841e305 | 2015-05-19 16:57:02 -0700 | [diff] [blame] | 129 | def __init__(self, *args, **kwargs): |
| 130 | request = kwargs.pop('request', None) |
| 131 | super(ContentProviderForm, self).__init__(*args, **kwargs) |
| 132 | |
| 133 | if self.instance and self.instance.pk: |
| 134 | self.fields['users'].initial = self.instance.users.all() |
| 135 | |
Scott Baker | fbe38ee | 2015-02-27 12:12:14 -0800 | [diff] [blame] | 136 | class ContentProviderAdmin(HPCAdmin): |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 137 | form = ContentProviderForm |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 138 | list_display = ('backend_status_icon', 'name','description','enabled' ) |
| 139 | list_display_links = ('backend_status_icon', 'name', ) |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 140 | readonly_fields = ('backend_status_text', ) |
Scott Baker | 5ec4534 | 2015-05-19 17:11:36 -0700 | [diff] [blame] | 141 | admin_readonly_fields = ('backend_status_text', ) |
| 142 | cp_readonly_fields = ('backend_status_text', 'name', 'enabled', 'serviceProvider', 'users') |
| 143 | fieldsets = [ (None, {'fields': ['backend_status_text', 'name','enabled','description','serviceProvider','users'], 'classes':['suit-tab suit-tab-general']})] |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 144 | |
Scott Baker | 2a3ee07 | 2015-05-19 16:44:09 -0700 | [diff] [blame] | 145 | inlines = [CDNPrefixInline, OriginServerInline] |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 146 | |
Scott Baker | 446eaca | 2014-05-23 13:16:57 -0700 | [diff] [blame] | 147 | user_readonly_fields = ('name','description','enabled','serviceProvider','users') |
Scott Baker | 446eaca | 2014-05-23 13:16:57 -0700 | [diff] [blame] | 148 | |
Scott Baker | 2a3ee07 | 2015-05-19 16:44:09 -0700 | [diff] [blame] | 149 | suit_form_tabs = (('general','Details'),('prefixes','CDN Prefixes'), ('origins','Origin Servers')) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 150 | |
Scott Baker | 5ec4534 | 2015-05-19 17:11:36 -0700 | [diff] [blame] | 151 | def change_view(self,request, *args, **kwargs): |
| 152 | if request.user.is_admin: |
| 153 | self.readonly_fields = self.admin_readonly_fields |
| 154 | else: |
| 155 | self.readonly_fields = self.cp_readonly_fields |
| 156 | |
| 157 | return super(ContentProviderAdmin, self).change_view(request, *args, **kwargs) |
| 158 | |
Scott Baker | 4609e48 | 2015-05-21 10:25:58 -0700 | [diff] [blame] | 159 | def has_add_permission(self, request): |
| 160 | return request.user.is_admin |
| 161 | |
| 162 | def has_delete_permission(self, request, obj=None): |
| 163 | return request.user.is_admin |
| 164 | |
Scott Baker | fbe38ee | 2015-02-27 12:12:14 -0800 | [diff] [blame] | 165 | class ServiceProviderAdmin(HPCAdmin): |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 166 | list_display = ('backend_status_icon', 'name', 'description', 'enabled') |
| 167 | list_display_links = ('backend_status_icon', 'name', ) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 168 | fieldsets = [ |
Scott Baker | 3035039 | 2015-03-13 09:17:33 -0700 | [diff] [blame] | 169 | (None, {'fields': ['backend_status_text', 'name','description','enabled', 'hpcService'], 'classes':['suit-tab suit-tab-general']})] |
Scott Baker | 446eaca | 2014-05-23 13:16:57 -0700 | [diff] [blame] | 170 | #, ('Content Providers', {'fields':['contentProviders'],'classes':['suit-tab suit-tab-cps']})] |
| 171 | |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 172 | readonly_fields = ('backend_status_text', ) |
Scott Baker | 446eaca | 2014-05-23 13:16:57 -0700 | [diff] [blame] | 173 | user_readonly_fields = ('name', 'description', 'enabled') |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 174 | |
| 175 | suit_form_tabs = (('general','Details'),('cps','Content Providers')) |
| 176 | inlines = [ContentProviderInline] |
| 177 | |
| 178 | class CDNPrefixForm(forms.ModelForm): |
| 179 | class Meta: |
| 180 | widgets = { |
| 181 | 'contentProvider' : LinkedSelect |
| 182 | } |
| 183 | |
Scott Baker | fbe38ee | 2015-02-27 12:12:14 -0800 | [diff] [blame] | 184 | class CDNPrefixAdmin(HPCAdmin): |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 185 | form = CDNPrefixForm |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 186 | list_display = ['backend_status_icon', 'prefix','contentProvider'] |
| 187 | list_display_links = ('backend_status_icon', 'prefix', ) |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 188 | fields = ['backend_status_text', 'prefix', 'contentProvider', 'cdn_prefix_id', 'description', 'defaultOriginServer', 'enabled'] |
| 189 | readonly_fields = ('backend_status_text', ) |
Scott Baker | 446eaca | 2014-05-23 13:16:57 -0700 | [diff] [blame] | 190 | user_readonly_fields = ['prefix','contentProvider', "cdn_prefix_id", "description", "defaultOriginServer", "enabled"] |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 191 | |
Scott Baker | fbe38ee | 2015-02-27 12:12:14 -0800 | [diff] [blame] | 192 | class SiteMapAdmin(HPCAdmin): |
Scott Baker | 5349108 | 2014-03-13 23:15:19 -0700 | [diff] [blame] | 193 | model = SiteMap |
| 194 | verbose_name = "Site Map" |
| 195 | verbose_name_plural = "Site Map" |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 196 | list_display = ("backend_status_icon", "name", "contentProvider", "serviceProvider") |
| 197 | list_display_links = ('backend_status_icon', 'name', ) |
Scott Baker | 69873fd | 2015-03-13 09:24:25 -0700 | [diff] [blame] | 198 | fields = ['backend_status_text', 'name', 'hpcService', 'cdnPrefix', 'contentProvider', 'serviceProvider', 'map', 'map_id'] |
| 199 | user_readonly_fields = ('backend_status_text', "name", "hpcService", "cdnPrefix", "contentProvider", "serviceProvider", "description", "map") |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 200 | readonly_fields = ('backend_status_text', ) |
Scott Baker | 5349108 | 2014-03-13 23:15:19 -0700 | [diff] [blame] | 201 | |
Scott Baker | fbe38ee | 2015-02-27 12:12:14 -0800 | [diff] [blame] | 202 | class AccessMapAdmin(HPCAdmin): |
Scott Baker | 5349108 | 2014-03-13 23:15:19 -0700 | [diff] [blame] | 203 | model = AccessMap |
| 204 | verbose_name = "Access Map" |
| 205 | verbose_name_plural = "Access Map" |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 206 | list_display = ("backend_status_icon", "name", "contentProvider") |
| 207 | list_display_links = ('backend_status_icon', 'name', ) |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 208 | user_readonly_fields = ('backend_status_text', "name", "contentProvider", "description", "map") |
| 209 | readonly_fields = ('backend_status_text', ) |
Scott Baker | 5349108 | 2014-03-13 23:15:19 -0700 | [diff] [blame] | 210 | |
Scott Baker | 3b5fa5b | 2015-03-18 17:37:46 -0700 | [diff] [blame] | 211 | class HpcHealthCheckAdmin(HPCAdmin): |
| 212 | model = HpcHealthCheck |
| 213 | verbose_name = "Health Check" |
| 214 | verbose_name = "Health Checks" |
| 215 | list_display = ["backend_status_icon", "resource_name", "kind"] |
| 216 | list_display_links = ["backend_status_icon", "resource_name"] |
| 217 | fields = ["backend_status_text", "hpcService", "resource_name", "kind", "result_contains", "result_min_size", "result_max_size"] |
| 218 | readonly_fields = ["backend_status_text",] |
| 219 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 220 | admin.site.register(ServiceProvider, ServiceProviderAdmin) |
| 221 | admin.site.register(ContentProvider, ContentProviderAdmin) |
| 222 | admin.site.register(CDNPrefix, CDNPrefixAdmin) |
| 223 | admin.site.register(OriginServer,OriginServerAdmin) |
Scott Baker | 446eaca | 2014-05-23 13:16:57 -0700 | [diff] [blame] | 224 | admin.site.register(HpcService, HpcServiceAdmin) |
Scott Baker | 5349108 | 2014-03-13 23:15:19 -0700 | [diff] [blame] | 225 | admin.site.register(SiteMap, SiteMapAdmin) |
| 226 | admin.site.register(AccessMap, AccessMapAdmin) |
Scott Baker | 3b5fa5b | 2015-03-18 17:37:46 -0700 | [diff] [blame] | 227 | admin.site.register(HpcHealthCheck, HpcHealthCheckAdmin) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 228 | |