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