Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 1 | from core.models import Site |
| 2 | from core.models import * |
| 3 | from openstack.manager import OpenStackManager |
Tony Mack | e59a7c8 | 2013-04-27 11:08:10 -0400 | [diff] [blame] | 4 | |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 5 | from django.contrib import admin |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 6 | from django.contrib.auth.models import Group |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 7 | from django import forms |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 8 | from django.utils.safestring import mark_safe |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 9 | from django.contrib.auth.admin import UserAdmin |
Scott Baker | 69e045d | 2014-11-17 23:44:03 -0800 | [diff] [blame] | 10 | from django.contrib.admin.widgets import FilteredSelectMultiple, AdminTextareaWidget |
Scott Baker | 1a6a390 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 11 | from django.contrib.auth.forms import ReadOnlyPasswordHashField, AdminPasswordChangeForm |
Scott Baker | acd4514 | 2013-05-19 16:19:16 -0700 | [diff] [blame] | 12 | from django.contrib.auth.signals import user_logged_in |
| 13 | from django.utils import timezone |
Siobhan Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 14 | from django.contrib.contenttypes import generic |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 15 | from suit.widgets import LinkedSelect |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 16 | from django.core.exceptions import PermissionDenied |
Tony Mack | 5817cb4 | 2015-02-16 19:54:24 -0500 | [diff] [blame] | 17 | from django.core.urlresolvers import reverse, resolve, NoReverseMatch |
Scott Baker | 69e045d | 2014-11-17 23:44:03 -0800 | [diff] [blame] | 18 | from django.utils.encoding import force_text, python_2_unicode_compatible |
| 19 | from django.utils.html import conditional_escape, format_html |
Scott Baker | a9b8f61 | 2015-02-26 20:42:11 -0800 | [diff] [blame] | 20 | from django.utils.text import capfirst |
Scott Baker | 69e045d | 2014-11-17 23:44:03 -0800 | [diff] [blame] | 21 | from django.forms.utils import flatatt, to_current_timezone |
Scott Baker | 92d2217 | 2014-10-21 21:03:08 -0700 | [diff] [blame] | 22 | from cgi import escape as html_escape |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 23 | |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 24 | import django_evolution |
Scott Baker | 6a99535 | 2014-10-06 17:51:20 -0700 | [diff] [blame] | 25 | import threading |
| 26 | |
| 27 | # thread locals necessary to work around a django-suit issue |
| 28 | _thread_locals = threading.local() |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 29 | |
Scott Baker | e5f9d7d | 2015-02-10 18:24:20 -0800 | [diff] [blame] | 30 | ICON_URLS = {"success": "/static/admin/img/icon_success.gif", |
| 31 | "clock": "/static/admin/img/icon_clock.gif", |
| 32 | "error": "/static/admin/img/icon_error.gif"} |
| 33 | |
| 34 | def backend_icon(obj): |
| 35 | (icon, tooltip) = obj.get_backend_icon() |
| 36 | icon_url = ICON_URLS.get(icon, "unknown") |
| 37 | |
| 38 | if tooltip: |
| 39 | return '<span style="min-width:16px;" title="%s"><img src="%s"></span>' % (tooltip, icon_url) |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 40 | else: |
Scott Baker | e5f9d7d | 2015-02-10 18:24:20 -0800 | [diff] [blame] | 41 | return '<span style="min-width:16px;"><img src="%s"></span>' % icon_url |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 42 | |
| 43 | def backend_text(obj): |
Scott Baker | e5f9d7d | 2015-02-10 18:24:20 -0800 | [diff] [blame] | 44 | (icon, tooltip) = obj.get_backend_icon() |
| 45 | icon_url = ICON_URLS.get(icon, "unknown") |
| 46 | |
| 47 | return '<img src="%s"> %s' % (icon_url, tooltip) |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 48 | |
Scott Baker | 69e045d | 2014-11-17 23:44:03 -0800 | [diff] [blame] | 49 | class UploadTextareaWidget(AdminTextareaWidget): |
| 50 | def render(self, name, value, attrs=None): |
| 51 | if value is None: |
S.Çağlar Onur | b6e63f0 | 2015-02-24 17:28:09 -0500 | [diff] [blame] | 52 | value = '' |
| 53 | final_attrs = self.build_attrs(attrs, name=name) |
| 54 | return format_html('<input type="file" style="width: 0; height: 0" id="btn_upload_%s" onChange="uploadTextarea(event,\'%s\');">' \ |
| 55 | '<button onClick="$(\'#btn_upload_%s\').click(); return false;">Upload</button>' \ |
| 56 | '<br><textarea{0}>\r\n{1}</textarea>' % (attrs["id"], attrs["id"], attrs["id"]), |
| 57 | flatatt(final_attrs), |
Scott Baker | 69e045d | 2014-11-17 23:44:03 -0800 | [diff] [blame] | 58 | force_text(value)) |
| 59 | |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 60 | class PlainTextWidget(forms.HiddenInput): |
| 61 | input_type = 'hidden' |
| 62 | |
| 63 | def render(self, name, value, attrs=None): |
| 64 | if value is None: |
| 65 | value = '' |
| 66 | return mark_safe(str(value) + super(PlainTextWidget, self).render(name, value, attrs)) |
| 67 | |
Scott Baker | 3a8aed6 | 2015-02-27 12:21:22 -0800 | [diff] [blame] | 68 | class XOSAdminMixin(object): |
Scott Baker | 1a6a390 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 69 | # call save_by_user and delete_by_user instead of save and delete |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 70 | |
| 71 | def has_add_permission(self, request, obj=None): |
| 72 | return (not self.__user_is_readonly(request)) |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 73 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 74 | def has_delete_permission(self, request, obj=None): |
| 75 | return (not self.__user_is_readonly(request)) |
| 76 | |
| 77 | def save_model(self, request, obj, form, change): |
| 78 | if self.__user_is_readonly(request): |
Scott Baker | 1a6a390 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 79 | # this 'if' might be redundant if save_by_user is implemented right |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 80 | raise PermissionDenied |
Scott Baker | 1a6a390 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 81 | |
| 82 | obj.caller = request.user |
| 83 | # update openstack connection to use this site/tenant |
| 84 | obj.save_by_user(request.user) |
| 85 | |
| 86 | def delete_model(self, request, obj): |
| 87 | obj.delete_by_user(request.user) |
| 88 | |
| 89 | def save_formset(self, request, form, formset, change): |
| 90 | instances = formset.save(commit=False) |
| 91 | for instance in instances: |
| 92 | instance.save_by_user(request.user) |
| 93 | |
| 94 | # BUG in django 1.7? Objects are not deleted by formset.save if |
| 95 | # commit is False. So let's delete them ourselves. |
| 96 | # |
| 97 | # code from forms/models.py save_existing_objects() |
| 98 | try: |
S.Çağlar Onur | b6e63f0 | 2015-02-24 17:28:09 -0500 | [diff] [blame] | 99 | forms_to_delete = formset.deleted_forms |
| 100 | except AttributeError: |
Scott Baker | 1a6a390 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 101 | forms_to_delete = [] |
| 102 | if formset.initial_forms: |
| 103 | for form in formset.initial_forms: |
| 104 | obj = form.instance |
| 105 | if form in forms_to_delete: |
| 106 | if obj.pk is None: |
| 107 | continue |
| 108 | formset.deleted_objects.append(obj) |
| 109 | obj.delete() |
| 110 | |
| 111 | formset.save_m2m() |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 112 | |
| 113 | def get_actions(self,request): |
Scott Baker | 3a8aed6 | 2015-02-27 12:21:22 -0800 | [diff] [blame] | 114 | actions = super(XOSAdminMixin,self).get_actions(request) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 115 | |
| 116 | if self.__user_is_readonly(request): |
| 117 | if 'delete_selected' in actions: |
| 118 | del actions['delete_selected'] |
| 119 | |
| 120 | return actions |
| 121 | |
Scott Baker | fbe0f65 | 2015-04-03 17:44:31 -0700 | [diff] [blame] | 122 | def url_for_model_changelist(self, request, model): |
| 123 | # used in add_extra_context |
| 124 | return reverse('admin:%s_%s_changelist' % (model._meta.app_label, model._meta.model_name), current_app=model._meta.app_label) |
| 125 | |
Scott Baker | a8ef274 | 2015-04-02 22:32:40 -0700 | [diff] [blame] | 126 | def add_extra_context(self, request, extra_context): |
Scott Baker | fbe38ee | 2015-02-27 12:12:14 -0800 | [diff] [blame] | 127 | # allow custom application breadcrumb url and name |
| 128 | extra_context["custom_app_breadcrumb_url"] = getattr(self, "custom_app_breadcrumb_url", None) |
| 129 | extra_context["custom_app_breadcrumb_name"] = getattr(self, "custom_app_breadcrumb_name", None) |
Scott Baker | f85c009 | 2015-04-02 22:07:18 -0700 | [diff] [blame] | 130 | extra_context["custom_changelist_breadcrumb_url"] = getattr(self, "custom_changelist_breadcrumb_url", None) |
Scott Baker | fbe38ee | 2015-02-27 12:12:14 -0800 | [diff] [blame] | 131 | |
| 132 | # for Service admins to render their Administration page |
| 133 | if getattr(self, "extracontext_registered_admins", False): |
| 134 | admins=[] |
| 135 | for model, model_admin in admin.site._registry.items():
|
| 136 | if model == self.model:
|
| 137 | continue
|
| 138 | if model._meta.app_label == self.model._meta.app_label:
|
| 139 | info = {"app": model._meta.app_label,
|
| 140 | "model": model._meta.model_name,
|
| 141 | "name": capfirst(model._meta.verbose_name_plural),
|
Scott Baker | fbe0f65 | 2015-04-03 17:44:31 -0700 | [diff] [blame] | 142 | "url": self.url_for_model_changelist(request,model) }
|
Scott Baker | fbe38ee | 2015-02-27 12:12:14 -0800 | [diff] [blame] | 143 | admins.append(info)
|
| 144 | extra_context["registered_admins"] = admins |
| 145 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 146 | def change_view(self,request,object_id, extra_context=None): |
Scott Baker | fbe38ee | 2015-02-27 12:12:14 -0800 | [diff] [blame] | 147 | extra_context = extra_context or {} |
| 148 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 149 | if self.__user_is_readonly(request): |
S.Çağlar Onur | b6e63f0 | 2015-02-24 17:28:09 -0500 | [diff] [blame] | 150 | if not hasattr(self, "readonly_save"): |
| 151 | # save the original readonly fields |
| 152 | self.readonly_save = self.readonly_fields |
| 153 | self.inlines_save = self.inlines |
| 154 | if hasattr(self, "user_readonly_fields"): |
| 155 | self.readonly_fields=self.user_readonly_fields |
| 156 | if hasattr(self, "user_readonly_inlines"): |
| 157 | self.inlines = self.user_readonly_inlines |
| 158 | else: |
| 159 | if hasattr(self, "readonly_save"): |
| 160 | # restore the original readonly fields |
| 161 | self.readonly_fields = self.readonly_save |
| 162 | if hasattr(self, "inlines_save"): |
Scott Baker | af73e10 | 2014-04-22 22:40:07 -0700 | [diff] [blame] | 163 | self.inlines = self.inlines_save |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 164 | |
Scott Baker | a8ef274 | 2015-04-02 22:32:40 -0700 | [diff] [blame] | 165 | self.add_extra_context(request, extra_context) |
Scott Baker | fbe38ee | 2015-02-27 12:12:14 -0800 | [diff] [blame] | 166 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 167 | try: |
Scott Baker | 3a8aed6 | 2015-02-27 12:21:22 -0800 | [diff] [blame] | 168 | return super(XOSAdminMixin, self).change_view(request, object_id, extra_context=extra_context) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 169 | except PermissionDenied: |
| 170 | pass |
| 171 | if request.method == 'POST': |
| 172 | raise PermissionDenied |
| 173 | request.readonly = True |
Scott Baker | 3a8aed6 | 2015-02-27 12:21:22 -0800 | [diff] [blame] | 174 | return super(XOSAdminMixin, self).change_view(request, object_id, extra_context=extra_context) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 175 | |
Scott Baker | fbe38ee | 2015-02-27 12:12:14 -0800 | [diff] [blame] | 176 | def changelist_view(self, request, extra_context = None): |
| 177 | extra_context = extra_context or {} |
| 178 | |
Scott Baker | a8ef274 | 2015-04-02 22:32:40 -0700 | [diff] [blame] | 179 | self.add_extra_context(request, extra_context) |
Scott Baker | fbe38ee | 2015-02-27 12:12:14 -0800 | [diff] [blame] | 180 | |
Scott Baker | 3a8aed6 | 2015-02-27 12:21:22 -0800 | [diff] [blame] | 181 | return super(XOSAdminMixin, self).changelist_view(request, extra_context=extra_context) |
Scott Baker | fbe38ee | 2015-02-27 12:12:14 -0800 | [diff] [blame] | 182 | |
Scott Baker | 88ac9d6 | 2015-04-14 17:01:18 -0700 | [diff] [blame] | 183 | def add_view(self, request, form_url='', extra_context = None): |
Scott Baker | fbe0f65 | 2015-04-03 17:44:31 -0700 | [diff] [blame] | 184 | extra_context = extra_context or {} |
| 185 | |
| 186 | self.add_extra_context(request, extra_context) |
| 187 | |
Scott Baker | 88ac9d6 | 2015-04-14 17:01:18 -0700 | [diff] [blame] | 188 | return super(XOSAdminMixin, self).add_view(request, form_url, extra_context=extra_context) |
Scott Baker | fbe0f65 | 2015-04-03 17:44:31 -0700 | [diff] [blame] | 189 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 190 | def __user_is_readonly(self, request): |
| 191 | return request.user.isReadOnlyUser() |
| 192 | |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 193 | def backend_status_text(self, obj): |
| 194 | return mark_safe(backend_text(obj)) |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 195 | |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 196 | def backend_status_icon(self, obj): |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 197 | return mark_safe(backend_icon(obj)) |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 198 | backend_status_icon.short_description = "" |
| 199 | |
Scott Baker | dc4724c | 2014-11-05 09:05:38 -0800 | [diff] [blame] | 200 | def get_form(self, request, obj=None, **kwargs): |
Scott Baker | 9b3c1af | 2014-10-16 00:57:55 -0700 | [diff] [blame] | 201 | # Save obj and request in thread-local storage, so suit_form_tabs can |
| 202 | # use it to determine whether we're in edit or add mode, and can |
| 203 | # determine whether the user is an admin. |
| 204 | _thread_locals.request = request |
| 205 | _thread_locals.obj = obj |
Scott Baker | 3a8aed6 | 2015-02-27 12:21:22 -0800 | [diff] [blame] | 206 | return super(XOSAdminMixin, self).get_form(request, obj, **kwargs) |
Scott Baker | 9b3c1af | 2014-10-16 00:57:55 -0700 | [diff] [blame] | 207 | |
| 208 | def get_inline_instances(self, request, obj=None): |
Scott Baker | 3a8aed6 | 2015-02-27 12:21:22 -0800 | [diff] [blame] | 209 | inlines = super(XOSAdminMixin, self).get_inline_instances(request, obj) |
Scott Baker | 9b3c1af | 2014-10-16 00:57:55 -0700 | [diff] [blame] | 210 | |
| 211 | # inlines that should only be shown to an admin user |
| 212 | if request.user.is_admin: |
| 213 | for inline_class in getattr(self, "admin_inlines", []): |
| 214 | inlines.append(inline_class(self.model, self.admin_site)) |
| 215 | |
| 216 | return inlines |
| 217 | |
Scott Baker | 3a8aed6 | 2015-02-27 12:21:22 -0800 | [diff] [blame] | 218 | class ReadOnlyAwareAdmin(XOSAdminMixin, admin.ModelAdmin): |
| 219 | # Note: Make sure XOSAdminMixin is listed before |
Scott Baker | f4aeedc | 2014-10-03 13:10:47 -0700 | [diff] [blame] | 220 | # admin.ModelAdmin in the class declaration. |
| 221 | |
Scott Baker | 1a6a390 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 222 | pass |
| 223 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 224 | class XOSBaseAdmin(ReadOnlyAwareAdmin): |
Scott Baker | 1a6a390 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 225 | save_on_top = False |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 226 | |
Scott Baker | e8859f9 | 2014-05-23 12:42:40 -0700 | [diff] [blame] | 227 | class SingletonAdmin (ReadOnlyAwareAdmin): |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 228 | def has_add_permission(self, request): |
Scott Baker | e8859f9 | 2014-05-23 12:42:40 -0700 | [diff] [blame] | 229 | if not super(SingletonAdmin, self).has_add_permission(request): |
| 230 | return False |
| 231 | |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 232 | num_objects = self.model.objects.count() |
| 233 | if num_objects >= 1: |
| 234 | return False |
| 235 | else: |
| 236 | return True |
| 237 | |
Scott Baker | a9b8f61 | 2015-02-26 20:42:11 -0800 | [diff] [blame] | 238 | class ServiceAppAdmin (SingletonAdmin): |
Scott Baker | fbe38ee | 2015-02-27 12:12:14 -0800 | [diff] [blame] | 239 | extracontext_registered_admins = True |
Scott Baker | a9b8f61 | 2015-02-26 20:42:11 -0800 | [diff] [blame] | 240 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 241 | class XOSTabularInline(admin.TabularInline): |
Scott Baker | 8656832 | 2014-01-12 16:53:31 -0800 | [diff] [blame] | 242 | def __init__(self, *args, **kwargs): |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 243 | super(XOSTabularInline, self).__init__(*args, **kwargs) |
Scott Baker | 8656832 | 2014-01-12 16:53:31 -0800 | [diff] [blame] | 244 | |
| 245 | # InlineModelAdmin as no get_fields() method, so in order to add |
| 246 | # the selflink field, we override __init__ to modify self.fields and |
| 247 | # self.readonly_fields. |
| 248 | |
Scott Baker | e2bbf7e | 2014-01-13 12:09:31 -0800 | [diff] [blame] | 249 | self.setup_selflink() |
| 250 | |
Scott Baker | 874936e | 2014-01-13 18:15:34 -0800 | [diff] [blame] | 251 | def get_change_url(self, model, id): |
| 252 | """ Get the URL to a change form in the admin for this model """ |
| 253 | reverse_path = "admin:%s_change" % (model._meta.db_table) |
Scott Baker | e2bbf7e | 2014-01-13 12:09:31 -0800 | [diff] [blame] | 254 | try: |
Scott Baker | 874936e | 2014-01-13 18:15:34 -0800 | [diff] [blame] | 255 | url = reverse(reverse_path, args=(id,)) |
Scott Baker | e2bbf7e | 2014-01-13 12:09:31 -0800 | [diff] [blame] | 256 | except NoReverseMatch: |
Scott Baker | 874936e | 2014-01-13 18:15:34 -0800 | [diff] [blame] | 257 | return None |
| 258 | |
| 259 | return url |
| 260 | |
| 261 | def setup_selflink(self): |
| 262 | if hasattr(self, "selflink_fieldname"): |
| 263 | """ self.selflink_model can be defined to punch through a relation |
| 264 | to its target object. For example, in SliceNetworkInline, set |
| 265 | selflink_model = "network", and the URL will lead to the Network |
| 266 | object instead of trying to bring up a change view of the |
| 267 | SliceNetwork object. |
| 268 | """ |
| 269 | self.selflink_model = getattr(self.model,self.selflink_fieldname).field.rel.to |
| 270 | else: |
| 271 | self.selflink_model = self.model |
| 272 | |
| 273 | url = self.get_change_url(self.selflink_model, 0) |
| 274 | |
| 275 | # We don't have an admin for this object, so don't create the |
| 276 | # selflink. |
| 277 | if (url == None): |
Scott Baker | e2bbf7e | 2014-01-13 12:09:31 -0800 | [diff] [blame] | 278 | return |
| 279 | |
Scott Baker | 874936e | 2014-01-13 18:15:34 -0800 | [diff] [blame] | 280 | # Since we need to add "selflink" to the field list, we need to create |
| 281 | # self.fields if it is None. |
Scott Baker | 0165fac | 2014-01-13 11:49:26 -0800 | [diff] [blame] | 282 | if (self.fields is None): |
| 283 | self.fields = [] |
| 284 | for f in self.model._meta.fields: |
| 285 | if f.editable and f.name != "id": |
| 286 | self.fields.append(f.name) |
Scott Baker | 8656832 | 2014-01-12 16:53:31 -0800 | [diff] [blame] | 287 | |
Scott Baker | 874936e | 2014-01-13 18:15:34 -0800 | [diff] [blame] | 288 | self.fields = tuple(self.fields) + ("selflink", ) |
Scott Baker | 8656832 | 2014-01-12 16:53:31 -0800 | [diff] [blame] | 289 | |
Scott Baker | 874936e | 2014-01-13 18:15:34 -0800 | [diff] [blame] | 290 | if self.readonly_fields is None: |
| 291 | self.readonly_fields = () |
Scott Baker | 8656832 | 2014-01-12 16:53:31 -0800 | [diff] [blame] | 292 | |
Scott Baker | 874936e | 2014-01-13 18:15:34 -0800 | [diff] [blame] | 293 | self.readonly_fields = tuple(self.readonly_fields) + ("selflink", ) |
Scott Baker | 8656832 | 2014-01-12 16:53:31 -0800 | [diff] [blame] | 294 | |
| 295 | def selflink(self, obj): |
Scott Baker | 874936e | 2014-01-13 18:15:34 -0800 | [diff] [blame] | 296 | if hasattr(self, "selflink_fieldname"): |
| 297 | obj = getattr(obj, self.selflink_fieldname) |
| 298 | |
Scott Baker | 8656832 | 2014-01-12 16:53:31 -0800 | [diff] [blame] | 299 | if obj.id: |
Scott Baker | 874936e | 2014-01-13 18:15:34 -0800 | [diff] [blame] | 300 | url = self.get_change_url(self.selflink_model, obj.id) |
| 301 | return "<a href='%s'>Details</a>" % str(url) |
S.Çağlar Onur | b6e63f0 | 2015-02-24 17:28:09 -0500 | [diff] [blame] | 302 | else: |
| 303 | return "Not present" |
Scott Baker | 8656832 | 2014-01-12 16:53:31 -0800 | [diff] [blame] | 304 | |
| 305 | selflink.allow_tags = True |
| 306 | selflink.short_description = "Details" |
Siobhan Tully | d351575 | 2013-06-21 16:34:53 -0400 | [diff] [blame] | 307 | |
Scott Baker | b27b62c | 2014-08-15 16:29:16 -0700 | [diff] [blame] | 308 | def has_add_permission(self, request): |
| 309 | return not request.user.isReadOnlyUser() |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 310 | |
| 311 | def get_readonly_fields(self, request, obj=None): |
Scott Baker | b27b62c | 2014-08-15 16:29:16 -0700 | [diff] [blame] | 312 | readonly_fields = list(self.readonly_fields)[:] |
| 313 | if request.user.isReadOnlyUser(): |
| 314 | for field in self.fields: |
| 315 | if not field in readonly_fields: |
| 316 | readonly_fields.append(field) |
| 317 | return readonly_fields |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 318 | |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 319 | def backend_status_icon(self, obj): |
| 320 | return mark_safe(backend_icon(obj)) |
| 321 | backend_status_icon.short_description = "" |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 322 | |
Scott Baker | b27b62c | 2014-08-15 16:29:16 -0700 | [diff] [blame] | 323 | class PlStackGenericTabularInline(generic.GenericTabularInline): |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 324 | def has_add_permission(self, request): |
Scott Baker | b27b62c | 2014-08-15 16:29:16 -0700 | [diff] [blame] | 325 | return not request.user.isReadOnlyUser() |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 326 | |
Scott Baker | b27b62c | 2014-08-15 16:29:16 -0700 | [diff] [blame] | 327 | def get_readonly_fields(self, request, obj=None): |
| 328 | readonly_fields = list(self.readonly_fields)[:] |
| 329 | if request.user.isReadOnlyUser(): |
| 330 | for field in self.fields: |
| 331 | if not field in readonly_fields: |
| 332 | readonly_fields.append(field) |
| 333 | return readonly_fields |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 334 | |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 335 | def backend_status_icon(self, obj): |
| 336 | return mark_safe(backend_icon(obj)) |
| 337 | backend_status_icon.short_description = "" |
| 338 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 339 | class ReservationInline(XOSTabularInline): |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 340 | model = Reservation |
| 341 | extra = 0 |
| 342 | suit_classes = 'suit-tab suit-tab-reservations' |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 343 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 344 | def queryset(self, request): |
| 345 | return Reservation.select_by_user(request.user) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 346 | |
Scott Baker | b27b62c | 2014-08-15 16:29:16 -0700 | [diff] [blame] | 347 | class TagInline(PlStackGenericTabularInline): |
Siobhan Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 348 | model = Tag |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 349 | extra = 0 |
| 350 | suit_classes = 'suit-tab suit-tab-tags' |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 351 | fields = ['service', 'name', 'value'] |
| 352 | |
| 353 | def queryset(self, request): |
| 354 | return Tag.select_by_user(request.user) |
Siobhan Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 355 | |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 356 | class NetworkLookerUpper: |
Siobhan Tully | 2c780ad | 2013-09-06 11:22:40 -0400 | [diff] [blame] | 357 | """ This is a callable that looks up a network name in a sliver and returns |
| 358 | the ip address for that network. |
| 359 | """ |
| 360 | |
Scott Baker | 434ca7e | 2014-08-15 12:29:20 -0700 | [diff] [blame] | 361 | byNetworkName = {} # class variable |
| 362 | |
Siobhan Tully | 2c780ad | 2013-09-06 11:22:40 -0400 | [diff] [blame] | 363 | def __init__(self, name): |
| 364 | self.short_description = name |
| 365 | self.__name__ = name |
| 366 | self.network_name = name |
| 367 | |
| 368 | def __call__(self, obj): |
| 369 | if obj is not None: |
| 370 | for nbs in obj.networksliver_set.all(): |
| 371 | if (nbs.network.name == self.network_name): |
| 372 | return nbs.ip |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 373 | return "" |
| 374 | |
| 375 | def __str__(self): |
| 376 | return self.network_name |
| 377 | |
Scott Baker | 434ca7e | 2014-08-15 12:29:20 -0700 | [diff] [blame] | 378 | @staticmethod |
| 379 | def get(network_name): |
| 380 | """ We want to make sure we alwars return the same NetworkLookerUpper |
| 381 | because sometimes django will cause them to be instantiated multiple |
| 382 | times (and we don't want different ones in form.fields vs |
| 383 | SliverInline.readonly_fields). |
| 384 | """ |
| 385 | if network_name not in NetworkLookerUpper.byNetworkName: |
| 386 | NetworkLookerUpper.byNetworkName[network_name] = NetworkLookerUpper(network_name) |
| 387 | return NetworkLookerUpper.byNetworkName[network_name] |
| 388 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 389 | class SliverInline(XOSTabularInline): |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 390 | model = Sliver |
Scott Baker | 9d85605 | 2015-01-19 11:32:20 -0800 | [diff] [blame] | 391 | fields = ['backend_status_icon', 'all_ips_string', 'instance_id', 'instance_name', 'slice', 'deployment', 'flavor', 'image', 'node'] |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 392 | extra = 0 |
Scott Baker | 9d85605 | 2015-01-19 11:32:20 -0800 | [diff] [blame] | 393 | readonly_fields = ['backend_status_icon', 'all_ips_string', 'instance_id', 'instance_name'] |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 394 | suit_classes = 'suit-tab suit-tab-slivers' |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 395 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 396 | def queryset(self, request): |
| 397 | return Sliver.select_by_user(request.user) |
| 398 | |
Scott Baker | b24cc93 | 2014-06-09 10:51:16 -0700 | [diff] [blame] | 399 | def formfield_for_foreignkey(self, db_field, request=None, **kwargs): |
Tony Mack | b2dba4b | 2014-12-26 13:38:02 -0500 | [diff] [blame] | 400 | if db_field.name == 'deployment': |
Tony Mack | 1b8975c | 2015-02-24 15:41:49 -0500 | [diff] [blame] | 401 | |
| 402 | kwargs['queryset'] = Deployment.select_by_acl(request.user).filter(sitedeployments__nodes__isnull=False).distinct() |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 403 | kwargs['widget'] = forms.Select(attrs={'onChange': "sliver_deployment_changed(this);"}) |
Tony Mack | b2dba4b | 2014-12-26 13:38:02 -0500 | [diff] [blame] | 404 | if db_field.name == 'flavor': |
Scott Baker | 4b6d944 | 2014-09-08 12:14:14 -0700 | [diff] [blame] | 405 | kwargs['widget'] = forms.Select(attrs={'onChange': "sliver_flavor_changed(this);"}) |
Scott Baker | 3b67874 | 2014-06-09 13:11:54 -0700 | [diff] [blame] | 406 | |
| 407 | field = super(SliverInline, self).formfield_for_foreignkey(db_field, request, **kwargs) |
Scott Baker | b24cc93 | 2014-06-09 10:51:16 -0700 | [diff] [blame] | 408 | |
| 409 | return field |
| 410 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 411 | class SiteInline(XOSTabularInline): |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 412 | model = Site |
| 413 | extra = 0 |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 414 | suit_classes = 'suit-tab suit-tab-sites' |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 415 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 416 | def queryset(self, request): |
| 417 | return Site.select_by_user(request.user) |
| 418 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 419 | class UserInline(XOSTabularInline): |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 420 | model = User |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 421 | fields = ['backend_status_icon', 'email', 'firstname', 'lastname'] |
| 422 | readonly_fields = ('backend_status_icon', ) |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 423 | extra = 0 |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 424 | suit_classes = 'suit-tab suit-tab-users' |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 425 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 426 | def queryset(self, request): |
| 427 | return User.select_by_user(request.user) |
| 428 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 429 | class SliceInline(XOSTabularInline): |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 430 | model = Slice |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 431 | fields = ['backend_status_icon', 'name', 'site', 'serviceClass', 'service'] |
| 432 | readonly_fields = ('backend_status_icon', ) |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 433 | extra = 0 |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 434 | suit_classes = 'suit-tab suit-tab-slices' |
| 435 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 436 | def queryset(self, request): |
| 437 | return Slice.select_by_user(request.user) |
| 438 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 439 | class NodeInline(XOSTabularInline): |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 440 | model = Node |
| 441 | extra = 0 |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 442 | suit_classes = 'suit-tab suit-tab-nodes' |
Tony Mack | 68a1e42 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 443 | fields = ['backend_status_icon', 'name', 'site_deployment'] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 444 | readonly_fields = ('backend_status_icon', ) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 445 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 446 | class DeploymentPrivilegeInline(XOSTabularInline): |
Tony Mack | 68a1e42 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 447 | model = DeploymentPrivilege |
| 448 | extra = 0 |
Tony Mack | 88c8990 | 2015-02-09 21:41:57 -0500 | [diff] [blame] | 449 | suit_classes = 'suit-tab suit-tab-deploymentprivileges' |
Tony Mack | 68a1e42 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 450 | fields = ['backend_status_icon', 'user','role','deployment'] |
| 451 | readonly_fields = ('backend_status_icon', ) |
| 452 | |
| 453 | def queryset(self, request): |
| 454 | return DeploymentPrivilege.select_by_user(request.user) |
| 455 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 456 | class ControllerSiteInline(XOSTabularInline): |
Tony Mack | 3066a95 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 457 | model = ControllerSite |
| 458 | extra = 0 |
| 459 | suit_classes = 'suit-tab suit-tab-admin-only' |
Tony Mack | e2363c1 | 2015-01-06 15:08:20 -0500 | [diff] [blame] | 460 | fields = ['controller', 'site', 'tenant_id'] |
Tony Mack | 3066a95 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 461 | |
| 462 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 463 | class SitePrivilegeInline(XOSTabularInline): |
Siobhan Tully | aa1bcd5 | 2013-05-10 12:43:09 -0400 | [diff] [blame] | 464 | model = SitePrivilege |
| 465 | extra = 0 |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 466 | suit_classes = 'suit-tab suit-tab-siteprivileges' |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 467 | fields = ['backend_status_icon', 'user','site', 'role'] |
| 468 | readonly_fields = ('backend_status_icon', ) |
Siobhan Tully | aa1bcd5 | 2013-05-10 12:43:09 -0400 | [diff] [blame] | 469 | |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 470 | def formfield_for_foreignkey(self, db_field, request, **kwargs): |
| 471 | if db_field.name == 'site': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 472 | kwargs['queryset'] = Site.select_by_user(request.user) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 473 | |
| 474 | if db_field.name == 'user': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 475 | kwargs['queryset'] = User.select_by_user(request.user) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 476 | return super(SitePrivilegeInline, self).formfield_for_foreignkey(db_field, request, **kwargs) |
| 477 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 478 | def queryset(self, request): |
| 479 | return SitePrivilege.select_by_user(request.user) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 480 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 481 | class SiteDeploymentInline(XOSTabularInline): |
Tony Mack | 3066a95 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 482 | model = SiteDeployment |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 483 | extra = 0 |
Tony Mack | b81d5e4 | 2015-01-30 10:58:29 -0500 | [diff] [blame] | 484 | suit_classes = 'suit-tab suit-tab-sitedeployments' |
Tony Mack | d14d48f | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 485 | fields = ['backend_status_icon', 'deployment','site', 'controller'] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 486 | readonly_fields = ('backend_status_icon', ) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 487 | |
| 488 | def formfield_for_foreignkey(self, db_field, request, **kwargs): |
| 489 | if db_field.name == 'site': |
| 490 | kwargs['queryset'] = Site.select_by_user(request.user) |
| 491 | |
| 492 | if db_field.name == 'deployment': |
| 493 | kwargs['queryset'] = Deployment.select_by_user(request.user) |
Tony Mack | d14d48f | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 494 | |
| 495 | if db_field.name == 'controller': |
Tony Mack | 5817cb4 | 2015-02-16 19:54:24 -0500 | [diff] [blame] | 496 | kwargs['queryset'] = Controller.select_by_user(request.user).filter(deployment__id=int(resolve(request.path).args[0])) |
Tony Mack | d14d48f | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 497 | |
Tony Mack | 3066a95 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 498 | return super(SiteDeploymentInline, self).formfield_for_foreignkey(db_field, request, **kwargs) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 499 | |
| 500 | def queryset(self, request): |
Tony Mack | 3066a95 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 501 | return SiteDeployment.select_by_user(request.user) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 502 | |
| 503 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 504 | class SlicePrivilegeInline(XOSTabularInline): |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 505 | model = SlicePrivilege |
| 506 | suit_classes = 'suit-tab suit-tab-sliceprivileges' |
| 507 | extra = 0 |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 508 | fields = ('backend_status_icon', 'user', 'slice', 'role') |
| 509 | readonly_fields = ('backend_status_icon', ) |
Siobhan Tully | aa1bcd5 | 2013-05-10 12:43:09 -0400 | [diff] [blame] | 510 | |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 511 | def formfield_for_foreignkey(self, db_field, request, **kwargs): |
| 512 | if db_field.name == 'slice': |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 513 | kwargs['queryset'] = Slice.select_by_user(request.user) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 514 | if db_field.name == 'user': |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 515 | kwargs['queryset'] = User.select_by_user(request.user) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 516 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 517 | return super(SlicePrivilegeInline, self).formfield_for_foreignkey(db_field, request, **kwargs) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 518 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 519 | def queryset(self, request): |
| 520 | return SlicePrivilege.select_by_user(request.user) |
| 521 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 522 | class SliceNetworkInline(XOSTabularInline): |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 523 | model = Network.slices.through |
Scott Baker | 874936e | 2014-01-13 18:15:34 -0800 | [diff] [blame] | 524 | selflink_fieldname = "network" |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 525 | extra = 0 |
| 526 | verbose_name = "Network Connection" |
| 527 | verbose_name_plural = "Network Connections" |
Siobhan Tully | 2d95e48 | 2013-09-06 10:56:06 -0400 | [diff] [blame] | 528 | suit_classes = 'suit-tab suit-tab-slicenetworks' |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 529 | fields = ['backend_status_icon', 'network'] |
| 530 | readonly_fields = ('backend_status_icon', ) |
Scott Baker | 2170b97 | 2014-06-03 12:14:07 -0700 | [diff] [blame] | 531 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 532 | class ImageDeploymentsInline(XOSTabularInline): |
Sapan Bhatia | 1b6bba2 | 2014-11-19 15:10:16 -0500 | [diff] [blame] | 533 | model = ImageDeployments |
Scott Baker | 2170b97 | 2014-06-03 12:14:07 -0700 | [diff] [blame] | 534 | extra = 0 |
| 535 | verbose_name = "Image Deployments" |
| 536 | verbose_name_plural = "Image Deployments" |
| 537 | suit_classes = 'suit-tab suit-tab-imagedeployments' |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 538 | fields = ['backend_status_icon', 'image', 'deployment'] |
| 539 | readonly_fields = ['backend_status_icon'] |
| 540 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 541 | class ControllerImagesInline(XOSTabularInline): |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 542 | model = ControllerImages |
| 543 | extra = 0 |
| 544 | verbose_name = "Controller Images" |
| 545 | verbose_name_plural = "Controller Images" |
| 546 | suit_classes = 'suit-tab suit-tab-admin-only' |
| 547 | fields = ['backend_status_icon', 'image', 'controller', 'glance_image_id'] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 548 | readonly_fields = ['backend_status_icon', 'glance_image_id'] |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 549 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 550 | class SliceRoleAdmin(XOSBaseAdmin): |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 551 | model = SliceRole |
| 552 | pass |
| 553 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 554 | class SiteRoleAdmin(XOSBaseAdmin): |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 555 | model = SiteRole |
| 556 | pass |
| 557 | |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 558 | class DeploymentAdminForm(forms.ModelForm): |
Scott Baker | de0f441 | 2014-06-11 15:40:26 -0700 | [diff] [blame] | 559 | images = forms.ModelMultipleChoiceField( |
| 560 | queryset=Image.objects.all(), |
| 561 | required=False, |
| 562 | help_text="Select which images should be deployed on this deployment", |
| 563 | widget=FilteredSelectMultiple( |
| 564 | verbose_name=('Images'), is_stacked=False |
| 565 | ) |
| 566 | ) |
Scott Baker | 37b4790 | 2014-09-02 14:37:41 -0700 | [diff] [blame] | 567 | flavors = forms.ModelMultipleChoiceField( |
| 568 | queryset=Flavor.objects.all(), |
| 569 | required=False, |
| 570 | help_text="Select which flavors should be usable on this deployment", |
| 571 | widget=FilteredSelectMultiple( |
| 572 | verbose_name=('Flavors'), is_stacked=False |
| 573 | ) |
| 574 | ) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 575 | class Meta: |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 576 | model = Deployment |
Scott Baker | 37b4790 | 2014-09-02 14:37:41 -0700 | [diff] [blame] | 577 | many_to_many = ["flavors",] |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 578 | |
Siobhan Tully | 320b462 | 2014-01-17 15:11:14 -0500 | [diff] [blame] | 579 | def __init__(self, *args, **kwargs): |
Scott Baker | 5380c52 | 2014-06-06 14:49:43 -0700 | [diff] [blame] | 580 | request = kwargs.pop('request', None) |
Siobhan Tully | 320b462 | 2014-01-17 15:11:14 -0500 | [diff] [blame] | 581 | super(DeploymentAdminForm, self).__init__(*args, **kwargs) |
| 582 | |
Scott Baker | 5380c52 | 2014-06-06 14:49:43 -0700 | [diff] [blame] | 583 | self.fields['accessControl'].initial = "allow site " + request.user.site.name |
| 584 | |
Siobhan Tully | 320b462 | 2014-01-17 15:11:14 -0500 | [diff] [blame] | 585 | if self.instance and self.instance.pk: |
Scott Baker | 69e045d | 2014-11-17 23:44:03 -0800 | [diff] [blame] | 586 | self.fields['images'].initial = [x.image for x in self.instance.imagedeployments.all()] |
Scott Baker | 37b4790 | 2014-09-02 14:37:41 -0700 | [diff] [blame] | 587 | self.fields['flavors'].initial = self.instance.flavors.all() |
Scott Baker | de0f441 | 2014-06-11 15:40:26 -0700 | [diff] [blame] | 588 | |
| 589 | def manipulate_m2m_objs(self, this_obj, selected_objs, all_relations, relation_class, local_attrname, foreign_attrname): |
| 590 | """ helper function for handling m2m relations from the MultipleChoiceField |
| 591 | |
| 592 | this_obj: the source object we want to link from |
| 593 | |
| 594 | selected_objs: a list of destination objects we want to link to |
| 595 | |
| 596 | all_relations: the full set of relations involving this_obj, including ones we don't want |
| 597 | |
| 598 | relation_class: the class that implements the relation from source to dest |
| 599 | |
| 600 | local_attrname: field name representing this_obj in relation_class |
| 601 | |
| 602 | foreign_attrname: field name representing selected_objs in relation_class |
| 603 | |
| 604 | This function will remove all newobjclass relations from this_obj |
| 605 | that are not contained in selected_objs, and add any relations that |
| 606 | are in selected_objs but don't exist in the data model yet. |
| 607 | """ |
| 608 | |
| 609 | existing_dest_objs = [] |
| 610 | for relation in list(all_relations): |
| 611 | if getattr(relation, foreign_attrname) not in selected_objs: |
| 612 | #print "deleting site", sdp.site |
| 613 | relation.delete() |
| 614 | else: |
| 615 | existing_dest_objs.append(getattr(relation, foreign_attrname)) |
| 616 | |
| 617 | for dest_obj in selected_objs: |
| 618 | if dest_obj not in existing_dest_objs: |
| 619 | #print "adding site", site |
| 620 | kwargs = {foreign_attrname: dest_obj, local_attrname: this_obj} |
| 621 | relation = relation_class(**kwargs) |
| 622 | relation.save() |
Siobhan Tully | 320b462 | 2014-01-17 15:11:14 -0500 | [diff] [blame] | 623 | |
| 624 | def save(self, commit=True): |
| 625 | deployment = super(DeploymentAdminForm, self).save(commit=False) |
| 626 | |
| 627 | if commit: |
| 628 | deployment.save() |
Scott Baker | 0057d05 | 2014-10-06 17:17:40 -0700 | [diff] [blame] | 629 | # this has to be done after save() if/when a deployment is first created |
| 630 | deployment.flavors = self.cleaned_data['flavors'] |
Siobhan Tully | 320b462 | 2014-01-17 15:11:14 -0500 | [diff] [blame] | 631 | |
| 632 | if deployment.pk: |
Scott Baker | c9b14f7 | 2014-05-22 13:44:20 -0700 | [diff] [blame] | 633 | # save_m2m() doesn't seem to work with 'through' relations. So we |
| 634 | # create/destroy the through models ourselves. There has to be |
| 635 | # a better way... |
| 636 | |
Tony Mack | b2fde61 | 2014-12-15 11:45:02 -0500 | [diff] [blame] | 637 | self.manipulate_m2m_objs(deployment, self.cleaned_data['images'], deployment.imagedeployments.all(), ImageDeployments, "deployment", "image") |
| 638 | # manipulate_m2m_objs doesn't work for Flavor/Deployment relationship |
| 639 | # so well handle that manually here |
| 640 | for flavor in deployment.flavors.all(): |
| 641 | if getattr(flavor, 'name') not in self.cleaned_data['flavors']: |
Tony Mack | d4ab782 | 2014-12-15 12:37:59 -0500 | [diff] [blame] | 642 | deployment.flavors.remove(flavor) |
Tony Mack | b2fde61 | 2014-12-15 11:45:02 -0500 | [diff] [blame] | 643 | for flavor in self.cleaned_data['flavors']: |
| 644 | if flavor not in deployment.flavors.all(): |
| 645 | flavor.deployments.add(deployment) |
Scott Baker | c9b14f7 | 2014-05-22 13:44:20 -0700 | [diff] [blame] | 646 | |
Scott Baker | 37b4790 | 2014-09-02 14:37:41 -0700 | [diff] [blame] | 647 | self.save_m2m() |
Siobhan Tully | 320b462 | 2014-01-17 15:11:14 -0500 | [diff] [blame] | 648 | |
| 649 | return deployment |
| 650 | |
Scott Baker | ff5e0f3 | 2014-05-22 14:40:27 -0700 | [diff] [blame] | 651 | class DeploymentAdminROForm(DeploymentAdminForm): |
| 652 | def save(self, commit=True): |
| 653 | raise PermissionDenied |
| 654 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 655 | class SiteAssocInline(XOSTabularInline): |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 656 | model = Site.deployments.through |
| 657 | extra = 0 |
| 658 | suit_classes = 'suit-tab suit-tab-sites' |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 659 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 660 | class DeploymentAdmin(XOSBaseAdmin): |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 661 | model = Deployment |
Scott Baker | 622bcf0 | 2015-02-10 08:40:34 -0800 | [diff] [blame] | 662 | fieldList = ['backend_status_text', 'name', 'images', 'flavors', 'accessControl'] |
| 663 | fieldsets = [(None, {'fields': fieldList, 'classes':['suit-tab suit-tab-general']})] |
Tony Mack | 68a1e42 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 664 | # node no longer directly connected to deployment |
| 665 | #inlines = [DeploymentPrivilegeInline,NodeInline,TagInline,ImageDeploymentsInline] |
Tony Mack | b81d5e4 | 2015-01-30 10:58:29 -0500 | [diff] [blame] | 666 | inlines = [DeploymentPrivilegeInline,TagInline,ImageDeploymentsInline,SiteDeploymentInline] |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 667 | list_display = ['backend_status_icon', 'name'] |
| 668 | list_display_links = ('backend_status_icon', 'name', ) |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 669 | readonly_fields = ('backend_status_text', ) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 670 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 671 | user_readonly_fields = ['name'] |
| 672 | |
Tony Mack | 68a1e42 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 673 | # nodes no longer direclty connected to deployments |
Scott Baker | 622bcf0 | 2015-02-10 08:40:34 -0800 | [diff] [blame] | 674 | suit_form_tabs =(('general','Deployment Details'),('deploymentprivileges','Privileges'), ('sitedeployments', 'Sites')) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 675 | |
Scott Baker | ff5e0f3 | 2014-05-22 14:40:27 -0700 | [diff] [blame] | 676 | def get_form(self, request, obj=None, **kwargs): |
Tony Mack | cf29cfa | 2015-02-05 06:13:04 -0500 | [diff] [blame] | 677 | if request.user.isReadOnlyUser() or not request.user.is_admin: |
Scott Baker | ff5e0f3 | 2014-05-22 14:40:27 -0700 | [diff] [blame] | 678 | kwargs["form"] = DeploymentAdminROForm |
| 679 | else: |
| 680 | kwargs["form"] = DeploymentAdminForm |
Scott Baker | 5380c52 | 2014-06-06 14:49:43 -0700 | [diff] [blame] | 681 | adminForm = super(DeploymentAdmin,self).get_form(request, obj, **kwargs) |
| 682 | |
| 683 | # from stackexchange: pass the request object into the form |
| 684 | |
| 685 | class AdminFormMetaClass(adminForm): |
| 686 | def __new__(cls, *args, **kwargs): |
| 687 | kwargs['request'] = request |
| 688 | return adminForm(*args, **kwargs) |
| 689 | |
| 690 | return AdminFormMetaClass |
| 691 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 692 | class ControllerAdmin(XOSBaseAdmin): |
Scott Baker | 622bcf0 | 2015-02-10 08:40:34 -0800 | [diff] [blame] | 693 | model = Controller |
Scott Baker | 180148a | 2015-02-16 11:55:09 -0800 | [diff] [blame] | 694 | fieldList = ['deployment', 'name', 'backend_type', 'version', 'auth_url', 'admin_user', 'admin_tenant','admin_password', 'domain'] |
Scott Baker | 622bcf0 | 2015-02-10 08:40:34 -0800 | [diff] [blame] | 695 | fieldsets = [(None, {'fields': fieldList, 'classes':['suit-tab suit-tab-general']})] |
Tony Mack | 3066a95 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 696 | inlines = [ControllerSiteInline] # ,ControllerImagesInline] |
Tony Mack | d14d48f | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 697 | list_display = ['backend_status_icon', 'name', 'version', 'backend_type'] |
| 698 | list_display_links = ('backend_status_icon', 'name', ) |
| 699 | readonly_fields = ('backend_status_text',) |
| 700 | |
| 701 | user_readonly_fields = [] |
| 702 | |
Tony Mack | 2e897fa | 2015-01-13 17:33:08 -0500 | [diff] [blame] | 703 | def save_model(self, request, obj, form, change): |
| 704 | # update openstack connection to use this site/tenant |
| 705 | obj.save_by_user(request.user) |
| 706 | |
| 707 | def delete_model(self, request, obj): |
Scott Baker | 622bcf0 | 2015-02-10 08:40:34 -0800 | [diff] [blame] | 708 | obj.delete_by_user(request.user) |
| 709 | |
Tony Mack | 78fc136 | 2015-02-18 11:41:36 -0500 | [diff] [blame] | 710 | def queryset(self, request): |
| 711 | return Controller.select_by_user(request.user) |
| 712 | |
Scott Baker | 622bcf0 | 2015-02-10 08:40:34 -0800 | [diff] [blame] | 713 | @property |
| 714 | def suit_form_tabs(self): |
| 715 | tabs = [('general', 'Controller Details'), |
| 716 | ] |
| 717 | |
| 718 | request=getattr(_thread_locals, "request", None) |
| 719 | if request and request.user.is_admin: |
| 720 | tabs.append( ('admin-only', 'Admin-Only') ) |
| 721 | |
| 722 | return tabs |
Tony Mack | 2e897fa | 2015-01-13 17:33:08 -0500 | [diff] [blame] | 723 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 724 | class ServiceAttrAsTabInline(XOSTabularInline): |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 725 | model = ServiceAttribute |
| 726 | fields = ['name','value'] |
| 727 | extra = 0 |
| 728 | suit_classes = 'suit-tab suit-tab-serviceattrs' |
| 729 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 730 | class ServiceAdmin(XOSBaseAdmin): |
Scott Baker | 0d30672 | 2015-04-15 20:58:20 -0700 | [diff] [blame] | 731 | list_display = ("backend_status_icon","name","kind","versionNumber","enabled","published") |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 732 | list_display_links = ('backend_status_icon', 'name', ) |
Scott Baker | 0d30672 | 2015-04-15 20:58:20 -0700 | [diff] [blame] | 733 | fieldList = ["backend_status_text","name","kind","description","versionNumber","enabled","published","view_url","icon_url"] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 734 | fieldsets = [(None, {'fields': fieldList, 'classes':['suit-tab suit-tab-general']})] |
| 735 | inlines = [ServiceAttrAsTabInline,SliceInline] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 736 | readonly_fields = ('backend_status_text', ) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 737 | |
| 738 | user_readonly_fields = fieldList |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 739 | |
| 740 | suit_form_tabs =(('general', 'Service Details'), |
| 741 | ('slices','Slices'), |
| 742 | ('serviceattrs','Additional Attributes'), |
| 743 | ) |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 744 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 745 | class SiteNodeInline(XOSTabularInline): |
Tony Mack | 4f134e6 | 2015-01-14 20:58:38 -0500 | [diff] [blame] | 746 | model = Node |
| 747 | fields = ['name', 'site_deployment'] |
| 748 | extra = 0 |
| 749 | suit_classes = 'suit-tab suit-tab-nodes' |
| 750 | |
Tony Mack | c2a0d31 | 2015-02-25 11:39:34 -0500 | [diff] [blame] | 751 | def formfield_for_foreignkey(self, db_field, request, **kwargs): |
| 752 | # only display site deployments associated with this site |
| 753 | if db_field.name == 'site_deployment': |
| 754 | kwargs['queryset'] = SiteDeployment.objects.filter(site__id=int(request.path.split('/')[-2])) |
| 755 | |
| 756 | return super(SiteNodeInline, self).formfield_for_foreignkey(db_field, request, **kwargs) |
| 757 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 758 | class SiteAdmin(XOSBaseAdmin): |
Tony Mack | 450b6e0 | 2015-01-25 12:35:29 -0500 | [diff] [blame] | 759 | #fieldList = ['backend_status_text', 'name', 'site_url', 'enabled', 'is_public', 'login_base', 'accountLink','location'] |
| 760 | fieldList = ['backend_status_text', 'name', 'site_url', 'enabled', 'is_public', 'login_base', 'location'] |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 761 | fieldsets = [ |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 762 | (None, {'fields': fieldList, 'classes':['suit-tab suit-tab-general']}), |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 763 | #('Deployment Networks', {'fields': ['deployments'], 'classes':['suit-tab suit-tab-deployments']}), |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 764 | ] |
Tony Mack | 450b6e0 | 2015-01-25 12:35:29 -0500 | [diff] [blame] | 765 | #readonly_fields = ['backend_status_text', 'accountLink'] |
| 766 | readonly_fields = ['backend_status_text'] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 767 | |
Tony Mack | 450b6e0 | 2015-01-25 12:35:29 -0500 | [diff] [blame] | 768 | #user_readonly_fields = ['name', 'deployments','site_url', 'enabled', 'is_public', 'login_base', 'accountLink'] |
| 769 | user_readonly_fields = ['name', 'deployments','site_url', 'enabled', 'is_public', 'login_base'] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 770 | |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 771 | list_display = ('backend_status_icon', 'name', 'login_base','site_url', 'enabled') |
| 772 | list_display_links = ('backend_status_icon', 'name', ) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 773 | filter_horizontal = ('deployments',) |
Tony Mack | b81d5e4 | 2015-01-30 10:58:29 -0500 | [diff] [blame] | 774 | inlines = [SliceInline,UserInline,TagInline, SitePrivilegeInline, SiteNodeInline] |
Tony Mack | de10018 | 2015-01-14 12:11:05 -0500 | [diff] [blame] | 775 | admin_inlines = [ControllerSiteInline] |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 776 | search_fields = ['name'] |
| 777 | |
Tony Mack | 30dfcd7 | 2015-01-10 23:08:10 -0500 | [diff] [blame] | 778 | @property |
| 779 | def suit_form_tabs(self): |
| 780 | tabs = [('general', 'Site Details'), |
| 781 | ('users','Users'), |
| 782 | ('siteprivileges','Privileges'), |
Tony Mack | 30dfcd7 | 2015-01-10 23:08:10 -0500 | [diff] [blame] | 783 | ('slices','Slices'), |
Tony Mack | 4f134e6 | 2015-01-14 20:58:38 -0500 | [diff] [blame] | 784 | ('nodes','Nodes'), |
Tony Mack | 30dfcd7 | 2015-01-10 23:08:10 -0500 | [diff] [blame] | 785 | ] |
| 786 | |
| 787 | request=getattr(_thread_locals, "request", None) |
| 788 | if request and request.user.is_admin: |
| 789 | tabs.append( ('admin-only', 'Admin-Only') ) |
| 790 | |
| 791 | return tabs |
| 792 | |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 793 | def queryset(self, request): |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 794 | return Site.select_by_user(request.user) |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 795 | |
Tony Mack | 5cd1320 | 2013-05-01 21:48:38 -0400 | [diff] [blame] | 796 | def get_formsets(self, request, obj=None): |
| 797 | for inline in self.get_inline_instances(request, obj): |
| 798 | # hide MyInline in the add view |
| 799 | if obj is None: |
| 800 | continue |
Tony Mack | 2bd5b41 | 2013-06-11 21:05:06 -0400 | [diff] [blame] | 801 | if isinstance(inline, SliverInline): |
| 802 | inline.model.caller = request.user |
Tony Mack | 5cd1320 | 2013-05-01 21:48:38 -0400 | [diff] [blame] | 803 | yield inline.get_formset(request, obj) |
| 804 | |
Scott Baker | 545db2a | 2013-12-09 18:44:43 -0800 | [diff] [blame] | 805 | def accountLink(self, obj): |
| 806 | link_obj = obj.accounts.all() |
| 807 | if link_obj: |
| 808 | reverse_path = "admin:core_account_change" |
| 809 | url = reverse(reverse_path, args =(link_obj[0].id,)) |
| 810 | return "<a href='%s'>%s</a>" % (url, "view billing details") |
| 811 | else: |
| 812 | return "no billing data for this site" |
| 813 | accountLink.allow_tags = True |
| 814 | accountLink.short_description = "Billing" |
| 815 | |
Tony Mack | 332ee1d | 2014-02-04 15:33:45 -0500 | [diff] [blame] | 816 | def save_model(self, request, obj, form, change): |
| 817 | # update openstack connection to use this site/tenant |
| 818 | obj.save_by_user(request.user) |
| 819 | |
| 820 | def delete_model(self, request, obj): |
| 821 | obj.delete_by_user(request.user) |
| 822 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 823 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 824 | class SitePrivilegeAdmin(XOSBaseAdmin): |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 825 | fieldList = ['backend_status_text', 'user', 'site', 'role'] |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 826 | fieldsets = [ |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 827 | (None, {'fields': fieldList, 'classes':['collapse']}) |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 828 | ] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 829 | readonly_fields = ('backend_status_text', ) |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 830 | list_display = ('backend_status_icon', 'user', 'site', 'role') |
| 831 | list_display_links = list_display |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 832 | user_readonly_fields = fieldList |
| 833 | user_readonly_inlines = [] |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 834 | |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 835 | def formfield_for_foreignkey(self, db_field, request, **kwargs): |
| 836 | if db_field.name == 'site': |
| 837 | if not request.user.is_admin: |
| 838 | # only show sites where user is an admin or pi |
| 839 | sites = set() |
| 840 | for site_privilege in SitePrivilege.objects.filer(user=request.user): |
| 841 | if site_privilege.role.role_type in ['admin', 'pi']: |
| 842 | sites.add(site_privilege.site) |
| 843 | kwargs['queryset'] = Site.objects.filter(site__in=list(sites)) |
| 844 | |
| 845 | if db_field.name == 'user': |
| 846 | if not request.user.is_admin: |
| 847 | # only show users from sites where caller has admin or pi role |
| 848 | roles = Role.objects.filter(role_type__in=['admin', 'pi']) |
| 849 | site_privileges = SitePrivilege.objects.filter(user=request.user).filter(role__in=roles) |
| 850 | sites = [site_privilege.site for site_privilege in site_privileges] |
| 851 | site_privileges = SitePrivilege.objects.filter(site__in=sites) |
| 852 | emails = [site_privilege.user.email for site_privilege in site_privileges] |
| 853 | users = User.objects.filter(email__in=emails) |
| 854 | kwargs['queryset'] = users |
| 855 | |
| 856 | return super(SitePrivilegeAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs) |
| 857 | |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 858 | def queryset(self, request): |
| 859 | # admins can see all privileges. Users can only see privileges at sites |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 860 | # where they have the admin role or pi role. |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 861 | qs = super(SitePrivilegeAdmin, self).queryset(request) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 862 | #if not request.user.is_admin: |
| 863 | # roles = Role.objects.filter(role_type__in=['admin', 'pi']) |
| 864 | # site_privileges = SitePrivilege.objects.filter(user=request.user).filter(role__in=roles) |
| 865 | # login_bases = [site_privilege.site.login_base for site_privilege in site_privileges] |
| 866 | # sites = Site.objects.filter(login_base__in=login_bases) |
| 867 | # qs = qs.filter(site__in=sites) |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 868 | return qs |
| 869 | |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 870 | class SliceForm(forms.ModelForm): |
| 871 | class Meta: |
| 872 | model = Slice |
| 873 | widgets = { |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 874 | 'service': LinkedSelect |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 875 | } |
| 876 | |
Tony Mack | e75841e | 2014-09-29 16:10:52 -0400 | [diff] [blame] | 877 | def clean(self): |
| 878 | cleaned_data = super(SliceForm, self).clean() |
| 879 | name = cleaned_data.get('name') |
Scott Baker | 3cb382c | 2014-10-06 23:09:59 -0700 | [diff] [blame] | 880 | site = cleaned_data.get('site') |
Tony Mack | cc9e259 | 2014-10-22 12:54:19 -0400 | [diff] [blame] | 881 | slice_id = self.instance.id |
| 882 | if not site and slice_id: |
| 883 | site = Slice.objects.get(id=slice_id).site |
Scott Baker | 3cb382c | 2014-10-06 23:09:59 -0700 | [diff] [blame] | 884 | if (not isinstance(site,Site)): |
| 885 | # previous code indicates 'site' could be a site_id and not a site? |
| 886 | site = Slice.objects.get(id=site.id) |
Tony Mack | e75841e | 2014-09-29 16:10:52 -0400 | [diff] [blame] | 887 | if not name.startswith(site.login_base): |
| 888 | raise forms.ValidationError('slice name must begin with %s' % site.login_base) |
| 889 | return cleaned_data |
| 890 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 891 | class ControllerSliceInline(XOSTabularInline): |
Tony Mack | 3066a95 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 892 | model = ControllerSlice |
Scott Baker | f9f1ef4 | 2014-10-15 16:54:04 -0700 | [diff] [blame] | 893 | extra = 0 |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 894 | verbose_name = "Controller Slices" |
| 895 | verbose_name_plural = "Controller Slices" |
Scott Baker | f9f1ef4 | 2014-10-15 16:54:04 -0700 | [diff] [blame] | 896 | suit_classes = 'suit-tab suit-tab-admin-only' |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 897 | fields = ['backend_status_icon', 'controller', 'tenant_id'] |
Tony Mack | 30dfcd7 | 2015-01-10 23:08:10 -0500 | [diff] [blame] | 898 | readonly_fields = ('backend_status_icon', 'controller' ) |
Scott Baker | f9f1ef4 | 2014-10-15 16:54:04 -0700 | [diff] [blame] | 899 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 900 | class SliceAdmin(XOSBaseAdmin): |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 901 | form = SliceForm |
Tony Mack | fbb26fc | 2014-09-02 07:03:27 -0400 | [diff] [blame] | 902 | fieldList = ['backend_status_text', 'site', 'name', 'serviceClass', 'enabled','description', 'service', 'slice_url', 'max_slivers'] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 903 | fieldsets = [('Slice Details', {'fields': fieldList, 'classes':['suit-tab suit-tab-general']}),] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 904 | readonly_fields = ('backend_status_text', ) |
Tony Mack | 7d45990 | 2014-09-03 13:18:57 -0400 | [diff] [blame] | 905 | list_display = ('backend_status_icon', 'name', 'site','serviceClass', 'slice_url', 'max_slivers') |
| 906 | list_display_links = ('backend_status_icon', 'name', ) |
Siobhan Tully | 2d95e48 | 2013-09-06 10:56:06 -0400 | [diff] [blame] | 907 | inlines = [SlicePrivilegeInline,SliverInline, TagInline, ReservationInline,SliceNetworkInline] |
Tony Mack | 3066a95 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 908 | admin_inlines = [ControllerSliceInline] |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 909 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 910 | user_readonly_fields = fieldList |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 911 | |
Scott Baker | f9f1ef4 | 2014-10-15 16:54:04 -0700 | [diff] [blame] | 912 | @property |
| 913 | def suit_form_tabs(self): |
| 914 | tabs =[('general', 'Slice Details'), |
| 915 | ('slicenetworks','Networks'), |
| 916 | ('sliceprivileges','Privileges'), |
| 917 | ('slivers','Slivers'), |
Tony Mack | 450b6e0 | 2015-01-25 12:35:29 -0500 | [diff] [blame] | 918 | #('reservations','Reservations'), |
Tony Mack | d243338 | 2015-01-15 14:44:06 -0500 | [diff] [blame] | 919 | ('tags','Tags'), |
Scott Baker | f9f1ef4 | 2014-10-15 16:54:04 -0700 | [diff] [blame] | 920 | ] |
| 921 | |
| 922 | request=getattr(_thread_locals, "request", None) |
| 923 | if request and request.user.is_admin: |
| 924 | tabs.append( ('admin-only', 'Admin-Only') ) |
| 925 | |
| 926 | return tabs |
Tony Mack | 0aa732a | 2014-10-22 11:54:29 -0400 | [diff] [blame] | 927 | |
| 928 | def add_view(self, request, form_url='', extra_context=None): |
| 929 | # revert to default read-only fields |
| 930 | self.readonly_fields = ('backend_status_text',) |
| 931 | return super(SliceAdmin, self).add_view(request, form_url, extra_context=extra_context) |
| 932 | |
| 933 | def change_view(self, request, object_id, form_url='', extra_context=None): |
Tony Mack | 0aa732a | 2014-10-22 11:54:29 -0400 | [diff] [blame] | 934 | # cannot change the site of an existing slice so make the site field read only |
| 935 | if object_id: |
| 936 | self.readonly_fields = ('backend_status_text','site') |
| 937 | return super(SliceAdmin, self).change_view(request, object_id, form_url) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 938 | |
Scott Baker | 510fdbb | 2014-08-05 17:19:24 -0700 | [diff] [blame] | 939 | def render_change_form(self, request, context, add=False, change=False, form_url='', obj=None): |
Scott Baker | 510fdbb | 2014-08-05 17:19:24 -0700 | [diff] [blame] | 940 | deployment_nodes = [] |
| 941 | for node in Node.objects.all(): |
Scott Baker | 66b11e2 | 2015-01-21 16:24:07 -0800 | [diff] [blame] | 942 | deployment_nodes.append( (node.site_deployment.deployment.id, node.id, node.name) ) |
Scott Baker | 510fdbb | 2014-08-05 17:19:24 -0700 | [diff] [blame] | 943 | |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 944 | deployment_flavors = [] |
| 945 | for flavor in Flavor.objects.all(): |
| 946 | for deployment in flavor.deployments.all(): |
| 947 | deployment_flavors.append( (deployment.id, flavor.id, flavor.name) ) |
| 948 | |
Tony Mack | 68a1e42 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 949 | deployment_images = [] |
Scott Baker | af36c4d | 2014-09-09 09:58:49 -0700 | [diff] [blame] | 950 | for image in Image.objects.all(): |
Tony Mack | 68a1e42 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 951 | for deployment_image in image.imagedeployments.all(): |
Scott Baker | f2c0c51 | 2014-12-22 17:35:34 -0800 | [diff] [blame] | 952 | deployment_images.append( (deployment_image.deployment.id, image.id, image.name) ) |
Scott Baker | af36c4d | 2014-09-09 09:58:49 -0700 | [diff] [blame] | 953 | |
Tony Mack | ec23b99 | 2014-09-02 21:18:45 -0400 | [diff] [blame] | 954 | site_login_bases = [] |
| 955 | for site in Site.objects.all(): |
Scott Baker | af36c4d | 2014-09-09 09:58:49 -0700 | [diff] [blame] | 956 | site_login_bases.append((site.id, site.login_base)) |
| 957 | |
Scott Baker | 510fdbb | 2014-08-05 17:19:24 -0700 | [diff] [blame] | 958 | context["deployment_nodes"] = deployment_nodes |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 959 | context["deployment_flavors"] = deployment_flavors |
Scott Baker | af36c4d | 2014-09-09 09:58:49 -0700 | [diff] [blame] | 960 | context["deployment_images"] = deployment_images |
Tony Mack | ec23b99 | 2014-09-02 21:18:45 -0400 | [diff] [blame] | 961 | context["site_login_bases"] = site_login_bases |
Scott Baker | 510fdbb | 2014-08-05 17:19:24 -0700 | [diff] [blame] | 962 | return super(SliceAdmin, self).render_change_form(request, context, add, change, form_url, obj) |
| 963 | |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 964 | def formfield_for_foreignkey(self, db_field, request, **kwargs): |
| 965 | if db_field.name == 'site': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 966 | kwargs['queryset'] = Site.select_by_user(request.user) |
Tony Mack | ec23b99 | 2014-09-02 21:18:45 -0400 | [diff] [blame] | 967 | kwargs['widget'] = forms.Select(attrs={'onChange': "update_slice_prefix(this, $($(this).closest('fieldset')[0]).find('.field-name input')[0].id)"}) |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 968 | |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 969 | return super(SliceAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs) |
| 970 | |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 971 | def queryset(self, request): |
| 972 | # admins can see all keys. Users can only see slices they belong to. |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 973 | return Slice.select_by_user(request.user) |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 974 | |
Tony Mack | 7974861 | 2013-05-01 14:52:03 -0400 | [diff] [blame] | 975 | def get_formsets(self, request, obj=None): |
| 976 | for inline in self.get_inline_instances(request, obj): |
| 977 | # hide MyInline in the add view |
| 978 | if obj is None: |
| 979 | continue |
Tony Mack | 2bd5b41 | 2013-06-11 21:05:06 -0400 | [diff] [blame] | 980 | if isinstance(inline, SliverInline): |
| 981 | inline.model.caller = request.user |
Tony Mack | 7974861 | 2013-05-01 14:52:03 -0400 | [diff] [blame] | 982 | yield inline.get_formset(request, obj) |
| 983 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 984 | class SlicePrivilegeAdmin(XOSBaseAdmin): |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 985 | fieldsets = [ |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 986 | (None, {'fields': ['backend_status_text', 'user', 'slice', 'role']}) |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 987 | ] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 988 | readonly_fields = ('backend_status_text', ) |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 989 | list_display = ('backend_status_icon', 'user', 'slice', 'role') |
| 990 | list_display_links = list_display |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 991 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 992 | user_readonly_fields = ['user', 'slice', 'role'] |
| 993 | user_readonly_inlines = [] |
| 994 | |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 995 | def formfield_for_foreignkey(self, db_field, request, **kwargs): |
| 996 | if db_field.name == 'slice': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 997 | kwargs['queryset'] = Slice.select_by_user(request.user) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 998 | |
| 999 | if db_field.name == 'user': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 1000 | kwargs['queryset'] = User.select_by_user(request.user) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 1001 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1002 | return super(SlicePrivilegeAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 1003 | |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 1004 | def queryset(self, request): |
| 1005 | # admins can see all memberships. Users can only see memberships of |
| 1006 | # slices where they have the admin role. |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 1007 | return SlicePrivilege.select_by_user(request.user) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1008 | |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 1009 | def save_model(self, request, obj, form, change): |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 1010 | # update openstack connection to use this site/tenant |
| 1011 | auth = request.session.get('auth', {}) |
Tony Mack | f7f79a1 | 2014-08-11 11:21:42 -0400 | [diff] [blame] | 1012 | auth['tenant'] = obj.slice.slicename |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 1013 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 1014 | obj.save() |
| 1015 | |
| 1016 | def delete_model(self, request, obj): |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 1017 | # update openstack connection to use this site/tenant |
| 1018 | auth = request.session.get('auth', {}) |
Tony Mack | f7f79a1 | 2014-08-11 11:21:42 -0400 | [diff] [blame] | 1019 | auth['tenant'] = obj.slice.slicename |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 1020 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 1021 | obj.delete() |
| 1022 | |
Siobhan Tully | 567e3e6 | 2013-06-21 18:03:16 -0400 | [diff] [blame] | 1023 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 1024 | class ImageAdmin(XOSBaseAdmin): |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1025 | |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 1026 | fieldsets = [('Image Details', |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1027 | {'fields': ['backend_status_text', 'name', 'disk_format', 'container_format'], |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1028 | 'classes': ['suit-tab suit-tab-general']}) |
| 1029 | ] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1030 | readonly_fields = ('backend_status_text', ) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1031 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 1032 | suit_form_tabs =(('general','Image Details'),('slivers','Slivers'),('imagedeployments','Deployments'), ('controllerimages', 'Controllers')) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1033 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 1034 | inlines = [SliverInline, ControllerImagesInline] |
Scott Baker | b6f9924 | 2014-06-11 11:34:44 -0700 | [diff] [blame] | 1035 | |
Tony Mack | 32e1ce3 | 2014-05-07 13:29:41 -0400 | [diff] [blame] | 1036 | user_readonly_fields = ['name', 'disk_format', 'container_format'] |
Scott Baker | b27b62c | 2014-08-15 16:29:16 -0700 | [diff] [blame] | 1037 | |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 1038 | list_display = ['backend_status_icon', 'name'] |
| 1039 | list_display_links = ('backend_status_icon', 'name', ) |
| 1040 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1041 | class NodeForm(forms.ModelForm): |
| 1042 | class Meta: |
| 1043 | widgets = { |
| 1044 | 'site': LinkedSelect, |
| 1045 | 'deployment': LinkedSelect |
| 1046 | } |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1047 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 1048 | class NodeAdmin(XOSBaseAdmin): |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1049 | form = NodeForm |
Tony Mack | 68a1e42 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 1050 | list_display = ('backend_status_icon', 'name', 'site_deployment') |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 1051 | list_display_links = ('backend_status_icon', 'name', ) |
Tony Mack | 68a1e42 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 1052 | list_filter = ('site_deployment',) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1053 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1054 | inlines = [TagInline,SliverInline] |
Tony Mack | 68a1e42 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 1055 | fieldsets = [('Node Details', {'fields': ['backend_status_text', 'name','site_deployment'], 'classes':['suit-tab suit-tab-details']})] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1056 | readonly_fields = ('backend_status_text', ) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1057 | |
Tony Mack | 68a1e42 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 1058 | user_readonly_fields = ['name','site_deployment'] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1059 | user_readonly_inlines = [TagInline,SliverInline] |
| 1060 | |
Tony Mack | 5fecf71 | 2015-01-12 21:40:09 -0500 | [diff] [blame] | 1061 | suit_form_tabs =(('details','Node Details'),('slivers','Slivers')) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1062 | |
Siobhan Tully | 567e3e6 | 2013-06-21 18:03:16 -0400 | [diff] [blame] | 1063 | |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 1064 | class SliverForm(forms.ModelForm): |
| 1065 | class Meta: |
Tony Mack | 1d6b85f | 2013-05-07 18:49:14 -0400 | [diff] [blame] | 1066 | model = Sliver |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 1067 | ip = forms.CharField(widget=PlainTextWidget) |
Tony Mack | 1826181 | 2013-05-02 16:39:20 -0400 | [diff] [blame] | 1068 | instance_name = forms.CharField(widget=PlainTextWidget) |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 1069 | widgets = { |
| 1070 | 'ip': PlainTextWidget(), |
Tony Mack | 1826181 | 2013-05-02 16:39:20 -0400 | [diff] [blame] | 1071 | 'instance_name': PlainTextWidget(), |
Scott Baker | 9d85605 | 2015-01-19 11:32:20 -0800 | [diff] [blame] | 1072 | 'instance_id': PlainTextWidget(), |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1073 | 'slice': LinkedSelect, |
Tony Mack | b2dba4b | 2014-12-26 13:38:02 -0500 | [diff] [blame] | 1074 | 'deployment': LinkedSelect, |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1075 | 'node': LinkedSelect, |
| 1076 | 'image': LinkedSelect |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1077 | } |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 1078 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 1079 | class TagAdmin(XOSBaseAdmin): |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 1080 | list_display = ['backend_status_icon', 'service', 'name', 'value', 'content_type', 'content_object',] |
| 1081 | list_display_links = list_display |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1082 | user_readonly_fields = ['service', 'name', 'value', 'content_type', 'content_object',] |
| 1083 | user_readonly_inlines = [] |
Siobhan Tully | d351575 | 2013-06-21 16:34:53 -0400 | [diff] [blame] | 1084 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 1085 | class SliverAdmin(XOSBaseAdmin): |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 1086 | form = SliverForm |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 1087 | fieldsets = [ |
Tony Mack | 707f7d7 | 2015-01-30 12:52:46 -0500 | [diff] [blame] | 1088 | ('Sliver Details', {'fields': ['backend_status_text', 'slice', 'deployment', 'node', 'all_ips_string', 'instance_id', 'instance_name', 'flavor', 'image', 'ssh_command'], 'classes': ['suit-tab suit-tab-general'], }) |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 1089 | ] |
Tony Mack | db8580b | 2015-01-30 17:20:46 -0500 | [diff] [blame] | 1090 | readonly_fields = ('backend_status_text', 'ssh_command', 'all_ips_string') |
Tony Mack | 707f7d7 | 2015-01-30 12:52:46 -0500 | [diff] [blame] | 1091 | list_display = ['backend_status_icon', 'all_ips_string', 'instance_id', 'instance_name', 'slice', 'flavor', 'image', 'node', 'deployment'] |
Scott Baker | 2f29540 | 2015-02-13 14:38:21 -0800 | [diff] [blame] | 1092 | list_display_links = ('backend_status_icon', 'all_ips_string', 'instance_id', ) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1093 | |
Scott Baker | c72997a | 2015-01-19 08:24:08 -0800 | [diff] [blame] | 1094 | suit_form_tabs =(('general', 'Sliver Details'),) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1095 | |
Siobhan Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 1096 | inlines = [TagInline] |
Tony Mack | 53106f3 | 2013-04-27 16:43:01 -0400 | [diff] [blame] | 1097 | |
Tony Mack | b2dba4b | 2014-12-26 13:38:02 -0500 | [diff] [blame] | 1098 | user_readonly_fields = ['slice', 'deployment', 'node', 'ip', 'instance_name', 'flavor', 'image'] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1099 | |
Scott Baker | 7ccc6ad | 2015-01-25 22:16:13 -0800 | [diff] [blame] | 1100 | def ssh_command(self, obj): |
| 1101 | ssh_command = obj.get_ssh_command() |
| 1102 | if ssh_command: |
| 1103 | return ssh_command |
| 1104 | else: |
| 1105 | return "(not available)" |
| 1106 | |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 1107 | def formfield_for_foreignkey(self, db_field, request, **kwargs): |
| 1108 | if db_field.name == 'slice': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 1109 | kwargs['queryset'] = Slice.select_by_user(request.user) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 1110 | |
| 1111 | return super(SliverAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs) |
| 1112 | |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 1113 | def queryset(self, request): |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 1114 | # admins can see all slivers. Users can only see slivers of |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 1115 | # the slices they belong to. |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 1116 | return Sliver.select_by_user(request.user) |
| 1117 | |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 1118 | |
Tony Mack | 1d6b85f | 2013-05-07 18:49:14 -0400 | [diff] [blame] | 1119 | def get_formsets(self, request, obj=None): |
| 1120 | # make some fields read only if we are updating an existing record |
| 1121 | if obj == None: |
Tony Mack | b428feb | 2015-01-30 17:42:10 -0500 | [diff] [blame] | 1122 | self.readonly_fields = ('backend_status_text', 'ssh_command', 'all_ips_string') |
Tony Mack | 1d6b85f | 2013-05-07 18:49:14 -0400 | [diff] [blame] | 1123 | else: |
Tony Mack | b428feb | 2015-01-30 17:42:10 -0500 | [diff] [blame] | 1124 | self.readonly_fields = ('backend_status_text', 'ssh_command', 'all_ips_string', 'slice', 'flavor', 'image', 'node') |
Tony Mack | 1d6b85f | 2013-05-07 18:49:14 -0400 | [diff] [blame] | 1125 | |
| 1126 | for inline in self.get_inline_instances(request, obj): |
| 1127 | # hide MyInline in the add view |
| 1128 | if obj is None: |
| 1129 | continue |
Scott Baker | 526b71e | 2014-05-13 13:18:01 -0700 | [diff] [blame] | 1130 | if isinstance(inline, SliverInline): |
| 1131 | inline.model.caller = request.user |
| 1132 | yield inline.get_formset(request, obj) |
Tony Mack | 53106f3 | 2013-04-27 16:43:01 -0400 | [diff] [blame] | 1133 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1134 | #def save_model(self, request, obj, form, change): |
| 1135 | # # update openstack connection to use this site/tenant |
| 1136 | # auth = request.session.get('auth', {}) |
| 1137 | # auth['tenant'] = obj.slice.name |
| 1138 | # obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
| 1139 | # obj.creator = request.user |
| 1140 | # obj.save() |
Tony Mack | 53106f3 | 2013-04-27 16:43:01 -0400 | [diff] [blame] | 1141 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1142 | #def delete_model(self, request, obj): |
| 1143 | # # update openstack connection to use this site/tenant |
| 1144 | # auth = request.session.get('auth', {}) |
| 1145 | # auth['tenant'] = obj.slice.name |
| 1146 | # obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
| 1147 | # obj.delete() |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 1148 | |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1149 | class UserCreationForm(forms.ModelForm): |
| 1150 | """A form for creating new users. Includes all the required |
| 1151 | fields, plus a repeated password.""" |
| 1152 | password1 = forms.CharField(label='Password', widget=forms.PasswordInput) |
| 1153 | password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput) |
| 1154 | |
| 1155 | class Meta: |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 1156 | model = User |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1157 | fields = ('email', 'firstname', 'lastname', 'phone', 'public_key') |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1158 | |
| 1159 | def clean_password2(self): |
| 1160 | # Check that the two password entries match |
| 1161 | password1 = self.cleaned_data.get("password1") |
| 1162 | password2 = self.cleaned_data.get("password2") |
| 1163 | if password1 and password2 and password1 != password2: |
| 1164 | raise forms.ValidationError("Passwords don't match") |
| 1165 | return password2 |
| 1166 | |
| 1167 | def save(self, commit=True): |
| 1168 | # Save the provided password in hashed format |
| 1169 | user = super(UserCreationForm, self).save(commit=False) |
Tony Mack | f9f4afb | 2013-05-01 21:02:12 -0400 | [diff] [blame] | 1170 | user.password = self.cleaned_data["password1"] |
| 1171 | #user.set_password(self.cleaned_data["password1"]) |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1172 | if commit: |
| 1173 | user.save() |
| 1174 | return user |
| 1175 | |
Siobhan Tully | 567e3e6 | 2013-06-21 18:03:16 -0400 | [diff] [blame] | 1176 | |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1177 | class UserChangeForm(forms.ModelForm): |
| 1178 | """A form for updating users. Includes all the fields on |
| 1179 | the user, but replaces the password field with admin's |
| 1180 | password hash display field. |
| 1181 | """ |
Siobhan Tully | 63b7ba4 | 2014-01-12 10:35:11 -0500 | [diff] [blame] | 1182 | password = ReadOnlyPasswordHashField(label='Password', |
| 1183 | help_text= '<a href=\"password/\">Change Password</a>.') |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1184 | |
| 1185 | class Meta: |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 1186 | model = User |
Scott Baker | 69e045d | 2014-11-17 23:44:03 -0800 | [diff] [blame] | 1187 | widgets = { 'public_key': UploadTextareaWidget, } |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1188 | |
| 1189 | def clean_password(self): |
| 1190 | # Regardless of what the user provides, return the initial value. |
| 1191 | # This is done here, rather than on the field, because the |
| 1192 | # field does not have access to the initial value |
| 1193 | return self.initial["password"] |
| 1194 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 1195 | class UserDashboardViewInline(XOSTabularInline): |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 1196 | model = UserDashboardView |
| 1197 | extra = 0 |
| 1198 | suit_classes = 'suit-tab suit-tab-dashboards' |
| 1199 | fields = ['user', 'dashboardView', 'order'] |
| 1200 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 1201 | class ControllerUserInline(XOSTabularInline): |
Tony Mack | 30dfcd7 | 2015-01-10 23:08:10 -0500 | [diff] [blame] | 1202 | model = ControllerUser |
| 1203 | extra = 0 |
| 1204 | suit_classes = 'suit-tab suit-tab-admin-only' |
| 1205 | fields = ['controller', 'user', 'kuser_id'] |
| 1206 | readonly_fields=['controller'] |
| 1207 | |
| 1208 | |
Scott Baker | 3a8aed6 | 2015-02-27 12:21:22 -0800 | [diff] [blame] | 1209 | class UserAdmin(XOSAdminMixin, UserAdmin): |
| 1210 | # Note: Make sure XOSAdminMixin is listed before |
Scott Baker | f4aeedc | 2014-10-03 13:10:47 -0700 | [diff] [blame] | 1211 | # admin.ModelAdmin in the class declaration. |
| 1212 | |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1213 | class Meta: |
| 1214 | app_label = "core" |
| 1215 | |
| 1216 | # The forms to add and change user instances |
| 1217 | form = UserChangeForm |
| 1218 | add_form = UserCreationForm |
| 1219 | |
| 1220 | # The fields to be used in displaying the User model. |
| 1221 | # These override the definitions on the base UserAdmin |
| 1222 | # that reference specific fields on auth.User. |
Scott Baker | a111f44 | 2015-01-24 13:33:26 -0800 | [diff] [blame] | 1223 | list_display = ('backend_status_icon', 'email', 'firstname', 'lastname', 'site', 'last_login') |
| 1224 | list_display_links = ("email",) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1225 | list_filter = ('site',) |
Scott Baker | 9047261 | 2015-01-29 10:55:53 -0800 | [diff] [blame] | 1226 | inlines = [SlicePrivilegeInline,SitePrivilegeInline] |
Tony Mack | 30dfcd7 | 2015-01-10 23:08:10 -0500 | [diff] [blame] | 1227 | admin_inlines = [ControllerUserInline] |
Scott Baker | 1a6a390 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 1228 | fieldListLoginDetails = ['backend_status_text', 'email','site','password','is_active','is_readonly','is_admin','public_key'] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1229 | fieldListContactInfo = ['firstname','lastname','phone','timezone'] |
| 1230 | |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1231 | fieldsets = ( |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1232 | ('Login Details', {'fields': ['backend_status_text', 'email', 'site','password', 'is_active', 'is_readonly', 'is_admin', 'public_key'], 'classes':['suit-tab suit-tab-general']}), |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1233 | ('Contact Information', {'fields': ('firstname','lastname','phone', 'timezone'), 'classes':['suit-tab suit-tab-contact']}), |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1234 | #('Important dates', {'fields': ('last_login',)}), |
| 1235 | ) |
| 1236 | add_fieldsets = ( |
| 1237 | (None, { |
| 1238 | 'classes': ('wide',), |
Tony Mack | 365545d | 2015-01-28 12:03:15 -0500 | [diff] [blame] | 1239 | 'fields': ('site', 'email', 'firstname', 'lastname', 'is_admin', 'is_readonly', 'phone', 'public_key','password1', 'password2')}, |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1240 | ), |
| 1241 | ) |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1242 | readonly_fields = ('backend_status_text', ) |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1243 | search_fields = ('email',) |
| 1244 | ordering = ('email',) |
| 1245 | filter_horizontal = () |
| 1246 | |
Scott Baker | 3ca51f6 | 2014-05-23 12:05:11 -0700 | [diff] [blame] | 1247 | user_readonly_fields = fieldListLoginDetails + fieldListContactInfo |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1248 | |
Scott Baker | 6a99535 | 2014-10-06 17:51:20 -0700 | [diff] [blame] | 1249 | @property |
| 1250 | def suit_form_tabs(self): |
| 1251 | if getattr(_thread_locals, "obj", None) is None: |
| 1252 | return [] |
| 1253 | else: |
Tony Mack | 30dfcd7 | 2015-01-10 23:08:10 -0500 | [diff] [blame] | 1254 | tabs = [('general','Login Details'), |
Scott Baker | 6a99535 | 2014-10-06 17:51:20 -0700 | [diff] [blame] | 1255 | ('contact','Contact Information'), |
| 1256 | ('sliceprivileges','Slice Privileges'), |
Scott Baker | 9047261 | 2015-01-29 10:55:53 -0800 | [diff] [blame] | 1257 | ('siteprivileges','Site Privileges')] |
Tony Mack | 30dfcd7 | 2015-01-10 23:08:10 -0500 | [diff] [blame] | 1258 | |
| 1259 | request=getattr(_thread_locals, "request", None) |
| 1260 | if request and request.user.is_admin: |
| 1261 | tabs.append( ('admin-only', 'Admin-Only') ) |
| 1262 | |
| 1263 | return tabs |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1264 | |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 1265 | def formfield_for_foreignkey(self, db_field, request, **kwargs): |
| 1266 | if db_field.name == 'site': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 1267 | kwargs['queryset'] = Site.select_by_user(request.user) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 1268 | |
| 1269 | return super(UserAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs) |
| 1270 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 1271 | def queryset(self, request): |
| 1272 | return User.select_by_user(request.user) |
| 1273 | |
Tony Mack | c8f443d | 2015-01-25 21:58:30 -0500 | [diff] [blame] | 1274 | def get_form(self, request, obj=None, **kwargs): |
Tony Mack | 03b9229 | 2015-01-28 12:37:12 -0500 | [diff] [blame] | 1275 | # copy login details list |
| 1276 | login_details_fields = list(self.fieldListLoginDetails) |
Tony Mack | 933b291 | 2015-01-28 12:49:58 -0500 | [diff] [blame] | 1277 | if not request.user.is_admin: |
Scott Baker | 9047261 | 2015-01-29 10:55:53 -0800 | [diff] [blame] | 1278 | # only admins can see 'is_admin' and 'is_readonly' fields |
Tony Mack | 03b9229 | 2015-01-28 12:37:12 -0500 | [diff] [blame] | 1279 | if 'is_admin' in login_details_fields: |
| 1280 | login_details_fields.remove('is_admin') |
| 1281 | if 'is_readonly' in login_details_fields: |
| 1282 | login_details_fields.remove('is_readonly') |
Tony Mack | 933b291 | 2015-01-28 12:49:58 -0500 | [diff] [blame] | 1283 | #if len(request.user.siteprivileges.filter(role__role = 'pi')) > 0: |
Tony Mack | 03b9229 | 2015-01-28 12:37:12 -0500 | [diff] [blame] | 1284 | # only admins and pis can change a user's site |
Tony Mack | 933b291 | 2015-01-28 12:49:58 -0500 | [diff] [blame] | 1285 | # self.readonly_fields = ('backend_status_text', 'site') |
Tony Mack | 03b9229 | 2015-01-28 12:37:12 -0500 | [diff] [blame] | 1286 | self.fieldsets = ( |
| 1287 | ('Login Details', {'fields': login_details_fields, 'classes':['suit-tab suit-tab-general']}), |
| 1288 | ('Contact Information', {'fields': self.fieldListContactInfo, 'classes':['suit-tab suit-tab-contact']}), |
| 1289 | ) |
Tony Mack | c8f443d | 2015-01-25 21:58:30 -0500 | [diff] [blame] | 1290 | return super(UserAdmin, self).get_form(request, obj, **kwargs) |
| 1291 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 1292 | class ControllerDashboardViewInline(XOSTabularInline): |
Scott Baker | f2c0c51 | 2014-12-22 17:35:34 -0800 | [diff] [blame] | 1293 | model = ControllerDashboardView |
Scott Baker | 786a9c1 | 2014-12-19 16:41:12 -0800 | [diff] [blame] | 1294 | extra = 0 |
| 1295 | fields = ["controller", "url"] |
| 1296 | suit_classes = 'suit-tab suit-tab-controllers' |
| 1297 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 1298 | class DashboardViewAdmin(XOSBaseAdmin): |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 1299 | fieldsets = [('Dashboard View Details', |
Scott Baker | 5924818 | 2015-02-17 13:34:32 -0800 | [diff] [blame] | 1300 | {'fields': ['backend_status_text', 'name', 'url', 'enabled', 'deployments'], |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 1301 | 'classes': ['suit-tab suit-tab-general']}) |
| 1302 | ] |
Scott Baker | 2c44e6e | 2015-01-18 16:46:26 -0800 | [diff] [blame] | 1303 | list_display = ["name", "enabled", "url"] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1304 | readonly_fields = ('backend_status_text', ) |
Scott Baker | f2c0c51 | 2014-12-22 17:35:34 -0800 | [diff] [blame] | 1305 | inlines = [ControllerDashboardViewInline] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1306 | |
Scott Baker | 786a9c1 | 2014-12-19 16:41:12 -0800 | [diff] [blame] | 1307 | suit_form_tabs =(('general','Dashboard View Details'), |
| 1308 | ('controllers', 'Per-controller Dashboard Details')) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1309 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 1310 | class ServiceResourceInline(XOSTabularInline): |
Scott Baker | 3de3e37 | 2013-05-10 16:50:44 -0700 | [diff] [blame] | 1311 | model = ServiceResource |
| 1312 | extra = 0 |
| 1313 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 1314 | class ServiceClassAdmin(XOSBaseAdmin): |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 1315 | list_display = ('backend_status_icon', 'name', 'commitment', 'membershipFee') |
| 1316 | list_display_links = ('backend_status_icon', 'name', ) |
Scott Baker | 3de3e37 | 2013-05-10 16:50:44 -0700 | [diff] [blame] | 1317 | inlines = [ServiceResourceInline] |
| 1318 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1319 | user_readonly_fields = ['name', 'commitment', 'membershipFee'] |
| 1320 | user_readonly_inlines = [] |
| 1321 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 1322 | class ReservedResourceInline(XOSTabularInline): |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1323 | model = ReservedResource |
| 1324 | extra = 0 |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1325 | suit_classes = 'suit-tab suit-tab-reservedresources' |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1326 | |
| 1327 | def formfield_for_foreignkey(self, db_field, request=None, **kwargs): |
| 1328 | field = super(ReservedResourceInline, self).formfield_for_foreignkey(db_field, request, **kwargs) |
| 1329 | |
| 1330 | if db_field.name == 'resource': |
| 1331 | # restrict resources to those that the slice's service class allows |
| 1332 | if request._slice is not None: |
| 1333 | field.queryset = field.queryset.filter(serviceClass = request._slice.serviceClass, calendarReservable=True) |
| 1334 | if len(field.queryset) > 0: |
| 1335 | field.initial = field.queryset.all()[0] |
S.Çağlar Onur | b6e63f0 | 2015-02-24 17:28:09 -0500 | [diff] [blame] | 1336 | else: |
| 1337 | field.queryset = field.queryset.none() |
| 1338 | elif db_field.name == 'sliver': |
| 1339 | # restrict slivers to those that belong to the slice |
| 1340 | if request._slice is not None: |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1341 | field.queryset = field.queryset.filter(slice = request._slice) |
| 1342 | else: |
S.Çağlar Onur | b6e63f0 | 2015-02-24 17:28:09 -0500 | [diff] [blame] | 1343 | field.queryset = field.queryset.none() |
| 1344 | |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1345 | return field |
| 1346 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 1347 | def queryset(self, request): |
| 1348 | return ReservedResource.select_by_user(request.user) |
| 1349 | |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1350 | class ReservationChangeForm(forms.ModelForm): |
| 1351 | class Meta: |
| 1352 | model = Reservation |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1353 | widgets = { |
| 1354 | 'slice' : LinkedSelect |
| 1355 | } |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1356 | |
| 1357 | class ReservationAddForm(forms.ModelForm): |
| 1358 | slice = forms.ModelChoiceField(queryset=Slice.objects.all(), widget=forms.Select(attrs={"onChange":"document.getElementById('id_refresh').value=1; submit()"})) |
| 1359 | refresh = forms.CharField(widget=forms.HiddenInput()) |
| 1360 | |
| 1361 | class Media: |
Scott Baker | 0686895 | 2015-02-18 15:15:58 -0800 | [diff] [blame] | 1362 | css = {'all': ('xos.css',)} # .field-refresh { display: none; } |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1363 | |
| 1364 | def clean_slice(self): |
| 1365 | slice = self.cleaned_data.get("slice") |
| 1366 | x = ServiceResource.objects.filter(serviceClass = slice.serviceClass, calendarReservable=True) |
| 1367 | if len(x) == 0: |
| 1368 | raise forms.ValidationError("The slice you selected does not have a service class that allows reservations") |
| 1369 | return slice |
| 1370 | |
| 1371 | class Meta: |
| 1372 | model = Reservation |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1373 | widgets = { |
| 1374 | 'slice' : LinkedSelect |
| 1375 | } |
| 1376 | |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1377 | |
| 1378 | class ReservationAddRefreshForm(ReservationAddForm): |
| 1379 | """ This form is displayed when the Reservation Form receives an update |
| 1380 | from the Slice dropdown onChange handler. It doesn't validate the |
| 1381 | data and doesn't save the data. This will cause the form to be |
| 1382 | redrawn. |
| 1383 | """ |
| 1384 | |
Scott Baker | 8737e5f | 2013-05-17 09:35:32 -0700 | [diff] [blame] | 1385 | """ don't validate anything other than slice """ |
| 1386 | dont_validate_fields = ("startTime", "duration") |
| 1387 | |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1388 | def full_clean(self): |
| 1389 | result = super(ReservationAddForm, self).full_clean() |
Scott Baker | 8737e5f | 2013-05-17 09:35:32 -0700 | [diff] [blame] | 1390 | |
| 1391 | for fieldname in self.dont_validate_fields: |
| 1392 | if fieldname in self._errors: |
| 1393 | del self._errors[fieldname] |
| 1394 | |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1395 | return result |
| 1396 | |
| 1397 | """ don't save anything """ |
| 1398 | def is_valid(self): |
| 1399 | return False |
| 1400 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 1401 | class ReservationAdmin(XOSBaseAdmin): |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1402 | fieldList = ['backend_status_text', 'slice', 'startTime', 'duration'] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1403 | fieldsets = [('Reservation Details', {'fields': fieldList, 'classes': ['suit-tab suit-tab-general']})] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1404 | readonly_fields = ('backend_status_text', ) |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1405 | list_display = ('startTime', 'duration') |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1406 | form = ReservationAddForm |
| 1407 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1408 | suit_form_tabs = (('general','Reservation Details'), ('reservedresources','Reserved Resources')) |
| 1409 | |
| 1410 | inlines = [ReservedResourceInline] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1411 | user_readonly_fields = fieldList |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1412 | |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1413 | def add_view(self, request, form_url='', extra_context=None): |
Scott Baker | acd4514 | 2013-05-19 16:19:16 -0700 | [diff] [blame] | 1414 | timezone.activate(request.user.timezone) |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1415 | request._refresh = False |
| 1416 | request._slice = None |
| 1417 | if request.method == 'POST': |
Scott Baker | 8737e5f | 2013-05-17 09:35:32 -0700 | [diff] [blame] | 1418 | # "refresh" will be set to "1" if the form was submitted due to |
| 1419 | # a change in the Slice dropdown. |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1420 | if request.POST.get("refresh","1") == "1": |
| 1421 | request._refresh = True |
| 1422 | request.POST["refresh"] = "0" |
Scott Baker | 8737e5f | 2013-05-17 09:35:32 -0700 | [diff] [blame] | 1423 | |
| 1424 | # Keep track of the slice that was selected, so the |
| 1425 | # reservedResource inline can filter items for the slice. |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1426 | request._slice = request.POST.get("slice",None) |
| 1427 | if (request._slice is not None): |
| 1428 | request._slice = Slice.objects.get(id=request._slice) |
| 1429 | |
| 1430 | result = super(ReservationAdmin, self).add_view(request, form_url, extra_context) |
| 1431 | return result |
| 1432 | |
Scott Baker | acd4514 | 2013-05-19 16:19:16 -0700 | [diff] [blame] | 1433 | def changelist_view(self, request, extra_context = None): |
| 1434 | timezone.activate(request.user.timezone) |
| 1435 | return super(ReservationAdmin, self).changelist_view(request, extra_context) |
| 1436 | |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1437 | def get_form(self, request, obj=None, **kwargs): |
Siobhan Tully | d351575 | 2013-06-21 16:34:53 -0400 | [diff] [blame] | 1438 | request._obj_ = obj |
| 1439 | if obj is not None: |
| 1440 | # For changes, set request._slice to the slice already set in the |
| 1441 | # object. |
| 1442 | request._slice = obj.slice |
| 1443 | self.form = ReservationChangeForm |
| 1444 | else: |
| 1445 | if getattr(request, "_refresh", False): |
| 1446 | self.form = ReservationAddRefreshForm |
| 1447 | else: |
| 1448 | self.form = ReservationAddForm |
| 1449 | return super(ReservationAdmin, self).get_form(request, obj, **kwargs) |
| 1450 | |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1451 | def get_readonly_fields(self, request, obj=None): |
Siobhan Tully | d351575 | 2013-06-21 16:34:53 -0400 | [diff] [blame] | 1452 | if (obj is not None): |
| 1453 | # Prevent slice from being changed after the reservation has been |
| 1454 | # created. |
| 1455 | return ['slice'] |
| 1456 | else: |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1457 | return [] |
Scott Baker | 3de3e37 | 2013-05-10 16:50:44 -0700 | [diff] [blame] | 1458 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 1459 | def queryset(self, request): |
| 1460 | return Reservation.select_by_user(request.user) |
| 1461 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 1462 | class NetworkParameterTypeAdmin(XOSBaseAdmin): |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 1463 | list_display = ("backend_status_icon", "name", ) |
| 1464 | list_display_links = ('backend_status_icon', 'name', ) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1465 | user_readonly_fields = ['name'] |
| 1466 | user_readonly_inlines = [] |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1467 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 1468 | class RouterAdmin(XOSBaseAdmin): |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 1469 | list_display = ("backend_status_icon", "name", ) |
| 1470 | list_display_links = ('backend_status_icon', 'name', ) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1471 | user_readonly_fields = ['name'] |
| 1472 | user_readonly_inlines = [] |
| 1473 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 1474 | class RouterInline(XOSTabularInline): |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1475 | model = Router.networks.through |
| 1476 | extra = 0 |
| 1477 | verbose_name_plural = "Routers" |
| 1478 | verbose_name = "Router" |
Siobhan Tully | 2d95e48 | 2013-09-06 10:56:06 -0400 | [diff] [blame] | 1479 | suit_classes = 'suit-tab suit-tab-routers' |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1480 | |
Scott Baker | b27b62c | 2014-08-15 16:29:16 -0700 | [diff] [blame] | 1481 | class NetworkParameterInline(PlStackGenericTabularInline): |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1482 | model = NetworkParameter |
Scott Baker | 618e379 | 2014-08-15 13:42:29 -0700 | [diff] [blame] | 1483 | extra = 0 |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1484 | verbose_name_plural = "Parameters" |
| 1485 | verbose_name = "Parameter" |
| 1486 | suit_classes = 'suit-tab suit-tab-netparams' |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1487 | fields = ['backend_status_icon', 'parameter', 'value'] |
| 1488 | readonly_fields = ('backend_status_icon', ) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1489 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 1490 | class NetworkSliversInline(XOSTabularInline): |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1491 | fields = ['backend_status_icon', 'network','sliver','ip'] |
| 1492 | readonly_fields = ("backend_status_icon", "ip", ) |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1493 | model = NetworkSliver |
Scott Baker | 874936e | 2014-01-13 18:15:34 -0800 | [diff] [blame] | 1494 | selflink_fieldname = "sliver" |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1495 | extra = 0 |
| 1496 | verbose_name_plural = "Slivers" |
| 1497 | verbose_name = "Sliver" |
Siobhan Tully | 2d95e48 | 2013-09-06 10:56:06 -0400 | [diff] [blame] | 1498 | suit_classes = 'suit-tab suit-tab-networkslivers' |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1499 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 1500 | class NetworkSlicesInline(XOSTabularInline): |
Scott Baker | d7d2a39 | 2013-08-06 08:57:30 -0700 | [diff] [blame] | 1501 | model = NetworkSlice |
Scott Baker | 874936e | 2014-01-13 18:15:34 -0800 | [diff] [blame] | 1502 | selflink_fieldname = "slice" |
Scott Baker | d7d2a39 | 2013-08-06 08:57:30 -0700 | [diff] [blame] | 1503 | extra = 0 |
| 1504 | verbose_name_plural = "Slices" |
| 1505 | verbose_name = "Slice" |
Siobhan Tully | 2d95e48 | 2013-09-06 10:56:06 -0400 | [diff] [blame] | 1506 | suit_classes = 'suit-tab suit-tab-networkslices' |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1507 | fields = ['backend_status_icon', 'network','slice'] |
| 1508 | readonly_fields = ('backend_status_icon', ) |
Scott Baker | d7d2a39 | 2013-08-06 08:57:30 -0700 | [diff] [blame] | 1509 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 1510 | class ControllerNetworkInline(XOSTabularInline): |
Tony Mack | 3066a95 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 1511 | model = ControllerNetwork |
Scott Baker | 8806cdf | 2014-10-17 16:27:23 -0700 | [diff] [blame] | 1512 | extra = 0 |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 1513 | verbose_name_plural = "Controller Networks" |
| 1514 | verbose_name = "Controller Network" |
Scott Baker | 8806cdf | 2014-10-17 16:27:23 -0700 | [diff] [blame] | 1515 | suit_classes = 'suit-tab suit-tab-admin-only' |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 1516 | fields = ['backend_status_icon', 'controller','net_id','subnet_id'] |
Scott Baker | 8806cdf | 2014-10-17 16:27:23 -0700 | [diff] [blame] | 1517 | readonly_fields = ('backend_status_icon', ) |
| 1518 | |
Scott Baker | 69e045d | 2014-11-17 23:44:03 -0800 | [diff] [blame] | 1519 | class NetworkForm(forms.ModelForm): |
| 1520 | class Meta: |
| 1521 | model = Network |
| 1522 | widgets = { |
| 1523 | 'topologyParameters': UploadTextareaWidget, |
| 1524 | 'controllerParameters': UploadTextareaWidget, |
| 1525 | } |
| 1526 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 1527 | class NetworkAdmin(XOSBaseAdmin): |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 1528 | list_display = ("backend_status_icon", "name", "subnet", "ports", "labels") |
| 1529 | list_display_links = ('backend_status_icon', 'name', ) |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1530 | readonly_fields = ("subnet", ) |
Siobhan Tully | 2d95e48 | 2013-09-06 10:56:06 -0400 | [diff] [blame] | 1531 | |
Scott Baker | d7d2a39 | 2013-08-06 08:57:30 -0700 | [diff] [blame] | 1532 | inlines = [NetworkParameterInline, NetworkSliversInline, NetworkSlicesInline, RouterInline] |
Tony Mack | 3066a95 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 1533 | admin_inlines = [ControllerNetworkInline] |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1534 | |
Scott Baker | 69e045d | 2014-11-17 23:44:03 -0800 | [diff] [blame] | 1535 | form=NetworkForm |
| 1536 | |
Siobhan Tully | 2d95e48 | 2013-09-06 10:56:06 -0400 | [diff] [blame] | 1537 | fieldsets = [ |
Scott Baker | 549aa25 | 2015-01-03 12:29:29 -0800 | [diff] [blame] | 1538 | (None, {'fields': ['backend_status_text', 'name','template','ports','labels','owner','guaranteed_bandwidth', 'permit_all_slices','permitted_slices','network_id','router_id','subnet_id','subnet'], |
Scott Baker | 3e28dd7 | 2014-11-17 16:04:45 -0800 | [diff] [blame] | 1539 | 'classes':['suit-tab suit-tab-general']}), |
Scott Baker | 549aa25 | 2015-01-03 12:29:29 -0800 | [diff] [blame] | 1540 | (None, {'fields': ['topology_parameters', 'controller_url', 'controller_parameters'], |
Scott Baker | 3e28dd7 | 2014-11-17 16:04:45 -0800 | [diff] [blame] | 1541 | 'classes':['suit-tab suit-tab-sdn']}), |
| 1542 | ] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1543 | |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1544 | readonly_fields = ('backend_status_text', ) |
Scott Baker | 549aa25 | 2015-01-03 12:29:29 -0800 | [diff] [blame] | 1545 | user_readonly_fields = ['name','template','ports','labels','owner','guaranteed_bandwidth', 'permit_all_slices','permitted_slices','network_id','router_id','subnet_id','subnet'] |
Siobhan Tully | 2d95e48 | 2013-09-06 10:56:06 -0400 | [diff] [blame] | 1546 | |
Scott Baker | 8806cdf | 2014-10-17 16:27:23 -0700 | [diff] [blame] | 1547 | @property |
| 1548 | def suit_form_tabs(self): |
| 1549 | tabs=[('general','Network Details'), |
Scott Baker | 3e28dd7 | 2014-11-17 16:04:45 -0800 | [diff] [blame] | 1550 | ('sdn', 'SDN Configuration'), |
Scott Baker | 8806cdf | 2014-10-17 16:27:23 -0700 | [diff] [blame] | 1551 | ('netparams', 'Parameters'), |
| 1552 | ('networkslivers','Slivers'), |
| 1553 | ('networkslices','Slices'), |
| 1554 | ('routers','Routers'), |
| 1555 | ] |
| 1556 | |
| 1557 | request=getattr(_thread_locals, "request", None) |
| 1558 | if request and request.user.is_admin: |
| 1559 | tabs.append( ('admin-only', 'Admin-Only') ) |
| 1560 | |
| 1561 | return tabs |
| 1562 | |
| 1563 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 1564 | class NetworkTemplateAdmin(XOSBaseAdmin): |
Scott Baker | 369f9b9 | 2015-01-03 12:03:38 -0800 | [diff] [blame] | 1565 | list_display = ("backend_status_icon", "name", "guaranteed_bandwidth", "visibility") |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 1566 | list_display_links = ('backend_status_icon', 'name', ) |
Scott Baker | 369f9b9 | 2015-01-03 12:03:38 -0800 | [diff] [blame] | 1567 | user_readonly_fields = ["name", "guaranteed_bandwidth", "visibility"] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1568 | user_readonly_inlines = [] |
Scott Baker | 3e28dd7 | 2014-11-17 16:04:45 -0800 | [diff] [blame] | 1569 | fieldsets = [ |
Scott Baker | 369f9b9 | 2015-01-03 12:03:38 -0800 | [diff] [blame] | 1570 | (None, {'fields': ['name', 'description', 'guaranteed_bandwidth', 'visibility', 'translation', 'shared_network_name', 'shared_network_id', 'topology_kind', 'controller_kind'], |
Scott Baker | 3e28dd7 | 2014-11-17 16:04:45 -0800 | [diff] [blame] | 1571 | 'classes':['suit-tab suit-tab-general']}),] |
| 1572 | suit_form_tabs = (('general','Network Template Details'), ) |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1573 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 1574 | class FlavorAdmin(XOSBaseAdmin): |
Scott Baker | 37b4790 | 2014-09-02 14:37:41 -0700 | [diff] [blame] | 1575 | list_display = ("backend_status_icon", "name", "flavor", "order", "default") |
| 1576 | list_display_links = ("backend_status_icon", "name") |
| 1577 | user_readonly_fields = ("name", "flavor") |
| 1578 | fields = ("name", "description", "flavor", "order", "default") |
| 1579 | |
Tony Mack | 31c2b8f | 2013-04-26 20:01:42 -0400 | [diff] [blame] | 1580 | # register a signal that caches the user's credentials when they log in |
| 1581 | def cache_credentials(sender, user, request, **kwds): |
| 1582 | auth = {'username': request.POST['username'], |
| 1583 | 'password': request.POST['password']} |
| 1584 | request.session['auth'] = auth |
| 1585 | user_logged_in.connect(cache_credentials) |
| 1586 | |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1587 | def dollar_field(fieldName, short_description): |
| 1588 | def newFunc(self, obj): |
| 1589 | try: |
| 1590 | x= "$ %0.2f" % float(getattr(obj, fieldName, 0.0)) |
| 1591 | except: |
| 1592 | x=getattr(obj, fieldName, 0.0) |
| 1593 | return x |
| 1594 | newFunc.short_description = short_description |
| 1595 | return newFunc |
| 1596 | |
| 1597 | def right_dollar_field(fieldName, short_description): |
| 1598 | def newFunc(self, obj): |
| 1599 | try: |
| 1600 | #x= '<div align=right style="width:6em">$ %0.2f</div>' % float(getattr(obj, fieldName, 0.0)) |
| 1601 | x= '<div align=right>$ %0.2f</div>' % float(getattr(obj, fieldName, 0.0)) |
| 1602 | except: |
| 1603 | x=getattr(obj, fieldName, 0.0) |
| 1604 | return x |
| 1605 | newFunc.short_description = short_description |
| 1606 | newFunc.allow_tags = True |
| 1607 | return newFunc |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1608 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 1609 | class InvoiceChargeInline(XOSTabularInline): |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1610 | model = Charge |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1611 | extra = 0 |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1612 | verbose_name_plural = "Charges" |
| 1613 | verbose_name = "Charge" |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1614 | exclude = ['account'] |
Scott Baker | 9cb88a2 | 2013-12-09 18:56:00 -0800 | [diff] [blame] | 1615 | fields = ["date", "kind", "state", "object", "coreHours", "dollar_amount", "slice"] |
| 1616 | readonly_fields = ["date", "kind", "state", "object", "coreHours", "dollar_amount", "slice"] |
| 1617 | can_delete = False |
| 1618 | max_num = 0 |
| 1619 | |
| 1620 | dollar_amount = right_dollar_field("amount", "Amount") |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1621 | |
| 1622 | class InvoiceAdmin(admin.ModelAdmin): |
| 1623 | list_display = ("date", "account") |
| 1624 | |
| 1625 | inlines = [InvoiceChargeInline] |
| 1626 | |
Scott Baker | 9cb88a2 | 2013-12-09 18:56:00 -0800 | [diff] [blame] | 1627 | fields = ["date", "account", "dollar_amount"] |
| 1628 | readonly_fields = ["date", "account", "dollar_amount"] |
| 1629 | |
| 1630 | dollar_amount = dollar_field("amount", "Amount") |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1631 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 1632 | class InvoiceInline(XOSTabularInline): |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1633 | model = Invoice |
| 1634 | extra = 0 |
| 1635 | verbose_name_plural = "Invoices" |
| 1636 | verbose_name = "Invoice" |
Scott Baker | 0165fac | 2014-01-13 11:49:26 -0800 | [diff] [blame] | 1637 | fields = ["date", "dollar_amount"] |
| 1638 | readonly_fields = ["date", "dollar_amount"] |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1639 | suit_classes = 'suit-tab suit-tab-accountinvoice' |
| 1640 | can_delete=False |
| 1641 | max_num=0 |
| 1642 | |
| 1643 | dollar_amount = right_dollar_field("amount", "Amount") |
| 1644 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 1645 | class PendingChargeInline(XOSTabularInline): |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1646 | model = Charge |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1647 | extra = 0 |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1648 | verbose_name_plural = "Charges" |
| 1649 | verbose_name = "Charge" |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1650 | exclude = ["invoice"] |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1651 | fields = ["date", "kind", "state", "object", "coreHours", "dollar_amount", "slice"] |
| 1652 | readonly_fields = ["date", "kind", "state", "object", "coreHours", "dollar_amount", "slice"] |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1653 | suit_classes = 'suit-tab suit-tab-accountpendingcharges' |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1654 | can_delete=False |
| 1655 | max_num=0 |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1656 | |
| 1657 | def queryset(self, request): |
| 1658 | qs = super(PendingChargeInline, self).queryset(request) |
| 1659 | qs = qs.filter(state="pending") |
| 1660 | return qs |
| 1661 | |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1662 | dollar_amount = right_dollar_field("amount", "Amount") |
| 1663 | |
Scott Baker | 67db95f | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 1664 | class PaymentInline(XOSTabularInline): |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1665 | model=Payment |
| 1666 | extra = 1 |
| 1667 | verbose_name_plural = "Payments" |
| 1668 | verbose_name = "Payment" |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1669 | fields = ["date", "dollar_amount"] |
| 1670 | readonly_fields = ["date", "dollar_amount"] |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1671 | suit_classes = 'suit-tab suit-tab-accountpayments' |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1672 | can_delete=False |
| 1673 | max_num=0 |
| 1674 | |
| 1675 | dollar_amount = right_dollar_field("amount", "Amount") |
| 1676 | |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1677 | class AccountAdmin(admin.ModelAdmin): |
| 1678 | list_display = ("site", "balance_due") |
| 1679 | |
| 1680 | inlines = [InvoiceInline, PaymentInline, PendingChargeInline] |
| 1681 | |
| 1682 | fieldsets = [ |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1683 | (None, {'fields': ['site', 'dollar_balance_due', 'dollar_total_invoices', 'dollar_total_payments'],'classes':['suit-tab suit-tab-general']}),] |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1684 | |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1685 | readonly_fields = ['site', 'dollar_balance_due', 'dollar_total_invoices', 'dollar_total_payments'] |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1686 | |
| 1687 | suit_form_tabs =( |
| 1688 | ('general','Account Details'), |
| 1689 | ('accountinvoice', 'Invoices'), |
| 1690 | ('accountpayments', 'Payments'), |
| 1691 | ('accountpendingcharges','Pending Charges'), |
| 1692 | ) |
| 1693 | |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1694 | dollar_balance_due = dollar_field("balance_due", "Balance Due") |
| 1695 | dollar_total_invoices = dollar_field("total_invoices", "Total Invoices") |
| 1696 | dollar_total_payments = dollar_field("total_payments", "Total Payments") |
| 1697 | |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1698 | # Now register the new UserAdmin... |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 1699 | admin.site.register(User, UserAdmin) |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1700 | # ... and, since we're not using Django's builtin permissions, |
| 1701 | # unregister the Group model from admin. |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 1702 | #admin.site.unregister(Group) |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1703 | |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 1704 | #Do not show django evolution in the admin interface |
| 1705 | from django_evolution.models import Version, Evolution |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1706 | #admin.site.unregister(Version) |
| 1707 | #admin.site.unregister(Evolution) |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 1708 | |
| 1709 | |
| 1710 | # When debugging it is often easier to see all the classes, but for regular use |
| 1711 | # only the top-levels should be displayed |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1712 | showAll = False |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1713 | |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 1714 | admin.site.register(Deployment, DeploymentAdmin) |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 1715 | admin.site.register(Controller, ControllerAdmin) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1716 | admin.site.register(Site, SiteAdmin) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1717 | admin.site.register(Slice, SliceAdmin) |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 1718 | admin.site.register(Service, ServiceAdmin) |
Tony Mack | 450b6e0 | 2015-01-25 12:35:29 -0500 | [diff] [blame] | 1719 | #admin.site.register(Reservation, ReservationAdmin) |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1720 | admin.site.register(Network, NetworkAdmin) |
| 1721 | admin.site.register(Router, RouterAdmin) |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1722 | admin.site.register(NetworkTemplate, NetworkTemplateAdmin) |
Tony Mack | 450b6e0 | 2015-01-25 12:35:29 -0500 | [diff] [blame] | 1723 | #admin.site.register(Account, AccountAdmin) |
| 1724 | #admin.site.register(Invoice, InvoiceAdmin) |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 1725 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1726 | if True: |
| 1727 | admin.site.register(NetworkParameterType, NetworkParameterTypeAdmin) |
| 1728 | admin.site.register(ServiceClass, ServiceClassAdmin) |
Siobhan Tully | d351575 | 2013-06-21 16:34:53 -0400 | [diff] [blame] | 1729 | admin.site.register(Tag, TagAdmin) |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 1730 | admin.site.register(ControllerRole) |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 1731 | admin.site.register(SiteRole) |
| 1732 | admin.site.register(SliceRole) |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 1733 | admin.site.register(Node, NodeAdmin) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1734 | #admin.site.register(SlicePrivilege, SlicePrivilegeAdmin) |
| 1735 | #admin.site.register(SitePrivilege, SitePrivilegeAdmin) |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 1736 | admin.site.register(Sliver, SliverAdmin) |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 1737 | admin.site.register(Image, ImageAdmin) |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 1738 | admin.site.register(DashboardView, DashboardViewAdmin) |
Scott Baker | 37b4790 | 2014-09-02 14:37:41 -0700 | [diff] [blame] | 1739 | admin.site.register(Flavor, FlavorAdmin) |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 1740 | |