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