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